![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
custom-fetch
Advanced tools
Creates a function with the same API as WHATWG's fetch
,
but with your own custom behaviour. Ideal for testing or
for modifying an HTTP request before sending.
import CustomFetch from 'custom-fetch';
import * as NodeFetch from 'node-fecth';
// You need to bring your own Request and Response constructors
// There are already libraries that implement these and I didn't want
// to re-implement them just for the sake of it.
const { Request, Response } = NodeFetch;
const fetch = new CustomFetch(async (request, signal) => {
// `request` is an instance of the provided Request class,
// constructed based on the arguments passed to `fetch`.
// `signal` is the value of `signal` on the fetch options object
// (or undefined if not provided)
switch(request.url) {
case 'http://horses.example/':
// You can return just a response body and it will be
// used as the first argument to the Response constructor
return 'Welcome to horses.example';
case 'http://flowers.example/foxglove.json':
// You can also return a preconstructed Response object
// (as long as it's of the same class as the given
// Response constructor)
return new Response(
JSON.stringify({
name: 'Foxglove',
scientificName: 'Digitalis purpurea',
gbifId: 5414995
}),
{
headers: {
'Content-Type': 'application/json'
}
}
)
default:
// Since NodeFetch.fetch returns a Response object,
// we can just forward the request on if we like,
// or we could make modifications to the request
// before doing so.
request = new Request(request, { headers: { 'X-Favourite-Color': 'blue' } })
return NodeFetch.fetch(request, { signal });
},
// Let CustomFetch know how to construct the request and response objects
{ Request, Response }
);
export default fetch('http://horses.example/');
function
function
Response
| string
Custom Fetch constructor
Param | Type | Description |
---|---|---|
fetchHandler | fetchHandler | Callback for handling fetch request. |
fetchConstructors | object | Object containing constructors for creating fetch Request and Response classes |
fetchConstructors.Request | Request | Constructor for a fetch Request object |
fetchConstructors.Response | Response | Constructor for a fetch Response object |
function
Kind: inner class of module.exports
Param | Type |
---|---|
url | string |
options | object |
function
Kind: inner class of module.exports
Param | Type |
---|---|
body | string |
options | object |
Response
| string
Kind: inner typedef of module.exports
Param | Type |
---|---|
request | Request |
signal | Signal |
FAQs
None
The npm package custom-fetch receives a total of 2 weekly downloads. As such, custom-fetch popularity was classified as not popular.
We found that custom-fetch 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.