|
1 | 1 | --- |
2 | | -title: Nest |
3 | | -description: Nest plugin for Hey API. Compatible with all our features. |
| 2 | +title: NestJS v11 Plugin |
| 3 | +description: Generate NestJS v11 controller methods from OpenAPI with the NestJS plugin for openapi-ts. Fully compatible with validators, transformers, and all core features. |
4 | 4 | --- |
5 | 5 |
|
6 | 6 | <script setup lang="ts"> |
7 | | -import FeatureStatus from '@components/FeatureStatus.vue'; |
| 7 | +import AuthorsList from '@components/AuthorsList.vue'; |
| 8 | +import Examples from '@components/Examples.vue'; |
| 9 | +import Heading from '@components/Heading.vue'; |
| 10 | +import { yuriMikhin } from '@data/people.js'; |
| 11 | +import VersionLabel from '@components/VersionLabel.vue'; |
8 | 12 | </script> |
9 | 13 |
|
10 | | -# Nest <span data-soon>soon</span> |
| 14 | +<Heading> |
| 15 | + <h1>NestJS<span class="sr-only"> v11</span></h1> |
| 16 | + <VersionLabel value="v11" /> |
| 17 | +</Heading> |
11 | 18 |
|
12 | | -<FeatureStatus issueNumber=1481 name="Nest" /> |
| 19 | +::: warning |
| 20 | +NestJS plugin is currently in beta. The interface might change before it becomes stable. We encourage you to leave feedback on [GitHub](https://github.com/hey-api/openapi-ts/issues). |
| 21 | +::: |
13 | 22 |
|
14 | 23 | ### About |
15 | 24 |
|
16 | 25 | [Nest](https://nestjs.com) is a progressive Node.js framework for building efficient, reliable and scalable server-side applications. |
17 | 26 |
|
| 27 | +The NestJS plugin for Hey API generates type-safe controller method signatures from your OpenAPI spec, fully compatible with all core features. |
| 28 | + |
| 29 | +### Collaborators |
| 30 | + |
| 31 | +<AuthorsList :people="[yuriMikhin]" /> |
| 32 | + |
| 33 | +## Features |
| 34 | + |
| 35 | +- NestJS v11 support |
| 36 | +- seamless integration with `@hey-api/openapi-ts` ecosystem |
| 37 | +- type-safe controller methods |
| 38 | +- minimal learning curve thanks to extending the underlying technology |
| 39 | + |
| 40 | +## Installation |
| 41 | + |
| 42 | +In your [configuration](/openapi-ts/get-started), add `nestjs` to your plugins and you'll be ready to generate NestJS artifacts. :tada: |
| 43 | + |
| 44 | +```js |
| 45 | +export default { |
| 46 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 47 | + output: 'src/client', |
| 48 | + plugins: [ |
| 49 | + // ...other plugins |
| 50 | + 'nestjs', // [!code ++] |
| 51 | + ], |
| 52 | +}; |
| 53 | +``` |
| 54 | + |
| 55 | +## Output |
| 56 | + |
| 57 | +The NestJS plugin will generate the following artifacts, depending on the input specification. |
| 58 | + |
| 59 | +## Controller Methods |
| 60 | + |
| 61 | +Operations are grouped by their first tag into separate types. |
| 62 | + |
| 63 | +::: code-group |
| 64 | + |
| 65 | +```ts [example] |
| 66 | +export type PetsControllerMethods = { |
| 67 | + createPet: (body: CreatePetData['body']) => Promise<CreatePetResponse>; |
| 68 | + listPets: (query?: ListPetsData['query']) => Promise<ListPetsResponse>; |
| 69 | + showPetById: (path: ShowPetByIdData['path']) => Promise<ShowPetByIdResponse>; |
| 70 | +}; |
| 71 | + |
| 72 | +export type StoreControllerMethods = { |
| 73 | + getInventory: () => Promise<GetInventoryResponse>; |
| 74 | +}; |
| 75 | +``` |
| 76 | + |
| 77 | +```ts [usage] |
| 78 | +import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common'; |
| 79 | + |
| 80 | +import type { PetsControllerMethods } from '../client/nestjs.gen'; |
| 81 | +import type { CreatePetData, ListPetsData, ShowPetByIdData } from '../client/types.gen'; |
| 82 | + |
| 83 | +@Controller('pets') |
| 84 | +export class PetsController implements Pick< |
| 85 | + PetsControllerMethods, |
| 86 | + 'createPet' | 'listPets' | 'showPetById' |
| 87 | +> { |
| 88 | + @Post() |
| 89 | + async createPet(@Body() body: CreatePetData['body']) {} |
| 90 | + |
| 91 | + @Get() |
| 92 | + async listPets(@Query() query?: ListPetsData['query']) {} |
| 93 | + |
| 94 | + @Get(':petId') |
| 95 | + async showPetById(@Param() path: ShowPetByIdData['path']) {} |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +::: |
| 100 | + |
| 101 | +## API |
| 102 | + |
| 103 | +You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/nestjs/types.ts) interface. |
| 104 | + |
| 105 | +<Examples githubExamplePath="/openapi-ts-nestjs" /> |
| 106 | + |
18 | 107 | <!--@include: ../../partials/sponsors.md--> |
0 commit comments