Skip to content
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
19 changes: 19 additions & 0 deletions packages/clients/src/helpers/is-response.ts
Original file line number Diff line number Diff line change
@@ -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'
3 changes: 2 additions & 1 deletion packages/clients/src/scw/fetch/response-parser.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -50,7 +51,7 @@ export const responseParser =
responseType: 'json' | 'text' | 'blob',
) =>
async (response: Response): Promise<T> => {
if (!(response instanceof Response)) {
if (!isResponse(response)) {
throw new TypeError('Invalid response object')
}

Expand Down