Skip to content

Commit

Permalink
Merge pull request #8 from nicolas-chaulet/fix/api-models-regexp
Browse files Browse the repository at this point in the history
fix(client): support regexp to select models to export
  • Loading branch information
mrlubos authored Jan 30, 2024
2 parents b0a2db3 + 8a4fad3 commit 76e6851
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $ openapi --help
--useUnionTypes Use union types instead of enums
--exportCore <value> Write core files to disk (default: true)
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
--exportModels <value> Write models to disk (default: true)
--exportModels <value> Write models to disk [true, false, regexp] (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
--postfixServices Service name postfix (default: "Service")
Expand Down
14 changes: 8 additions & 6 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ const params = program

const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));

if (OpenAPI) {
let exportServices;
const parseBooleanOrString = value => {
try {
exportServices = JSON.parse(params.exportServices) === true;
return JSON.parse(value) === true;
} catch (error) {
exportServices = params.exportServices;
return value;
}
};

if (OpenAPI) {
OpenAPI.generate({
input: params.input,
output: params.output,
Expand All @@ -44,8 +46,8 @@ if (OpenAPI) {
useOptions: params.useOptions,
useUnionTypes: params.useUnionTypes,
exportCore: JSON.parse(params.exportCore) === true,
exportServices,
exportModels: JSON.parse(params.exportModels) === true,
exportServices: parseBooleanOrString(params.exportServices),
exportModels: parseBooleanOrString(params.exportModels),
exportSchemas: JSON.parse(params.exportSchemas) === true,
indent: params.indent,
postfixServices: params.postfixServices,
Expand Down
4 changes: 3 additions & 1 deletion bin/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('bin', () => {
expect(result.stderr.toString()).toBe('');
});

it('it should support regexp in exportSchemas', async () => {
it('it should support regexp params', async () => {
const result = crossSpawn.sync('node', [
'./bin/index.js',
'--input',
Expand All @@ -52,6 +52,8 @@ describe('bin', () => {
'./test/generated/bin',
'--exportServices',
'^(Simple|Types)',
'--exportModels',
'^(Simple|Types)',
]);
expect(result.stdout.toString()).toBe('');
expect(result.stderr.toString()).toBe('');
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type Options = {
useUnionTypes?: boolean;
exportCore?: boolean;
exportServices?: boolean | string;
exportModels?: boolean;
exportModels?: boolean | string;
exportSchemas?: boolean;
indent?: Indent;
postfixServices?: string;
Expand Down
7 changes: 6 additions & 1 deletion src/utils/writeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const writeClient = async (
useUnionTypes: boolean,
exportCore: boolean,
exportServices: boolean | string,
exportModels: boolean,
exportModels: boolean | string,
exportSchemas: boolean,
indent: Indent,
postfixServices: string,
Expand All @@ -65,6 +65,11 @@ export const writeClient = async (
client.services = client.services.filter(service => regexp.test(service.name));
}

if (typeof exportModels === 'string') {
const regexp = new RegExp(exportModels);
client.models = client.models.filter(model => regexp.test(model.name));
}

if (exportCore) {
await rmdir(outputPathCore);
await mkdir(outputPathCore);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/writeClientIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const writeClientIndex = async (
useUnionTypes: boolean,
exportCore: boolean,
exportServices: boolean | string,
exportModels: boolean,
exportModels: boolean | string,
exportSchemas: boolean,
postfixServices: string,
postfixModels: string,
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type Options = {
useUnionTypes?: boolean;
exportCore?: boolean;
exportServices?: boolean | string;
exportModels?: boolean;
exportModels?: boolean | string;
exportSchemas?: boolean;
indent?: Indent | '4' | '2' | 'tab';
postfixServices?: string;
Expand Down

0 comments on commit 76e6851

Please sign in to comment.