Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@bitty/create-request
Advanced tools
Readme
@bitty/create-request
Apply interceptors to fetch
and create a custom request function.
This library is published in the NPM registry and can be installed using any compatible package manager.
npm install @bitty/create-request --save
# Use the command below if you're using Yarn.
yarn add @bitty/create-request
@bitty/create-request
is curry function, which applies interceptors to fetch
and returns a new request function.
import createRequest from '@bitty/create-request';
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
const request = createRequest(window.fetch, {
onRequest: (method: Method, route: string, data: any = undefined) => ({
url: 'https://api.example.com' + route,
body: JSON.stringify(data),
method,
headers: { 'Content-Type': 'application/json' }
}),
onResponse: (response: Response) => {
if (response.status === 403)
throw new Error('Authorization error.');
return response.json();
},
onError: (error:? Error) => {
sentry.captureException(error);
return Promise.reject(message);
}
});
request('POST', '/user', {
name: 'Vitor'
})
.then((response) => response.success && alert('User was created!'));
onError
onError?: (reason?: Error) => Promise<never>;
Handle request and response errors.
onRequest
onRequest?: <A extends any[] = [RequestOptions]>(...params: A) => RequestOptions;
Handle request and define request arguments. A
generic type defines returned request
function arguments type.
onRequestError
onRequestError?: (reason?: Error) => Promise<never>;
Handle request errors. Overwrites onError
handling request errors.
onResponse
onResponse?: <R = Response>(response: R) => R | PromiseLike<R>;
Handle response and define the request return. R
generic type defines returned request
function result type.
onResponseError
onResponseError?: (reason?: Error) => Promise<never>;
Handle response errors. Overwrites onError
handling response errors.
Older browsers don't support Fetch API
, but you can use unfetch
or other polyfill to achieve it.
import fetch from 'unfetch';
import createRequest from '@bitty/create-request';
export default createRequest(fetch, {
onRequest: (options) => ({
...options,
headers: {
...options.headers,
'Content-Type': 'application/json'
}
}),
onResponse: (response) => response.json()
});
Node environment does not provide global fetch
function, but you can use node-fetch
to achieve it.
const fetch = require('node-fetch');
const createRequest = require('@bitty/create-request');
module.exports = createRequest(fetch, {
onResponse (response) {
return response.json();
}
});
Released under MIT License.
FAQs
Apply interceptors in `fetch` to create a custom request functions.
The npm package @bitty/create-request receives a total of 10 weekly downloads. As such, @bitty/create-request popularity was classified as not popular.
We found that @bitty/create-request demonstrated a not healthy version release cadence and project activity because the last version was released 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.