⎔
sleeperA high-level network abstraction that makes working with REST APIs simple.
This library is very old and you should probably use ky instead, which has a similar API but is based on Fetch rather than XMLHttpRequest.
Instantiation
Sleeper is available in ES Modules, CommonJS and UMD formats.
Using Modules:
; const users = ; // users is a sleeper.Resource instance:;
Without AMD:
API
sleeper(url) / new sleeper.Resource(url)
Create a new sleeper.Resource
instance for the resource at a given URL.
const users = ; // equivalent to:const users = '/api/users';
.index(callback)
Get a list of resources
usersindex { // list is an Array of users console;};
.get(id, callback)
Get a single resource
users;
.post(data, callback)
Create a new resource.
users;
.put(id, data, callback)
Update/replace an existing resource, indicated by its ID.
users;
.patch(id, data, callback)
Update/patch an existing resource, indicated by its ID.
users;
.delete(id, callback)
Update an existing resource, indicated by its ID.
If you care about old browsers, a del()
alias is provided.
users;
.param(key [, value])
Get or set a querystring parameter to send on each request.
- If
value
is set: adds a global querystring parameterkey
with a value ofvalue
- If
value
is empty: returns the current value of the global parameterkey
- If
key
is an Object: addskey
's key-value property pairs as global parameters
// Send a token on all subsequent requests:users; // Get the current token value:const token = users;console;
Hooks
Modify functionality by changing these values.
.serializeBody(body, req)
When a request has a body, this function is responsible for serializing it.
The default implementation of serializeBody()
provides an example usage:
{ if !reqheaders'content-type' reqheaders'content-type' = 'application/json'; return JSON;}
.idKey
This key path is used to obtain an Object identifier if one is not explicitly passed to put()
/ patch()
.
Defaults to
"id"
.
Events
req
(req) => {}
Hook the req
event to be notified prior to all requests.
Event handlers get passed a Request
instance.
users;
req:/url
(req) => {}
Add an event handler for "req:" followed by relative URL (ex: req:/users
) to be notified when a request is made to the given URL.
This is just a more specific version of the req
event.
users;
status
(req, res) => {}
Hook the status
event to be notified of the status of every response.
Event handlers get passed Request
and Response
.
users;
status:N
(req, res) => {}
Add an event handler for "status:" followed by a specific response status code (ex: status:401
) to be notified when a response is issued with that status.
This is just a more specific version of the status
event.
users;
res
(req, res) => {}
Hook the res
event to be notified of all responses.
Event handlers get passed a Request
and Response
.
users;
res:/url
(req, res) => {}
Add an event handler for "res:" followed by relative URL (ex: res:/users
) to be notified when a response is received from the given URL.
This is just a more specific version of the res
event.
users;