@ditsmod/rest
As the name suggests, the @ditsmod/rest module provides support for REST. It includes:
- init decorators for the root module and the feature module -
restRootModule,restModule; - extensions that provide REST route creation -
RouteExtension,PreRouterExtension; - a router of the following type:
interface Router {
on(method: HttpMethod, path: string, handle: RouteHandler): this;
all(path: string, handle: RouteHandler): this;
find(method: HttpMethod, path: string): RouterReturns;
}
type RouteHandler = (
rawReq: RawRequest,
rawRes: RawResponse,
params: PathParam[],
queryString: any
) => Promise<void>;
class RouterReturns {
handle: RouteHandler | null;
params: PathParam[] | null;
}
interface PathParam {
key: string;
value: string;
}
A ready-made example of using this module can be found in the examples Ditsmod repository.
Installation and importing
Installation:
npm i @ditsmod/rest
Importing:
import { restModule } from '@ditsmod/rest';
@restModule({
// ..
})
export class AppModule {}
Custom router integration
If you want to integrate a custom router for the Ditsmod application, it is enough for your router to implement the above Router interface, after which it can be added to the providers at the application level:
import { restModule, Router } from '@ditsmod/rest';
import { MyRouter } from './my-router.js';
@restModule({
providersPerApp: [{ token: Router, useClass: MyRouter }],
})
export class MyCustomRouterModule {}
RouteExtension
This module exports RouteExtension. This extension returns metadata with the Array<MetadataPerMod3> interface — an array of dynamic metadata intended for creating request handlers. Each element of this array represents a separate route.
PreRouterExtension
This module also exports PreRouterExtension. This extension uses the metadata returned by RouteExtension to create HTTP request handlers.