Skip to content

Commit

Permalink
feat: group by tag (#35)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jhowlett-scottlogic authored Nov 10, 2022
1 parent 9a5bbd1 commit df82c9d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 40 deletions.
63 changes: 52 additions & 11 deletions features/support/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { binding, after, then, when, given } from "cucumber-tsflow";
import { assert, expect } from "chai";
import { RequestParameters } from "../../template/request";
import { BaseModelStep } from "./base";
import fs = require("fs");

const isJson = (str: string): boolean => {
try {
Expand All @@ -12,6 +13,10 @@ const isJson = (str: string): boolean => {
return true;
};

const getTagFileName = (tag: string): string => {
return tag === "" ? "" : tag.charAt(0).toUpperCase() + tag.slice(1);
};

@binding()
export class ModelSteps extends BaseModelStep {
private requestParams: RequestParameters;
Expand All @@ -21,17 +26,21 @@ export class ModelSteps extends BaseModelStep {
private request: any;

createApi(serverIndex = 0): any {
const apiModule = require("../api/api.ts");
const configurationModule = require("../api/configuration.ts");
const mockTransport = async (params: RequestParameters) => {
this.requestParams = params;
return this.serverResponseObject;
};

const config = new configurationModule.default(mockTransport);
config.selectedServerIndex = serverIndex;

return new apiModule.default(config);
try {
const apiModule = require("../api/api.ts");
const configurationModule = require("../api/configuration.ts");
const mockTransport = async (params: RequestParameters) => {
this.requestParams = params;
return this.serverResponseObject;
};

const config = new configurationModule.default(mockTransport);
config.selectedServerIndex = serverIndex;

return new apiModule.default(config);
} catch {
return null;
}
}

@after()
Expand Down Expand Up @@ -159,4 +168,36 @@ export class ModelSteps extends BaseModelStep {
public checkMethodType(value: string) {
assert.equal(this.requestParams.method, value);
}

@then(/the api file with tag "(.*)" exists/)
public checkFileExists(tag: string) {
fs.existsSync(`../api/api${getTagFileName(tag)}.ts`);
}

@then(/the api file with tag "(.*)" does not exist/)
public checkFileDoesNotExist(tag: string) {
!fs.existsSync(`../api/api${getTagFileName(tag)}.ts`);
}

@then(/the method "(.*)" should be present in the api file with tag "(.*)"/)
public checkMethodExists(methodName: string, tag: string) {
fs.existsSync(`../api/api${getTagFileName(tag)}.ts`);
const apiFile = require(`../api/api${tag}.ts`);

const module = new apiFile.default();

expect(module[methodName]).to.exist;
}

@then(
/the method "(.*)" should not be present in the api file with tag "(.*)"/
)
public checkMethodDoesNotExist(methodName: string, tag: string) {
fs.existsSync(`../api/api${getTagFileName(tag)}.ts`);
const apiFile = require(`../api/api${tag}.ts`);

const module = new apiFile.default();

expect(module[methodName]).to.not.exist;
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "openapi-forge-typescript",
"version": "0.1.0",
"description": "",
"apiTemplates": [
"api.ts.handlebars"
],
"scripts": {
"prepare": "husky install",
"test": "\"./node_modules/.bin/cucumber-js\" -p default",
Expand Down
62 changes: 33 additions & 29 deletions template/api.ts.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { deserialize, serialize } from "./serializer";
Version: {{info.version}}
*/

export default class Api {
{{#if _tag.description}}// {{_tag.description}}{{/if}}

export default class Api{{_tag.name}} {
private config: Configuration;

constructor(config: Configuration) {
Expand All @@ -21,39 +23,41 @@ export default class Api {
{{#each paths}}
{{setVar "path" @key}}
{{#each this}}

{{#if summary}}// {{summary}}{{/if}}
{{#if description}}// {{description}}{{/if}}
{{#each _sortedParameters}}
// @param {{name}} {{description}}
{{/each}}
async {{operationId}}(
{{#ifEquals ../../_tag.name _tag.name}}

{{#if summary}}// {{summary}}{{/if}}
{{#if description}}// {{description}}{{/if}}
{{#if tags}}// tags: {{tags}}{{/if}}
{{#each _sortedParameters}}
{{name}}{{#if _optional}}?{{/if}}:
{{typeConvert schema ../../../_options}}
{{#if schema.default}} = {{{quoteIfString schema.default}}}{{/if}},
// @param {{name}} {{description}}
{{/each}}
): Promise<{{#if _response.schema}}{{typeConvert _response.schema ../../_options}}{{else}}null{{/if}}> {
async {{operationId}}(
{{#each _sortedParameters}}
{{name}}{{#if _optional}}?{{/if}}:
{{typeConvert schema ../../../_options}}
{{#if schema.default}} = {{{quoteIfString schema.default}}}{{/if}},
{{/each}}
): Promise<{{#if _response.schema}}{{typeConvert _response.schema ../../_options}}{{else}}null{{/if}}> {

const builder = new ParameterBuilder();
{{#each _sortedParameters}}{{#unless _optional}}
builder.add("{{name}}", {{name}}, "{{typeConvert schema}}", "{{in}}");
{{/unless}}{{/each}}

{{#each _sortedParameters}}{{#if _optional}}
if ({{name}}) {
const builder = new ParameterBuilder();
{{#each _sortedParameters}}{{#unless _optional}}
builder.add("{{name}}", {{name}}, "{{typeConvert schema}}", "{{in}}");
}
{{/if}}{{/each}}

const response = await request(this.config, "{{@root.path}}", "{{@key}}", builder.parameters);
{{#if _response.schema}}
return deserialize(response, "{{typeConvert _response.schema}}");
{{else}}
return null;
{{/if}}
};
{{/unless}}{{/each}}

{{#each _sortedParameters}}{{#if _optional}}
if ({{name}}) {
builder.add("{{name}}", {{name}}, "{{typeConvert schema}}", "{{in}}");
}
{{/if}}{{/each}}

const response = await request(this.config, "{{@root.path}}", "{{@key}}", builder.parameters);
{{#if _response.schema}}
return deserialize(response, "{{typeConvert _response.schema}}");
{{else}}
return null;
{{/if}}
};
{{/ifEquals}}
{{/each}}
{{/each}}
}
Expand Down

0 comments on commit df82c9d

Please sign in to comment.