fork of hey-api/openapi-ts because I need some additional things
1import { Body, Controller, Get, NotFoundException, Param, Post, Query } from '@nestjs/common';
2
3import type { PetsControllerMethods } from '../client/nestjs.gen';
4import type { CreatePetData, ListPetsData, Pet, ShowPetByIdData } from '../client/types.gen';
5
6@Controller('pets')
7export class PetsController implements Pick<
8 PetsControllerMethods,
9 'createPet' | 'listPets' | 'showPetById'
10> {
11 private readonly pets: Pet[] = [
12 { id: '1', name: 'Fido', status: 'available', tag: 'dog' },
13 { id: '2', name: 'Kitty', status: 'available', tag: 'cat' },
14 ];
15
16 @Get()
17 async listPets(@Query() query?: ListPetsData['query']) {
18 const limit = query?.limit ?? 20;
19
20 return this.pets.slice(0, limit);
21 }
22
23 @Post()
24 async createPet(@Body() body: CreatePetData['body']) {
25 const pet: Pet = {
26 id: crypto.randomUUID(),
27 name: body.name,
28 status: 'available',
29 tag: body.tag,
30 };
31
32 this.pets.push(pet);
33
34 return pet;
35 }
36
37 @Get(':petId')
38 async showPetById(@Param() path: ShowPetByIdData['path']) {
39 const pet = this.pets.find((p) => p.id === path.petId);
40
41 if (!pet) {
42 throw new NotFoundException(`Pet ${path.petId} not found`);
43 }
44
45 return pet;
46 }
47}