Skip to content

Commit c12f703

Browse files
author
A.K Sahin
committed
feat(exportService): introduce serviceOptions to enhance generated service class
1 parent 00cb70b commit c12f703

File tree

7 files changed

+66
-18
lines changed

7 files changed

+66
-18
lines changed

bin/index.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,32 @@ const params = program
2121
.option('--exportSchemas <value>', 'Write schemas to disk', false)
2222
.option('--postfix <value>', 'Service name postfix', 'Service')
2323
.option('--request <value>', 'Path to custom request file')
24+
.option('-so, --serviceOptions <value>, Service options path')
2425
.parse(process.argv)
2526
.opts();
2627

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

30+
const genereteParams = {
31+
input: program.input,
32+
output: program.output,
33+
httpClient: program.client,
34+
useOptions: program.useOptions,
35+
useUnionTypes: program.useUnionTypes,
36+
exportCore: JSON.parse(program.exportCore) === true,
37+
exportServices: JSON.parse(program.exportServices) === true,
38+
exportModels: JSON.parse(program.exportModels) === true,
39+
exportSchemas: JSON.parse(program.exportSchemas) === true,
40+
postfix: params.postfix,
41+
request: params.request,
42+
};
43+
44+
if (program.serviceOptions) {
45+
genereteParams.serviceOptions = require(path.resolve(__dirname, '../../', program.serviceOptions));
46+
}
47+
2948
if (OpenAPI) {
30-
OpenAPI.generate({
31-
input: params.input,
32-
output: params.output,
33-
httpClient: params.client,
34-
useOptions: params.useOptions,
35-
useUnionTypes: params.useUnionTypes,
36-
exportCore: JSON.parse(params.exportCore) === true,
37-
exportServices: JSON.parse(params.exportServices) === true,
38-
exportModels: JSON.parse(params.exportModels) === true,
39-
exportSchemas: JSON.parse(params.exportSchemas) === true,
40-
postfix: params.postfix,
41-
request: params.request,
42-
})
49+
OpenAPI.generate(genereteParams)
4350
.then(() => {
4451
process.exit(0);
4552
})

src/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ import { writeClient } from './utils/writeClient';
1010

1111
export { HttpClient } from './HttpClient';
1212

13-
export type Options = {
13+
export interface ServiceOptions {
14+
serviceImports?: string;
15+
serviceConstructor?: string;
16+
serviceDecorator?: string;
17+
staticMethods?: boolean;
18+
}
19+
20+
export interface Options {
1421
input: string | Record<string, any>;
1522
output: string;
23+
serviceOptions?: ServiceOptions;
1624
httpClient?: HttpClient;
1725
useOptions?: boolean;
1826
useUnionTypes?: boolean;
@@ -23,7 +31,7 @@ export type Options = {
2331
postfix?: string;
2432
request?: string;
2533
write?: boolean;
26-
};
34+
}
2735

2836
/**
2937
* Generate the OpenAPI client. This method will read the OpenAPI specification and based on the
@@ -55,6 +63,9 @@ export async function generate({
5563
postfix = 'Service',
5664
request,
5765
write = true,
66+
serviceOptions = {
67+
staticMethods: true,
68+
},
5869
}: Options): Promise<void> {
5970
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
6071
const openApiVersion = getOpenApiVersion(openApi);
@@ -81,6 +92,7 @@ export async function generate({
8192
exportModels,
8293
exportSchemas,
8394
postfix,
95+
serviceOptions,
8496
request
8597
);
8698
break;
@@ -102,6 +114,7 @@ export async function generate({
102114
exportModels,
103115
exportSchemas,
104116
postfix,
117+
serviceOptions,
105118
request
106119
);
107120
break;

src/templates/core/ApiRequestOptions.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export type ApiRequestOptions = {
1111
readonly mediaType?: string;
1212
readonly responseHeader?: string;
1313
readonly errors?: Record<number, string>;
14+
readonly context?: any;
1415
}

src/templates/exportService.hbs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@ import { request as __request } from '../core/request';
1010
{{#if @root.useVersion}}
1111
import { OpenAPI } from '../core/OpenAPI';
1212
{{/if}}
13+
{{#if @root.serviceImports}}
14+
{{@root.serviceImports}}
15+
{{/if}}
1316

17+
{{#if @root.serviceDecorator}}
18+
{{@root.serviceDecorator}}
19+
{{/if}}
1420
export class {{{name}}}{{{@root.postfix}}} {
21+
{{#if @root.serviceConstructor}}
22+
{{@root.serviceConstructor}}
23+
{{/if}}
1524

1625
{{#each operations}}
1726
/**
@@ -36,7 +45,7 @@ export class {{{name}}}{{{@root.postfix}}} {
3645
{{/each}}
3746
* @throws ApiError
3847
*/
39-
public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
48+
public{{#if @root.staticMethods}} static{{/if}} {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
4049
return __request({
4150
method: '{{{method}}}',
4251
path: `{{{path}}}`,
@@ -89,6 +98,9 @@ export class {{{name}}}{{{@root.postfix}}} {
8998
{{/each}}
9099
},
91100
{{/if}}
101+
{{#if @root.serviceRequestContext}}
102+
context: {{@root.serviceRequestContext}},
103+
{{/if}}
92104
});
93105
}
94106

src/utils/writeClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'path';
22

33
import type { Client } from '../client/interfaces/Client';
44
import { HttpClient } from '../HttpClient';
5+
import { ServiceOptions } from '../index';
56
import { mkdir, rmdir } from './fileSystem';
67
import { isSubDirectory } from './isSubdirectory';
78
import { Templates } from './registerHandlebarTemplates';
@@ -39,6 +40,7 @@ export async function writeClient(
3940
exportModels: boolean,
4041
exportSchemas: boolean,
4142
postfix: string,
43+
serviceOptions: ServiceOptions,
4244
request?: string
4345
): Promise<void> {
4446
const outputPath = resolve(process.cwd(), output);
@@ -67,7 +69,8 @@ export async function writeClient(
6769
httpClient,
6870
useUnionTypes,
6971
useOptions,
70-
postfix
72+
postfix,
73+
serviceOptions
7174
);
7275
}
7376

src/utils/writeClientServices.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { resolve } from 'path';
2+
import { ServiceOptions } from '..';
23

34
import type { Service } from '../client/interfaces/Service';
45
import { HttpClient } from '../HttpClient';
@@ -25,7 +26,8 @@ export async function writeClientServices(
2526
httpClient: HttpClient,
2627
useUnionTypes: boolean,
2728
useOptions: boolean,
28-
postfix: string
29+
postfix: string,
30+
serviceOptions: ServiceOptions
2931
): Promise<void> {
3032
for (const service of services) {
3133
const file = resolve(outputPath, `${service.name}${postfix}.ts`);
@@ -37,6 +39,7 @@ export async function writeClientServices(
3739
useVersion,
3840
useOptions,
3941
postfix,
42+
...serviceOptions,
4043
});
4144
await writeFile(file, format(templateResult));
4245
}

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ const generate = async (input, output) => {
1616
exportServices: true,
1717
// postfix: 'Api',
1818
// request: './test/custom/request.ts',
19+
// serviceOptions: {
20+
// staticMethods: false,
21+
// serviceImports: `
22+
// import {Injectable} from '@angular/core';
23+
// import {HttpClient} from '@angular/common/http';
24+
// `,
25+
// serviceDecorator: `@Injectable()`,
26+
// serviceConstructor: `constructor(private http: HttpClient) {}`,
27+
// },
1928
});
2029
};
2130

0 commit comments

Comments
 (0)