Piggyback
A resource-oriented wrapper over the window.fetch
API. Defaults to the JSON content type.
Install
npm install --save piggyback
Usage
Resource Builder
The resource
function constructs a complete set of CRUD methods to interact with a conventional HTTP API.
All methods return a promise that resolves the JSON body
of the response.
; const getTasks getTaskById createTask updateTask deleteTask = ; // GET /tasks; // GET /tasks/{id}; // POST /tasks; // PUT /tasks/{id}; // DELETE /tasks/{id};
Sending HTTP Requests
The following methods are provided to interact directly with JSON web APIs:
sendGet
sendPost
sendPut
sendPatch
sendDelete
All methods return a promise that resolves to a Response
, including the Body
mixin of the Fetch API.
; { return ;} { return ;} { return ;} { return ;} { return ;}
Notes
- Depends on
es6-promise
andisomorphic-fetch
polyfills (this is handled automatically). - Browser auth credentials are always sent with the request.
- HTTP error codes result in a JavaScript
Error
being thrown by default. - How you handle resolving and rejecting is up to you.
If these defaults aren’t suitable for your use-case, consider using the isomorphic-fetch
library directly.