-
Notifications
You must be signed in to change notification settings - Fork 503
/
requestExpressController.ts
113 lines (104 loc) · 4.47 KB
/
requestExpressController.ts
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
import { Request as ExRequest } from 'express';
import { Body, Controller, Get, Produces, Path, Post, Response, Res, Request, Route, SuccessResponse, TsoaResponse } from '@tsoa/runtime';
import { ErrorResponseModel, UserResponseModel } from '../../fixtures/testModel';
interface UserResponseV2Model {
id: number;
username: string;
}
interface UserResponseV3Model {
id: number;
nickname: string;
}
interface UserResponseV4Model {
id: number;
codename: string;
}
interface UserRequestV3Model {
nickname: string;
codename?: string;
}
interface UserRequestV4Model {
codename: string;
nickname?: string;
}
@Route('RequestAcceptHeaderTest')
@Produces('application/vnd.mycompany.myapp+json')
@Produces('application/vnd.mycompany.myapp.v2+json')
export class RequestAcceptHeaderTestController extends Controller {
@Get('Default/{userId}')
public async getDefaultProduces(@Path() userId: number, @Request() req: ExRequest): Promise<UserResponseModel | UserResponseV2Model> {
if (req.accepts(['application/vnd.mycompany.myapp+json', 'application/json'])) {
this.setHeader('Content-Type', 'application/vnd.mycompany.myapp+json');
return Promise.resolve({
id: userId,
name: 'foo',
} as UserResponseModel);
} else if (req.accepts('application/vnd.mycompany.myapp.v2+json')) {
this.setHeader('Content-Type', 'application/vnd.mycompany.myapp.v2+json');
return Promise.resolve({
id: userId,
username: 'foo',
} as UserResponseV2Model);
}
throw new Error('unsupported media type');
}
@Produces('application/vnd.mycompany.myapp+json')
@Produces('application/vnd.mycompany.myapp.v2+json')
@Produces('application/vnd.mycompany.myapp.v3+json')
@Produces('application/vnd.mycompany.myapp.v4+json')
@Get('Multi/{userId}')
public async getMultiProduces(@Path() userId: number, @Request() req: ExRequest): Promise<UserResponseModel | UserResponseV2Model | UserResponseV3Model | UserResponseV4Model> {
if (req.accepts(['application/vnd.mycompany.myapp+json', 'application/json'])) {
this.setHeader('Content-Type', 'application/vnd.mycompany.myapp+json');
return Promise.resolve({
id: userId,
name: 'foo',
} as UserResponseModel);
} else if (req.accepts('application/vnd.mycompany.myapp.v2+json')) {
this.setHeader('Content-Type', 'application/vnd.mycompany.myapp.v2+json');
return Promise.resolve({
id: userId,
username: 'foo',
} as UserResponseV2Model);
} else if (req.accepts('application/vnd.mycompany.myapp.v3+json')) {
this.setHeader('Content-Type', 'application/vnd.mycompany.myapp.v3+json');
return Promise.resolve({
id: userId,
nickname: 'foo',
} as UserResponseV3Model);
} else if (req.accepts('application/vnd.mycompany.myapp.v4+json')) {
this.setHeader('Content-Type', 'application/vnd.mycompany.myapp.v4+json');
return Promise.resolve({
id: userId,
codename: 'foo',
} as UserResponseV4Model);
}
throw new Error('unsupported media type');
}
@SuccessResponse('202', 'Accepted', ['application/vnd.mycompany.myapp.v3+json', 'application/vnd.mycompany.myapp.v4+json'])
@Response<ErrorResponseModel>('400', 'Bad Request', undefined, ['application/problem+json', 'application/json'])
@Post('Multi')
async postCustomProduces(
@Body() model: UserRequestV3Model | UserRequestV4Model,
@Res() conflictRes: TsoaResponse<409, { message: string }, { 'Content-Type': 'application/problem+json' }>,
@Request() req: ExRequest,
): Promise<UserResponseV3Model | UserResponseV4Model> {
const { nickname, codename } = model;
if (nickname === 'bar' || codename === 'bar') {
this.setHeader('Content-Type', 'application/problem+json');
return conflictRes?.(409, { message: 'Conflict' });
}
if (req.headers['content-type'] === 'application/vnd.mycompany.myapp.v3+json') {
const body = { id: model.nickname?.length, nickname: model.nickname };
this.setStatus(202);
this.setHeader('Content-Type', 'application/vnd.mycompany.myapp.v3+json');
return body as UserResponseV3Model;
} else if (req.headers['content-type'] === 'application/vnd.mycompany.myapp.v4+json') {
const body = { id: model.codename?.length, codename: model.codename };
this.setStatus(202);
this.setHeader('Content-Type', 'application/vnd.mycompany.myapp.v4+json');
return body as UserResponseV4Model;
}
throw new Error('unsupported media type');
}
}