Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only fallback to text on error #14

Merged
merged 2 commits into from
Jan 29, 2024
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"module": "./lib/index.js",
"exports": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
"require": "./lib/index.cjs",
"default": "./lib/index.js"
},
"types": "./lib/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const createFetchApi = (fetch = globalThis.fetch) => {
return response
}
throw Object.assign(new Error(response.statusText), {
data: extractDataFromResponse(response, type),
data: extractDataFromResponse(response, type, true),
response,
})
}
Expand Down
28 changes: 23 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,38 @@ export const normalizeUrl = (url: string, query?: URLSearchParamsOptions) => {
export async function extractDataFromResponse(
res: Response,
type: null,
fallback?: boolean,
): Promise<Response>
export async function extractDataFromResponse(
res: Response,
type: 'arrayBuffer',
fallback?: boolean,
): Promise<ArrayBuffer>
export async function extractDataFromResponse(
res: Response,
type: 'blob',
fallback?: boolean,
): Promise<Blob>
export async function extractDataFromResponse<T>(
res: Response,
type: 'json',
fallback?: boolean,
): Promise<T>
export async function extractDataFromResponse(
res: Response,
type: 'text',
fallback?: boolean,
): Promise<string>
export async function extractDataFromResponse(
res: Response,
type: ResponseType,
fallback?: boolean,
): Promise<unknown>
// eslint-disable-next-line sonarjs/cognitive-complexity
export async function extractDataFromResponse(
res: Response,
type: ResponseType,
fallback?: boolean,
) {
let data: unknown
if (type != null) {
Expand All @@ -93,15 +100,26 @@ export async function extractDataFromResponse(
// data could be empty text
data = await res.clone().text()
} catch {}
if (type === 'json' && (data = (data as string).trim())) {
try {
data = JSON.parse(data as string)
} catch {}
if (type === 'json') {
if ((data = (data as string).trim())) {
try {
data = JSON.parse(data as string)
} catch (err) {
if (!fallback) {
throw err
}
}
} else {
data = null
}
}
} else {
try {
data = await res.clone()[type]()
} catch {
} catch (err) {
if (!fallback) {
throw err
}
data = await res.clone().text()
}
}
Expand Down
10 changes: 9 additions & 1 deletion test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ test('extractDataFromResponse', async () => {
expect(await extractDataFromResponse(new Response(), 'text')).toBe('')
expect(await extractDataFromResponse(new Response(null), 'text')).toBe('')
expect(await extractDataFromResponse(new Response('foo'), 'text')).toBe('foo')
expect(await extractDataFromResponse(new Response('foo'), 'json')).toBe('foo')
await expect(() =>
extractDataFromResponse(new Response('foo'), 'json'),
).rejects.toThrow(
// eslint-disable-next-line unicorn/better-regex, regexp/no-dupe-characters-character-class
/[SyntaxError: Unexpected token (('o', "foo" is not valid JSON)|(o in JSON at position 1))]/,

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'S' is [repeated in the same character class](1). Character 'S' is [repeated in the same character class](2).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'n' is [repeated in the same character class](1). Character 'n' is [repeated in the same character class](2). Character 'n' is [repeated in the same character class](3). Character 'n' is [repeated in the same character class](4). Character 'n' is [repeated in the same character class](5).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 't' is [repeated in the same character class](1). Character 't' is [repeated in the same character class](2). Character 't' is [repeated in the same character class](3). Character 't' is [repeated in the same character class](4). Character 't' is [repeated in the same character class](5).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'a' is [repeated in the same character class](1). Character 'a' is [repeated in the same character class](2).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'x' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'r' is [repeated in the same character class](1). Character 'r' is [repeated in the same character class](2).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'o' is [repeated in the same character class](1). Character 'o' is [repeated in the same character class](2). Character 'o' is [repeated in the same character class](3). Character 'o' is [repeated in the same character class](4). Character 'o' is [repeated in the same character class](5). Character 'o' is [repeated in the same character class](6). Character 'o' is [repeated in the same character class](7). Character 'o' is [repeated in the same character class](8).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character ' ' is [repeated in the same character class](1). Character ' ' is [repeated in the same character class](2). Character ' ' is [repeated in the same character class](3). Character ' ' is [repeated in the same character class](4). Character ' ' is [repeated in the same character class](5). Character ' ' is [repeated in the same character class](6). Character ' ' is [repeated in the same character class](7). Character ' ' is [repeated in the same character class](8). Character ' ' is [repeated in the same character class](9). Character ' ' is [repeated in the same character class](10). Character ' ' is [repeated in the same character class](11). Character ' ' is [repeated in the same character class](12).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'e' is [repeated in the same character class](1). Character 'e' is [repeated in the same character class](2). Character 'e' is [repeated in the same character class](3).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'p' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'd' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character '(' is [repeated in the same character class](1). Character '(' is [repeated in the same character class](2).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character ''' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character '"' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'i' is [repeated in the same character class](1). Character 'i' is [repeated in the same character class](2). Character 'i' is [repeated in the same character class](3). Character 'i' is [repeated in the same character class](4).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 's' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'J' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'O' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character 'N' is [repeated in the same character class](1).

Check warning

Code scanning / CodeQL

Duplicate character in character class

Character ')' is [repeated in the same character class](1). Character ')' is [repeated in the same character class](2).
)
expect(await extractDataFromResponse(new Response('foo'), 'json', true)).toBe(
'foo',
)
expect(
await extractDataFromResponse(new Response('{"foo":"bar"}'), 'json'),
).toEqual({ foo: 'bar' })
Expand Down