
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@apicase/adapter-xhr
Advanced tools
XHR adapter for @apicase/core
npm install @apicase/adapter-xhr
import { apicase } from '@apicase/core'
import xhr from '@apicase/adapter-xhr'
const xhrAPI = apicase(xhr)
We use node-fetch as polyfill for Node.js
const req = await xhrAPI({
url: '/api/posts',
method: 'GET',
headers: { token: 'my_secret_token' },
query: { userId: 1 }
})
if (req.success) {
console.log(req.result)
} else {
console.error(req.result)
}
It will call:
var xhr = new XMLHttpRequest()
xhr.open('GET', '/api/posts?userId=1', true)
xhr.onload = function onload (e) {
// See validators
if (isSuccess(e.target, e)) {
console.log(e)
} else {
console.error(e)
}
}
xhr.setRequestHeader('token', 'my_secret_token')
xhr.send(null)
Fetch adapter also has path-to-regexp to pass urls params smarter. Params are stored in params property
xhrAPI({
url: '/api/posts/:id',
params: { id: 1 }
})
// => GET /api/posts/1
If you want to create dynamic headers object so you can pass headers property as function that returns headers object
xhrAPI({
url: '/api/posts',
method: 'POST',
headers: () => ({
token: localStorage.getItem('token')
})
})
It will be called every time you make a request so if token will be removed, header won't be sent too.
I've added possibility to customizate resolve/reject apicase requests with validator callback.
It accepts xhr target and onload event
xhrAPI({
url: '/api/posts',
validateStatus: (status) =>
status >= 200 && status <= 299
})
Default validator function is here:
function defaultValidator (status) {
return status >= 200 && status < 300
}
MIT
FAQs
XHR adapter for apicase-core
We found that @apicase/adapter-xhr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.