Skip to content

Commit

Permalink
Merge pull request #726 from wazo-platform/abort_controller_polyfill
Browse files Browse the repository at this point in the history
Do not use `node-abort-controller` polyfill when not required
  • Loading branch information
manuquentin authored Aug 28, 2023
2 parents 51c2aba + 3bab970 commit 98b2971
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
11 changes: 4 additions & 7 deletions src/__tests__/api-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { AbortController } from 'node-abort-controller';
import { Base64 } from 'js-base64';

import WazoApiClient from '../api-client';
import ServerError from '../domain/ServerError';
import BadResponse from '../domain/BadResponse';
import Session from '../domain/Session';

const controller = new AbortController();

const mockedResponse = {
data: {
token: 1,
Expand Down Expand Up @@ -116,7 +113,7 @@ describe('With correct API results', () => {
expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token`, {
method: 'post',
body: JSON.stringify(data),
signal: controller.signal,
signal: expect.any(Object),
headers,
agent: null,
});
Expand All @@ -131,7 +128,7 @@ describe('With correct API results', () => {
expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token/${oldToken}`, {
method: 'delete',
body: null,
signal: controller.signal,
signal: expect.any(Object),
headers: {},
agent: null,
});
Expand All @@ -154,7 +151,7 @@ describe('With unAuthorized API results', () => {
expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token/${tokenToCheck}`, {
method: 'head',
body: null,
signal: controller.signal,
signal: expect.any(Object),
headers: {},
agent: null,
});
Expand Down Expand Up @@ -186,7 +183,7 @@ describe('With not found API results', () => {
expect(global.fetch).toBeCalledWith(`https://${server}/api/calld/1.0/users/me/voicemails`, {
method: 'get',
body: null,
signal: controller.signal,
signal: expect.any(Object),
headers: {
'X-Auth-Token': token,
'Content-Type': 'application/json',
Expand Down
9 changes: 3 additions & 6 deletions src/utils/__tests__/api-requester.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AbortController } from 'node-abort-controller';

import ApiRequester from '../api-requester';

const server = 'localhost';
Expand All @@ -16,7 +14,6 @@ const headers = {
'Content-Type': 'application/json',
'X-Auth-Token': token,
};
const controller = new AbortController();

describe('Generating query string', () => {
it('should generate a simple query string from an object', () => {
Expand Down Expand Up @@ -103,7 +100,7 @@ describe('Calling fetch', () => {
expect(global.fetch).toBeCalledWith(url, {
method: 'get',
body: null,
signal: controller.signal,
signal: expect.any(Object),
headers: {},
agent: null,
});
Expand Down Expand Up @@ -148,14 +145,14 @@ describe('With a refresh token', () => {
expect(global.fetch).toHaveBeenNthCalledWith(1, url, {
method: 'get',
body: null,
signal: controller.signal,
signal: expect.any(Object),
headers,
agent: null,
});
expect(global.fetch).toHaveBeenNthCalledWith(2, url, {
method: 'get',
body: null,
signal: controller.signal,
signal: expect.any(Object),
headers: updatedHeaders,
agent: null,
});
Expand Down
7 changes: 3 additions & 4 deletions src/utils/api-requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/* eslint-disable import/no-dynamic-require */
import { Base64 } from 'js-base64';
import { AbortController } from 'node-abort-controller';
import { AbortController as NodeAbortController } from 'node-abort-controller';

import BadResponse from '../domain/BadResponse';
import ServerError from '../domain/ServerError';
Expand Down Expand Up @@ -171,9 +171,8 @@ export default class ApiRequester {
const isHead = method === 'head';
const hasEmptyResponse = method === 'delete' || isHead;
const newParse = hasEmptyResponse ? ApiRequester.successResponseParser : parse;
const fetchOptions = { ...(this.fetchOptions || {}),
};
const controller = new AbortController();
const fetchOptions = { ...(this.fetchOptions || {}) };
const controller = typeof AbortController !== 'undefined' ? new AbortController() : new NodeAbortController();
const extraHeaders = fetchOptions.headers || {};
delete fetchOptions.headers;
const options = {
Expand Down

0 comments on commit 98b2971

Please sign in to comment.