Package Exports
- fetch-as
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fetch-as) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
fetch-as
Fetch data in Node.js
Fetch API in Node.js with specific response type
Table of contents
Pre-requisites
Setup
Install
# Install via NPM
$ npm install --save fetch-asUsage
Node.js
const { fetchAs } = require('fetch-as');
// OR require each method explicitly
// const {
// fetchAsArrayBuffer,
// fetchAsBlob,
// fetchAsBuffer,
// fetchAsJson,
// fetchAsText,
// fetchAsTextConverted,
// } = require('fetch-as');
async function runFetch() {
const url = 'http://www.mocky.io/v2/5a50cfa82f000085158d5315';
const jsonData = await fetchAs.json(url); // OR fetchAsJson(url);
console.log('# json', jsonData);
// {
// "status": 200,
// "message": "OK",
// "by": "fetch-as"
// }
}
runFetch();Native ES modules or TypeScript
import fetchAs from 'fetch-as';
// OR import each method explicitly
// import {
// fetchAsArrayBuffer,
// fetchAsBlob,
// fetchAsBuffer,
// fetchAsJson,
// fetchAsText,
// fetchAsTextConverted,
// } from 'fetch-as';
async function runFetch() {
const url = 'http://www.mocky.io/v2/5a50cfa82f000085158d5315';
const jsonData = await fetchAs.json(url); // OR fetchAsJson(url);
console.log('# json', jsonData);
// {
// "status": 200,
// "message": "OK",
// "by": "fetch-as"
// }
}
runFetch();API Reference
fetchAs
This contains a collection of methods that will convert the response into the specified data type:
.arrayBuffer(url[, options])Method which will return a ArrayBuffer..blob(url[,options])Method which will return a Blob..buffer(url[, options])Method which will return a Buffer..json(url[, options])Method which will return a JSON data which can consumed by JavaScript as Object..text(url[, options])Method which will return a text/ string..textConverted(url[, options])Method which will return a text/ string, except instead of always converting toUTF-8, encoding sniffing will be performed and text converted toUTF-8, if possible.
FetchAsData<T>
Response data returned in the type of T where T might be one of the values: Buffer, Object, or string.
- <status> HTTP response status code. Any response that has a HTTP status greater than
399can be regarded as an error response. - <
data<T>> This contains the successful response data of typeT. Only shows when the HTTP response status code is less than400. - <
error<T>> This contains the error response data of typeT. Only shows when the HTTP response status code is greater than399.
fetchAsArrayBuffer(url[, options])
- url <string> A string representing the URL for fetching.
- options <Object> Options for HTTP(S) request. See Options for a list of supported options.
- returns: <Promise<FetchAsData<ArrayBuffer>> Promise which resolves with a FetchAsData of type ArrayBuffer.
fetchAsBlob(url[, options])
- url <string> A string representing the URL for fetching.
- options <Object> Options for HTTP(S) request. See Options for a list of supported options.
- returns: <Promise<FetchAsData<Blob>> Promise which resolves with a FetchAsData of type Blob.
fetchAsBuffer(url[, options])
- url <string> A string representing the URL for fetching.
- options <Object> Options for HTTP(S) request. See Options for a list of supported options.
- returns: <Promise<FetchAsData<Buffer>> Promise which resolves with a FetchAsData of type Buffer.
fetchAsJson(url[, options])
- url <string> A string representing the URL for fetching.
- options <Object> Options for HTTP(S) request. See Options for a list of supported options.
- returns: <Promise<FetchAsData<Object>> Promise which resolves with a FetchAsData of type JSON which can consumed by JavaScript as Object.
fetchAsText(url[, options])
- url <string> A string representing the URL for fetching.
- options <Object> Options for HTTP(S) request. See Options for a list of supported options.
- returns: <Promise<FetchAsData<string>> Promise which resolves with a FetchAsData of type string.
fetchAsTextConverted(url[, options])
* Please note that encoding is required to be installed in order to use this method.
- Identical to fetchAsText(url[, options]), except instead of always converting to
UTF-8, encoding sniffing will be performed and text converted toUTF-8, if possible.
License
MIT License © Rong Sen Ng
