Skip to content

Commit 76e6851

Browse files
authored
Merge pull request ferdikoomen#8 from nicolas-chaulet/fix/api-models-regexp
fix(client): support regexp to select models to export
2 parents b0a2db3 + 8a4fad3 commit 76e6851

File tree

7 files changed

+21
-12
lines changed

7 files changed

+21
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ $ openapi --help
4444
--useUnionTypes Use union types instead of enums
4545
--exportCore <value> Write core files to disk (default: true)
4646
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
47-
--exportModels <value> Write models to disk (default: true)
47+
--exportModels <value> Write models to disk [true, false, regexp] (default: true)
4848
--exportSchemas <value> Write schemas to disk (default: false)
4949
--indent <value> Indentation options [4, 2, tab] (default: "4")
5050
--postfixServices Service name postfix (default: "Service")

bin/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ const params = program
2929

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

32-
if (OpenAPI) {
33-
let exportServices;
32+
const parseBooleanOrString = value => {
3433
try {
35-
exportServices = JSON.parse(params.exportServices) === true;
34+
return JSON.parse(value) === true;
3635
} catch (error) {
37-
exportServices = params.exportServices;
36+
return value;
3837
}
38+
};
39+
40+
if (OpenAPI) {
3941
OpenAPI.generate({
4042
input: params.input,
4143
output: params.output,
@@ -44,8 +46,8 @@ if (OpenAPI) {
4446
useOptions: params.useOptions,
4547
useUnionTypes: params.useUnionTypes,
4648
exportCore: JSON.parse(params.exportCore) === true,
47-
exportServices,
48-
exportModels: JSON.parse(params.exportModels) === true,
49+
exportServices: parseBooleanOrString(params.exportServices),
50+
exportModels: parseBooleanOrString(params.exportModels),
4951
exportSchemas: JSON.parse(params.exportSchemas) === true,
5052
indent: params.indent,
5153
postfixServices: params.postfixServices,

bin/index.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('bin', () => {
4343
expect(result.stderr.toString()).toBe('');
4444
});
4545

46-
it('it should support regexp in exportSchemas', async () => {
46+
it('it should support regexp params', async () => {
4747
const result = crossSpawn.sync('node', [
4848
'./bin/index.js',
4949
'--input',
@@ -52,6 +52,8 @@ describe('bin', () => {
5252
'./test/generated/bin',
5353
'--exportServices',
5454
'^(Simple|Types)',
55+
'--exportModels',
56+
'^(Simple|Types)',
5557
]);
5658
expect(result.stdout.toString()).toBe('');
5759
expect(result.stderr.toString()).toBe('');

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type Options = {
2121
useUnionTypes?: boolean;
2222
exportCore?: boolean;
2323
exportServices?: boolean | string;
24-
exportModels?: boolean;
24+
exportModels?: boolean | string;
2525
exportSchemas?: boolean;
2626
indent?: Indent;
2727
postfixServices?: string;

src/utils/writeClient.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const writeClient = async (
4242
useUnionTypes: boolean,
4343
exportCore: boolean,
4444
exportServices: boolean | string,
45-
exportModels: boolean,
45+
exportModels: boolean | string,
4646
exportSchemas: boolean,
4747
indent: Indent,
4848
postfixServices: string,
@@ -65,6 +65,11 @@ export const writeClient = async (
6565
client.services = client.services.filter(service => regexp.test(service.name));
6666
}
6767

68+
if (typeof exportModels === 'string') {
69+
const regexp = new RegExp(exportModels);
70+
client.models = client.models.filter(model => regexp.test(model.name));
71+
}
72+
6873
if (exportCore) {
6974
await rmdir(outputPathCore);
7075
await mkdir(outputPathCore);

src/utils/writeClientIndex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const writeClientIndex = async (
3030
useUnionTypes: boolean,
3131
exportCore: boolean,
3232
exportServices: boolean | string,
33-
exportModels: boolean,
33+
exportModels: boolean | string,
3434
exportSchemas: boolean,
3535
postfixServices: string,
3636
postfixModels: string,

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type Options = {
2121
useUnionTypes?: boolean;
2222
exportCore?: boolean;
2323
exportServices?: boolean | string;
24-
exportModels?: boolean;
24+
exportModels?: boolean | string;
2525
exportSchemas?: boolean;
2626
indent?: Indent | '4' | '2' | 'tab';
2727
postfixServices?: string;

0 commit comments

Comments
 (0)