Skip to content

Commit 1c6144b

Browse files
committed
Refactor generation options
1 parent 6a18281 commit 1c6144b

13 files changed

+126
-158
lines changed

bin/commands/FromSpec/generateDefaultConfig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function getDefaultConfig (): StructuredOptions {
3030
exportCore: true,
3131
exportServices: true,
3232
exportModels: true,
33-
exportSchemas: false,
33+
exportSchemas: true,
34+
exportControllers: true,
3435
},
3536
// write: false,
3637
// request: false,

src/index.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { writeClient } from './utils/writeClient';
1010
import { Client } from "./client/interfaces/Client";
1111
import { OpenApi as OpenApiV3 } from "./openApi/v3/interfaces/OpenApi";
1212
import { OpenApi as OpenApiV2 } from "./openApi/v2/interfaces/OpenApi";
13-
import { GenerationOptions } from "./types/default";
13+
import { ExportOptions, ExportOptionsMight, GenerationOptions } from "./types/default";
1414

1515
export { HttpClient } from './HttpClient';
1616

@@ -23,10 +23,7 @@ export { HttpClient } from './HttpClient';
2323
* @param httpClient The selected httpClient (fetch or XHR)
2424
* @param useOptions Use options or arguments functions
2525
* @param useUnionTypes Use union types instead of enums
26-
* @param exportCore: Generate core client classes
27-
* @param exportServices: Generate services
28-
* @param exportModels: Generate models
29-
* @param exportSchemas: Generate schemas
26+
* @param export_options
3027
* @param request: Path to custom request file
3128
* @param write Write the files to disk (true or false)
3229
*/
@@ -36,10 +33,7 @@ export async function generate({
3633
httpClient = HttpClient.FETCH,
3734
useOptions = false,
3835
useUnionTypes = false,
39-
exportCore = true,
40-
exportServices = true,
41-
exportModels = true,
42-
exportSchemas = false,
36+
export_options = { },
4337
request,
4438
write = true,
4539
}: GenerationOptions): Promise<void> {
@@ -51,10 +45,35 @@ export async function generate({
5145
useOptions,
5246
});
5347

48+
const defined_export_options = fixOptions(export_options);
49+
5450
const clientFinal = getClient(openApiVersion, openApi);
5551
if (write) {
56-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
52+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, defined_export_options, request);
53+
}
54+
}
55+
56+
const fixOptions = (some: ExportOptionsMight): ExportOptions => {
57+
let a = {...some};
58+
59+
if (a.exportCore === undefined) {
60+
a.exportCore = true;
5761
}
62+
if (a.exportServices === undefined) {
63+
a.exportServices = true;
64+
}
65+
if (a.exportModels === undefined) {
66+
a.exportModels = true;
67+
}
68+
if (a.exportSchemas === undefined) {
69+
a.exportSchemas = true;
70+
}
71+
if (a.exportControllers === undefined) {
72+
a.exportControllers = true;
73+
}
74+
75+
//@ts-ignore
76+
return a;
5877
}
5978

6079
function getClient (openApiVersion: OpenApiVersion, openApi: OpenApiV3 | OpenApiV2): Client {

src/types/default.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,25 @@ export type GenerationOptions = {
66
httpClient?: HttpClient;
77
useOptions?: boolean;
88
useUnionTypes?: boolean;
9+
export_options?: ExportOptionsMight;
10+
request?: string;
11+
write?: boolean;
12+
};
13+
14+
export interface ExportOptions {
15+
exportCore: boolean;
16+
exportServices: boolean;
17+
exportModels: boolean;
18+
exportSchemas: boolean;
19+
exportControllers: boolean;
20+
}
21+
22+
export interface ExportOptionsMight {
923
exportCore?: boolean;
1024
exportServices?: boolean;
1125
exportModels?: boolean;
1226
exportSchemas?: boolean;
13-
request?: string;
14-
write?: boolean;
15-
};
27+
exportControllers?: boolean;
28+
}
29+
30+

src/utils/common_test_files.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Templates } from "./registerHandlebarTemplates";
2+
3+
export const test_templates: Templates = {
4+
index: () => 'index',
5+
// TODO :: How to use this??
6+
backend_index: () => 'backend_index',
7+
exports: {
8+
model: () => 'model',
9+
schema: () => 'schema',
10+
service: () => 'service',
11+
},
12+
core: {
13+
settings: () => 'settings',
14+
apiError: () => 'apiError',
15+
apiRequestOptions: () => 'apiRequestOptions',
16+
apiResult: () => 'apiResult',
17+
request: () => 'request',
18+
},
19+
};

src/utils/registerHandlebarTemplates.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { register_partials, register_generic_functions, register_fetch_files, re
2222

2323
export interface Templates {
2424
index: Handlebars.TemplateDelegate;
25+
backend_index: Handlebars.TemplateDelegate;
2526
exports: {
2627
model: Handlebars.TemplateDelegate;
2728
schema: Handlebars.TemplateDelegate;
@@ -48,6 +49,7 @@ export function registerHandlebarTemplates(root: { httpClient: HttpClient; useOp
4849
// Main templates (entry points for the files we write to disk)
4950
const templates: Templates = {
5051
index: Handlebars.template(templateIndex),
52+
backend_index: Handlebars.template(templateIndex),
5153
exports: {
5254
model: Handlebars.template(templateExportModel),
5355
schema: Handlebars.template(templateExportSchema),

src/utils/writeClient.spec.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Client } from '../client/interfaces/Client';
22
import { HttpClient } from '../HttpClient';
33
import { mkdir, rmdir, writeFile } from './fileSystem';
4-
import { Templates } from './registerHandlebarTemplates';
54
import { writeClient } from './writeClient';
5+
import { test_templates } from "./common_test_files";
66

77
jest.mock('./fileSystem');
88

@@ -15,23 +15,13 @@ describe('writeClient', () => {
1515
services: [],
1616
};
1717

18-
const templates: Templates = {
19-
index: () => 'index',
20-
exports: {
21-
model: () => 'model',
22-
schema: () => 'schema',
23-
service: () => 'service',
24-
},
25-
core: {
26-
settings: () => 'settings',
27-
apiError: () => 'apiError',
28-
apiRequestOptions: () => 'apiRequestOptions',
29-
apiResult: () => 'apiResult',
30-
request: () => 'request',
31-
},
32-
};
33-
34-
await writeClient(client, templates, './dist', HttpClient.FETCH, false, false, true, true, true, true);
18+
await writeClient(client, test_templates, './dist', HttpClient.FETCH, false, false, {
19+
exportCore: true,
20+
exportServices: true,
21+
exportModels: true,
22+
exportSchemas: true,
23+
exportControllers: false
24+
});
3525

3626
expect(rmdir).toBeCalled();
3727
expect(mkdir).toBeCalled();

src/utils/writeClient.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { writeClientIndex } from './writeClientIndex';
1010
import { writeClientModels } from './writeClientModels';
1111
import { writeClientSchemas } from './writeClientSchemas';
1212
import { writeBackendControllers, writeClientServices } from './writeClientServices';
13+
import { ExportOptions } from "../types/default";
1314

1415
/**
1516
* Write our OpenAPI client, using the given templates at the given output
@@ -19,10 +20,7 @@ import { writeBackendControllers, writeClientServices } from './writeClientServi
1920
* @param httpClient The selected httpClient (fetch, xhr or node)
2021
* @param useOptions Use options or arguments functions
2122
* @param useUnionTypes Use union types instead of enums
22-
* @param exportCore: Generate core client classes
23-
* @param exportServices: Generate services
24-
* @param exportModels: Generate models
25-
* @param exportSchemas: Generate schemas
23+
* @param exportOptions
2624
* @param request: Path to custom request file
2725
*/
2826
export async function writeClient(
@@ -32,10 +30,7 @@ export async function writeClient(
3230
httpClient: HttpClient,
3331
useOptions: boolean,
3432
useUnionTypes: boolean,
35-
exportCore: boolean,
36-
exportServices: boolean,
37-
exportModels: boolean,
38-
exportSchemas: boolean,
33+
exportOptions: ExportOptions,
3934
request?: string
4035
): Promise<void> {
4136
// Todo :: Configurable folder names
@@ -52,44 +47,45 @@ export async function writeClient(
5247
throw new Error(`Output folder is not a subdirectory of the current working directory`);
5348
}
5449

55-
if (exportCore) {
50+
if (exportOptions.exportCore) {
5651
await rmdir(outputPathCore);
5752
await mkdir(outputPathCore);
5853
const promise = writeClientCore(client, templates, outputPathCore, httpClient, request);
5954
promises.push(promise);
6055
}
6156

62-
if (exportServices) {
57+
if (exportOptions.exportServices) {
6358
await rmdir(outputPathServices);
6459
await mkdir(outputPathServices);
6560
const promise = writeClientServices(client.services, templates, outputPathServices, httpClient, useUnionTypes, useOptions);
6661
promises.push(promise);
6762
}
6863

69-
if (client.controllers) {
64+
if (exportOptions.exportControllers && client.controllers) {
7065
await rmdir(output_path_controllers);
7166
await mkdir(output_path_controllers);
7267
const promise = writeBackendControllers(client.controllers, templates, output_path_controllers, httpClient, useUnionTypes, useOptions);
7368
promises.push(promise);
7469
}
7570

76-
if (exportSchemas) {
71+
if (exportOptions.exportControllers) {
7772
await rmdir(outputPathSchemas);
7873
await mkdir(outputPathSchemas);
7974
const promise = writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes);
8075
promises.push(promise);
8176
}
8277

83-
if (exportModels) {
78+
if (exportOptions.exportControllers) {
8479
await rmdir(outputPathModels);
8580
await mkdir(outputPathModels);
8681
const promise = writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes);
8782
promises.push(promise);
8883
}
8984

90-
if (exportCore || exportServices || exportSchemas || exportModels) {
85+
if ( exportOptions.exportCore || exportOptions.exportServices
86+
|| exportOptions.exportSchemas || exportOptions.exportModels) {
9187
await mkdir(outputPath);
92-
const promise = writeClientIndex(client, templates, outputPath, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
88+
const promise = writeClientIndex(client, templates, outputPath, useUnionTypes, exportOptions);
9389
promises.push(promise);
9490
}
9591

src/utils/writeClientCore.spec.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Client } from '../client/interfaces/Client';
22
import { HttpClient } from '../HttpClient';
33
import { writeFile } from './fileSystem';
4-
import { Templates } from './registerHandlebarTemplates';
54
import { writeClientCore } from './writeClientCore';
5+
import { test_templates } from "./common_test_files";
66

77
jest.mock('./fileSystem');
88

@@ -15,23 +15,7 @@ describe('writeClientCore', () => {
1515
services: [],
1616
};
1717

18-
const templates: Templates = {
19-
index: () => 'index',
20-
exports: {
21-
model: () => 'model',
22-
schema: () => 'schema',
23-
service: () => 'service',
24-
},
25-
core: {
26-
settings: () => 'settings',
27-
apiError: () => 'apiError',
28-
apiRequestOptions: () => 'apiRequestOptions',
29-
apiResult: () => 'apiResult',
30-
request: () => 'request',
31-
},
32-
};
33-
34-
await writeClientCore(client, templates, '/', HttpClient.FETCH);
18+
await writeClientCore(client, test_templates, '/', HttpClient.FETCH);
3519

3620
expect(writeFile).toBeCalledWith('/OpenAPI.ts', 'settings');
3721
expect(writeFile).toBeCalledWith('/ApiError.ts', 'apiError');

src/utils/writeClientIndex.spec.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Client } from '../client/interfaces/Client';
22
import { writeFile } from './fileSystem';
3-
import { Templates } from './registerHandlebarTemplates';
43
import { writeClientIndex } from './writeClientIndex';
4+
import { test_templates } from "./common_test_files";
55

66
jest.mock('./fileSystem');
77

@@ -14,23 +14,13 @@ describe('writeClientIndex', () => {
1414
services: [],
1515
};
1616

17-
const templates: Templates = {
18-
index: () => 'index',
19-
exports: {
20-
model: () => 'model',
21-
schema: () => 'schema',
22-
service: () => 'service',
23-
},
24-
core: {
25-
settings: () => 'settings',
26-
apiError: () => 'apiError',
27-
apiRequestOptions: () => 'apiRequestOptions',
28-
apiResult: () => 'apiResult',
29-
request: () => 'request',
30-
},
31-
};
32-
33-
await writeClientIndex(client, templates, '/', true, true, true, true, true);
17+
await writeClientIndex(client, test_templates, '/', true, {
18+
exportCore: true,
19+
exportServices: true,
20+
exportModels: true,
21+
exportSchemas: true,
22+
exportControllers: false,
23+
});
3424

3525
expect(writeFile).toBeCalledWith('/index.ts', 'index');
3626
});

0 commit comments

Comments
 (0)