apirequest.js
A tool to help building API libraries. Apirequest allows you to create a library for accessing http services easily, in a centralized way. An API defined by apirequest will return a native Javascript Promise when called, wrapping fetch
calls.
Installation
Get the npm module:
npm install apirequest --save-dev
Since fetch is not fully supported by all browsers it is recommended to use the fetch polyfill from Github: https://github.com/github/fetch
Usage
// Load the library from npm; // Create a new API instance, with optional parametersconst api = // The paramTransformer option lets you specify a function that operates on the // parameters passed down to the `fetch` request. // The function has a single RequestParams argument, allowing you to access the `url`, // `params` and `options` properties. The return value must be a RequestParams instance. { // Set a default "maxreturned" parameter on GET queries if requestoptionsmethod === 'GET' && !requestparamsmaxreturned requestparamsmaxreturned = 10; return request; } // The responseTransformer option lets you process responses before returning to clients. // The function takes the response JSON object as an argument. { // Given this API response envelope: // { // meta: { status: 200, ... }, // data: { ... } // } // The following function simply checks the returned status, and returns the wrapped data // in the "data" field, or rejects the request promise. return responsemeta && responsemetastatus === 200 ? responsedata : Promise; };
Creating the API clients to be distributed can be done using the get
, post
, put
or delete
methods.
Syntax:
get: url: string params: Object = {} options: Object = {}: Function
The required url
parameter is the URL of the API. Placeholders in double curly brackets will be replaced with the values provided by clients.
params
allows the client library to provide default parameter values. These parameters can be overridden by the user specified values at run-time.
options
lets the client set request options, these will be passed to fetch
. This is the good place for specifying custom headers for example.
All parameter of the client can be specified as a JavaScript value or a "thenable" object or function.
const getItem = api;const postItem = api;
Generated API client functions accept two parameters:
params
-- an object containing the request payloadoptions
-- an object setting custom request options (using the fetch API)
The return value is a native JavaScript Promise object, to allow easy processing or further chaining. Usage:
// Get a single item ; // Create a new item ;
Examples
Creating simple clients
Define an API client
A simple request to fetch an object from a fixed URL:
;const api = ;const getLuke = api; ;
Working with payload
Add user specified payload to the request
Dynamically pass values to the URL:
const getPerson = api; // Called URL will be http://swapi.co/api/people/1/ ;
Add default payload to request
const getPerson = api; // URL will be http://swapi.co/api/people/1/?format=wookiee// Note that while `id` was used for the URL generation, the `format` param is passed as// regular GET query param, as the only remaining payload attribute. ;// Parameter specified at call time will override the default value, the resulting URL// will be http://swapi.co/api/people/1/?format=json ;
Add payload async
Parameters and options both can be specified as a JavaScript value, function or Promise.
const getPerson = api;// parameter as a functionlet {id: nameToIdMapname}; ; // parameter as a Promiselet getPersonIdPromise = { // Get the person ID from a remote server if remoteResponse ; else ; }; ;
Response and request processing
Process the response
As client functions return with a Promise they can be chained for further processing
const getPerson = api; // Define a method that works with the resultlet { personheight = personheight * 0393701; return person;} ;
Handling errors
Handle errors
TBD
Development
See CONTRIBUTING
Flow support
Flow annotation is not complete yet, will be done later, providing an external type definition.