@reste/x v1.0.11
Reste x is a lightweight, powerful library for creating rest controllers based on the express framework and typescript.
Philosophy
The Reste X was created to simplify the creation of endpoints for framework express. The logic of the library does not affect the framework, but wraps it into a simpler solution for creating request mapping.
TODO
- Endpoint auth
- Implementation for Websocket
- Functionality ala Swagger
- MicroserviceConnector
Installation
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 0.10 or higher is required.
NPM repository: https://www.npmjs.com/package/@reste/x
Installation is done using the npm install command:
npm install @reste/x
Also install the required dependencies
npm install express
npm install @types/express
npm install @types/node
Usage
In the first step you need to create an express application.
// typescript implementation const express = require('express'); const app = express(); app.listen(8080, () => console.log('Server started at port 8080')); app.on('error', (error: any) => console.log('Error:', error));
Then we can import our library and assign it a reference to the express application.
// typescript implementation import { ResteX } from '@reste/x'; const express = require('express'); const app = express(); ResteX.createApp(app, [SampleController]); app.listen(8080, () => console.log('Server started at port 8080')); app.on('error', (error: any) => console.log('Error:', error));
Now you can create a controller
// typescript implementation import { Callback, Controller, Endpoint, EndpointType } from '@reste/x'; import { NextFunction, Request, Response } from 'express'; @Controller('/') export class SampleController { public static auth(req?: Request, res?: Response, next?: NextFunction): any { console.log('SampleController::AUTH'); next(); } @Endpoint() public sampleGet(callback: Callback): void { callback.response.send('SampleController.sampleGet()'); } @Endpoint('/', EndpointType.POST) public samplePost(callback: Callback): void { callback.response.send('SampleController.samplePost()'); } @Endpoint('/sample') public samplePath(callback: Callback): void { callback.response.send('SampleController.samplePath()'); } @Endpoint('/sample/:id') public samplePath(callback: Callback): void { callback.response.send('SampleController.samplePath() with id param'); } @Endpoint('/sample/auth', EndpointType.GET, SampleController.auth) public sample(callback: Callback): void { callback.response.send('HomeController.sample()'); } }
Documentation
@Controller
The controller has an optional path parameter, if it is not set, it is assigned /
Sample usage:
@Controller()
@Controller('/')
@Controller('/some-path')
@Controller('/some-path/foo/bar')
@Controller('/some-path/:foo/:bar')
@Endpoint
Endpoint has an optional path parameter, if it is not set, it is assigned "/",
and the parameter type (EndpointType
), and an optional auth paramater.
When an endpoint is called up, 3 express parameters are injected into the auth method:
- req
- res
- next
Sample usage:
@Endpoint()
@Endpoint('/')
@Endpoint('/some-path')
@Endpoint('/some-path/foo/bar')
@Endpoint('/some-path/:foo/:bar')
@Endpoint('/', EndpointType.GET)
@Endpoint('/', EndpointType.POST)
@Endpoint('/some-path', EndpointType.PUT)
@Endpoint('/some-path/foo/bar', EndpointType.DELETE)
@Endpoint('/some/path/with/auth', EndpointType.GET, Controller.AUTH)
ResteX
For proper operation of the application, we need to call our library, with the reference parameter for the express application and optionally a list of controllers.
Sample usage:
import { ResteX } from '@reste/x';
import { ApiController } from './some-path/ApiController';
import { HomeController } from './some-path/HomeController';
const express = require('express');
const app = express();
ResteX.createApp(app, [HomeController, ApiController /* ... */]);
app.listen(8080, () => console.log('Server started at port 8080'));
app.on('error', (error: any) => console.log('Error:', error));
EndpointType
EndpointType declares possible http requests.
export enum EndpointType {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE'
}
Callback
Callback is a parameter passed to the method with the Endpoint annotation.
Callback consists of parameters available in framework express, such as: Request
, Response
, NextFunction
.
import { NextFunction, Request, Response } from 'express';
export interface Callback {
request: Request;
response: Response;
next: NextFunction;
}
People
The original author of Reste X is Dominik Linkowski.
License
MIT