Skip to main content
Version: v10.0.0

NestJS

NestJS describes your controllers and DTOs as an OpenAPI document through @nestjs/swagger. The HyperFetch CLI reads that document and writes a typed SDK, so your frontend calls every route with the response, payload, params and query types of your DTOs. There is no NestJS package to install: the OpenAPI document is the contract.


Purpose
  1. No glue package: @nestjs/swagger and the HyperFetch CLI are all you need.
  2. Types from your DTOs: Responses, bodies, path params and query params come from the DTOs you already have.
  3. One command to resync: Change a controller, run the generate script, and the compiler shows every call to fix.

How it fits together​

StepWhereWhat happens
1NestJS@nestjs/swagger serves the OpenAPI document as JSON
2Terminal@hyper-fetch/cli generate reads it and writes src/api/nest.sdk.ts
3FrontendcreateSdk(client) gives you sdk.users.$get and the other requests

1. Serve the OpenAPI document​

Install the Swagger module in your NestJS app and register it in main.ts:

npm install @nestjs/swagger
main.ts
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";

import { AppModule } from "./app.module";

async function bootstrap() {
const app = await NestFactory.create(AppModule);

const config = new DocumentBuilder()
.setTitle("Shop API")
.setVersion("1.0")
.build();
SwaggerModule.setup("docs", app, () =>
SwaggerModule.createDocument(app, config),
);

await app.listen(3000);
}
bootstrap();

The UI is now on http://localhost:3000/docs and the JSON document on http://localhost:3000/docs-json. The CLI reads the JSON one.

DTO properties

@nestjs/swagger only sees the DTO properties it is told about. Decorate them with @ApiProperty(), or add the Swagger CLI plugin to nest-cli.json so it reads them from your TypeScript types:

nest-cli.json
{
"compilerOptions": {
"plugins": ["@nestjs/swagger"]
}
}

2. Generate the SDK​

Run the CLI in your frontend project while the NestJS app is running:

npx @hyper-fetch/cli@latest generate --template openapi --url http://localhost:3000/docs-json --fileName nest.sdk.ts

It writes nest.sdk.ts to src/api. On the first run it also creates api.json with that location. Every path becomes a key and every method a $-prefixed key on it, so GET /users/{id} is sdk.users.$id.$get.


3. Call your API​

import { createClient } from "@hyper-fetch/core";

import { createSdk } from "./api/nest.sdk";

export const client = createClient({ url: "http://localhost:3000" });
export const sdk = createSdk(client);


import { sdk } from "./client";

// Typed from the DTO of GET /users/{id}
const { data, error } = await sdk.users.$id.$get.setParams({ id: "42" }).send();

// Typed from the DTO of POST /users
await sdk.users.$post.send({
payload: { name: "Ada", email: "ada@example.com" },
});

The same requests work with useFetch and useSubmit from @hyper-fetch/react, with caching, retries and deduplication included.


Keep the SDK in sync​

Install the CLI in the frontend project and add a script, so regenerating is one command locally and in CI:

package.json
{
"scripts": {
"api:generate": "hyper-fetch generate -t openapi -u http://localhost:3000/docs-json -f nest.sdk.ts -o"
}
}

-o overwrites the old file without asking. To generate without a running server, write the document to a file in main.ts (writeFileSync("openapi.json", JSON.stringify(document))) and pass that path to --url instead.

When it's helpful
  • Your frontend and NestJS backend live in different repositories, so they can't share TypeScript types.
  • You want a renamed DTO field to fail the frontend build instead of failing at runtime.
  • You already keep Swagger docs for your API and want the client to come from the same source.
OpenAPI TypeScript client
Everything the generated SDK can do, from React hooks to regenerating it in CI.
Summary
  • @nestjs/swagger serves your API as an OpenAPI document at /docs-json.
  • @hyper-fetch/cli generate turns that document into a typed SDK.
  • createSdk(client) gives you typed requests for send(), useFetch and useSubmit.