elections-cpl.api.hscc.bdpa.org • Docs
elections-cpl.api.hscc.bdpa.org / lib/json-unfetch / jsonFetch
jsonFetch<
JsonType
,ErrorType
>(url
,init
?):Promise
<object
>
Fetches a resource and returns an object containing two items: the response
itself under res
and the response body parsed as JSON under either error
(if the response has a non-2xx status) or json
.
If the response was not received with an application/json
content-type
header or has a non-2xx status and unparseable response body, json
will
be undefined and error
will be an empty object.
This function rejects if 1) the request body cannot be parsed as JSON but is
being sent with an application/json
content-type header or 2) the response
body cannot be parsed as JSON but was received with an application/json
content-type header.
• JsonType extends JsonObject
= JsonObject
• ErrorType extends JsonObject
= JsonType
• url: string
• init?: Omit
<JsonRequestInit
, "rejectIfNotOk"
| "rejectIfNonJsonContentType"
| "swr"
> & object
Promise
<object
>
error:
Partial
<ErrorType
> |undefined
json:
JsonType
|undefined
res:
Response
type ResJson = { myNumber: number };
type ResErr = { reason: string };
const { res, json, error } = await jsonFetch<ResJson, ResErr>(
'api/endpoint',
{
method: 'POST',
headers: { authorization: `Bearer ${apiKey}` },
body: requestData
}
);
if (error) {
console.error(error?.reason ?? (res.ok
? 'bad json'
: res.statusText));
} else {
console.log(`number is: ${json?.myNumber}`);
}
jsonFetch<
JsonType
,ErrorType
>(url
,init
):Promise
<object
>
Fetches a resource and returns an object containing two items: the response
itself under res
and the response body parsed as JSON under either error
(if the response has a non-2xx status) or json
.
If the response was received with a non-2xx status and unparseable response
body, json
will be undefined and error
will be an empty object.
This function rejects if 1) the request body cannot be parsed as JSON but is
being sent with an application/json
content-type header, 2) the response
body cannot be parsed as JSON but was received with an application/json
content-type header, or 3) the response was received with a content-type
header other than application/json
.
• JsonType extends JsonObject
= JsonObject
• ErrorType extends JsonObject
= JsonType
• url: string
• init: Omit
<JsonRequestInit
, "rejectIfNotOk"
| "rejectIfNonJsonContentType"
| "swr"
> & object
Promise
<object
>
error:
Partial
<ErrorType
> |undefined
json:
JsonType
|undefined
res:
Response
type ResJson = { myNumber: number };
type ResErr = { reason: string };
try {
const { res, json, error } = await jsonFetch<ResJson, ResErr>(
'api/endpoint',
{ rejectIfNonJsonContentType: true }
);
if (error) {
console.error(error?.reason ?? res.statusText);
} else {
console.log(`number is: ${json?.myNumber}`);
}
} catch(e) {
if(e instanceof JsonFetchError) {
// Special handling for non-json response bodies
specialHandler(e.res.status, e.json);
} else {
throw e;
}
}
jsonFetch<
JsonType
,ErrorType
>(url
,init
):Promise
<object
>
Fetches a resource and returns an object containing two items: the response
itself under res
and either the response body parsed as JSON under json
or, if the response was received with a content-type header other than
application/json
, an empty object under error
.
This function rejects if 1) the request body cannot be parsed as JSON but is
being sent with an application/json
content-type header, 2) the response
body cannot be parsed as JSON but was received with an application/json
content-type header, or 3) the response was received with a non-2xx status.
• JsonType extends JsonObject
= JsonObject
• ErrorType extends JsonObject
= JsonType
• url: string
• init: Omit
<JsonRequestInit
, "rejectIfNotOk"
| "rejectIfNonJsonContentType"
| "swr"
> & object
Promise
<object
>
error:
Partial
<ErrorType
> |undefined
json:
JsonType
|undefined
res:
Response
type ResJson = { myNumber: number };
type ResErr = { reason: string };
try {
const { res, json, error } = await jsonFetch<ResJson, ResErr>(
'api/endpoint',
{ rejectIfNotOk: true }
);
if (error) {
console.error(error?.reason ?? 'bad json');
} else {
console.log(`number is: ${json?.myNumber}`);
}
} catch(e) {
if(e instanceof JsonFetchError) {
// Special handling for non-2xx responses
specialHandler(e.res.status, e.json);
} else {
throw e;
}
}
jsonFetch<
JsonType
,ErrorType
>(url
,init
):Promise
<object
>
Fetches a resource and returns an object containing two items: the response
itself under res
and and the response body parsed as JSON under json
.
This function rejects if 1) the request body cannot be parsed as JSON but is
being sent with an application/json
content-type header, 2) the response
body cannot be parsed as JSON but was received with an application/json
content-type header, 3) the response was received with a content-type header
other than application/json
, or 4) the response was received with a non-2xx
status.
Hence, when jsonFetch is called in this way, json
will always be defined
and error
will always be undefined.
• JsonType extends JsonObject
= JsonObject
• ErrorType extends JsonObject
= JsonType
• url: string
• init: Omit
<JsonRequestInit
, "rejectIfNotOk"
| "rejectIfNonJsonContentType"
| "swr"
> & object
Promise
<object
>
error:
undefined
json:
JsonType
res:
Response
try {
const url = 'https://some.resource.com/data.json';
const { json } = await jsonFetch(url, {
rejectIfNotOk: true,
rejectIfNonJsonContentType: true
});
doSomethingWith(json);
} catch(e) {
if(e instanceof JsonFetchError) {
// Special handling for non-2xx/non-json response bodies
specialHandler(e.res.status, e.json);
} else {
throw e;
}
}
jsonFetch<
JsonType
,ErrorType
>(url
,init
):Promise
<JsonType
>
Fetches a resource and returns the response body parsed as a JSON object.
This function rejects if 1) the request body cannot be parsed as JSON but is
being sent with an application/json
content-type header, 2) the response
body cannot be parsed as JSON but was received with an application/json
content-type header, 3) the response was received with a content-type header
other than application/json
, or 4) the response was received with a non-2xx
status.
The object SWR returns will contain the rejection reason under the error
property. Usually, error
is as an instance of JsonUnfetchError complete
with json
and res
properties. If unfetch itself fails, the error
object returned will not have these properties.
• JsonType extends JsonObject
= JsonObject
• ErrorType extends JsonObject
= JsonType
• url: string
• init: Omit
<JsonRequestInit
, "swr"
> & object
Promise
<JsonType
>
const { data: json, error } = useSwr('api/endpoint', swrFetch);
// Or: ... = useSwr('api/endpoint', key => jsonFetch(key, { swr: true }));
if(error) <div>Error: {error.message}</div>;
return <div>Hello, your data is: {json.data}</div>;