diff --git a/index.d.ts b/index.d.ts index 4673b93c..045425f9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -34,6 +34,16 @@ interface GenerateApiParams { */ generateUnionEnums?: boolean; + /** + * Extend the interfaces with this class + */ + interfaceExtend: ""; + + /** + * A custom import string to be used with interface extend + */ + customImport: ""; + /** * generate type definitions for API routes (default: false) */ @@ -300,6 +310,7 @@ export interface GenerateApiConfiguration { generateRouteTypes: boolean; generateClient: boolean; generateUnionEnums: boolean; + interfaceExtend: string; swaggerSchema: object; originalSchema: object; componentsMap: Record; diff --git a/index.js b/index.js index a42b61a5..7ecd2b9f 100755 --- a/index.js +++ b/index.js @@ -38,6 +38,14 @@ program false, ) .option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false) + .option( + "--interface-extend ", + "if chosendefines a class that all interfaces will extend ", + ) + .option( + "--custom-import ", + "Specify a string for custom importstatement (can be used with --interface-extend) ", + ) .option("--route-types", "generate type definitions for API routes", false) .option("--no-client", "do not generate an API class", false) .option( @@ -86,6 +94,8 @@ const { name, templates, unionEnums, + interfaceExtend, + customImport, routeTypes, client, defaultAsSuccess, @@ -119,6 +129,8 @@ generateApi({ defaultResponseType: defaultResponse, unwrapResponseData: unwrapResponseData, generateUnionEnums: unionEnums, + interfaceExtend: interfaceExtend, + customImport: customImport, generateResponses: responses, extractRequestParams: !!extractRequestParams, extractRequestBody: !!extractRequestBody, diff --git a/src/config.js b/src/config.js index b9f3d63a..04087360 100644 --- a/src/config.js +++ b/src/config.js @@ -14,6 +14,10 @@ const config = { generateClient: true, /** CLI flag */ generateUnionEnums: false, + /** CLI flag */ + interfaceExtend: "", + /** CLI flag */ + customImport: "", enumNamesAsValues: false, /** parsed swagger schema from getSwaggerObject() */ diff --git a/src/index.js b/src/index.js index 6ccbdd3a..f55a6868 100644 --- a/src/index.js +++ b/src/index.js @@ -39,6 +39,8 @@ module.exports = { generateClient = config.generateClient, httpClientType = config.httpClientType, generateUnionEnums = config.generateUnionEnums, + interfaceExtend = config.interfaceExtend, + customImport = config.customImport, moduleNameIndex = config.moduleNameIndex, moduleNameFirstTag = config.moduleNameFirstTag, extractRequestParams = config.extractRequestParams, @@ -66,6 +68,8 @@ module.exports = { generateResponses, templates, generateUnionEnums, + interfaceExtend, + customImport, moduleNameIndex, moduleNameFirstTag, prettierOptions, diff --git a/templates/base/data-contracts.eta b/templates/base/data-contracts.eta index 7e3ef4ae..ca90768a 100644 --- a/templates/base/data-contracts.eta +++ b/templates/base/data-contracts.eta @@ -1,5 +1,5 @@ <% - const { modelTypes, utils } = it; + const { modelTypes, utils, config} = it; const { formatDescription, require, _ } = utils; @@ -8,7 +8,10 @@ return `enum ${contract.name} {\r\n${contract.content} \r\n }`; }, interface: (contract) => { - return `interface ${contract.name} {\r\n${contract.content}}`; + if (config.interfaceExtend.length > 1) + return `interface ${contract.name} extends ${config.interfaceExtend} {\r\ntype:"${contract.name}";${contract.content}}`; + else + return `interface ${contract.name} {\r\n${contract.content}}`; }, type: (contract) => { return `type ${contract.name} = ${contract.content}`; @@ -30,10 +33,16 @@ ]); } %> + +<%~ config.customImport %> + + <% modelTypes.forEach((contract) => { %> <% const description = createDescription(contract); %> <% if (description.length) { %> /** + +<%~ contract.interfaceExtend %> <%~ description.map(part => `* ${part}`).join("\n") %> */