JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 82
  • Score
    100M100P100Q9408F
  • 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 Dependency Status NSP Status

Code of Conduct Codecov Coverage Status

codebeat-badge codacy-badge inch-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 {
//   fetchAsBuffer,
//   fetchAsJson,
//   fetchAsText,
// } = 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 {
//   fetchAsBuffer,
//   fetchAsJson,
//   fetchAsText,
// } 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:

  • .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.

fetchAsBuffer(url[, options])

  • name <string> Name of the person to greet at.
  • options <Object> Options for HTTP(S) request. See Options for a list of supported options.
  • returns: <Promise<Buffer>> Promise which resolves with a Buffer.

fetchAsJson(url[, options])

  • name <string> Name of the person to greet at.
  • options <Object> Options for HTTP(S) request. See Options for a list of supported options.
  • returns: <Promise<Object>> Promise which resolves with a JSON data which can consumed by JavaScript as Object.

fetchAsText(url[, options])

  • name <string> Name of the person to greet at.
  • options <Object> Options for HTTP(S) request. See Options for a list of supported options.
  • returns: <Promise<string>> Promise which resolves with a text/ string.

License

MIT License © Rong Sen Ng