Skip to content

Commit 921dfeb

Browse files
committed
Add an "injected" HTTP client option
If this option is used, services will be exported as uninstantiated classes that accept a method that will handle HTTP requests. The request.ts file will not be generated as it would be unecessary.
1 parent 9fad3ad commit 921dfeb

File tree

6 files changed

+55
-25
lines changed

6 files changed

+55
-25
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export enum HttpClient {
1111
FETCH = 'fetch',
1212
XHR = 'xhr',
1313
NODE = 'node',
14+
INJECTED = 'injected'
1415
}
1516

1617
export type Options = {

src/templates/index.hbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{{>header}}
22
{{#if @root.exportServices}}
33
{{#if services}}
4+
{{#notEquals @root.httpClient 'injected'}}
45

56
{{#each services}}
67
import { {{{name}}} as _{{{name}}} } from './services/{{{name}}}';
78
{{/each}}
89
import { request } from './core/request';
10+
{{/notEquals}}
911
{{/if}}
1012
{{/if}}
1113
{{#if @root.exportCore}}
@@ -40,8 +42,15 @@ export { ${{{name}}} } from './schemas/${{{name}}}';
4042
{{#if @root.exportServices}}
4143
{{#if services}}
4244

45+
{{#notEquals @root.httpClient 'injected'}}
4346
{{#each services}}
4447
export const {{{name}}} = new _{{{name}}}(request);
4548
{{/each}}
49+
{{/notEquals}}
50+
{{~#equals @root.httpClient 'injected'}}
51+
{{#each services}}
52+
export { {{{name}}} } from './services/{{{name}}}';
53+
{{/each}}
54+
{{/equals~}}
4655
{{/if}}
4756
{{/if}}

src/utils/writeClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ export async function writeClient(
7272

7373
if (exportCore || exportServices || exportSchemas || exportModels) {
7474
await mkdir(outputPath);
75-
await writeClientIndex(client, templates, outputPath, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
75+
await writeClientIndex(client, templates, outputPath, httpClient, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
7676
}
7777
}

src/utils/writeClientCore.spec.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,34 @@ import { writeClientCore } from './writeClientCore';
77
jest.mock('./fileSystem');
88

99
describe('writeClientCore', () => {
10-
it('should write to filesystem', async () => {
11-
const client: Client = {
12-
server: 'http://localhost:8080',
13-
version: '1.0',
14-
models: [],
15-
services: [],
16-
};
17-
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-
};
10+
const client: Client = {
11+
server: 'http://localhost:8080',
12+
version: '1.0',
13+
models: [],
14+
services: [],
15+
};
16+
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+
afterEach(() => {
34+
jest.clearAllMocks();
35+
});
3336

37+
it('should write to filesystem', async () => {
3438
await writeClientCore(client, templates, '/', HttpClient.FETCH);
3539

3640
expect(writeFile).toBeCalledWith('/OpenAPI.ts', 'settings');
@@ -39,4 +43,14 @@ describe('writeClientCore', () => {
3943
expect(writeFile).toBeCalledWith('/ApiResult.ts', 'apiResult');
4044
expect(writeFile).toBeCalledWith('/request.ts', 'request');
4145
});
46+
47+
it('should not write request.ts if HTTP client is INJECTED', async () => {
48+
await writeClientCore(client, templates, '/', HttpClient.INJECTED);
49+
50+
expect(writeFile).toBeCalledWith('/OpenAPI.ts', 'settings');
51+
expect(writeFile).toBeCalledWith('/ApiError.ts', 'apiError');
52+
expect(writeFile).toBeCalledWith('/ApiRequestOptions.ts', 'apiRequestOptions');
53+
expect(writeFile).toBeCalledWith('/ApiResult.ts', 'apiResult');
54+
expect(writeFile).not.toBeCalledWith('/request.ts', 'request');
55+
});
4256
});

src/utils/writeClientCore.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ export async function writeClientCore(client: Client, templates: Templates, outp
2222
await writeFile(path.resolve(outputPath, 'ApiError.ts'), templates.core.apiError({}));
2323
await writeFile(path.resolve(outputPath, 'ApiRequestOptions.ts'), templates.core.apiRequestOptions({}));
2424
await writeFile(path.resolve(outputPath, 'ApiResult.ts'), templates.core.apiResult({}));
25-
await writeFile(path.resolve(outputPath, 'request.ts'), templates.core.request(context));
25+
26+
if (httpClient !== HttpClient.INJECTED) {
27+
await writeFile(path.resolve(outputPath, 'request.ts'), templates.core.request(context));
28+
}
2629
}

src/utils/writeClientIndex.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'path';
22

33
import type { Client } from '../client/interfaces/Client';
4+
import {HttpClient} from '..';
45
import { writeFile } from './fileSystem';
56
import { Templates } from './registerHandlebarTemplates';
67
import { sortModelsByName } from './sortModelsByName';
@@ -23,6 +24,7 @@ export async function writeClientIndex(
2324
client: Client,
2425
templates: Templates,
2526
outputPath: string,
27+
httpClient: HttpClient,
2628
useUnionTypes: boolean,
2729
exportCore: boolean,
2830
exportServices: boolean,
@@ -36,6 +38,7 @@ export async function writeClientIndex(
3638
exportServices,
3739
exportModels,
3840
exportSchemas,
41+
httpClient,
3942
useUnionTypes,
4043
server: client.server,
4144
version: client.version,

0 commit comments

Comments
 (0)