Skip to content

Commit 9fad3ad

Browse files
committed
Inject the request method into service classes
Rather than import the request method directly, service classes will now receive it as a constructor argument and save it as a private field. This makes the constructor classes more generic and testable. To avoid breaking changes, service class instances are instantiated and exported from index.ts with PascalCase naming. This allows users to update with no changes, because the public API does not change noticeably.
1 parent 662f599 commit 9fad3ad

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/templates/exportService.hbs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import type { {{{this}}} } from '../models/{{{this}}}';
66
{{/each}}
77
{{/if}}
8-
import { request as __request } from '../core/request';
8+
import { ApiRequestOptions } from '../core/ApiRequestOptions';
99
{{#if @root.useVersion}}
1010
import { OpenAPI } from '../core/OpenAPI';
1111
{{/if}}
1212

1313
export class {{{name}}} {
14+
constructor(private __request: (options: ApiRequestOptions) => Promise<any>) {}
1415

1516
{{#each operations}}
1617
/**
@@ -35,8 +36,8 @@ export class {{{name}}} {
3536
{{/each}}
3637
* @throws ApiError
3738
*/
38-
public static async {{{name}}}({{>parameters}}): Promise<{{>result}}> {
39-
return await __request({
39+
public async {{{name}}}({{>parameters}}): Promise<{{>result}}> {
40+
return await this.__request({
4041
method: '{{{method}}}',
4142
path: `{{{path}}}`,
4243
{{#if parametersCookie}}

src/templates/index.hbs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
{{>header}}
2+
{{#if @root.exportServices}}
3+
{{#if services}}
4+
5+
{{#each services}}
6+
import { {{{name}}} as _{{{name}}} } from './services/{{{name}}}';
7+
{{/each}}
8+
import { request } from './core/request';
9+
{{/if}}
10+
{{/if}}
211
{{#if @root.exportCore}}
312

413
export { ApiError } from './core/ApiError';
@@ -32,7 +41,7 @@ export { ${{{name}}} } from './schemas/${{{name}}}';
3241
{{#if services}}
3342

3443
{{#each services}}
35-
export { {{{name}}} } from './services/{{{name}}}';
44+
export const {{{name}}} = new _{{{name}}}(request);
3645
{{/each}}
3746
{{/if}}
3847
{{/if}}

0 commit comments

Comments
 (0)