Skip to content

Extension options for generated service classes #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,32 @@ const params = program
.option('--exportSchemas <value>', 'Write schemas to disk', false)
.option('--postfix <value>', 'Service name postfix', 'Service')
.option('--request <value>', 'Path to custom request file')
.option('-so, --serviceOptions <value>, Service options path')
.parse(process.argv)
.opts();

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

const genereteParams = {
input: params.input,
output: params.output,
httpClient: params.client,
useOptions: params.useOptions,
useUnionTypes: params.useUnionTypes,
exportCore: JSON.parse(params.exportCore) === true,
exportServices: JSON.parse(params.exportServices) === true,
exportModels: JSON.parse(params.exportModels) === true,
exportSchemas: JSON.parse(params.exportSchemas) === true,
postfix: params.postfix,
request: params.request,
};

if (params.serviceOptions) {
genereteParams.serviceOptions = require(path.resolve(process.cwd(), params.serviceOptions));
}

if (OpenAPI) {
OpenAPI.generate({
input: params.input,
output: params.output,
httpClient: params.client,
useOptions: params.useOptions,
useUnionTypes: params.useUnionTypes,
exportCore: JSON.parse(params.exportCore) === true,
exportServices: JSON.parse(params.exportServices) === true,
exportModels: JSON.parse(params.exportModels) === true,
exportSchemas: JSON.parse(params.exportSchemas) === true,
postfix: params.postfix,
request: params.request,
})
OpenAPI.generate(genereteParams)
.then(() => {
process.exit(0);
})
Expand Down
18 changes: 16 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ import { writeClient } from './utils/writeClient';

export { HttpClient } from './HttpClient';

export type Options = {
export interface ServiceOptions {
serviceImports?: string;
serviceConstructor?: string;
serviceDecorator?: string;
staticMethods?: boolean;
serviceRequestContext?: string;
}

export interface Options {
input: string | Record<string, any>;
output: string;
serviceOptions?: ServiceOptions;
httpClient?: HttpClient;
useOptions?: boolean;
useUnionTypes?: boolean;
Expand All @@ -23,7 +32,7 @@ export type Options = {
postfix?: string;
request?: string;
write?: boolean;
};
}

/**
* Generate the OpenAPI client. This method will read the OpenAPI specification and based on the
Expand Down Expand Up @@ -55,6 +64,9 @@ export async function generate({
postfix = 'Service',
request,
write = true,
serviceOptions = {
staticMethods: true,
},
}: Options): Promise<void> {
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
const openApiVersion = getOpenApiVersion(openApi);
Expand All @@ -81,6 +93,7 @@ export async function generate({
exportModels,
exportSchemas,
postfix,
serviceOptions,
request
);
break;
Expand All @@ -102,6 +115,7 @@ export async function generate({
exportModels,
exportSchemas,
postfix,
serviceOptions,
request
);
break;
Expand Down
1 change: 1 addition & 0 deletions src/templates/core/ApiRequestOptions.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export type ApiRequestOptions = {
readonly mediaType?: string;
readonly responseHeader?: string;
readonly errors?: Record<number, string>;
readonly context?: any;
}
16 changes: 14 additions & 2 deletions src/templates/exportService.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ import { request as __request } from '../core/request';
{{#if @root.useVersion}}
import { OpenAPI } from '../core/OpenAPI';
{{/if}}
{{#if @root.serviceImports}}
{{@root.serviceImports}}
{{/if}}

{{#if @root.serviceDecorator}}
{{@root.serviceDecorator}}
{{/if}}
export class {{{name}}}{{{@root.postfix}}} {
{{#if @root.serviceConstructor}}
{{@root.serviceConstructor}}
{{/if}}

{{#each operations}}
/**
Expand All @@ -36,8 +45,8 @@ export class {{{name}}}{{{@root.postfix}}} {
{{/each}}
* @throws ApiError
*/
public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
return __request({
public{{#if @root.staticMethods}} static{{/if}} {{{name}}}({{>parameters}}) {
return __request<{{>result}}>({
method: '{{{method}}}',
path: `{{{path}}}`,
{{#if parametersCookie}}
Expand Down Expand Up @@ -89,6 +98,9 @@ export class {{{name}}}{{{@root.postfix}}} {
{{/each}}
},
{{/if}}
{{#if @root.serviceRequestContext}}
context: {{@root.serviceRequestContext}},
{{/if}}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/writeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('writeClient', () => {
},
};

await writeClient(client, templates, './dist', HttpClient.FETCH, false, false, true, true, true, true, '');
await writeClient(client, templates, './dist', HttpClient.FETCH, false, false, true, true, true, true, '', {});

expect(rmdir).toBeCalled();
expect(mkdir).toBeCalled();
Expand Down
5 changes: 4 additions & 1 deletion src/utils/writeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { resolve } from 'path';

import type { Client } from '../client/interfaces/Client';
import { HttpClient } from '../HttpClient';
import { ServiceOptions } from '../index';
import { mkdir, rmdir } from './fileSystem';
import { isSubDirectory } from './isSubdirectory';
import { Templates } from './registerHandlebarTemplates';
Expand Down Expand Up @@ -39,6 +40,7 @@ export async function writeClient(
exportModels: boolean,
exportSchemas: boolean,
postfix: string,
serviceOptions: ServiceOptions,
request?: string
): Promise<void> {
const outputPath = resolve(process.cwd(), output);
Expand Down Expand Up @@ -67,7 +69,8 @@ export async function writeClient(
httpClient,
useUnionTypes,
useOptions,
postfix
postfix,
serviceOptions
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/writeClientServices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('writeClientServices', () => {
},
};

await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, 'Service');
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, 'Service', {});

expect(writeFile).toBeCalledWith('/UserService.ts', 'service');
});
Expand Down
5 changes: 4 additions & 1 deletion src/utils/writeClientServices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'path';

import { ServiceOptions } from '..';
import type { Service } from '../client/interfaces/Service';
import { HttpClient } from '../HttpClient';
import { writeFile } from './fileSystem';
Expand All @@ -25,7 +26,8 @@ export async function writeClientServices(
httpClient: HttpClient,
useUnionTypes: boolean,
useOptions: boolean,
postfix: string
postfix: string,
serviceOptions: ServiceOptions
): Promise<void> {
for (const service of services) {
const file = resolve(outputPath, `${service.name}${postfix}.ts`);
Expand All @@ -37,6 +39,7 @@ export async function writeClientServices(
useVersion,
useOptions,
postfix,
...serviceOptions,
});
await writeFile(file, format(templateResult));
}
Expand Down
Loading