Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
An axios similar API request library but use fetch, and more.
Features:
axios.create
/ axios.interceptors.request.use
/ axios.interceptors.response.use
/ .get/post/put/patch/delete/head/options
npm i xior
import xior from 'xior';
const http = xior.create({ baseURL: 'https://exmaple.com', timeout: 120 * 1000 });
http.interceptors.request.use((config) => {
// do something
return config;
});
http.interceptors.response.use((result) => {
const { config, response, data } = result;
// do something
return result;
});
// GET
async function getList() {
const { data } = await http.get('/list', { params: { page: 1, perPage: 20 } });
return data;
}
// POST
async function create() {
const { data } = await http.post(
'/create',
{ name: 'test', desc: 'foobar..foobar' },
{ params: { redirect: '/list' } }
);
return data;
}
import xior from 'xior';
const instance = xior.create({});
await instance.get('http://httpbin.org', {
params: {
a: 1,
b: 2,
},
});
await instance.post('http://httpbin.org', {
a: 1,
b: 2,
});
In xior, URI encoded strings default use lite encode, means if your params
is nested object, it will be [object object]
:
import xior from 'xior';
const instance = xior.create({});
instance.get('http://httpbin.org', {
params: {
a: 1,
b: {
c: 2,
},
},
});
The url will be like: http://httpbin.org?a=1&b=[object object]
, to support nested objects url encoded, use qs
's stringify
module:
import xior from 'xior';
// @ts-ignore
import stringify from 'qs/lib/stringify';
const instance = xior.create({
encode: (params: Record<string, any>) => stringify(params, {}),
});
instance.get('http://httpbin.org', {
params: {
a: 1,
b: {
c: 2,
},
},
});
// http://httpbin.org?a=1&b[c]=2
Not like axios, xior doesn't support upload progess or download progress.
Use FormData to upload files.
import xior from 'xior';
const instance = xior.create();
const formData = new FormData();
formData.append('file', fileObject);
formData.append('filed1', 'val1');
formData.append('filed2', 'val2');
instance.post('/upload', formData).then((res) => {
console.log(res.data);
});
import xior from 'xior';
const instance = xior.create({
timeout: 120 * 1000,
});
await instance.post(
'http://httpbin.org',
{
a: 1,
b: 2,
},
{
timeout: 60 * 1000, // override default timeout 120 * 1000
}
);
import xior from 'xior';
const instance = xior.create();
const controller = new AbortController();
xiorInstance.get('http://httpbin.org', { signal: controller.signal }).then((res) => {
console.log(res.data);
});
class CancelRequestError extends Error {}
controller.abort(new CancelRequestError()); // abort request with custom error
import xior, { merge as deepMerge } from 'xior';
const instance = xior.create();
instance.interceptors.request.use((config) => {
return deepMerge(config, {
headers: {
token: localStorage.getItem('token') || '',
},
});
});
import xior, { merge as deepMerge } from 'xior';
const instance = xior.create();
instance.interceptors.response.use(
(res) => {
const { data, request, response } = res;
console.log(request, resposne, data);
return res;
},
function onRejected(error) {
throw error;
}
);
if the options
responseType
isresponseType: 'stream' | 'document' | 'arraybuffer' | 'blob'
, then xior will just return the original response:{ response }
, you can do anthing with response you like:
import xior, { merge as deepMerge } from 'xior';
const instance = xior.create({
baseURL: 'http://httpbin.org',
});
instance.get('/stream', { responseType: 'stream' }).then(({ response }) => {
// `response` is the original response, like fetch('/stream').then(response => { console.log(response)})
});
❗️❗️❗️ WIP (Work in Progress) ❗️❗️❗️
import xior from 'xior';
import xiorCachePlugin from 'xior/lib/plugins/cache';
import xiorErrorRetryPlugin from 'xior/lib/plugins/error-retry';
import xiorRepeatRequestsFilterPlugin from 'xior/lib/plugins/repeat-requests-filter';
const instance = xior.create();
instance.plugins.use(xiorCachePlugin());
instance.plugins.use(xiorErrorRetryPlugin());
instance.plugins.use(xiorAvoidRepeatRequestsPlugin());
❗️❗️❗️ WIP (Work in Progress) ❗️❗️❗️
import xior from 'xior';
const instance = xior.create();
instance.plugins.use(async (request, response, error) => {
const inRequestPhase = !response;
const inResponsePhase = Boolean(response);
const isError = Boolean(error);
if (isError) {
//
}
if (inRequestPhase) {
//
} else if (inResponsePhase) {
//
}
});
xior
100% compatiable with axios
? NoFormData
'stream' | 'document' | 'arraybuffer' | 'blob'
? Use responseType: 'stream' | 'document' | 'arraybuffer' | 'blob'
, will return original { response }
FAQs
A lite request lib based on fetch with plugin support and similar API to axios.
We found that xior demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.