diff --git a/packages/clients/src/helpers/is-response.ts b/packages/clients/src/helpers/is-response.ts new file mode 100644 index 000000000..abdeed273 --- /dev/null +++ b/packages/clients/src/helpers/is-response.ts @@ -0,0 +1,19 @@ +/** + * Validates an object is of type Response without using `instanceof`. + * + * @remarks Check issue #509 for more context. + * + * @internal + */ +export const isResponse = (obj: unknown): obj is Response => + obj !== null && + obj !== undefined && + typeof obj === 'object' && + 'status' in obj && + typeof obj.status === 'number' && + 'statusText' in obj && + typeof obj.statusText === 'string' && + 'headers' in obj && + typeof obj.headers === 'object' && + 'body' in obj && + typeof obj.body !== 'undefined' diff --git a/packages/clients/src/scw/fetch/response-parser.ts b/packages/clients/src/scw/fetch/response-parser.ts index 80535f565..bdcbeedc9 100644 --- a/packages/clients/src/scw/fetch/response-parser.ts +++ b/packages/clients/src/scw/fetch/response-parser.ts @@ -1,3 +1,4 @@ +import { isResponse } from '../../helpers/is-response' import { isJSONObject } from '../../helpers/json' import { parseScalewayError } from '../errors/error-parser' import { ScalewayError } from '../errors/scw-error' @@ -50,7 +51,7 @@ export const responseParser = responseType: 'json' | 'text' | 'blob', ) => async (response: Response): Promise => { - if (!(response instanceof Response)) { + if (!isResponse(response)) { throw new TypeError('Invalid response object') }