
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
@subiz/ajax
Advanced tools
[![npm][npm]][npm-url] [![deps][deps]][deps-url] [![size][size]][size-url]
fetch api wrapper
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:
addQuery(key, value)create a new derived object by appending new (key, value) pair into old request query
removeQuery(key)create a new derived object by removing a (key, value) pair from old request query
setQuery(query)create a new derived object by replacing old query with new one.
examples:
req = req.setQuery({a:"xin chao", b: 6})
beforeHook(promise)create a new derived object by registering (appending) a before hook
clearHookscreate a new derived object by removing all before hooks and after hooks
afterHook(promise)create a new derived object by registering (appending) a hook
setBaseUrl(url)create a new derived object by changing old request base url. New derived request with relative url will be append to this base url.
setHeader(header)create a new derived object by merging old header with new header
setMeta(key, value)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"})
get(url, data, cb)post(url, data, cb)put(url, data, cb)del(url, data, cb)head(url, data, cb)patch(url, data, cb)setBaseUrlsetMethodcontentTypeJsoncontentTypeFormsetContentTypesetParsersendsetMetaAjax object is lets you create request object. Available requests:
get(url): // create new GET request to addresspost(url): // create new POST request to addressput(url): // create new PUT request to addressdel(url): // create new DELETE request to addresshead(url): // create new HEAD request to addresspatch(url): // create new PATCH request to addressbefore 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")
FAQs
[![npm][npm]][npm-url] [![deps][deps]][deps-url] [![size][size]][size-url]
We found that @subiz/ajax demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.