-
Notifications
You must be signed in to change notification settings - Fork 500
/
getController.ts
206 lines (184 loc) · 4.86 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
import {
Controller,
Example,
Get,
OperationId,
Query,
Request,
Route,
SuccessResponse,
Tags,
} from '../../../src';
import '../duplicateTestModel';
import {
GenericModel,
TestClassModel,
TestModel,
TestSubModel,
} from '../testModel';
import {
ModelService,
} from './../services/modelService';
@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')
@Example<TestModel>({
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],
numberValue: 1,
object: {
a: 'a',
},
objectArray: [{
a: 'a',
}],
optionalString: 'optional string',
strLiteralArr: ['Foo', 'Bar'],
strLiteralVal: 'Foo',
stringArray: ['string one', 'string two'],
stringValue: 'a string',
})
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('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('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 as any).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')
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 new Buffer('testbuffer');
}
@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,
};
}
}
export interface ErrorResponse {
code: string;
msg: string;
}
export interface CustomError extends Error {
message: string;
status: number;
}
export interface Result {
value: 'success' | 'failure';
}