Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@bitty/create-request
Advanced tools
Apply interceptors in `fetch` to create a custom request functions.
@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.
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
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.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.