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.
- No glue package:
@nestjs/swaggerand the HyperFetch CLI are all you need. - Types from your DTOs: Responses, bodies, path params and query params come from the DTOs you already have.
- One command to resync: Change a controller, run the generate script, and the compiler shows every call to fix.
How it fits together
| Step | Where | What happens |
|---|---|---|
| 1 | NestJS | @nestjs/swagger serves the OpenAPI document as JSON |
| 2 | Terminal | @hyper-fetch/cli generate reads it and writes src/api/nest.sdk.ts |
| 3 | Frontend | createSdk(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
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.
@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:
{
"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:
{
"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.
- 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.
@nestjs/swaggerserves your API as an OpenAPI document at/docs-json.@hyper-fetch/cli generateturns that document into a typed SDK.createSdk(client)gives you typed requests forsend(),useFetchanduseSubmit.
