-
Notifications
You must be signed in to change notification settings - Fork 504
/
getController.ts
396 lines (345 loc) · 11.1 KB
/
getController.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
///<reference path="../tsoaTestModule.d.ts" />
import { Controller, Example, Get, OperationId, Queries, Query, Request, Res, Response, Route, SuccessResponse, Tags, TsoaResponse } from '@tsoa/runtime';
import { Readable } from 'stream';
import TsoaTest from 'tsoaTest';
import '../duplicateTestModel';
import {
GenericModel,
GetterClass,
GetterInterface,
GetterInterfaceHerited,
IndexedValue,
IndexedValueGeneric,
IndexedValueReference,
IndexedValueTypeReference,
ParenthesizedIndexedValue,
SimpleClassWithToJSON,
TestClassModel,
TestModel,
TestSubModel,
} from '../testModel';
import { ModelService } from './../services/modelService';
export type BadRequest = TsoaResponse<400, TestModel, { name: 'some_thing' }>;
export type ForbiddenRequest = TsoaResponse<401, TestModel, { name: 'another some_thing' }>;
export type BadAndInternalErrorRequest = TsoaResponse<400 | 500, TestModel, { name: 'combine' }>;
export const PathFromConstant = 'PathFromConstantValue';
export enum EnumPaths {
PathFromEnum = 'PathFromEnumValue',
}
@Route('GetTest')
export class GetTestController extends Controller {
/**
* This is a description of the getModel method
* this is some more text on another line
*/
@Get()
@SuccessResponse('200', 'Returns TestModel')
// Have to give also values that are not part of the Swagger schema to get example to caller.
@Example<TestModel>({
and: { value1: 'foo', value2: 'bar' },
boolArray: [true, false],
boolValue: true,
dateValue: new Date(),
id: 1,
modelValue: {
email: 'test@test.com',
id: 100,
},
modelsArray: new Array<TestSubModel>(),
numberArray: [1, 2, 3],
numberArrayReadonly: [1, 2, 3],
numberValue: 1,
objLiteral: {
name: 'a string',
},
object: {
a: 'a',
},
objectArray: [
{
a: 'a',
},
],
optionalString: 'optional string',
or: { value1: 'Foo' },
referenceAnd: { value1: 'foo', value2: 'bar' },
strLiteralArr: ['Foo', 'Bar'],
strLiteralVal: 'Foo',
stringArray: ['string one', 'string two'],
stringValue: 'a string',
nullableStringLiteral: 'NULLABLE_LIT_1',
undefineableUnionPrimitiveType: undefined,
undefinedValue: undefined,
})
public async getModel(): Promise<TestModel> {
return new ModelService().getModel();
}
@Get('Current')
public async getCurrentModel(): Promise<TestModel> {
return new ModelService().getModel();
}
@Get('ClassModel')
public async getClassModel(): Promise<TestClassModel> {
return new ModelService().getClassModel();
}
@Get('GetterClass')
public async getGetterClass(): Promise<GetterClass> {
return new GetterClass();
}
@Get('SimpleClassWithToJSON')
public async simpleClassWithToJSON(): Promise<SimpleClassWithToJSON> {
return new SimpleClassWithToJSON('hello, world', true);
}
@Get('GetterInterface')
public async getGetterInterface(): Promise<GetterInterface> {
return {} as GetterInterface;
}
@Get('GetterInterfaceHerited')
public async getGetterInterfaceHerited(): Promise<GetterInterfaceHerited> {
return {} as GetterInterfaceHerited;
}
@Get('ModuleRedeclarationAndNamespace')
public async getModuleRedeclarationAndNamespace(): Promise<TsoaTest.TestModel73> {
return {} as TsoaTest.TestModel73;
}
@Get('NamespaceWithTypeCastedObject')
public async getNamespaceWithTypeCastedObject() {
const test = { value: 'test' };
return {
value: test as TsoaTest.TestModel73,
};
}
@Get('Multi')
public async getMultipleModels(): Promise<TestModel[]> {
return [new ModelService().getModel(), new ModelService().getModel(), new ModelService().getModel()];
}
/**
* @param numberPathParam This is a description for numberPathParam
* @param numberParam This is a description for numberParam
* @isDouble numberPathParam
* @minimum numberPathParam 1
* @maximum numberPathParam 10
*
* @minLength stringPathParam 1
* @maxLength stringPathParam 10
*
* @isString stringParam Custom error message
* @minLength stringParam 3
* @maxLength stringParam 10
*/
@Get('{numberPathParam}/{booleanPathParam}/{stringPathParam}')
public async getModelByParams(
numberPathParam: number,
stringPathParam: string,
booleanPathParam: boolean,
@Query() booleanParam: boolean,
@Query() stringParam: string,
@Query() numberParam: number,
@Query() optionalStringParam = '',
) {
const model = new ModelService().getModel();
model.optionalString = optionalStringParam;
model.numberValue = numberPathParam;
model.boolValue = booleanPathParam;
model.stringValue = stringPathParam;
return model;
}
@Get('AllQueriesInOneObject')
public async getAllQueriesInOneObject(@Queries() queryParams: QueryParams) {
const model = new ModelService().getModel();
model.optionalString = queryParams.optionalStringParam;
model.numberValue = queryParams.numberParam;
model.boolValue = queryParams.booleanParam;
model.stringValue = queryParams.stringParam;
return model;
}
@Get('WildcardQueries')
public async getWildcardQueries(@Queries() queryParams: { [name: string]: any }) {
const model = new ModelService().getModel();
model.anyType = queryParams;
return model;
}
@Get('TypedRecordQueries')
public async getTypedRecordQueries(@Queries() queryParams: { [name: string]: number }) {
const model = new ModelService().getModel();
model.anyType = queryParams;
return model;
}
@Get('ResponseWithUnionTypeProperty')
public async getResponseWithUnionTypeProperty(): Promise<Result> {
return {
value: 'success',
};
}
@Get('UnionTypeResponse')
public async getUnionTypeResponse(): Promise<string | boolean> {
return '';
}
@Get('Request')
public async getRequest(@Request() request: any): Promise<TestModel> {
const model = new ModelService().getModel();
// set the stringValue from the request context to test successful injection
model.stringValue = request.stringValue;
return model;
}
@Get('DateParam')
public async getByDataParam(@Query() date: Date): Promise<TestModel> {
const model = new ModelService().getModel();
model.dateValue = date;
return model;
}
@Get('ThrowsError')
@Response<CustomError>('400')
public async getThrowsError(): Promise<TestModel> {
throw {
message: 'error thrown',
status: 400,
};
}
@Get('GeneratesTags')
@Tags('test', 'test-two')
public async getGeneratesTags(): Promise<TestModel> {
return new ModelService().getModel();
}
@Get('CustomOperationId')
@OperationId('MyCustomOperationId')
public async getCustomOperationId(): Promise<TestModel> {
return new ModelService().getModel();
}
@Get('HandleBufferType')
public async getBuffer(@Query() buffer: Buffer): Promise<Buffer> {
return Buffer.from('testbuffer');
}
@Get('HandleStreamType')
public async getStream(): Promise<Readable> {
const readable = new Readable();
readable._read = () => ({});
readable.push(Buffer.from('testbuffer'));
readable.push(null);
return readable;
}
@Get('GenericModel')
public async getGenericModel(): Promise<GenericModel<TestModel>> {
return {
result: new ModelService().getModel(),
};
}
@Get('GenericModelArray')
public async getGenericModelArray(): Promise<GenericModel<TestModel[]>> {
return {
result: [new ModelService().getModel()],
};
}
@Get('GenericPrimitive')
public async getGenericPrimitive(): Promise<GenericModel<string>> {
return {
result: new ModelService().getModel().stringValue,
};
}
@Get('GenericPrimitiveArray')
public async getGenericPrimitiveArray(): Promise<GenericModel<string[]>> {
return {
result: new ModelService().getModel().stringArray,
};
}
@Get('Void')
public async getVoid(): Promise<void> {
return Promise.resolve();
}
/**
* @param res The alternate response
*/
@Get('Res')
public async getRes(@Res() res: TsoaResponse<400, TestModel, { 'custom-header': string }>): Promise<void> {
res?.(400, new ModelService().getModel(), { 'custom-header': 'hello' });
}
/**
* @param res The alternate response
*/
@Get('Res_Alias')
public async getResAlias(@Res() res: BadRequest): Promise<void> {
res?.(400, new ModelService().getModel(), { name: 'some_thing' });
}
/**
* @param res The alternate response
* @param res Another alternate response
*/
@Get('MultipleRes')
public async multipleRes(@Res() res: TsoaResponse<400, TestModel, { 'custom-header': string }>, @Res() anotherRes: TsoaResponse<401, TestModel, { 'custom-header': string }>): Promise<Result> {
res?.(400, new ModelService().getModel(), { 'custom-header': 'hello' });
anotherRes?.(401, new ModelService().getModel(), { 'custom-header': 'another hello' });
return {
value: 'success',
};
}
/**
* @param res The alternate response
*/
@Get('MultipleRes_Alias')
public async multipleResAlias(@Res() res: BadRequest, @Res() anotherRes: ForbiddenRequest): Promise<Result> {
res?.(400, new ModelService().getModel(), { name: 'some_thing' });
anotherRes?.(401, new ModelService().getModel(), { name: 'another some_thing' });
return {
value: 'success',
};
}
/**
* @param res The alternate response
*/
@Get('MultipleStatusCodeRes')
public async multipleStatusCodeRes(@Res() res: TsoaResponse<400 | 500, TestModel, { 'custom-header': string }>, @Query('statusCode') statusCode: 400 | 500): Promise<void> {
res?.(statusCode, new ModelService().getModel(), { 'custom-header': 'hello' });
}
/**
* @param res The alternate response
*/
@Get('MultipleStatusCodeRes_Alias')
public async multipleStatusCodeResAlias(@Res() res: BadAndInternalErrorRequest, @Query('statusCode') statusCode: 400 | 500): Promise<void> {
res?.(statusCode, new ModelService().getModel(), { name: 'combine' });
}
@Get(PathFromConstant)
public async getPathFromConstantValue(): Promise<TestModel> {
return new ModelService().getModel();
}
@Get(EnumPaths.PathFromEnum)
public async getPathFromEnumValue(): Promise<TestModel> {
return new ModelService().getModel();
}
@Get('IndexedValue')
public async getIndexedValue(): Promise<IndexedValue> {
return 'FOO';
}
@Get('ParenthesizedIndexedValue')
public async getParenthesizedIndexedValue(): Promise<ParenthesizedIndexedValue> {
return 'FOO';
}
@Get('IndexedValueReference')
public async getIndexedValueReference(): Promise<IndexedValueReference> {
return 'FOO';
}
@Get('IndexedValueGeneric')
public async getIndexedValueGeneric(): Promise<IndexedValueGeneric<IndexedValueTypeReference>> {
return 'FOO';
}
@Get('UnionTypeWithDefault')
public async getUnionTypeWithDefault(@Query() unionType: 'a' | 'b' | undefined = 'a'): Promise<void> {
return;
}
}
export interface ErrorResponse {
code: string;
msg: string;
}
export interface CustomError extends Error {
message: string;
status: number;
}
export interface Result {
value: 'success' | 'failure';
}
export interface QueryParams {
numberParam: number;
stringParam: string;
booleanParam: boolean;
optionalStringParam?: string;
}