Skip to content

Commit df82c9d

Browse files
feat: group by tag (#35)
* ci: update package-lock.json * feat: group paths by tags * feat: capitalizeFirst in tag generation * feat: add testing * feat: update testing to use new steps * feat: remove merge changes
1 parent 9a5bbd1 commit df82c9d

File tree

3 files changed

+88
-40
lines changed

3 files changed

+88
-40
lines changed

features/support/api.ts

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { binding, after, then, when, given } from "cucumber-tsflow";
22
import { assert, expect } from "chai";
33
import { RequestParameters } from "../../template/request";
44
import { BaseModelStep } from "./base";
5+
import fs = require("fs");
56

67
const isJson = (str: string): boolean => {
78
try {
@@ -12,6 +13,10 @@ const isJson = (str: string): boolean => {
1213
return true;
1314
};
1415

16+
const getTagFileName = (tag: string): string => {
17+
return tag === "" ? "" : tag.charAt(0).toUpperCase() + tag.slice(1);
18+
};
19+
1520
@binding()
1621
export class ModelSteps extends BaseModelStep {
1722
private requestParams: RequestParameters;
@@ -21,17 +26,21 @@ export class ModelSteps extends BaseModelStep {
2126
private request: any;
2227

2328
createApi(serverIndex = 0): any {
24-
const apiModule = require("../api/api.ts");
25-
const configurationModule = require("../api/configuration.ts");
26-
const mockTransport = async (params: RequestParameters) => {
27-
this.requestParams = params;
28-
return this.serverResponseObject;
29-
};
30-
31-
const config = new configurationModule.default(mockTransport);
32-
config.selectedServerIndex = serverIndex;
33-
34-
return new apiModule.default(config);
29+
try {
30+
const apiModule = require("../api/api.ts");
31+
const configurationModule = require("../api/configuration.ts");
32+
const mockTransport = async (params: RequestParameters) => {
33+
this.requestParams = params;
34+
return this.serverResponseObject;
35+
};
36+
37+
const config = new configurationModule.default(mockTransport);
38+
config.selectedServerIndex = serverIndex;
39+
40+
return new apiModule.default(config);
41+
} catch {
42+
return null;
43+
}
3544
}
3645

3746
@after()
@@ -159,4 +168,36 @@ export class ModelSteps extends BaseModelStep {
159168
public checkMethodType(value: string) {
160169
assert.equal(this.requestParams.method, value);
161170
}
171+
172+
@then(/the api file with tag "(.*)" exists/)
173+
public checkFileExists(tag: string) {
174+
fs.existsSync(`../api/api${getTagFileName(tag)}.ts`);
175+
}
176+
177+
@then(/the api file with tag "(.*)" does not exist/)
178+
public checkFileDoesNotExist(tag: string) {
179+
!fs.existsSync(`../api/api${getTagFileName(tag)}.ts`);
180+
}
181+
182+
@then(/the method "(.*)" should be present in the api file with tag "(.*)"/)
183+
public checkMethodExists(methodName: string, tag: string) {
184+
fs.existsSync(`../api/api${getTagFileName(tag)}.ts`);
185+
const apiFile = require(`../api/api${tag}.ts`);
186+
187+
const module = new apiFile.default();
188+
189+
expect(module[methodName]).to.exist;
190+
}
191+
192+
@then(
193+
/the method "(.*)" should not be present in the api file with tag "(.*)"/
194+
)
195+
public checkMethodDoesNotExist(methodName: string, tag: string) {
196+
fs.existsSync(`../api/api${getTagFileName(tag)}.ts`);
197+
const apiFile = require(`../api/api${tag}.ts`);
198+
199+
const module = new apiFile.default();
200+
201+
expect(module[methodName]).to.not.exist;
202+
}
162203
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "openapi-forge-typescript",
33
"version": "0.1.0",
44
"description": "",
5+
"apiTemplates": [
6+
"api.ts.handlebars"
7+
],
58
"scripts": {
69
"prepare": "husky install",
710
"test": "\"./node_modules/.bin/cucumber-js\" -p default",

template/api.ts.handlebars

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { deserialize, serialize } from "./serializer";
1111
Version: {{info.version}}
1212
*/
1313

14-
export default class Api {
14+
{{#if _tag.description}}// {{_tag.description}}{{/if}}
15+
16+
export default class Api{{_tag.name}} {
1517
private config: Configuration;
1618

1719
constructor(config: Configuration) {
@@ -21,39 +23,41 @@ export default class Api {
2123
{{#each paths}}
2224
{{setVar "path" @key}}
2325
{{#each this}}
24-
25-
{{#if summary}}// {{summary}}{{/if}}
26-
{{#if description}}// {{description}}{{/if}}
27-
{{#each _sortedParameters}}
28-
// @param {{name}} {{description}}
29-
{{/each}}
30-
async {{operationId}}(
26+
{{#ifEquals ../../_tag.name _tag.name}}
27+
28+
{{#if summary}}// {{summary}}{{/if}}
29+
{{#if description}}// {{description}}{{/if}}
30+
{{#if tags}}// tags: {{tags}}{{/if}}
3131
{{#each _sortedParameters}}
32-
{{name}}{{#if _optional}}?{{/if}}:
33-
{{typeConvert schema ../../../_options}}
34-
{{#if schema.default}} = {{{quoteIfString schema.default}}}{{/if}},
32+
// @param {{name}} {{description}}
3533
{{/each}}
36-
): Promise<{{#if _response.schema}}{{typeConvert _response.schema ../../_options}}{{else}}null{{/if}}> {
34+
async {{operationId}}(
35+
{{#each _sortedParameters}}
36+
{{name}}{{#if _optional}}?{{/if}}:
37+
{{typeConvert schema ../../../_options}}
38+
{{#if schema.default}} = {{{quoteIfString schema.default}}}{{/if}},
39+
{{/each}}
40+
): Promise<{{#if _response.schema}}{{typeConvert _response.schema ../../_options}}{{else}}null{{/if}}> {
3741

38-
const builder = new ParameterBuilder();
39-
{{#each _sortedParameters}}{{#unless _optional}}
40-
builder.add("{{name}}", {{name}}, "{{typeConvert schema}}", "{{in}}");
41-
{{/unless}}{{/each}}
42-
43-
{{#each _sortedParameters}}{{#if _optional}}
44-
if ({{name}}) {
42+
const builder = new ParameterBuilder();
43+
{{#each _sortedParameters}}{{#unless _optional}}
4544
builder.add("{{name}}", {{name}}, "{{typeConvert schema}}", "{{in}}");
46-
}
47-
{{/if}}{{/each}}
48-
49-
const response = await request(this.config, "{{@root.path}}", "{{@key}}", builder.parameters);
50-
{{#if _response.schema}}
51-
return deserialize(response, "{{typeConvert _response.schema}}");
52-
{{else}}
53-
return null;
54-
{{/if}}
55-
};
45+
{{/unless}}{{/each}}
46+
47+
{{#each _sortedParameters}}{{#if _optional}}
48+
if ({{name}}) {
49+
builder.add("{{name}}", {{name}}, "{{typeConvert schema}}", "{{in}}");
50+
}
51+
{{/if}}{{/each}}
5652

53+
const response = await request(this.config, "{{@root.path}}", "{{@key}}", builder.parameters);
54+
{{#if _response.schema}}
55+
return deserialize(response, "{{typeConvert _response.schema}}");
56+
{{else}}
57+
return null;
58+
{{/if}}
59+
};
60+
{{/ifEquals}}
5761
{{/each}}
5862
{{/each}}
5963
}

0 commit comments

Comments
 (0)