Skip to content

Commit

Permalink
feat: support openapi-typescript options config
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Apr 26, 2023
1 parent 3bbac2b commit 3bc7eaf
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/openapi-gen-ts/src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
removeFilesSync,
} from '@liangskyli/utils';
import fs from 'fs-extra';
import type { OpenAPITSOptions } from 'openapi-typescript/src/types';
import path from 'path';
import openapiTS, {
defaultSchemaObjectTransform,
Expand Down Expand Up @@ -35,6 +36,7 @@ export type IGenTsDataOpts = {
};
requestQueryOmit?: string[];
requestBodyOmit?: string[];
openAPITSOptions?: Omit<OpenAPITSOptions, 'commentHeader'>;
};

export type IGenTsDataOptsCLI = IGenTsDataOpts | IGenTsDataOpts[];
Expand All @@ -47,6 +49,7 @@ const genTsData = async (opts: IGenTsDataOpts) => {
requestFile,
requestQueryOmit,
requestBodyOmit,
openAPITSOptions = {},
} = opts;

const genTsPath = path.join(genTsDir, 'schema-api');
Expand All @@ -69,6 +72,9 @@ const genTsData = async (opts: IGenTsDataOpts) => {

fs.ensureDirSync(genTsAbsolutePath);

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

// openapi生成TS类型文件
const schemaString = await openapiTS(schema, {
commentHeader: `${fileTip}`,
Expand All @@ -86,11 +92,14 @@ const genTsData = async (opts: IGenTsDataOpts) => {

return `[${result.join(',')}]`;
}
return undefined;
return transform?.(schemaObject, options);
},
postTransform: (type: string) => {
return type.replace(/\\\\"/gi, '\\"');
postTransform: (type, options) => {
type = type.replace(/\\\\"/gi, '\\"');

return postTransform?.(type, options) ?? type;
},
...otherOpenAPITSOptions,
});
const tsSchemaPath = path.join(genTsAbsolutePath, 'ts-schema.ts');
fs.writeFileSync(
Expand Down

0 comments on commit 3bc7eaf

Please sign in to comment.