json-grab
TypeScript icon, indicating that this package has built-in type declarations

0.1.0-alpha.2 • Public • Published

Grab

Grab Node JS library for calling JSON-based HTTP-apis. Grab wraps fetch() and takes care about JSON serialization / deserialization and error handling

Installation

npm install json-grab or yarn add json-grab or pnpm add json-grab

Example

With fetch

async function getDataFromServer() {
    const result = await fetch(url + `?${param}=${value}`, {
      method: "POST", 
      body: JSON.stringify(body), 
      headers: {
        "Content-Type": "application/json", 
        "Accept": "application/json"}
    });
    if (!result.ok) {
       throw new Error(`Can't post data to ${url} - ${result.statusText}`)
    }
    return await result.json();
}

With grab

async function getDataFromServer() {
   return await grab(url, { body, query: {param: "value"} })
}

Works in NodeJS Browser

grab works in Browser without additional configuration. In NodeJS you need to provide fetch implementation. For example, you can use node-fetch:

import fetch from 'node-fetch';
import grab from 'json-grap';

grab.setup({fetch})

Features

  • API compatibility. Grab API is compatible with fetch
  • Smart defaults. For example, you don't need to set an HTTP method if you set a request body. Grab is smart enough to set POST method in this case. Also, it sets Content-Type and Accept headers to application/json automatically
  • Error handling. Excellent error handling with detailed, clear error messages
  • No more string concatenation for URLs params. Use: grab(url, {query: {a: 1, b: 2}})

Future features

Timeouts

await grab(url, { body, timeoutMs: 2000 })

Templates

import fetch from 'cross-fetch';

const myGrab = grab.template({headers: {'Authorization': `Bearer XXXX`}, fetchImpl: fetch })
//no need to set authorization header for each request
const result = await myGrab(url, { body })

Readme

Keywords

none

Package Sidebar

Install

npm i json-grab

Weekly Downloads

4

Version

0.1.0-alpha.2

License

ISC

Unpacked Size

18.4 kB

Total Files

18

Last publish

Collaborators

  • vklmn