Skip to content

Allow requests to override 'fetch' #1

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

Merged
merged 2 commits into from
Jun 22, 2022
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"eslint": "eslint .",
"eslint:fix": "eslint . --fix",
"prepublishOnly": "npm run clean && npm run release",
"codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b"
"codecov": "codecov"
},
"dependencies": {
"camelcase": "^6.3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/templates/core/node/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import type { OpenAPIConfig } from './OpenAPI';
* @returns CancelablePromise<T>
* @throws ApiError
*/
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, fetchFunction?: typeof fetch): CancelablePromise<T> => {
return new CancelablePromise(async (resolve, reject, onCancel) => {
try {
const url = getUrl(config, options);
Expand All @@ -76,7 +76,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
const headers = await getHeaders(config, options);

if (!onCancel.isCancelled) {
const response = await sendRequest(options, url, body, formData, headers, onCancel);
const response = await sendRequest(options, url, body, formData, headers, onCancel, fetchFunction);
const responseBody = await getResponseBody(response);
const responseHeader = getResponseHeader(response, options.responseHeader);

Expand Down
5 changes: 3 additions & 2 deletions src/templates/core/node/sendRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export const sendRequest = async (
body: any,
formData: FormData | undefined,
headers: Headers,
onCancel: OnCancel
onCancel: OnCancel,
fetchFunction: typeof fetch = fetch
): Promise<Response> => {
const controller = new AbortController();

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

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

return await fetch(url, request);
return await fetchFunction(url, request);
};
29 changes: 29 additions & 0 deletions test/e2e/client.node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetch from 'node-fetch';
import { cleanup } from './scripts/cleanup';
import { compileWithTypescript } from './scripts/compileWithTypescript';
import { generateClient } from './scripts/generateClient';
Expand Down Expand Up @@ -147,4 +148,32 @@ describe('client.node', () => {
})
);
});

it('can override fetch', async () => {
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
const { ApiClient, BaseHttpRequest } = require('./generated/client/node/index.js');
const { request } = require('./generated/client/node/core/request');
const customFetch = jest.fn().mockImplementation((url, init) => fetch(url, init));
class MockHttpRequest extends BaseHttpRequest {
constructor(config) {
super(config);
}

public request<T>(options) {
return request(this.config, options, customFetch);
}
}
const client = new ApiClient(
{
TOKEN: tokenRequest,
USERNAME: undefined,
PASSWORD: undefined,
},
MockHttpRequest
);
const result = await client.simple.getCallWithoutParametersAndResponse();
expect(tokenRequest.mock.calls.length).toBe(1);
expect(customFetch.mock.calls.length).toBe(1);
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
});
});