Extends the reponse to have a useful Url property that provides quick access to the URL parts. It also inherits the extended json method from {@link IBodyMutable}
Url
json
import { fetcher } from '@ef-carbon/fetcher'; const response = await fetcher('https://google.com'); console.log(response.Url.protocol); // `https:`
import { Json, isIJsonObject } from '@ef-carbon/fetch'; import { fetcher, Verify } from '@ef-carbon/fetcher'; interface IData { name: string; } function isIData(json: Json): json is IData { return isIJsonObject(json) && typeof json.name === 'string'; } const verify: Verify<IData> = isIData; // expected payload `{ "name": "Rick Sanchez" }` const response = await fetcher('http://endpoint.com/data.json'); const data = response.json(verify); // verification console.log(data.name);
import { Json, isIJsonObject } from '@ef-carbon/fetch'; import { fetcher, Convert } from '@ef-carbon/fetcher'; interface IData { name: string; } function isIData(json: Json): json is IData { return isIJsonObject(json) && typeof json.name === 'string'; } function getName(json: Json): string { if (!isIData(json)) { throw TypeError(`Invalid JSON payload: ${JSON.stringify(json)}`); } return json.name; } const convert: Convert<string> = getName; // expected payload `{ "name": "Rick Sanchez" }` const response = await fetcher('http://endpoint.com/data.json'); const name = response.json(convert); // conversion console.log(name);
Extends the reponse to have a useful
Url
property that provides quick access to the URL parts. It also inherits the extendedjson
method from {@link IBodyMutable}import { fetcher } from '@ef-carbon/fetcher'; const response = await fetcher('https://google.com'); console.log(response.Url.protocol); // `https:`
import { Json, isIJsonObject } from '@ef-carbon/fetch'; import { fetcher, Verify } from '@ef-carbon/fetcher'; interface IData { name: string; } function isIData(json: Json): json is IData { return isIJsonObject(json) && typeof json.name === 'string'; } const verify: Verify<IData> = isIData; // expected payload `{ "name": "Rick Sanchez" }` const response = await fetcher('http://endpoint.com/data.json'); const data = response.json(verify); // verification console.log(data.name);
import { Json, isIJsonObject } from '@ef-carbon/fetch'; import { fetcher, Convert } from '@ef-carbon/fetcher'; interface IData { name: string; } function isIData(json: Json): json is IData { return isIJsonObject(json) && typeof json.name === 'string'; } function getName(json: Json): string { if (!isIData(json)) { throw TypeError(`Invalid JSON payload: ${JSON.stringify(json)}`); } return json.name; } const convert: Convert<string> = getName; // expected payload `{ "name": "Rick Sanchez" }` const response = await fetcher('http://endpoint.com/data.json'); const name = response.json(convert); // conversion console.log(name);