Namespaces
A namespace decides where an operation sits in the generated client. A router that declares one puts its endpoints in a nested object named after it, so the client's shape follows your routers:
// src/endpoints/shop/router.module.ts
export default EndpointRouterModule.create({ namespace: true });
// src/endpoints/shop/recipes/router.module.ts
export default EndpointRouterModule.create({ namespace: true });// the request function and its hook, side by side
await client.shop.recipes.create({ body: { name: 'Pizza' } });
const create = client.shop.recipes.useCreate();Namespaces have no effect on URLs. Without any, every operation sits at the client's root under its full name: client.shopRecipesCreate().
The namespace option
| Value | Meaning |
|---|---|
Left out, or false | The router adds nothing. Its endpoints sit in its parent's namespace. |
true | The router's basePath if it sets one, otherwise its folder's name, in a router.module.ts. A router created elsewhere, such as in app.module.ts, needs a basePath or a string. |
| A string | That exact name. |
A folder with no router.module.ts is part of the URL but not of the namespace. Under a shop router with a namespace, shop/catering/book.endpoint.ts serves /shop/catering/book and is called as client.shop.cateringBook().
A router's description becomes a comment on its object in the client, which your editor shows on hover.
How names are derived
- An operation's id is its URL in PascalCase, without the
:paramsegments:/shop/recipes/edit/:recipeIdisShopRecipesEdit. - The request function is that id with a lowercase first letter, and the hook is
useand the id:shopRecipesEdit,useShopRecipesEdit. - Inside a namespace, the namespace's own prefix is dropped: in
shop.recipes, they areeditanduseEdit.
File, under namespaced shop and recipes routers | Client |
|---|---|
shop/homepage.endpoint.ts | client.shop.homepage() |
shop/recipes/create.endpoint.ts | client.shop.recipes.create() |
shop/recipes/edit/$recipeId.endpoint.ts | client.shop.recipes.edit({ path: { recipeId } }) |
shop/catering/book.endpoint.ts | client.shop.cateringBook() |
Two things follow from these rules:
- Give an endpoint file a name of its own in a namespaced folder. A bare
endpoint.tsinshop/recipes/has the idShopRecipes, which is the whole namespace, so it keeps its full name:client.shop.recipes.shopRecipes(). - Names must be unique. Two operations that land on the same name stop codegen with a message saying which. Rename the endpoint file or the namespace.
A client per front end
One server can generate several clients, each limited to the top-level namespaces it needs:
await setupCodegen(app, {
clients: [
// The whole API, for other back-end services.
{ type: 'axios', outputFile: process.cwd() + '/generated/backend.ts' },
// One client per front end.
{
type: 'react-query',
outputFile: process.cwd() + '/generated/admin-client.tsx',
namespaces: ['auth', 'admin'],
},
{
type: 'react-query',
outputFile: process.cwd() + '/generated/shop-client.tsx',
namespaces: ['auth', 'shop'],
},
],
});A filtered client has only the operations in those namespaces, and only the types they use. Operations outside any namespace are left out of it.