-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fetch): make sure the body is consumed in case of http error
- Loading branch information
1 parent
da4e905
commit 50d0fc8
Showing
3 changed files
with
136 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// TODO: Move to a shared package ? | ||
|
||
export type JsonScalar = string | number | boolean | null | ||
export type Json = JsonScalar | Json[] | { [key: string]: undefined | Json } | ||
export type JsonObject = { [key: string]: Json } | ||
export type JsonArray = Json[] | ||
|
||
declare global { | ||
interface JSON { | ||
parse(text: string, reviver?: (key: any, value: any) => any): Json | ||
} | ||
} | ||
|
||
// TODO: Move to a shared package ? | ||
|
||
const plainObjectProto = Object.prototype | ||
export const ifObject = <V>(v?: V) => { | ||
if (typeof v === 'object' && v != null && !Array.isArray(v)) { | ||
const proto = Object.getPrototypeOf(v) | ||
if (proto === null || proto === plainObjectProto) { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
return v as V extends JsonScalar | JsonArray | Function | symbol | ||
? never | ||
: V extends Json | ||
? V | ||
: // Plain object are (mostly) safe to access as Json | ||
{ [key: string]: unknown } | ||
} | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
export const ifArray = <V>(v?: V) => (Array.isArray(v) ? v : undefined) | ||
export const ifScalar = <V>(v?: V) => { | ||
switch (typeof v) { | ||
case 'string': | ||
case 'number': | ||
case 'boolean': | ||
return v | ||
default: | ||
if (v === null) return null as V & null | ||
return undefined as V extends JsonScalar ? never : undefined | ||
} | ||
} | ||
export const ifBoolean = <V>(v?: V) => (typeof v === 'boolean' ? v : undefined) | ||
export const ifString = <V>(v?: V) => (typeof v === 'string' ? v : undefined) | ||
export const ifNumber = <V>(v?: V) => (typeof v === 'number' ? v : undefined) | ||
export const ifNull = <V>(v?: V) => (v === null ? v : undefined) |