Skip to content

Commit e1184d1

Browse files
authored
fix(fetch): check response is of proper type without using instanceof (#536)
1 parent f2d6e46 commit e1184d1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Validates an object is of type Response without using `instanceof`.
3+
*
4+
* @remarks Check issue #509 for more context.
5+
*
6+
* @internal
7+
*/
8+
export const isResponse = (obj: unknown): obj is Response =>
9+
obj !== null &&
10+
obj !== undefined &&
11+
typeof obj === 'object' &&
12+
'status' in obj &&
13+
typeof obj.status === 'number' &&
14+
'statusText' in obj &&
15+
typeof obj.statusText === 'string' &&
16+
'headers' in obj &&
17+
typeof obj.headers === 'object' &&
18+
'body' in obj &&
19+
typeof obj.body !== 'undefined'

packages/clients/src/scw/fetch/response-parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isResponse } from '../../helpers/is-response'
12
import { isJSONObject } from '../../helpers/json'
23
import { parseScalewayError } from '../errors/error-parser'
34
import { ScalewayError } from '../errors/scw-error'
@@ -50,7 +51,7 @@ export const responseParser =
5051
responseType: 'json' | 'text' | 'blob',
5152
) =>
5253
async (response: Response): Promise<T> => {
53-
if (!(response instanceof Response)) {
54+
if (!isResponse(response)) {
5455
throw new TypeError('Invalid response object')
5556
}
5657

0 commit comments

Comments
 (0)