Migrating to 2.0
This page lists what changed between @navios/core 1.x and @navios/server 2.0, the Fastify and Bun adapters, @navios/jwt, @navios/schedule, @navios/openapi, @navios/otel and @navios/adapter-xml. Upgrade them together. The packages that take part in HTTP require @navios/server 2.0; @navios/jwt, @navios/schedule and the main entry of @navios/otel require only @navios/core 2.0.
2.0 rewrites the request pipeline. Endpoint declarations made with @navios/builder stay as they are apart from the changes in Builder's migration page. Controllers, guards, bootstrap code and tests change. The main ideas:
- Controllers, guards and interceptors are singletons. Per-request data lives in a
RequestContextthat is passed to them. - One
@Endpointdecorator serves every kind of endpoint. Body kind, response kind and status codes come from the declaration. - Every error response is
application/problem+jsonunless the endpoint declares a body for that status. - Guards run before the request body is read.
- Any Standard Schema library works on the server. Zod keeps working unchanged.
- CORS, multipart, OpenAPI, OpenTelemetry and XML are configured the same way on every adapter.
- The HTTP framework is now
@navios/server.@navios/coreis the part that does not depend on HTTP.
The package is now @navios/server
In 1.x @navios/core was the HTTP framework. In 2.0 it is split in two:
@navios/coreholds what an application is made of whatever reaches it: dependency injection, modules, plugins, lifecycle, configuration and logging.@navios/serveris the HTTP framework on top of it: controllers, endpoints, the request pipeline, adapters andNaviosFactory. It re-exports everything from@navios/core.
For an HTTP application the change is the package name:
The same goes for the entry points @navios/core/testing and @navios/core/legacy-compat, and for the Register declaration, which is now written declare module '@navios/server'.
Packages that do not need HTTP depend on @navios/core alone: @navios/jwt, @navios/schedule and the main entry of @navios/otel. An application can also run without HTTP, or with HTTP and something else side by side; see Other transports.
What changed
Application and adapters
Controllers and handlers
Guards and request data
Dependency injection
These are the DI changes that show up in server code. DI's migration page has the full list.
Errors, plugins and tests
Modules and lifecycle
Configuration and logging
@navios/jwt
@navios/schedule
@navios/openapi
@navios/otel
@navios/adapter-xml
Bootstrap
The Register declaration is optional and types ctx.raw and app.getServer(). methods is needed on Fastify, where @fastify/cors allows a preflight request the methods GET, HEAD and POST only. The adapter pages list every option: Fastify, Bun.
Controllers
The declaration of createUser changes from responseSchema: userSchema to responses: { 201: userSchema }.
- The method takes the
RequestContextas an optional second parameter. - No return annotation is needed;
@Endpointchecks the returned value against the declaration. handler()is an optional alternative that defines a handler as a class field with inferred parameter types:getUser = handler(getUser, async ({ urlParams }, ctx) => ...).throw new NotFoundException('User not found')still works, but the response body is now a problem document,{ type, title, status, detail }, unless the endpoint declares 404 inerrorSchema. Clients that read the old body shape need updating.
See Controllers and handlers and Errors.
Guards and the current user
Two behaviour changes to check:
- Guards are singletons. A guard that stored anything per request in a field must move it to the context.
- Guards run before the body is read and before the query is validated. A guard that inspected the parsed body has to become a check in the handler, or read what it needs from headers.
See Guards and interceptors and Authentication with JWT.
Request-scoped services
In 1.x a request-scoped service injected into a controller made the controller request-scoped too. In 2.0 that fails at init(). Most such services become a request value:
Services that used to inject the request-scoped service now take the value as an argument, as ItemsService.list(tenantId) does here. When passing it down is impractical, turn on asyncContext and read currentContext()?.get(Tenant). A class that really has to be constructed per request stays request-scoped and is reached with ctx.use(TheClass) in the handler. See Per-request state.
Streams and multipart
The declarations carry the kind: response: 'stream' on exportCsv and body: 'multipart' on upload. Multipart data contains strings and File objects on both adapters. See Streams and server-sent events and File uploads.
Plugins
Plugins no longer choose a stage. The same shape applies to the plugins of @navios/openapi, @navios/otel and @navios/adapter-xml, which are all passed in plugins. @navios/schedule is a transport.
See Plugins.
Tests
Tests that started a server and used supertest against app.getServer() can run in process instead:
app.getServer() still returns the native server. On Bun it exists only after listen(). See Testing.
Not available in 2.0
- A test client that returns the builder's typed result.
createTestClientreturns the raw status and body. - Sockets on the server.
@navios/clihot reload. The CLI has not been ported to 2.0.