Skip to content

Commit

Permalink
feat: support typescript-json-schema options config
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Apr 26, 2023
1 parent 3bc7eaf commit 54b83c6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/openapi-gen-ts/src/gen/gen-json-schema-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@ import type { IPrettierOptions } from '@liangskyli/utils';
import { colors, prettierData } from '@liangskyli/utils';
import fs from 'fs-extra';
import path from 'path';
import type { PartialArgs } from 'typescript-json-schema';
import * as TJS from 'typescript-json-schema';

type IOpts = {
export type IGenSchemaDataFile = {
tsSchemaPath: string;
genSchemaAPIAbsolutePath: string;
compilerOptions?: TJS.CompilerOptions;
prettierOptions?: IPrettierOptions;
typescriptJsonSchemaOptions?: PartialArgs;
};

const genSchemaDataFile = async (opts: IOpts) => {
const genSchemaDataFile = async (opts: IGenSchemaDataFile) => {
const {
tsSchemaPath,
genSchemaAPIAbsolutePath,
compilerOptions = { strictNullChecks: true },
typescriptJsonSchemaOptions = {},
} = opts;
let { prettierOptions } = opts;
const program = TJS.getProgramFromFiles([tsSchemaPath], compilerOptions);
const program = TJS.getProgramFromFiles([tsSchemaPath]);
const schemaDefinition = TJS.generateSchema(program, 'paths', {
required: true,
ignoreErrors: true,
...typescriptJsonSchemaOptions,
});
if (prettierOptions === undefined) {
prettierOptions = { parser: 'json' };
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-gen-ts/src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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';
import { genSchemaDataFile } from './gen-json-schema-file';

export type IGenTsDataOpts = {
Expand All @@ -37,6 +38,7 @@ export type IGenTsDataOpts = {
requestQueryOmit?: string[];
requestBodyOmit?: string[];
openAPITSOptions?: Omit<OpenAPITSOptions, 'commentHeader'>;
typescriptJsonSchemaOptions?: IGenSchemaDataFile['typescriptJsonSchemaOptions'];
};

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

const genTsPath = path.join(genTsDir, 'schema-api');
Expand Down Expand Up @@ -112,6 +115,7 @@ const genTsData = async (opts: IGenTsDataOpts) => {
tsSchemaPath,
genSchemaAPIAbsolutePath: genTsAbsolutePath,
prettierOptions: copyOptions(prettierOptions),
typescriptJsonSchemaOptions,
});

// 生成接口文件
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-gen-ts/test/http/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ genTsData({
},
//requestQueryOmit: ['activityId','b'],
//requestBodyOmit: ['a','b'],
typescriptJsonSchemaOptions: { strictNullChecks: true },
}).then();

genTsData({
Expand Down

0 comments on commit 54b83c6

Please sign in to comment.