Migrating from NestJS
Navios will look familiar to a NestJS developer: modules, controllers, injectable services, guards, interceptors and the same exception class names. The differences are in where the description of an endpoint lives and in how per-request data is reached. This page maps the concepts and then covers the differences that need more than a rename.
Navios uses standard decorators. A NestJS project compiles with experimentalDecorators; either turn the option off for the code you move over, or keep it and import decorators from the legacy entry points.
Concept map
Endpoints are declared, not decorated
In NestJS the route, the DTOs and the status code are spread over decorators on the controller method. In Navios they are one declaration, made with @navios/builder, that the server validates against and a client can call.
What follows from this:
- There are no DTO classes and no
ValidationPipe.data,paramsandurlParamsarrive validated and typed from the schemas. Any Standard Schema library works. - Responses are validated too. Fields the response schema does not declare are stripped, so a
passwordcolumn cannot leak because someone returned the whole entity. @Endpointchecks the method at compile time: returning the wrong shape, or a status the declaration does not have, is a type error.- The declarations can live in a package shared with the front end. See Controllers and handlers and Using with Navios server.
Injection is a function call
There is no constructor injection and no emitDecoratorMetadata. A dependency is a field initialised with inject():
- A class decorated with
@Injectable()is resolvable by being imported. It does not have to be listed in a module'sproviders. providersexists for replacing what a token resolves to:provide(UserStore, PgUserStore).- A circular dependency is detected and reported. Break it with
asyncInject()on one side.
Modules group routes
A Navios module is a group of routes with a shared prefix, shared guards and shared interceptors. It is not an injection boundary: an application has one container, and there is no exports. A NestJS module that existed only to export providers has no counterpart and can be deleted; its services are imported where they are used.
Application-wide guards go into the guards option of NaviosFactory.create instead of APP_GUARD. See Modules.
Controllers are singletons
Controllers, guards and interceptors are created once. Nothing is injected per request, and a singleton that injects a request-scoped service fails at start-up instead of silently becoming request-scoped, as a NestJS provider would.
Per-request data travels in the RequestContext, which guards and interceptors receive as their first argument and handlers as their second:
This replaces request.user, custom parameter decorators such as @CurrentUser(), and most Scope.REQUEST providers. Per-request state covers the alternatives, and Request context lists everything on ctx.
Guards run before the body is read
The order of a request is guards, interceptors, validation, handler, as in NestJS, except that interceptors of the application (app.intercept) run around the guards. In NestJS pipes run after guards as well, but the body has already been parsed by then; in Navios it has not been read. A guard decides from headers, cookies, the route and its attributes, URL parameters and the query string, the last two as raw strings.
- A guard that needs the parsed body becomes a check in the handler.
- Only
truelets a request through. Returningfalseanswers 403; throw an exception for any other status. - Metadata read with
Reflectorbecomes an attribute read fromctx.route.
See Guards and interceptors and How a request is handled.
No middleware and no pipes
Errors are problem documents
The exception classes have the same names (NotFoundException, BadRequestException, and so on), but the response body differs. Navios answers every error as application/problem+json:
NestJS answers { "statusCode": 404, "message": "User not found", "error": "Not Found" }. Clients that read message or statusCode need updating, or the endpoint can declare its own body for a status in errorSchema. Validation errors are a 400 problem with source and an errors list, and the message of an unexpected error is not sent to the client unless exposeErrorDetails is on.
An exception filter becomes app.mapError(DomainError, (error, ctx) => ...). See Errors.