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:
nestjs-endpointsin thedependenciesof apackage.json.- Files named
*.endpoint.tsorendpoint.ts, orrouter.module.ts. - A call to
setupCodegen(in amain.tsor 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) orendpoint.mutation({...})(a write). Its URL comes from where the file sits, under the folder of the nearestrouter.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
controllersor a router'sendpoints, unless it is registered by hand on purpose, with apath. - The app's root module calls
NestjsEndpoints.forRoot({ router }), once. Every other router isEndpointRouterModule.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
tscornest build, never Webpack or a bundler. queryandparamsvalues arrive as strings. Read them withz.coerce.number(),z.coerce.date()orz.stringbool().- A new endpoint file is served after the app restarts.
nest start --watchrestarts by itself; withnode --watch,NestjsEndpoints.forRootneedswatch: 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:
| Deprecated | Use |
|---|---|
EndpointsRouterModule | EndpointRouterModule |
EndpointsRouterModule.register() | EndpointRouterModule.create() |
injectMethod, injectAtRequest | injectOnRequest |
Add an endpoint
- Create
<router folder>/<segments>/<name>.endpoint.ts. The path from the router's folder is the URL:orders/create.endpoint.tsisPOST /orders/create. export default endpoint.query({...})for a read,endpoint.mutation({...})for a write, with itsinput,query,paramsandoutputschemas and itshandler. See Writing an endpoint and Endpoint options.- Give it the providers it needs with
inject, and list them in theprovidersof the router that owns the folder. See Dependency injection. - 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 addparams: 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
pathon the endpoint, orbasePathon 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, inmain.tsor 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
namespacenests its operations (client.shop.recipes.list). A client'snamespaceslimits 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
namespacesleaves out.
Test an endpoint
- Over HTTP: start the app with
Test.createTestingModule, and send requests with supertest. See Over HTTP. - Through the client: point a generated client at the test app. See Through the generated client.
- One router alone, without the app module: see Testing one router.
- The handler alone: call it with
invoke(). See Integration tests.
An error or a 404
Troubleshooting has each error message the library throws or answers with, and its cause.