1.2.2 • Published 6 years ago

node-callapi v1.2.2

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

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

namedescription
error.statusthe response status
error.fetchOptionsthe fetch options
error.durationthe time of the fetch request processing time
error.datathe data from server, it will parsed even if request failed
error.responsethe full response object
error.urlthe 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.

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago