Skip to content

Commit 6947775

Browse files
committedSep 21, 2023
feat: Per-request OpenAPI config overrides
1 parent 3dee68a commit 6947775

25 files changed

+6111
-670
lines changed
 

‎docs/authorization.md

+19
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,22 @@ const getToken = async () => {
2222

2323
OpenAPI.TOKEN = getToken;
2424
```
25+
26+
Also, you can pass `TOKEN` or any OpenAPI configuration params to method call.
27+
```typescript
28+
const organizations = new OrganizationsService();
29+
OpenAPI.BASE = 'https://example.com/api';
30+
31+
express.use(async (req, res) => {
32+
const response = await organizations.createOrganization(
33+
{
34+
name: 'OrgName',
35+
description: 'OrgDescription',
36+
},
37+
{
38+
TOKEN: getToken(req)
39+
}
40+
);
41+
res.send(response);
42+
})
43+
```

‎src/templates/core/BaseHttpRequest.hbs

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ export abstract class BaseHttpRequest {
2323
constructor(public readonly config: OpenAPIConfig) {}
2424
{{/equals}}
2525

26+
public abstract request<T>(
27+
options: ApiRequestOptions,
28+
configOverrides?: Partial<OpenAPIConfig>,
2629
{{#equals @root.httpClient 'angular'}}
27-
public abstract request<T>(options: ApiRequestOptions): Observable<T>;
30+
): Observable<T>;
2831
{{else}}
29-
public abstract request<T>(options: ApiRequestOptions): CancelablePromise<T>;
32+
): CancelablePromise<T>;
3033
{{/equals}}
3134
}

‎src/templates/core/HttpRequest.hbs

+18-12
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,31 @@ export class {{httpRequest}} extends BaseHttpRequest {
3737
}
3838
{{/equals}}
3939

40-
{{#equals @root.httpClient 'angular'}}
4140
/**
4241
* Request method
4342
* @param options The request options from the service
43+
* @param [configOverrides] Overrides OpenAPIConfig
44+
{{#equals @root.httpClient 'angular'}}
4445
* @returns Observable<T>
45-
* @throws ApiError
46-
*/
47-
public override request<T>(options: ApiRequestOptions): Observable<T> {
48-
return __request(this.config, this.http, options);
49-
}
5046
{{else}}
51-
/**
52-
* Request method
53-
* @param options The request options from the service
5447
* @returns CancelablePromise<T>
48+
{{/equals}}
5549
* @throws ApiError
5650
*/
57-
public override request<T>(options: ApiRequestOptions): CancelablePromise<T> {
58-
return __request(this.config, options);
59-
}
51+
public override request<T>(
52+
options: ApiRequestOptions,
53+
configOverrides?: Partial<OpenAPIConfig>,
54+
{{#equals @root.httpClient 'angular'}}
55+
): Observable<T> {
56+
{{else}}
57+
): CancelablePromise<T> {
6058
{{/equals}}
59+
return __request(
60+
{...this.config, ...configOverrides},
61+
{{#equals @root.httpClient 'angular'}}
62+
this.http,
63+
{{/equals}}
64+
options,
65+
);
66+
}
6167
}

‎src/templates/exportService.hbs

+85-66
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import { BaseHttpRequest } from '../core/BaseHttpRequest';
2626
{{else}}
2727
import type { BaseHttpRequest } from '../core/BaseHttpRequest';
2828
{{/equals}}
29+
import type { OpenAPIConfig } from '../core/OpenAPI';
2930
{{else}}
31+
import type { OpenAPIConfig } from '../core/OpenAPI';
3032
import { OpenAPI } from '../core/OpenAPI';
3133
import { request as __request } from '../core/request';
3234
{{/if}}
@@ -64,88 +66,105 @@ export class {{{name}}}{{{@root.postfix}}} {
6466
* @param {{{name}}} {{#if description}}{{{escapeComment description}}}{{/if}}
6567
{{/each}}
6668
{{/if}}
69+
* @param [configOverrides] Overrides OpenAPIConfig
6770
{{/unless}}
6871
{{#each results}}
6972
* @returns {{{type}}} {{#if description}}{{{escapeComment description}}}{{/if}}
7073
{{/each}}
7174
* @throws ApiError
7275
*/
7376
{{#if @root.exportClient}}
77+
public {{{name}}}(
78+
{{>parameters}}
79+
configOverrides?: Partial<OpenAPIConfig>,
7480
{{#equals @root.httpClient 'angular'}}
75-
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
76-
return this.httpRequest.request({
81+
): Observable<{{>result}}> {
7782
{{else}}
78-
public {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
79-
return this.httpRequest.request({
83+
): CancelablePromise<{{>result}}> {
8084
{{/equals}}
85+
return this.httpRequest.request(
8186
{{else}}
8287
{{#equals @root.httpClient 'angular'}}
83-
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
84-
return __request(OpenAPI, this.http, {
88+
public {{{name}}}(
89+
{{>parameters}}
90+
configOverrides?: Partial<OpenAPIConfig>,
91+
): Observable<{{>result}}> {
92+
return __request(
93+
{...OpenAPI, ...configOverrides},
94+
this.http,
8595
{{else}}
86-
public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
87-
return __request(OpenAPI, {
96+
public static {{{name}}}(
97+
{{>parameters}}
98+
configOverrides?: Partial<OpenAPIConfig>,
99+
): CancelablePromise<{{>result}}> {
100+
return __request(
101+
{...OpenAPI, ...configOverrides},
88102
{{/equals}}
89103
{{/if}}
90-
method: '{{{method}}}',
91-
url: '{{{path}}}',
92-
{{#if parametersPath}}
93-
path: {
94-
{{#each parametersPath}}
95-
'{{{prop}}}': {{{name}}},
96-
{{/each}}
104+
{
105+
method: '{{{method}}}',
106+
url: '{{{path}}}',
107+
{{#if parametersPath}}
108+
path: {
109+
{{#each parametersPath}}
110+
'{{{prop}}}': {{{name}}},
111+
{{/each}}
112+
},
113+
{{/if}}
114+
{{#if parametersCookie}}
115+
cookies: {
116+
{{#each parametersCookie}}
117+
'{{{prop}}}': {{{name}}},
118+
{{/each}}
119+
},
120+
{{/if}}
121+
{{#if parametersHeader}}
122+
headers: {
123+
{{#each parametersHeader}}
124+
'{{{prop}}}': {{{name}}},
125+
{{/each}}
126+
},
127+
{{/if}}
128+
{{#if parametersQuery}}
129+
query: {
130+
{{#each parametersQuery}}
131+
'{{{prop}}}': {{{name}}},
132+
{{/each}}
133+
},
134+
{{/if}}
135+
{{#if parametersForm}}
136+
formData: {
137+
{{#each parametersForm}}
138+
'{{{prop}}}': {{{name}}},
139+
{{/each}}
140+
},
141+
{{/if}}
142+
{{#if parametersBody}}
143+
{{#equals parametersBody.in 'formData'}}
144+
formData: {{{parametersBody.name}}},
145+
{{/equals}}
146+
{{#equals parametersBody.in 'body'}}
147+
body: {{{parametersBody.name}}},
148+
{{/equals}}
149+
{{#if parametersBody.mediaType}}
150+
mediaType: '{{{parametersBody.mediaType}}}',
151+
{{/if}}
152+
{{/if}}
153+
{{#if responseHeader}}
154+
responseHeader: '{{{responseHeader}}}',
155+
{{/if}}
156+
{{#if errors}}
157+
errors: {
158+
{{#each errors}}
159+
{{{code}}}: `{{{escapeDescription description}}}`,
160+
{{/each}}
161+
},
162+
{{/if}}
97163
},
98-
{{/if}}
99-
{{#if parametersCookie}}
100-
cookies: {
101-
{{#each parametersCookie}}
102-
'{{{prop}}}': {{{name}}},
103-
{{/each}}
104-
},
105-
{{/if}}
106-
{{#if parametersHeader}}
107-
headers: {
108-
{{#each parametersHeader}}
109-
'{{{prop}}}': {{{name}}},
110-
{{/each}}
111-
},
112-
{{/if}}
113-
{{#if parametersQuery}}
114-
query: {
115-
{{#each parametersQuery}}
116-
'{{{prop}}}': {{{name}}},
117-
{{/each}}
118-
},
119-
{{/if}}
120-
{{#if parametersForm}}
121-
formData: {
122-
{{#each parametersForm}}
123-
'{{{prop}}}': {{{name}}},
124-
{{/each}}
125-
},
126-
{{/if}}
127-
{{#if parametersBody}}
128-
{{#equals parametersBody.in 'formData'}}
129-
formData: {{{parametersBody.name}}},
130-
{{/equals}}
131-
{{#equals parametersBody.in 'body'}}
132-
body: {{{parametersBody.name}}},
133-
{{/equals}}
134-
{{#if parametersBody.mediaType}}
135-
mediaType: '{{{parametersBody.mediaType}}}',
136-
{{/if}}
137-
{{/if}}
138-
{{#if responseHeader}}
139-
responseHeader: '{{{responseHeader}}}',
140-
{{/if}}
141-
{{#if errors}}
142-
errors: {
143-
{{#each errors}}
144-
{{{code}}}: `{{{escapeDescription description}}}`,
145-
{{/each}}
146-
},
147-
{{/if}}
148-
});
164+
{{#if @root.exportClient}}
165+
configOverrides,
166+
{{/if}}
167+
);
149168
}
150169

151170
{{/each}}

‎src/templates/partials/parameters.hbs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#if parameters}}
2-
{{#if @root.useOptions~}}
2+
{{#if @root.useOptions}}
33
{
44
{{#each parameters}}
55
{{{name}}}{{#if default}} = {{{default}}}{{/if}},
@@ -18,9 +18,8 @@
1818
{{/ifdef}}
1919
{{{name}}}{{>isRequired}}: {{>type}},
2020
{{/each}}
21-
}
22-
{{~else}}
23-
21+
},
22+
{{else}}
2423
{{#each parameters}}
2524
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
2625
{{/each}}

‎test/__snapshots__/index.spec.ts.snap

+5,711-574
Large diffs are not rendered by default.

‎test/e2e/client.angular.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('client.angular', () => {
1515
buildAngularProject('client/angular', 'app', 'dist');
1616
await server.start('client/angular/app/dist');
1717
await browser.start();
18+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1819
}, 30000);
1920

2021
afterAll(async () => {
@@ -23,7 +24,6 @@ describe('client.angular', () => {
2324
});
2425

2526
it('requests token', async () => {
26-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2727
const result = await browser.evaluate(async () => {
2828
return await new Promise<any>(resolve => {
2929
const { SimpleService } = (window as any).api;
@@ -36,6 +36,21 @@ describe('client.angular', () => {
3636
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3737
});
3838

39+
it('overrides token', async () => {
40+
const result = await browser.evaluate(async () => {
41+
return await new Promise<any>(resolve => {
42+
const { SimpleService } = (window as any).api;
43+
SimpleService.httpRequest.config.TOKEN = 'BAD_TOKEN';
44+
SimpleService.httpRequest.config.USERNAME = undefined;
45+
SimpleService.httpRequest.config.PASSWORD = undefined;
46+
SimpleService.getCallWithoutParametersAndResponse({
47+
TOKEN: (window as any).tokenRequest,
48+
}).subscribe(resolve);
49+
});
50+
});
51+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
52+
});
53+
3954
it('uses credentials', async () => {
4055
const result = await browser.evaluate(async () => {
4156
return await new Promise<any>(resolve => {

‎test/e2e/client.axios.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ describe('client.axios', () => {
2828
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
2929
});
3030

31+
it('overrides token', async () => {
32+
const { ApiClient } = require('./generated/client/axios/index.js');
33+
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
34+
const client = new ApiClient({
35+
TOKEN: 'BAD_TOKEN',
36+
USERNAME: undefined,
37+
PASSWORD: undefined,
38+
});
39+
const result = await client.simple.getCallWithoutParametersAndResponse({
40+
TOKEN: tokenRequest,
41+
});
42+
expect(tokenRequest.mock.calls.length).toBe(1);
43+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
44+
});
45+
3146
it('uses credentials', async () => {
3247
const { ApiClient } = require('./generated/client/axios/index.js');
3348
const client = new ApiClient({

‎test/e2e/client.babel.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('client.babel', () => {
1414
compileWithBabel('client/babel');
1515
await server.start('client/babel');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('client.babel', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { ApiClient } = (window as any).api;
2828
const client = new ApiClient({
@@ -35,6 +35,21 @@ describe('client.babel', () => {
3535
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3636
});
3737

38+
it('overrides token', async () => {
39+
const result = await browser.evaluate(async () => {
40+
const { ApiClient } = (window as any).api;
41+
const client = new ApiClient({
42+
TOKEN: 'BAD_TOKEN',
43+
USERNAME: undefined,
44+
PASSWORD: undefined,
45+
});
46+
return await client.simple.getCallWithoutParametersAndResponse({
47+
TOKEN: (window as any).tokenRequest,
48+
});
49+
});
50+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
51+
});
52+
3853
it('uses credentials', async () => {
3954
const result = await browser.evaluate(async () => {
4055
const { ApiClient } = (window as any).api;

‎test/e2e/client.fetch.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('client.fetch', () => {
1414
compileWithTypescript('client/fetch');
1515
await server.start('client/fetch');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('client.fetch', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { ApiClient } = (window as any).api;
2828
const client = new ApiClient({
@@ -35,6 +35,21 @@ describe('client.fetch', () => {
3535
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3636
});
3737

38+
it('overrides token', async () => {
39+
const result = await browser.evaluate(async () => {
40+
const { ApiClient } = (window as any).api;
41+
const client = new ApiClient({
42+
TOKEN: 'BAD_TOKEN',
43+
USERNAME: undefined,
44+
PASSWORD: undefined,
45+
});
46+
return await client.simple.getCallWithoutParametersAndResponse({
47+
TOKEN: (window as any).tokenRequest,
48+
});
49+
});
50+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
51+
});
52+
3853
it('uses credentials', async () => {
3954
const result = await browser.evaluate(async () => {
4055
const { ApiClient } = (window as any).api;

‎test/e2e/client.node.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ describe('client.node', () => {
2828
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
2929
});
3030

31+
it('overrides token', async () => {
32+
const { ApiClient } = require('./generated/client/node/index.js');
33+
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
34+
const client = new ApiClient({
35+
TOKEN: 'BAD_TOKEN',
36+
USERNAME: undefined,
37+
PASSWORD: undefined,
38+
});
39+
const result = await client.simple.getCallWithoutParametersAndResponse({
40+
TOKEN: tokenRequest,
41+
});
42+
expect(tokenRequest.mock.calls.length).toBe(1);
43+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
44+
});
45+
3146
it('uses credentials', async () => {
3247
const { ApiClient } = require('./generated/client/node/index.js');
3348
const client = new ApiClient({

‎test/e2e/client.xhr.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('client.xhr', () => {
1414
compileWithTypescript('client/xhr');
1515
await server.start('client/xhr');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('client.xhr', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { ApiClient } = (window as any).api;
2828
const client = new ApiClient({
@@ -35,6 +35,21 @@ describe('client.xhr', () => {
3535
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3636
});
3737

38+
it('overrides token', async () => {
39+
const result = await browser.evaluate(async () => {
40+
const { ApiClient } = (window as any).api;
41+
const client = new ApiClient({
42+
TOKEN: 'BAD_TOKEN',
43+
USERNAME: undefined,
44+
PASSWORD: undefined,
45+
});
46+
return await client.simple.getCallWithoutParametersAndResponse({
47+
TOKEN: (window as any).tokenRequest,
48+
});
49+
});
50+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
51+
});
52+
3853
it('uses credentials', async () => {
3954
const result = await browser.evaluate(async () => {
4055
const { ApiClient } = (window as any).api;

‎test/e2e/v2.angular.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('v2.angular', () => {
1515
buildAngularProject('v2/angular', 'app', 'dist');
1616
await server.start('v2/angular/app/dist');
1717
await browser.start();
18+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1819
}, 30000);
1920

2021
afterAll(async () => {
@@ -23,7 +24,6 @@ describe('v2.angular', () => {
2324
});
2425

2526
it('requests token', async () => {
26-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2727
const result = await browser.evaluate(async () => {
2828
return await new Promise<any>(resolve => {
2929
const { OpenAPI, SimpleService } = (window as any).api;
@@ -34,6 +34,19 @@ describe('v2.angular', () => {
3434
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3535
});
3636

37+
it('overrides token', async () => {
38+
const result = await browser.evaluate(async () => {
39+
return await new Promise<any>(resolve => {
40+
const { OpenAPI, SimpleService } = (window as any).api;
41+
OpenAPI.TOKEN = 'BAD_TOKEN';
42+
SimpleService.getCallWithoutParametersAndResponse({
43+
TOKEN: (window as any).tokenRequest,
44+
}).subscribe(resolve);
45+
});
46+
});
47+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
48+
});
49+
3750
it('supports complex params', async () => {
3851
const result = await browser.evaluate(async () => {
3952
return await new Promise<any>(resolve => {

‎test/e2e/v2.axios.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ describe('v2.axios', () => {
2424
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
2525
});
2626

27+
it('overrides token', async () => {
28+
const { OpenAPI, SimpleService } = require('./generated/v2/axios/index.js');
29+
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
30+
OpenAPI.TOKEN = 'BAD_TOKEN';
31+
const result = await SimpleService.getCallWithoutParametersAndResponse({
32+
TOKEN: tokenRequest,
33+
});
34+
expect(tokenRequest.mock.calls.length).toBe(1);
35+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
36+
});
37+
2738
it('supports complex params', async () => {
2839
const { ComplexService } = require('./generated/v2/axios/index.js');
2940
const result = await ComplexService.complexTypes({

‎test/e2e/v2.babel.spec.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('v2.babel', () => {
1414
compileWithBabel('v2/babel');
1515
await server.start('v2/babel');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('v2.babel', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { OpenAPI, SimpleService } = (window as any).api;
2828
OpenAPI.TOKEN = (window as any).tokenRequest;
@@ -31,6 +31,17 @@ describe('v2.babel', () => {
3131
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3232
});
3333

34+
it('overrides token', async () => {
35+
const result = await browser.evaluate(async () => {
36+
const { OpenAPI, SimpleService } = (window as any).api;
37+
OpenAPI.TOKEN = 'BAD_TOKEN';
38+
return await SimpleService.getCallWithoutParametersAndResponse({
39+
TOKEN: (window as any).tokenRequest,
40+
});
41+
});
42+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
43+
});
44+
3445
it('supports complex params', async () => {
3546
const result = await browser.evaluate(async () => {
3647
const { ComplexService } = (window as any).api;

‎test/e2e/v2.fetch.spec.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('v2.fetch', () => {
1414
compileWithTypescript('v2/fetch');
1515
await server.start('v2/fetch');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('v2.fetch', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { OpenAPI, SimpleService } = (window as any).api;
2828
OpenAPI.TOKEN = (window as any).tokenRequest;
@@ -31,6 +31,15 @@ describe('v2.fetch', () => {
3131
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3232
});
3333

34+
it('overrides token', async () => {
35+
const result = await browser.evaluate(async () => {
36+
const { OpenAPI, SimpleService } = (window as any).api;
37+
OpenAPI.TOKEN = 'BAD_TOKEN';
38+
return await SimpleService.getCallWithoutParametersAndResponse({ TOKEN: (window as any).tokenRequest });
39+
});
40+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
41+
});
42+
3443
it('supports complex params', async () => {
3544
const result = await browser.evaluate(async () => {
3645
const { ComplexService } = (window as any).api;

‎test/e2e/v2.node.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ describe('v2.node', () => {
2424
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
2525
});
2626

27+
it('overrides token', async () => {
28+
const { OpenAPI, SimpleService } = require('./generated/v2/node/index.js');
29+
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
30+
OpenAPI.TOKEN = 'BAD_TOKEN';
31+
const result = await SimpleService.getCallWithoutParametersAndResponse({
32+
TOKEN: tokenRequest,
33+
});
34+
expect(tokenRequest.mock.calls.length).toBe(1);
35+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
36+
});
37+
2738
it('supports complex params', async () => {
2839
const { ComplexService } = require('./generated/v2/node/index.js');
2940
const result = await ComplexService.complexTypes({

‎test/e2e/v2.xhr.spec.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('v2.xhr', () => {
1414
compileWithTypescript('v2/xhr');
1515
await server.start('v2/xhr');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('v2.xhr', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { OpenAPI, SimpleService } = (window as any).api;
2828
OpenAPI.TOKEN = (window as any).tokenRequest;
@@ -31,6 +31,17 @@ describe('v2.xhr', () => {
3131
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3232
});
3333

34+
it('overrides token', async () => {
35+
const result = await browser.evaluate(async () => {
36+
const { OpenAPI, SimpleService } = (window as any).api;
37+
OpenAPI.TOKEN = 'BAD_TOKEN';
38+
return await SimpleService.getCallWithoutParametersAndResponse({
39+
TOKEN: (window as any).tokenRequest,
40+
});
41+
});
42+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
43+
});
44+
3445
it('supports complex params', async () => {
3546
const result = await browser.evaluate(async () => {
3647
const { ComplexService } = (window as any).api;

‎test/e2e/v3.angular.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('v3.angular', () => {
1515
buildAngularProject('v3/angular', 'app', 'dist');
1616
await server.start('v3/angular/app/dist');
1717
await browser.start();
18+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1819
}, 30000);
1920

2021
afterAll(async () => {
@@ -23,7 +24,6 @@ describe('v3.angular', () => {
2324
});
2425

2526
it('requests token', async () => {
26-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2727
const result = await browser.evaluate(async () => {
2828
return await new Promise<any>(resolve => {
2929
const { OpenAPI, SimpleService } = (window as any).api;
@@ -36,6 +36,21 @@ describe('v3.angular', () => {
3636
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3737
});
3838

39+
it('overrides token', async () => {
40+
const result = await browser.evaluate(async () => {
41+
return await new Promise<any>(resolve => {
42+
const { OpenAPI, SimpleService } = (window as any).api;
43+
OpenAPI.TOKEN = 'BAD_TOKEN';
44+
OpenAPI.USERNAME = undefined;
45+
OpenAPI.PASSWORD = undefined;
46+
SimpleService.getCallWithoutParametersAndResponse({
47+
TOKEN: (window as any).tokenRequest,
48+
}).subscribe(resolve);
49+
});
50+
});
51+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
52+
});
53+
3954
it('uses credentials', async () => {
4055
const result = await browser.evaluate(async () => {
4156
return await new Promise<any>(resolve => {

‎test/e2e/v3.axios.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ describe('v3.axios', () => {
2626
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
2727
});
2828

29+
it('overrides token', async () => {
30+
const { OpenAPI, SimpleService } = require('./generated/v3/axios/index.js');
31+
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
32+
OpenAPI.TOKEN = 'BAD_TOKEN';
33+
OpenAPI.USERNAME = undefined;
34+
OpenAPI.PASSWORD = undefined;
35+
const result = await SimpleService.getCallWithoutParametersAndResponse({
36+
TOKEN: tokenRequest,
37+
});
38+
expect(tokenRequest.mock.calls.length).toBe(1);
39+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
40+
});
41+
2942
it('uses credentials', async () => {
3043
const { OpenAPI, SimpleService } = require('./generated/v3/axios/index.js');
3144
OpenAPI.TOKEN = undefined;

‎test/e2e/v3.babel.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('v3.babel', () => {
1414
compileWithBabel('v3/babel');
1515
await server.start('v3/babel');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('v3.babel', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { OpenAPI, SimpleService } = (window as any).api;
2828
OpenAPI.TOKEN = (window as any).tokenRequest;
@@ -33,6 +33,19 @@ describe('v3.babel', () => {
3333
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3434
});
3535

36+
it('overrides token', async () => {
37+
const result = await browser.evaluate(async () => {
38+
const { OpenAPI, SimpleService } = (window as any).api;
39+
OpenAPI.TOKEN = 'BAD_TOKEN';
40+
OpenAPI.USERNAME = undefined;
41+
OpenAPI.PASSWORD = undefined;
42+
return await SimpleService.getCallWithoutParametersAndResponse({
43+
TOKEN: (window as any).tokenRequest,
44+
});
45+
});
46+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
47+
});
48+
3649
it('uses credentials', async () => {
3750
const result = await browser.evaluate(async () => {
3851
const { OpenAPI, SimpleService } = (window as any).api;

‎test/e2e/v3.fetch.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('v3.fetch', () => {
1414
compileWithTypescript('v3/fetch');
1515
await server.start('v3/fetch');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('v3.fetch', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { OpenAPI, SimpleService } = (window as any).api;
2828
OpenAPI.TOKEN = (window as any).tokenRequest;
@@ -33,6 +33,19 @@ describe('v3.fetch', () => {
3333
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3434
});
3535

36+
it('overrides token', async () => {
37+
const result = await browser.evaluate(async () => {
38+
const { OpenAPI, SimpleService } = (window as any).api;
39+
OpenAPI.TOKEN = 'BAD_TOKEN';
40+
OpenAPI.USERNAME = undefined;
41+
OpenAPI.PASSWORD = undefined;
42+
return await SimpleService.getCallWithoutParametersAndResponse({
43+
TOKEN: (window as any).tokenRequest,
44+
});
45+
});
46+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
47+
});
48+
3649
it('uses credentials', async () => {
3750
const result = await browser.evaluate(async () => {
3851
const { OpenAPI, SimpleService } = (window as any).api;

‎test/e2e/v3.node.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ describe('v3.node', () => {
2626
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
2727
});
2828

29+
it('overrides token', async () => {
30+
const { OpenAPI, SimpleService } = require('./generated/v3/node/index.js');
31+
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
32+
OpenAPI.TOKEN = 'BAD_TOKEN';
33+
OpenAPI.USERNAME = undefined;
34+
OpenAPI.PASSWORD = undefined;
35+
const result = await SimpleService.getCallWithoutParametersAndResponse({
36+
TOKEN: tokenRequest,
37+
});
38+
expect(tokenRequest.mock.calls.length).toBe(1);
39+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
40+
});
41+
2942
it('uses credentials', async () => {
3043
const { OpenAPI, SimpleService } = require('./generated/v3/node/index.js');
3144
OpenAPI.TOKEN = undefined;

‎test/e2e/v3.xhr.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('v3.xhr', () => {
1414
compileWithTypescript('v3/xhr');
1515
await server.start('v3/xhr');
1616
await browser.start();
17+
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
1718
}, 30000);
1819

1920
afterAll(async () => {
@@ -22,7 +23,6 @@ describe('v3.xhr', () => {
2223
});
2324

2425
it('requests token', async () => {
25-
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
2626
const result = await browser.evaluate(async () => {
2727
const { OpenAPI, SimpleService } = (window as any).api;
2828
OpenAPI.TOKEN = (window as any).tokenRequest;
@@ -33,6 +33,19 @@ describe('v3.xhr', () => {
3333
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
3434
});
3535

36+
it('overrides token', async () => {
37+
const result = await browser.evaluate(async () => {
38+
const { OpenAPI, SimpleService } = (window as any).api;
39+
OpenAPI.TOKEN = 'BAD_TOKEN';
40+
OpenAPI.USERNAME = undefined;
41+
OpenAPI.PASSWORD = undefined;
42+
return await SimpleService.getCallWithoutParametersAndResponse({
43+
TOKEN: (window as any).tokenRequest,
44+
});
45+
});
46+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
47+
});
48+
3649
it('uses credentials', async () => {
3750
const result = await browser.evaluate(async () => {
3851
const { OpenAPI, SimpleService } = (window as any).api;

‎test/index.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@ import { sync } from 'glob';
33

44
import { generate, HttpClient } from '../';
55

6+
describe('client', () => {
7+
it('should generate', async () => {
8+
await generate({
9+
input: './test/spec/v3.json',
10+
output: './test/generated/client/',
11+
httpClient: HttpClient.FETCH,
12+
useOptions: false,
13+
useUnionTypes: false,
14+
exportCore: true,
15+
exportSchemas: true,
16+
exportModels: true,
17+
exportServices: true,
18+
clientName: 'ApiClient',
19+
});
20+
21+
sync('./test/generated/client/**/*.ts').forEach(file => {
22+
const content = readFileSync(file, 'utf8').toString();
23+
expect(content).toMatchSnapshot(file);
24+
});
25+
});
26+
});
27+
628
describe('v2', () => {
729
it('should generate', async () => {
830
await generate({

0 commit comments

Comments
 (0)