Ƭ FetchConfig: Omit<RequestInit, "body"
> & { body?
: Record<string,
unknown> ; ignoreParseErrors?
: boolean ; rejects?
: boolean ; swr?
:
boolean }
Defined in: src/index.ts:6
▸ fetch<JsonType, ErrorType>(url
: string, config
:
Omit<FetchConfig, "swr"
> & { swr
: true
}):
Promise<JsonType>
Performs an isomorphic (un)fetch and returns the JsonType response or throws as SWR expects.
example
const { data: json, error } = useSwr('api/endpoint', fetch.swr);
// Or: ... = useSwr('api/endpoint', key => fetch(key, { swr: true }));
if(error) <div>Error: {error.message}</div>;
return <div>Hello, your data is: {json.data}</div>;
Name | Type | Default |
---|---|---|
JsonType |
SerializedValue | Record<string, unknown> |
ErrorType |
SerializedValue | JsonType |
Name | Type |
---|---|
url |
string |
config |
Omit<FetchConfig, "swr" > & { swr : true } |
Returns: Promise<JsonType>
Defined in: src/index.ts:57
▸ fetch<JsonType, ErrorType>(url
: string, config?
:
FetchConfig): Promise<{ error
: ErrorType |
undefined ; json
: JsonType | undefined ; res
: Response }>
Performs an isomorphic (un)fetch, following redirects as necessary.
example
const { json, error } = fetch.post<{ data: number }, { message: string }>(
'api/endpoint',
{
headers: { key: apiKey },
body: requestData,
rejects: false // false is the default
}
);
if(error) throw error.message;
return json.data;
throws
- When parsing the body for JSON content fails and
{ ignoreParseErrors: true }
. - When
{ rejects: true }
or{ swr: true }
and a non-2xx response is received.
Name | Type | Default |
---|---|---|
JsonType |
SerializedValue | Record<string, unknown> |
ErrorType |
SerializedValue | JsonType |
Name | Type |
---|---|
url |
string |
config? |
FetchConfig |
Returns: Promise<{ error
: ErrorType | undefined ; json
: JsonType |
undefined ; res
: Response }>
- A Response object
res
and parsed response bodyjson
error
(undefined
on 2xx response)
Note: json
is undefined on non-2xx responses while error
is undefined on 2xx
responses.
Defined in: src/index.ts:96
▸ getGlobalFetchConfig(): FetchConfig
Get the default config object merged in during all fetch()
calls.
Returns: FetchConfig
Defined in: src/index.ts:33
▸ setGlobalFetchConfig(config
: FetchConfig):
void
Set the default config object merged in during all fetch()
calls.
Name | Type |
---|---|
config |
FetchConfig |
Returns: void
Defined in: src/index.ts:40
▸ Const
unfetch(input
: RequestInfo, init?
: RequestInit):
Promise<Response>
Name | Type |
---|---|
input |
RequestInfo |
init? |
RequestInit |
Returns: Promise<Response>
Defined in: node_modules/isomorphic-unfetch/index.d.ts:17