node-callapi
Making api call with node-fetch, with middleware.
Last updated a year ago by abramstyle .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install node-callapi 
SYNC missed versions from official npm registry.

Node Call API

Making api call with node-fetch, with middleware.

npm version CircleCI Coverage Status


Basic usage

const Caller = require('node-callapi');
const caller = new Caller(setting);

caller.call(endpoint, options)

Constructor

new Caller(options);

options.middlewares

middlewares will be applied after every request finished.

Middleware

a middleware is an async function, it recevied an object. and returns a object that has the shape same as received data.

async function middleware(data) {
  const {
    // fetch options
    fetchOptions,

    // the endpoint
    url,

    // response, if no response, it will be null
    response,

    // the request duration
    duration,

    // if any error occured, it will be an Error instance
    error,

    // the result data, it will be one of [array, object, string, null]
    data,
  } = data;
  // do something
  return result;
}

data.error

name description
error.status the response status
error.fetchOptions the fetch options
error.duration the time of the fetch request processing time
error.data the data from server, it will parsed even if request failed
error.response the full response object
error.url the fetch url

api

call(url, options)

url should use absolute url.

it will return the parsed result.

options.method(optional)

request method. should be one of ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE', 'HEAD', 'CONNECT'].

options.data

send data to server if use 'POST', 'PUT' method.

options.middlewares

append middlewares. should be a valid middleware.

options.headers

append headers. should be an key-value object.

Current Tags

  • 1.2.2                                ...           latest (a year ago)

8 Versions

  • 1.2.2                                ...           a year ago
  • 1.2.1                                ...           a year ago
  • 1.2.0                                ...           2 years ago
  • 1.1.4                                ...           2 years ago
  • 1.1.3                                ...           2 years ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 7
Dependencies (4)
Dev Dependencies (10)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |