Skip to content

Commit 9e8f5f3

Browse files
authored
Allow requests to override 'fetch' (#1)
* Allow requests to override 'fetch' * Remove codecov token Co-authored-by: Flyyn <[email protected]>
1 parent 8485f9d commit 9e8f5f3

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"eslint": "eslint .",
5757
"eslint:fix": "eslint . --fix",
5858
"prepublishOnly": "npm run clean && npm run release",
59-
"codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b"
59+
"codecov": "codecov"
6060
},
6161
"dependencies": {
6262
"camelcase": "^6.3.0",

src/templates/core/node/request.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import type { OpenAPIConfig } from './OpenAPI';
6767
* @returns CancelablePromise<T>
6868
* @throws ApiError
6969
*/
70-
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
70+
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, fetchFunction?: typeof fetch): CancelablePromise<T> => {
7171
return new CancelablePromise(async (resolve, reject, onCancel) => {
7272
try {
7373
const url = getUrl(config, options);
@@ -76,7 +76,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
7676
const headers = await getHeaders(config, options);
7777

7878
if (!onCancel.isCancelled) {
79-
const response = await sendRequest(options, url, body, formData, headers, onCancel);
79+
const response = await sendRequest(options, url, body, formData, headers, onCancel, fetchFunction);
8080
const responseBody = await getResponseBody(response);
8181
const responseHeader = getResponseHeader(response, options.responseHeader);
8282

src/templates/core/node/sendRequest.hbs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export const sendRequest = async (
44
body: any,
55
formData: FormData | undefined,
66
headers: Headers,
7-
onCancel: OnCancel
7+
onCancel: OnCancel,
8+
fetchFunction: typeof fetch = fetch
89
): Promise<Response> => {
910
const controller = new AbortController();
1011

@@ -17,5 +18,5 @@ export const sendRequest = async (
1718

1819
onCancel(() => controller.abort());
1920

20-
return await fetch(url, request);
21+
return await fetchFunction(url, request);
2122
};

test/e2e/client.node.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fetch from 'node-fetch';
12
import { cleanup } from './scripts/cleanup';
23
import { compileWithTypescript } from './scripts/compileWithTypescript';
34
import { generateClient } from './scripts/generateClient';
@@ -147,4 +148,32 @@ describe('client.node', () => {
147148
})
148149
);
149150
});
151+
152+
it('can override fetch', async () => {
153+
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
154+
const { ApiClient, BaseHttpRequest } = require('./generated/client/node/index.js');
155+
const { request } = require('./generated/client/node/core/request');
156+
const customFetch = jest.fn().mockImplementation((url, init) => fetch(url, init));
157+
class MockHttpRequest extends BaseHttpRequest {
158+
constructor(config) {
159+
super(config);
160+
}
161+
162+
public request<T>(options) {
163+
return request(this.config, options, customFetch);
164+
}
165+
}
166+
const client = new ApiClient(
167+
{
168+
TOKEN: tokenRequest,
169+
USERNAME: undefined,
170+
PASSWORD: undefined,
171+
},
172+
MockHttpRequest
173+
);
174+
const result = await client.simple.getCallWithoutParametersAndResponse();
175+
expect(tokenRequest.mock.calls.length).toBe(1);
176+
expect(customFetch.mock.calls.length).toBe(1);
177+
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
178+
});
150179
});

0 commit comments

Comments
 (0)