A fast and flexible REST API framework built using TypeScript decorators.
The Aether framework is a simplistic RESTful API framework built on top of Express, and TypeScript, you will need to use TypeScript to utilise the functionality. The framework is built around my immediate issue and was for prototyping applications with very little resistance as the framework provides a series of decorators and a server. Another note to make is that the application passes all paths directly to express so using path params is supported, example:
/user/:user_id, I shall create a proper example when I get some time.
NOTE: Please note this framework is for my own learning and is heavily based around my personal coding style, if you have better ideas of how something could be done better, then please, let me know so I can update it or add new ideas.
To install, you can install from NPM with:
npm install --save aether-framework
Below lists features that are currently implemented.
|Web Server||The implementation is a standard Express HTTP server.||
|Middleware||This relates to implementing a function for applying Express middleware functions.||
|Controller Decorator||This decorator implements a prefixed path for all child methods.||
|Route Decorator||Defines the HTTP method and the path (after the prefix) for that method.||
|Inject Decorator||The inject method will allow you to inject provided classes, these can be helpers etc.||
|Provide Decorator||The provide decorator will allow other methods to inject and resolve the provided class.||
|Model Decorator||The model decorator allows you to define a class as a model and use the built in ORM to map data.||
|InjectModel Decorator||This decorator will allow you to inject one or many models into a controller.||
Below lists features that will be implemented in time.
|Threaded Decorator||Defines whether a method should be called in a separate thread because it may be CPU intensive and could slow the application.|
|Authenticated Decorator||The decorator defines that a user should be authenticated, passing an optional role, will check against that user's role.|
|WebSocket Support||The plan is to implement WebSocket decorators for creating and managing a WebSocket, thinking something like
|Plugin Support||The ability to expand functionality using plugins, this is later on, but being able to add plugins to add or change functionality.|
Visit here for a full example you can download.
Below is our quick start example.
// Import the parts of the package we need.;// Create async function.;
If you want to use any kind of express based middleware then the functionality is the exact same as normal, to do it do the following:
The controller decorator is used to define a controller and will use the path to generate a prefix path for all routes inside.
In recent changes you must extend the abstract controller class, for support with models.
The above defines that all child routes will be prefixed with /test.
@Route(method: string, path: string)
The route method defines a route around a controller method, the method parameter is either the HTTP method: POST, GET, PUT, etc. The path parameter is the last part of the path (after the prefix) to that method.
All routes are proxied directly to express, therefore using dynamic paths like
/user/:user_idwill work, you can use context.params to get the parameters.
So if we say that our server is running on port 8080, then the path to that method would be:
@Model(name: string, fields: Object)
The validate function which can be called inside of the controller as
this.models.<model_name>.validate(record); or can be called inside of the model as
this.validate(record); is synchronous and will validate the record properties against the defined model, and will return an object with
valid: boolean and
reason?: string if there is a reason.
Additionally if you want a parameter to be optional then you just need to put ? in front of the type, see example.
The model is instantiated and given to a controller once injected with the collection automatically assigned to the model as
this.collection so you can call methods from this.
The above decorator should be applied to controllers, and will instatiate models and assign the database collection to the model, once this is done the model becomes available in the controllers
this.models.<modelName> property from within the controller.
The provide decorator will accept a unique name for the class you wish to provide and then save it ready for it to be called from any other application. Use the Inject decorator to inject a provided class into an application.
@Inject(uniqueName: string | string)
Once a class is provided using the decorator, then you can use the Inject decorator to inject into a class, now when using inject, you use it against a method, but will enable it for all methods inside, this is a bug I am trying to figure out, and is my own take on it.