Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding option to extend interface (and add custom import) #352

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down Expand Up @@ -300,6 +310,7 @@ export interface GenerateApiConfiguration {
generateRouteTypes: boolean;
generateClient: boolean;
generateUnionEnums: boolean;
interfaceExtend: string;
swaggerSchema: object;
originalSchema: object;
componentsMap: Record<string, SchemaComponent>;
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ program
false,
)
.option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false)
.option(
"--interface-extend <interfaceExtend>",
"if chosendefines a class that all interfaces will extend ",
)
.option(
"--custom-import <interfaceExtend>",
"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(
Expand Down Expand Up @@ -86,6 +94,8 @@ const {
name,
templates,
unionEnums,
interfaceExtend,
customImport,
routeTypes,
client,
defaultAsSuccess,
Expand Down Expand Up @@ -119,6 +129,8 @@ generateApi({
defaultResponseType: defaultResponse,
unwrapResponseData: unwrapResponseData,
generateUnionEnums: unionEnums,
interfaceExtend: interfaceExtend,
customImport: customImport,
generateResponses: responses,
extractRequestParams: !!extractRequestParams,
extractRequestBody: !!extractRequestBody,
Expand Down
4 changes: 4 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() */

Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -66,6 +68,8 @@ module.exports = {
generateResponses,
templates,
generateUnionEnums,
interfaceExtend,
customImport,
moduleNameIndex,
moduleNameFirstTag,
prettierOptions,
Expand Down
13 changes: 11 additions & 2 deletions templates/base/data-contracts.eta
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%
const { modelTypes, utils } = it;
const { modelTypes, utils, config} = it;
const { formatDescription, require, _ } = utils;


Expand All @@ -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}`;
Expand All @@ -30,10 +33,16 @@
]);
}
%>

<%~ config.customImport %>


<% modelTypes.forEach((contract) => { %>
<% const description = createDescription(contract); %>
<% if (description.length) { %>
/**

<%~ contract.interfaceExtend %>
<%~ description.map(part => `* ${part}`).join("\n") %>

*/
Expand Down