n9-node-routing
Wrapper of project routing-controllers
Easily create express app in TypeScript with Decorators
Example :
import { Acl, Body, Get, JsonController, Service, Post } from 'n9-node-routing';
@Service()
@JsonController('/foo')
export class ValidateController {
@Acl([{ action: 'readFoo', user: '@' }])
@Get('/details')
public async getFoo(): Promise<object> {
return {
foo: 'bar',
};
}
@Post('/')
public async createFoo(@Body() body: ElementRequestCreate): Promise<any> {
return body;
}
}
- The "Custom validation decorators" require a new class instance and not only the class reference/name. Here an example.
- The validation with schema, here is the issue opened
n9-node-routing
V3
- Drop Node 14 support
- Rename from
n9-node-routing
to@neo9/n9-node-routing
- Services injected names change :
-
@Inject('N9HttpClient') httpClient: N9HttpClient
→@Inject() httpClient: N9HttpClient
-
@Inject('conf') conf: Configuration
→@Inject() conf: Configuration
-
@Inject('logger') logger: N9Log
→@Inject() logger: N9Log
or in constructor parameterslogger: N9Log
- Remove
global
properties :-
global.conf
andglobal.log
are not set or used anymore -
global.db
andglobal.dbClient
are not used too
-
-
- Log level is by default debug in development environment
- New lifecycle hook :
callbacksBeforeShutdownAfterExpressEnded
to stop databases - Exported functions has been renamed : Some of the exported functions has been renamed to better reflect what they are doing. This come from
class-transformer
update.classToPlain
→instanceToPlain
plainToClass
→plainToInstance
-
class-validator
andclass-transformer
update to version0.14.0
and0.5.1
n9-node-routing
V2
- Drop Node 12 support
- Startup hooks signature change (
beforeRoutingControllerLaunchHook
andafterRoutingControllerLaunchHook
) now an object is passed - Logger labels changes
- Swagger option
isEnable
is renamedisEnabled
with same default value as before totrue
-
n9-node-routing
now load the app configuration -
/ping
response is now an object :{ response: 'pong' }
-
/
response is now an object:{ name: 'myApi' }
-
/version' response is now an object :
{ version: '1.2.3' }` - Prometheus metrics are enabled by default. To disable them use
n9NodeRoutingOptions.prometheus.isEnabled: false
Features
API Documentation
Documentation available as openapi 3.0 format : /documentation.json
Swagger UI for API available at : /documentation
Starter
A starter app is available here : https://github.com/neo9/n9-node-microservice-skeleton
Init and started files
At startup n9NodeRouting
does in order :
- Find current environment (dev/.../pre-prod/prod)
- Load the conf for current environment
- Create a logger
- Print the current environment, app name and Node.js version (
process.version
) - * Validate configuration is enabled
- * Start APM if any
- Run
*.init.ts
(actually*.init.js
). Call default exported function with params :logger
,conf
- Startup express then start listening on the port 5000 by default
- * Register shutdown callback if any
- Run
*.started.ts
(actually*.started.js
). Call default exported function with params :logger
,conf
Some utils
-
Unified HttpClient using got
-
Cargo to group multiple small task into a bigger one, for example, multiple http calls
-
HttpCargoBuilder a simpler way to build a cargo to group HTTP calls
-
Validate configuration at startup and expose it on endpoint
/conf
⚠️ To hide passord, use the transformer like one of this usage@Allow() @Transform(SecretTransformer.GET_TRANSFORMER()) secret?: string; @Allow() @Transform(SecretTransformer.GET_TRANSFORMER(SecretType.OPAQUE)) secretOpaque?: string; @Allow() @Transform(SecretTransformer.GET_TRANSFORMER(SecretType.OPAQUE)) secretOpaqueNil?: string; @Allow() @Transform(SecretTransformer.GET_TRANSFORMER(SecretType.INVISIBLE)) // default secretInvisible?: string; @Allow() @Transform(SecretTransformer.GET_TRANSFORMER(SecretType.URI)) secretUri?: string;
Sentry
To use Sentry you only have to ask it to n9-node-routing :
- Basic usage : define the env variable
SENTRY_DSN
and it will activate it with default options. - Fill the sentry options with at least the
dsn
.
Default enabled options are :
- setting the app version in sentry release
- set the NODE_ENV as sentry environment
- enable tracing for ALL requests
Tests
To run all test : yarn test
To run a test containing foo : yarn test **/*foo*
To debug a test containing foo : yarn test:dev **/*foo*
it will watch your files a re-run this test each time
Dev
-
Install all dependencies and install git hooks with husky :
yarn
-
Run the project tests:
yarn test