Skip to content

Commit a2bbbc9

Browse files
committed
fix(cli): update template with responses object
1 parent 0ecaecd commit a2bbbc9

File tree

2 files changed

+157
-42
lines changed

2 files changed

+157
-42
lines changed

packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,98 @@ export class <%= className %>Controller {
1616
public <%= repositoryNameCamel %> : <%= repositoryName %>,
1717
) {}
1818

19-
@post('<%= httpPathName %>')
19+
@post('<%= httpPathName %>', {
20+
responses: {
21+
'200': {
22+
description: '<%= modelName %> model instance',
23+
content: {'application/json': {'x-ts-type': <%= modelName %>}},
24+
},
25+
},
26+
})
2027
async create(@requestBody() <%= name %>: <%= modelName %>)
2128
: Promise<<%= modelName %>> {
2229
return await this.<%= repositoryNameCamel %>.create(<%= name %>);
2330
}
2431

25-
@get('<%= httpPathName %>/count')
32+
@get('<%= httpPathName %>/count', {
33+
responses: {
34+
'200': {
35+
description: '<%= modelName %> model count',
36+
content: {'application/json': {'x-ts-type': Number}},
37+
},
38+
},
39+
})
2640
async count(@param.query.string('where') where?: Where): Promise<number> {
2741
return await this.<%= repositoryNameCamel %>.count(where);
2842
}
2943

30-
@get('<%= httpPathName %>')
44+
@get('<%= httpPathName %>', {
45+
responses: {
46+
'200': {
47+
description: 'Array of <%= modelName %> model instances',
48+
content: {
49+
'application/json': {
50+
schema: {type: 'array', items: {'x-ts-type': <%= modelName %>}},
51+
},
52+
},
53+
},
54+
},
55+
})
3156
async find(@param.query.string('filter') filter?: Filter)
3257
: Promise<<%= modelName %>[]> {
3358
return await this.<%= repositoryNameCamel %>.find(filter);
3459
}
3560

36-
@patch('<%= httpPathName %>')
61+
@patch('<%= httpPathName %>', {
62+
responses: {
63+
'200': {
64+
description: '<%= modelName %> PATCH success count',
65+
content: {'application/json': {'x-ts-type': Number}},
66+
},
67+
},
68+
})
3769
async updateAll(
3870
@requestBody() <%= name %>: <%= modelName %>,
3971
@param.query.string('where') where?: Where
4072
): Promise<number> {
4173
return await this.<%= repositoryNameCamel %>.updateAll(<%= name %>, where);
4274
}
4375

44-
@get('<%= httpPathName %>/{id}')
76+
@get('<%= httpPathName %>/{id}', {
77+
responses: {
78+
'200': {
79+
description: '<%= modelName %> model instance',
80+
content: {'application/json': {'x-ts-type': <%= modelName %>}},
81+
},
82+
},
83+
})
4584
async findById(@param.path.<%= idType %>('id') id: <%= idType %>): Promise<<%= modelName %>> {
4685
return await this.<%= repositoryNameCamel %>.findById(id);
4786
}
4887

49-
@patch('<%= httpPathName %>/{id}')
88+
@patch('<%= httpPathName %>/{id}', {
89+
responses: {
90+
'200': {
91+
description: '<%= modelName %> PATCH success',
92+
content: {'application/json': {'x-ts-type': Boolean}},
93+
},
94+
},
95+
})
5096
async updateById(
5197
@param.path.<%= idType %>('id') id: <%= idType %>,
5298
@requestBody() <%= name %>: <%= modelName %>
5399
): Promise<boolean> {
54100
return await this.<%= repositoryNameCamel %>.updateById(id, <%= name %>);
55101
}
56102

57-
@del('<%= httpPathName %>/{id}')
103+
@del('<%= httpPathName %>/{id}', {
104+
responses: {
105+
'200': {
106+
description: '<%= modelName %> DELETE success',
107+
content: {'application/json': {'x-ts-type': Boolean}},
108+
},
109+
},
110+
})
58111
async deleteById(@param.path.<%= idType %>('id') id: <%= idType %>): Promise<boolean> {
59112
return await this.<%= repositoryNameCamel %>.deleteById(id);
60113
}

packages/cli/test/integration/generators/controller.integration.js

Lines changed: 97 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -241,34 +241,96 @@ function checkRestCrudContents() {
241241
assert.fileContent(expectedFile, /barRepository \: BarRepository/);
242242

243243
// Assert that the decorators are present in the correct groupings!
244-
assert.fileContent(
245-
expectedFile,
246-
/\@post\('\/product-reviews'\)\s{1,}async create\(\@requestBody\(\)/,
247-
);
248-
assert.fileContent(
249-
expectedFile,
250-
/\@get\('\/product-reviews\/count'\)\s{1,}async count\(\@param.query.string\('where'\)/,
251-
);
252-
assert.fileContent(
253-
expectedFile,
254-
/\@get\('\/product-reviews'\)\s{1,}async find\(\@param.query.string\('filter'\)/,
255-
);
256-
assert.fileContent(
257-
expectedFile,
258-
/\@patch\('\/product-reviews'\)\s{1,}async updateAll\(\s{1,}\@requestBody\(\).*,\s{1,}\@param.query.string\('where'\) where\?: Where/,
259-
);
260-
assert.fileContent(
261-
expectedFile,
262-
/\@get\('\/product-reviews\/{id}'\)\s{1,}async findById\(\@param.path.number\('id'\)/,
263-
);
264-
assert.fileContent(
265-
expectedFile,
266-
/\@patch\('\/product-reviews\/{id}'\)\s{1,}async updateById\(\s{1,}\@param.path.number\('id'\) id: number,\s{1,}\@requestBody\(\)/,
267-
);
268-
assert.fileContent(
269-
expectedFile,
270-
/\@del\('\/product-reviews\/{id}'\)\s{1,}async deleteById\(\@param.path.number\('id'\) id: number\)/,
271-
);
244+
// @post - create
245+
const postCreateRegEx = [
246+
/\@post\('\/product-reviews', {/,
247+
/responses: {/,
248+
/'200': {/,
249+
/description: 'ProductReview model instance'/,
250+
/content: {'application\/json': {'x-ts-type': ProductReview}},\s{1,}},\s{1,}},\s{1,}}\)/,
251+
/async create\(\@requestBody\(\)/,
252+
];
253+
postCreateRegEx.forEach(regex => {
254+
assert.fileContent(expectedFile, regex);
255+
});
256+
257+
// @get - count
258+
const getCountRegEx = [
259+
/\@get\('\/product-reviews\/count', {/,
260+
/responses: {/,
261+
/'200': {/,
262+
/description: 'ProductReview model count'/,
263+
/content: {'application\/json': {'x-ts-type': Number}},\s{1,}},\s{1,}},\s{1,}}\)/,
264+
/async count\(\@param.query.string\('where'\)/,
265+
];
266+
getCountRegEx.forEach(regex => {
267+
assert.fileContent(expectedFile, regex);
268+
});
269+
270+
// @get - find
271+
const getFindRegEx = [
272+
/\@get\('\/product-reviews', {/,
273+
/responses: {/,
274+
/'200': {/,
275+
/description: 'Array of ProductReview model instances'/,
276+
/content: {'application\/json': {'x-ts-type': ProductReview}},\s{1,}},\s{1,}},\s{1,}}\)/,
277+
/async find\(\@param.query.string\('filter'\)/,
278+
];
279+
getFindRegEx.forEach(regex => {
280+
assert.fileContent(expectedFile, regex);
281+
});
282+
283+
// @patch - updateAll
284+
const patchUpdateAllRegEx = [
285+
/\@patch\('\/product-reviews', {/,
286+
/responses: {/,
287+
/'200': {/,
288+
/description: 'ProductReview PATCH success count'/,
289+
/content: {'application\/json': {'x-ts-type': Number}},\s{1,}},\s{1,}},\s{1,}}\)/,
290+
/async updateAll\(\s{1,}\@requestBody\(\).*,\s{1,}\@param.query.string\('where'\) where\?: Where/,
291+
];
292+
patchUpdateAllRegEx.forEach(regex => {
293+
assert.fileContent(expectedFile, regex);
294+
});
295+
296+
// @get - findById
297+
const getFindByIdRegEx = [
298+
/\@get\('\/product-reviews\/{id}', {/,
299+
/responses: {/,
300+
/'200': {/,
301+
/description: 'ProductReview model instance'/,
302+
/content: {'application\/json': {'x-ts-type': ProductReview}},\s{1,}},\s{1,}},\s{1,}}\)/,
303+
/async findById\(\@param.path.number\('id'\)/,
304+
];
305+
getFindByIdRegEx.forEach(regex => {
306+
assert.fileContent(expectedFile, regex);
307+
});
308+
309+
// @patch - updateById
310+
const patchUpdateByIdRegEx = [
311+
/\@patch\('\/product-reviews\/{id}'/,
312+
/responses: {/,
313+
/'200': {/,
314+
/description: 'ProductReview PATCH success'/,
315+
/content: {'application\/json': {'x-ts-type': Boolean}},\s{1,}},\s{1,}},\s{1,}}\)/,
316+
/async updateById\(\s{1,}\@param.path.number\('id'\) id: number,\s{1,}\@requestBody\(\)/,
317+
];
318+
patchUpdateByIdRegEx.forEach(regex => {
319+
assert.fileContent(expectedFile, regex);
320+
});
321+
322+
// @del - deleteById
323+
const deleteByIdRegEx = [
324+
/\@del\('\/product-reviews\/{id}', {/,
325+
/responses: {/,
326+
/'200': {/,
327+
/description: 'ProductReview DELETE success'/,
328+
/content: {'application\/json': {'x-ts-type': Boolean}},\s{1,}},\s{1,}},\s{1,}}\)/,
329+
/async deleteById\(\@param.path.number\('id'\) id: number\)/,
330+
];
331+
deleteByIdRegEx.forEach(regex => {
332+
assert.fileContent(expectedFile, regex);
333+
});
272334
}
273335

274336
/**
@@ -278,30 +340,30 @@ function checkRestCrudContents() {
278340
function checkRestPaths(restUrl) {
279341
assert.fileContent(
280342
expectedFile,
281-
new RegExp(/@post\('/.source + restUrl + /'\)/.source),
343+
new RegExp(/@post\('/.source + restUrl + /', {/.source),
282344
);
283345
assert.fileContent(
284346
expectedFile,
285-
new RegExp(/@get\('/.source + restUrl + /\/count'\)/.source),
347+
new RegExp(/@get\('/.source + restUrl + /\/count', {/.source),
286348
);
287349
assert.fileContent(
288350
expectedFile,
289-
new RegExp(/@get\('/.source + restUrl + /'\)/.source),
351+
new RegExp(/@get\('/.source + restUrl + /', {/.source),
290352
);
291353
assert.fileContent(
292354
expectedFile,
293-
new RegExp(/@patch\('/.source + restUrl + /'\)/.source),
355+
new RegExp(/@patch\('/.source + restUrl + /', {/.source),
294356
);
295357
assert.fileContent(
296358
expectedFile,
297-
new RegExp(/@get\('/.source + restUrl + /\/{id}'\)/.source),
359+
new RegExp(/@get\('/.source + restUrl + /\/{id}', {/.source),
298360
);
299361
assert.fileContent(
300362
expectedFile,
301-
new RegExp(/@patch\('/.source + restUrl + /\/{id}'\)/.source),
363+
new RegExp(/@patch\('/.source + restUrl + /\/{id}', {/.source),
302364
);
303365
assert.fileContent(
304366
expectedFile,
305-
new RegExp(/@del\('/.source + restUrl + /\/{id}'\)/.source),
367+
new RegExp(/@del\('/.source + restUrl + /\/{id}', {/.source),
306368
);
307369
}

0 commit comments

Comments
 (0)