Skip to content

Agent playbook ​

Recipes for a coding agent working on an HTTP API built with nestjs-endpoints. Each one links to the page with the details. The pages sit beside this one, as Markdown.

Is this repo using it? ​

Check, in order:

  1. nestjs-endpoints in the dependencies of a package.json.
  2. Files named *.endpoint.ts or endpoint.ts, or router.module.ts.
  3. A call to setupCodegen( in a main.ts or another bootstrap file.

If none of those appear, the library is not in play, and these pages don't apply.

Rules ​

  • One file, one endpoint. An endpoint file default-exports one endpoint.query({...}) (a read) or endpoint.mutation({...}) (a write). Its URL comes from where the file sits, under the folder of the nearest router.module.ts. See File-based routing.
  • Nothing registers an endpoint file by hand. A router serves every endpoint file in its folder. Don't add one to a module's controllers or a router's endpoints, unless it is registered by hand on purpose, with a path.
  • The app's root module calls NestjsEndpoints.forRoot({ router }), once. Every other router is EndpointRouterModule.create(). See Routers.
  • ESM only. The project has "type": "module", and its relative imports end in .js.
  • No bundling. File-based routing reads each endpoint file on disk, so the app is built with tsc or nest build, never Webpack or a bundler.
  • query and params values arrive as strings. Read them with z.coerce.number(), z.coerce.date() or z.stringbool().
  • A new endpoint file is served after the app restarts. nest start --watch restarts by itself; with node --watch, NestjsEndpoints.forRoot needs watch: true. See Development.
  • Every Vitest config inlines the package: server.deps.inline: ['nestjs-endpoints']. See Testing.
  • Don't edit a generated client. It is rewritten from the endpoints. Change the endpoint, and regenerate.
  • Use the current names. These still work, but new code uses the name beside them:
DeprecatedUse
EndpointsRouterModuleEndpointRouterModule
EndpointsRouterModule.register()EndpointRouterModule.create()
injectMethod, injectAtRequestinjectOnRequest

Add an endpoint ​

  1. Create <router folder>/<segments>/<name>.endpoint.ts. The path from the router's folder is the URL: orders/create.endpoint.ts is POST /orders/create.
  2. export default endpoint.query({...}) for a read, endpoint.mutation({...}) for a write, with its input, query, params and output schemas and its handler. See Writing an endpoint and Endpoint options.
  3. Give it the providers it needs with inject, and list them in the providers of the router that owns the folder. See Dependency injection.
  4. Restart the app if it doesn't restart by itself, and regenerate the client if the app has one.

An endpoint is a POST unless it sets method. A GET takes no input: its request is query.

Change an endpoint's URL ​

  • A path parameter: name the file or folder $name (orders/$orderId.endpoint.ts), and add params: z.object({ orderId: z.coerce.number() }). See Path parameters.
  • A folder that groups files without adding to the URL: start its name with _.
  • A different URL, without moving the file: set path on the endpoint, or basePath on its router. See Changing a route.

Give a folder its own providers or guards ​

Put a router.module.ts in the folder, with export default EndpointRouterModule.create({ providers, guards }). The parent router imports it by itself, and its base path is the folder's name. Its middleware, interceptors and guards apply to every endpoint under it. See Nested routers and Middleware, interceptors and guards.

A value every endpoint of a router needs, such as the current user, goes in the router's endpointConfig. See Shared injections.

Regenerate and call the client ​

  • Where it is generated: find the setupCodegen( call, in main.ts or a script of its own. It writes the clients when the app starts, and rewrites one only when what it is generated from changed, such as an endpoint's schema. See Generating clients.
  • Calling it: a method takes one object, { path, query, body }, with the parts the endpoint declares. See axios and React Query.
  • Its shape: a router with namespace nests its operations (client.shop.recipes.list). A client's namespaces limits it to part of the API. See Namespaces.
  • A route missing from a client: it is a plain controller, or in a namespace the client's namespaces leaves out.

Test an endpoint ​

An error or a 404 ​

Troubleshooting has each error message the library throws or answers with, and its cause.

Released under the MIT License.