From e8b22da3b491c4c63ee21afd12b9853d481225da Mon Sep 17 00:00:00 2001 From: Flyyn <_@rst.vn> Date: Wed, 22 Jun 2022 11:27:44 -0400 Subject: [PATCH 1/2] Allow requests to override 'fetch' --- src/templates/core/node/request.hbs | 4 ++-- src/templates/core/node/sendRequest.hbs | 5 +++-- test/e2e/client.node.spec.ts | 29 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/templates/core/node/request.hbs b/src/templates/core/node/request.hbs index 8e6f6110e..5ba20cb15 100644 --- a/src/templates/core/node/request.hbs +++ b/src/templates/core/node/request.hbs @@ -67,7 +67,7 @@ import type { OpenAPIConfig } from './OpenAPI'; * @returns CancelablePromise * @throws ApiError */ -export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise => { +export const request = (config: OpenAPIConfig, options: ApiRequestOptions, fetchFunction?: typeof fetch): CancelablePromise => { return new CancelablePromise(async (resolve, reject, onCancel) => { try { const url = getUrl(config, options); @@ -76,7 +76,7 @@ export const request = (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); diff --git a/src/templates/core/node/sendRequest.hbs b/src/templates/core/node/sendRequest.hbs index a2ebf86d4..d50dd4590 100644 --- a/src/templates/core/node/sendRequest.hbs +++ b/src/templates/core/node/sendRequest.hbs @@ -4,7 +4,8 @@ export const sendRequest = async ( body: any, formData: FormData | undefined, headers: Headers, - onCancel: OnCancel + onCancel: OnCancel, + fetchFunction: typeof fetch = fetch ): Promise => { const controller = new AbortController(); @@ -17,5 +18,5 @@ export const sendRequest = async ( onCancel(() => controller.abort()); - return await fetch(url, request); + return await fetchFunction(url, request); }; diff --git a/test/e2e/client.node.spec.ts b/test/e2e/client.node.spec.ts index 9967cf019..7108efc6c 100644 --- a/test/e2e/client.node.spec.ts +++ b/test/e2e/client.node.spec.ts @@ -1,3 +1,4 @@ +import fetch from 'node-fetch'; import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; import { generateClient } from './scripts/generateClient'; @@ -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(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'); + }); }); From fc833f8d8d611568fd04cc209850962f45328485 Mon Sep 17 00:00:00 2001 From: Flyyn <_@rst.vn> Date: Wed, 22 Jun 2022 11:32:49 -0400 Subject: [PATCH 2/2] Remove codecov token --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 755cbc9e9..3f5ea729c 100644 --- a/package.json +++ b/package.json @@ -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",