aladov3.13.0

Docs/Features

OpenAPI / Swagger

Alado generates OpenAPI 3.0 documentation automatically from your decorators. No extra config needed — just enable it in server options:

TypeScript
const app = initializeApplication({
  serverOptions: {
    port: 3000,
    openApiDoc: {
      enable: true,
      route: '/', // serve Swagger UI at this path
      info: {
        title: 'My API',
        description: 'Auto-generated with Alado',
        version: '1.0.0',
      },
    },
  },
  controllers: [UserController],
});

Open http://localhost:3000/ to view the Swagger UI. The page title (the browser tab) is Alado | <info.title>, Alado | My API here, or Alado when info.title is empty (3.9.13; Swagger UI's own title, "Swagger UI", before).

With enable: true, route (a string; '' is the same as '/') and info (an object of strings: title, version, description) are required. A missing or wrong one stops the server at startup with an error naming it, such as AladoServerOptions.openApiDoc.info should be an object; before 3.9.15 these errors said documentProperty, and info: null or an array was accepted.

The OpenAPI document is served under route: /docs/swagger.json when route is /docs, and /swagger.json only when route is /. Swagger UI's own files are served under route too (for example /docs/swagger-ui.css), so the docs never take over paths of your API. Only web assets from swagger-ui-dist are served, not its package.json, README or license files.

The docs answer HEAD like GET: same status and headers, no body. A GET or HEAD route on a path the docs answer (the docs page, swagger.json, a Swagger UI file) is refused at startup with an AladoConfigurationError (code: 'ROUTE_RESERVED_FOR_OPEN_API'). Other methods on those paths, and other paths under route, stay free for your API.

The docs are public: if they should not be, leave openApiDoc disabled in production or keep route behind your proxy's access control.

Each server has its own document: two applications in one process (say a public API and an admin API) never list each other's routes. Path parameters are always documented as required, also when their DTO does not declare them.

Documentation is built from:

  • @get, @post, etc. → endpoint paths and methods
  • tags, title, description in decorator options → grouping and descriptions
  • @defineRequest + DTO @validateProperty (the schema of its validate chain, 3.9.22) and @documentProperty (description, example, schema keys on top) → request schema; see @documentProperty
  • @defineResponse → response schema
  • isHidden: true → excludes endpoint from docs (the route still answers)
  • disable: true → the route is switched off: not in the docs and not served (see Switching a route off)

The generated document is valid OpenAPI 3.0:

  • Response schemas are named by entity, otherwise after the route (GetUsersById200), so routes never overwrite each other's schema. See @defineResponse.
  • Every response has a description (description, then title, then "<status> response"); a 204 or 304 has no content.
  • Request body fields declared with required: true, or validated by a chain with .required() (3.9.20), are listed in the schema's required, and the request body is marked required; such header and query parameters are required: true.
  • Every parameter and body property has a schema, {} (any value) when neither its validator nor @documentProperty describes it (3.9.21); a body property carries its description and example.
  • @withAuth adds an apiKey security scheme for each strategy that reads a header or a query parameter; optional auth (required: false) is documented as optional. A credential read from the body or the path is documented by that field only: OpenAPI has no security scheme for it.
  • Several strategies keep their meaning in the document: @withAuth(bearerAuth, apiKeyAuth) gives security: [{ headersAuthorization: [] }, { 'headersX-api-key': [] }] (either one), and a group such as [apiKeyAuth, bearerAuth] gives one entry naming both schemes (both needed). See Authentication.
  • In the @defineResponse example body, an array is documented by its first item, null as nullable, a Date as a date-time string and nested objects by their properties. An object that has schema keywords only and names a schema type ({ type: 'string', format: 'uuid' }) is taken as a schema written by hand; { type: 'admin', name: 'x' } is example data.

With CORS enabled the docs carry the same CORS headers as the API, so a Swagger UI or a client generator on an allowed origin can load swagger.json. The docs are public: there is no auth in front of them, so enable them only where the API description may be read.