- exception-free
- simple
- tiny (2K Gzipped)
- zero dependencies
- immutable
fetch api wrapper
- docs
- add timeout options
const ajax = require('@subiz/ajax')
// try to making GET https://httpstat.us/200
let res = await ajax.get("https://httpstat.us/200")
console.log(req.body, res.code, res.error) // "200, OK" 200 undefined
var req = ajax.setBaseUrl("https://httpstat.us")
res = await req.post("200")
// POST https://httpstat.us/200
Request objects represences HTTP request.
Request object are immutable, which mean, you cannot change its current state or data. Immutiblity makes object behavious more predictable, reducing bug.
The object's state is initialize once when creating the object. You can create request object by either using Ajax object or making a derived object from old one.
Available methods:
create a new derived object by appending new (key, value) pair into old request query
create a new derived object by removing a (key, value) pair from old request query
create a new derived object by replacing old query with new one.
examples:
req = req.setQuery({a:"xin chao", b: 6})
create a new derived object by registering (appending) a before hook
create a new derived object by removing all before hooks and after hooks
create a new derived object by registering (appending) a hook
create a new derived object by changing old request base url. New derived request with relative url will be append to this base url.
create a new derived object by merging old header with new header
attach hidden metadata to request, those key-value will not be sent to the server, designed to keep state in hooks
examples:
req = req.setHeader({"x-real-ip": "193.155.45.3"})
Ajax object is lets you create request object. Available requests:
-
get(url)
: // create new GET request to address -
post(url)
: // create new POST request to address -
put(url)
: // create new PUT request to address -
del(url)
: // create new DELETE request to address -
head(url)
: // create new HEAD request to address -
patch(url)
: // create new PATCH request to address
before hooks let you modify the request right before sending it.
You register a before hook by calling beforeHook
. beforeHook function return a promise and take in a reference to the request object as parameter.
for examples: to add query ?a=5
and ?b=6
right before sending the request
let req = ajax.setBaseUrl("https://google.com")
req = req.beforeHook(async param => {
param.request = param.request.addQuery('a', 5)
}).beforeHook(async param => {
param.request = param.request.addQuery('b', 6)
})
await req.get() // https://google.com?a=5&b=6
before hook is called one by one in registered order, which hook registered first will be called first.
const apireq = ajax
.setBaseUrl("https://appv4.subiz.com/4.0/")
.afterHook(param => {
if (param.code !== 500) return
var retry = param.request.meta.retry || 0
if (retry === 3) return
var req = param.request.setMeta('retry', retry + 1) // increase number of attempt
// continue retry
return req.get().then(out => {
var [code, body, err] = out
param.code = code
param.body = body
param.err = err
})
})
})
let [code, body, err] = await apireq.get("me")