
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
@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.
The npm package @bitty/create-request receives a total of 13 weekly downloads. As such, @bitty/create-request popularity was classified as not popular.
We found that @bitty/create-request 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.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.