Skip to content

Commit

Permalink
feat: upgrade openapi-typescript and remove temporary code
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Apr 26, 2023
1 parent f3901eb commit 18eee85
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/openapi-gen-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"axios": "^1.3.6",
"commander": "^10.0.1",
"fs-extra": "^11.1.1",
"openapi-typescript": "^6.2.2",
"openapi-typescript": "^6.2.3",
"typescript-json-schema": "^0.56.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import type IOpenapiTS from 'openapi-typescript';
register.register({});

const openapiTS = require('openapi-typescript').default as typeof IOpenapiTS;
const defaultSchemaObjectTransform =
require('openapi-typescript/dist/transform/schema-object').defaultSchemaObjectTransform;

register.restore();

export default openapiTS;
export { defaultSchemaObjectTransform };
24 changes: 2 additions & 22 deletions packages/openapi-gen-ts/src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
import fs from 'fs-extra';
import type { OpenAPITSOptions } from 'openapi-typescript';
import path from 'path';
import openapiTS, {
defaultSchemaObjectTransform,
} from '../esm-to-commjs/openapi-typescript';
import openapiTS from '../esm-to-commjs/openapi-typescript';
import { fileTip } from '../utils';
import { genInterfaceFile } from './gen-interface-file';
import type { IGenSchemaDataFile } from './gen-json-schema-file';
Expand Down Expand Up @@ -75,28 +73,10 @@ const genTsData = async (opts: IGenTsDataOpts) => {

fs.ensureDirSync(genTsAbsolutePath);

const { transform, ...otherOpenAPITSOptions } = openAPITSOptions;

// 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 transform?.(schemaObject, options);
},
...otherOpenAPITSOptions,
...openAPITSOptions,
});
const tsSchemaPath = path.join(genTsAbsolutePath, 'ts-schema.ts');
fs.writeFileSync(
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 18eee85

Please sign in to comment.