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

feature: add --prefer-types-over-interfaces options #750

Open
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Options:
-r, --responses generate additional information about request responses
also add typings for bad responses (default: false)
--union-enums generate all "enum" types as union types (T1 | T2 | TN) (default: false)
--prefer-types-over-interface generate types instead of interfaces for DTOs (default: false)
--add-readonly generate readonly properties (default: false)
--route-types generate type definitions for API routes (default: false)
--no-client do not generate an API class
Expand Down Expand Up @@ -136,7 +137,8 @@ generateApi({
singleHttpClient: true,
cleanOutput: false,
enumNamesAsValues: false,
moduleNameFirstTag: false,
moduleNameFirstTag: false,
preferTypesOverInterface: false,
generateUnionEnums: false,
typePrefix: "",
typeSuffix: "",
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ interface GenerateApiParamsBase {
*/
generateUnionEnums?: boolean;

/**
* generate with types instead of interface
*/
preferTypesOverInterface?: boolean;

/**
* generate type definitions for API routes (default: false)
*/
Expand Down Expand Up @@ -613,6 +618,7 @@ export interface GenerateApiConfiguration {
generateRouteTypes: boolean;
generateClient: boolean;
generateUnionEnums: boolean;
preferTypesOverInterface: boolean;
swaggerSchema: object;
originalSchema: object;
componentsMap: Record<string, SchemaComponent>;
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ const program = cli({
default: codeGenBaseConfig.generateUnionEnums,
internal: { name: "generateUnionEnums" },
},
{
flags: "--prefer-types-over-interface",
description: "prefer types over interfaces",
default: codeGenBaseConfig.preferTypesOverInterface,
internal: { name: "preferTypesOverInterface" },
},
{
flags: "--add-readonly",
description: "generate readonly properties",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"test:--templates": "node tests/spec/templates/test.js",
"test:--type-suffix--type-prefix": "node tests/spec/typeSuffixPrefix/test.js",
"test:--union-enums": "node tests/spec/unionEnums/test.js",
"test:--prefer-types-over-interface": "node tests/spec/preferTypesOverInterface/test.js",
"test:additionalProperties2.0": "node tests/spec/additional-properties-2.0/test.js",
"test:another-query-params": "node tests/spec/another-query-params/test.js",
"test:const-keyword": "node tests/spec/const-keyword/test.js",
Expand Down
2 changes: 2 additions & 0 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class CodeGenConfig {
/** CLI flag */
generateUnionEnums = false;
/** CLI flag */
preferTypesOverInterface = false;
/** CLI flag */
addReadonly = false;
enumNamesAsValues = false;
/** parsed swagger schema from getSwaggerObject() */
Expand Down
4 changes: 4 additions & 0 deletions templates/base/data-contracts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const dataContractTemplates = {
return `enum ${contract.name} {\r\n${contract.content} \r\n }`;
},
interface: (contract) => {
if (config.preferTypesOverInterface) {
return `type ${contract.name}${buildGenerics(contract)} = {\r\n${contract.content}}`;
}

return `interface ${contract.name}${buildGenerics(contract)} {\r\n${contract.content}}`;
},
type: (contract) => {
Expand Down
18 changes: 18 additions & 0 deletions tests/spec/preferTypesOverInterface/expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/

export type Pet = {
/** @format int64 */
readonly id: number;
readonly name: string;
readonly tag?: string;
readonly multiple?: string | number;
};
64 changes: 64 additions & 0 deletions tests/spec/preferTypesOverInterface/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Swagger API Team"
},
"license": {
"name": "MIT"
}
},
"host": "petstore.swagger.io",
"basePath": "/api",
"schemes": ["http"],
"consumes": ["application/json"],
"produces": ["application/json"],
"paths": {
"/pets": {
"get": {
"description": "Returns all pets from the system that the user has access to",
"produces": ["application/json"],
"responses": {
"200": {
"description": "A list of pets.",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Pet"
}
}
}
}
}
}
},
"definitions": {
"Pet": {
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer",
"readOnly": true,
"format": "int64"
},
"name": {
"type": "string",
"readOnly": true
},
"tag": {
"type": "string",
"readOnly": true
},
"multiple": {
"type": ["string", "number"],
"readOnly": true
}
}
}
}
}
18 changes: 18 additions & 0 deletions tests/spec/preferTypesOverInterface/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/

export type Pet = {
/** @format int64 */
readonly id: number;
readonly name: string;
readonly tag?: string;
readonly multiple?: string | number;
};
28 changes: 28 additions & 0 deletions tests/spec/preferTypesOverInterface/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { generateApiForTest } = require("../../helpers/generateApiForTest");
const { resolve } = require("node:path");
const validateGeneratedModule = require("../../helpers/validateGeneratedModule");
const createSchemaInfos = require("../../helpers/createSchemaInfos");
const assertGeneratedModule = require("../../helpers/assertGeneratedModule");

const schemas = createSchemaInfos({
absolutePathToSchemas: resolve(__dirname, "./"),
});

schemas.forEach(({ absolutePath, apiFileName }) => {
generateApiForTest({
testName: "prefer types over interface test",
silent: true,
name: apiFileName,
input: absolutePath,
output: resolve(__dirname, "./"),
addReadonly: true,
generateClient: false,
preferTypesOverInterface: true,
}).then(() => {
validateGeneratedModule(resolve(__dirname, `./${apiFileName}`));
assertGeneratedModule(
resolve(__dirname, `./${apiFileName}`),
resolve(__dirname, "./expected.ts"),
);
});
});