Coder Social home page Coder Social logo

json-grab's Introduction

Grab

Grab Node JS library for calling JSON-based HTTP API. Grab wraps fetch() and takes care of 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 })

json-grab's People

Contributors

vklimontovich avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.