JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 82
  • Score
    100M100P100Q9409F
  • License MIT

Fetch API in Node.js with specific response type

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


NPM

Build Status Version Downloads MIT License Code of Conduct

Dependency Status NSP Status Codecov Coverage Status

codebeat-badge codacy-badge

Fetch API in Node.js with specific response type

Table of contents

Pre-requisites

Setup

Install

# Install via NPM
$ npm install --save fetch-as

Usage

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 to UTF-8, encoding sniffing will be performed and text converted to UTF-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 399 can be regarded as an error response.
  • <data<T>> This contains the successful response data of type T. Only shows when the HTTP response status code is less than 400.
  • <error<T>> This contains the error response data of type T. Only shows when the HTTP response status code is greater than 399.

fetchAsArrayBuffer(url[, options])

fetchAsBlob(url[, options])

fetchAsBuffer(url[, options])

fetchAsJson(url[, options])

fetchAsText(url[, options])

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 to UTF-8, if possible.

License

MIT License © Rong Sen Ng