Skip to content

Commit

Permalink
feat: tuple type support generator
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Apr 23, 2023
1 parent 811fe7e commit 72671a6
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// eslint-disable-next-line no-global-assign,no-native-reassign
require = require('@qiwi/esm')(module /*, options*/);
const openapiTS = require('openapi-typescript').default;
const defaultSchemaObjectTransform =
require('openapi-typescript/dist/transform/schema-object').defaultSchemaObjectTransform;
export default openapiTS;
export { defaultSchemaObjectTransform };
23 changes: 21 additions & 2 deletions packages/openapi-gen-ts/src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
removeFilesSync,
} from '@liangskyli/utils';
import fs from 'fs-extra';
import type { OpenAPITSOptions } from 'openapi-typescript';
import path from 'path';
import openapiTS from '../esm-to-commjs/openapi-typescript';
import openapiTS, {
defaultSchemaObjectTransform,
} from '../esm-to-commjs/openapi-typescript';
import { fileTip } from '../utils';
import { genInterfaceFile } from './gen-interface-file';
import { genSchemaDataFile } from './gen-json-schema-file';
Expand Down Expand Up @@ -70,10 +73,26 @@ const genTsData = async (opts: IGenTsDataOpts) => {
// openapi生成TS类型文件
const schemaString = await openapiTS(schema, {
commentHeader: `${fileTip}`,
transform: (schemaObject, options) => {
if (
'type' in schemaObject &&
schemaObject.type === 'array' &&
Array.isArray(schemaObject.items)
) {
// tuple type support
const result: string[] = [];
schemaObject.items.forEach((item) => {
result.push(defaultSchemaObjectTransform(item, options));
});

return `[${result.join(',')}]`;
}
return undefined;
},
postTransform: (type: string) => {
return type.replace(/\\\\"/gi, '\\"');
},
});
} as OpenAPITSOptions);
const tsSchemaPath = path.join(genTsAbsolutePath, 'ts-schema.ts');
fs.writeFileSync(
tsSchemaPath,
Expand Down
188 changes: 179 additions & 9 deletions packages/openapi-gen-ts/test/openapi/openapiv3-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,7 @@
"type": "number"
},
{
"type": "boolean",
"enum": [false]
},
{
"type": "boolean",
"enum": [true]
"type": "boolean"
}
]
}
Expand Down Expand Up @@ -533,15 +528,83 @@
"in": "query",
"schema": {
"anyOf": [
{
"type": ["string", "number"],
"enum": ["1", "2", 3]
},
{
"type": "boolean",
"enum": [true]
}
]
}
},
{
"name": "queryParam11",
"in": "query",
"schema": {
"anyOf": [
{
"type": ["string", "number"],
"enum": ["1", "2", 3]
},
{
"type": "boolean"
}
]
}
},
{
"name": "queryParam12",
"in": "query",
"schema": {
"anyOf": [
{
"type": ["string", "number"],
"enum": ["1", "2", 3]
},
{
"type": "boolean"
}
]
}
},
{
"name": "queryParam13",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "queryParam14",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "queryParam15",
"in": "query",
"schema": {
"type": "array",
"items": {
"items": [
{
"$ref": "#/components/schemas/IParam2"
},
{
"type": "number"
},
{
"$ref": "#/components/schemas/getQueryParams1Request"
}
],
"type": "array",
"maxItems": 3,
"minItems": 3
}
}
}
]
}
Expand Down Expand Up @@ -865,6 +928,7 @@
{
"name": "inlineQueryParam2",
"in": "query",
"required": true,
"schema": {
"anyOf": [
{
Expand All @@ -875,6 +939,90 @@
}
]
}
},
{
"name": "inlineQueryParam3",
"in": "query",
"required": true,
"schema": {}
},
{
"name": "inlineQueryParam4",
"in": "query",
"required": true,
"schema": {
"type": "null"
}
},
{
"name": "inlineQueryParam5",
"in": "query",
"required": true,
"schema": {
"items": [
{
"type": "string"
},
{
"type": "boolean"
}
],
"type": "array",
"maxItems": 2,
"minItems": 2
}
},
{
"name": "inlineQueryParam6",
"in": "query",
"required": true,
"schema": {
"type": "array",
"items": {
"items": [
{
"type": "string"
},
{
"type": "boolean"
}
],
"type": "array",
"maxItems": 2,
"minItems": 2
}
}
},
{
"name": "inlineQueryParam7",
"in": "query",
"required": true,
"schema": {
"type": "array",
"items": {
"items": [
{
"items": [
{
"type": "string"
},
{
"type": "boolean"
}
],
"type": "array",
"maxItems": 2,
"minItems": 2
},
{
"type": "number"
}
],
"type": "array",
"maxItems": 2,
"minItems": 2
}
}
}
]
}
Expand Down Expand Up @@ -998,6 +1146,14 @@
},
"description": "单行注释"
},
{
"name": "param2",
"in": "header",
"required": true,
"schema": {
"type": "null"
}
},
{
"name": "param1",
"in": "cookie",
Expand All @@ -1007,6 +1163,14 @@
},
"description": "单行注释"
},
{
"name": "param2",
"in": "cookie",
"required": true,
"schema": {
"type": "null"
}
},
{
"name": "queryParam1",
"in": "query",
Expand Down Expand Up @@ -1705,9 +1869,12 @@
"param1": {
"description": "单行注释",
"type": "string"
},
"param2": {
"type": "null"
}
},
"required": ["param1"]
"required": ["param1", "param2"]
},
"IParam1": {
"type": "object",
Expand Down Expand Up @@ -1797,9 +1964,12 @@
"param1": {
"description": "单行注释",
"type": "string"
},
"param2": {
"type": "null"
}
},
"required": ["param1"]
"required": ["param1", "param2"]
},
{
"enum": ["1", "2", 3]
Expand Down Expand Up @@ -2033,7 +2203,7 @@
"tags": [
{
"name": "Test4",
"description": "Test3Controller 注释\r\n注释3"
"description": "Test3Controller 注释\n注释3"
},
{
"name": "Test3",
Expand Down

0 comments on commit 72671a6

Please sign in to comment.