quickrest
A simple library for quickly building browser-based REST API clients.
This project is a WIP
Status
Features
- Offers both a callback and promise-based API.
- Bring your own
Promise
and request libraries. - Makes reasonable assumptions about the structure of your REST API.
- Supports complex API versions.
- Offers a fluent API for requesting nested resources.
Simple Example
const api = // callback interface// GET /users/9000api // promise interface// GET /users/9000api // get nested resource// GET /users/9000/posts/3api // get nested resource, and pass response to callback// GET /users/9000/postsapiposts // create nested resource// POST /users/9000/postsapiposts // create doubly-nested resource// POST /users/9000/posts/3/commentsapicomments // delete nested resource// DELETE /users/9000/posts/3api // api versioning via url// GET /v2/usersapi // the backward-incompatible and deprecated comments API// PUT /commentsapicomments // /v2/commentsapicomments
Motivation
Constructing API urls via string concatenation (or even template strings) can be annoying and error prone. Using JavaScript's language featues to model a REST API's resources makes for a more pleasant programming experience.
Caveats
The library is currently meant for environments where high-level REST parameters are set once for the lifetime of the app/process, (e.g., bearer tokens for an app) like in a browser. It lacks a simple mechanism to scope request-specific REST data (e.g., per-request data within an express.js app).
This capability is not a priority at the moment, but a PR with it is welcome =). It might look something like this:
const api = ; // and then for each request (e.g., in an express middleware function){ reqapi = api }
Installation
npm install quickrest --save
If you're indifferent to the http and Promise libraries used, this lib will attempt to use
superagent
and es6-promise
, respectively. These libraries are considered peer dependencies, so it's your responsibility to install them alongside this lib.
npm install --save es6-promise superagent
Node/CommonJS
const quickrest =
ES6 Module
Documented Example
const api =
Development
Clone respository and install dependencies.
All development dependencies are managed via npm.
npm install
License
MIT