From df82c9d6ad8d333c30c7ea4064b0d945bfdd0eca Mon Sep 17 00:00:00 2001 From: Jake Howlett <107993905+jhowlett-scottlogic@users.noreply.github.com> Date: Thu, 10 Nov 2022 15:31:58 +0000 Subject: [PATCH] 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 --- features/support/api.ts | 63 +++++++++++++++++++++++++++++++------- package.json | 3 ++ template/api.ts.handlebars | 62 +++++++++++++++++++------------------ 3 files changed, 88 insertions(+), 40 deletions(-) diff --git a/features/support/api.ts b/features/support/api.ts index 0e42a9b..b2ca5b3 100644 --- a/features/support/api.ts +++ b/features/support/api.ts @@ -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 { @@ -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; @@ -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() @@ -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; + } } diff --git a/package.json b/package.json index 9538aca..7466f88 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/template/api.ts.handlebars b/template/api.ts.handlebars index 62d5304..744ee05 100644 --- a/template/api.ts.handlebars +++ b/template/api.ts.handlebars @@ -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) { @@ -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}} }