-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathstarships.controller.ts
More file actions
114 lines (108 loc) · 3.62 KB
/
starships.controller.ts
File metadata and controls
114 lines (108 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Controller, Get, Post, Body, Res } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ZodResponse } from 'nestjs-zod';
import { CreateStarshipFormDto, StarshipDto, StarshipListDto, Starship } from './starships.dto';
import { Response } from 'express';
@ApiTags('Starships')
@Controller('api/starships')
export class StarshipsController {
private mockStarships: Starship[] = [
{
id: 1,
name: 'CR90 corvette',
model: 'CR90 corvette',
manufacturer: 'Corellian Engineering Corporation',
costInCredits: '3500000',
length: '150',
maxAtmospheringSpeed: '950',
crew: '30-165',
passengers: '600',
cargoCapacity: '3000000',
consumables: '1 year',
hyperdriveRating: '2.0',
mglt: '60',
starshipClass: 'corvette',
pilotIds: [],
filmIds: ['1', '3', '6'],
},
{
id: 2,
name: 'Star Destroyer',
model: 'Imperial I-class Star Destroyer',
manufacturer: 'Kuat Drive Yards',
costInCredits: '150000000',
length: '1,600',
maxAtmospheringSpeed: '975',
crew: '47,060',
passengers: 'n/a',
cargoCapacity: '36000000',
consumables: '2 years',
hyperdriveRating: '2.0',
mglt: '60',
starshipClass: 'Star Destroyer',
pilotIds: [],
filmIds: ['1', '2', '3'],
},
{
id: 3,
name: 'Sentinel-class landing craft',
model: 'Sentinel-class landing craft',
manufacturer: 'Sienar Fleet Systems, Cyngus Spaceworks',
costInCredits: '240000',
length: '38',
maxAtmospheringSpeed: '1000',
crew: '5',
passengers: '75',
cargoCapacity: '180000',
consumables: '1 month',
hyperdriveRating: '1.0',
mglt: '70',
starshipClass: 'landing craft',
pilotIds: [],
filmIds: ['1'],
},
];
@Get()
@ZodResponse({ type: StarshipListDto, description: 'List of all starships' })
getStarships() {
return {
data: this.mockStarships,
};
}
@Post()
@ZodResponse({ status: 201, type: StarshipDto, description: 'Starship created successfully' })
createStarship(@Body() createStarshipDto: CreateStarshipFormDto) {
const newStarship = {
...createStarshipDto,
id: Math.floor(Math.random() * 1000) + 4
};
this.mockStarships.push(newStarship);
return newStarship;
}
/**
* This example shows how to use the @Res decorator to set headers on the
* response. Note that in order for `@ZodResponse` to work, we need to return
* the data from the function and use `passthrough: true`. We can't use
* `res.send()`! `res.send()` bypasses the zod nestjs interceptor
*
* For more information, search `passthrough` in the zod documentation page
* for controllers here: https://docs.nestjs.com/controllers
*
* > Nest detects when the handler is using either @Res() or @Next(), indicating
* > you have chosen the library-specific option. If both approaches are used at
* > the same time, the Standard approach is automatically disabled for this
* > single route and will no longer work as expected. To use both approaches at
* > the same time (for example, by injecting the response object to only set
* > cookies/headers but still leave the rest to the framework), you must set
* > the passthrough option to true in the @Res({ passthrough: true })
* > decorator.
*/
@Get('headers-example')
@ZodResponse({ type: StarshipListDto, description: 'List of all starships' })
getStarships2(@Res({ passthrough: true }) res: Response) {
res.header('X-Example', 'example');
return {
data: this.mockStarships,
};
}
}