npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@windyroad/decorate-fetch-response

v3.1.0

Published

A library for decorating fetch responses.

Downloads

40

Readme

@windyroad/decorate-fetch-response

This package provides a function for decorating the response of a fetch request with additional properties. It uses the wrapFetch function from the @windyroad/wrap-fetch package to wrap the fetch implementation with a decorator function.

Installation

To use this package in your project, you can install it via npm:

npm install --save @windyroad/decorate-fetch-response

Usage

To use this package in your project, you can import the decorateFetchResponse function and use it to decorate the response of a fetch request:

import decorateFetchResponse from '@windyroad/decorate-fetch-response';

const fetchWithCustomHeader = decorateFetchResponse((response) => {
  return response.headers.set('X-Custom-Header', 'custom-value');
});

fetchWithCustomHeader('https://example.com').then((response) => {
  console.log(response.headers.get('X-Custom-Header')); // 'custom-value'
});

In this example, the decorateFetchResponse function is used to add a custom header to the response of a fetch request. The fetchWithCustomHeader function is a decorated version of the fetch implementation that adds the X-Custom-Header header to the response.

Examples

Here are a few examples of how you can use the decorateFetchResponse function:

Adding custom headers to the response

import decorateFetchResponse from '@windyroad/decorate-fetch-response';

const fetchWithCustomHeader = decorateFetchResponse((response) => {
  return response.headers.set('X-Custom-Header', 'custom-value');
});

fetchWithCustomHeader('https://example.com').then((response) => {
  console.log(response.headers.get('X-Custom-Header')); // 'custom-value'
});

Modifying the response body

import decorateFetchResponse from '@windyroad/decorate-fetch-response';

const fetchWithCustomBody = decorateFetchResponse(async (response) => {
  const body = await response.text();
  return new Response('custom-body', {
    status: response.status,
    statusText: response.statusText,
    headers: response.headers,
  });
});

fetchWithCustomBody('https://example.com').then((response) => {
  response.text().then((body) => {
    console.log(body); // 'custom-body'
  });
});

Handling errors

import decorateFetchResponse from '@windyroad/decorate-fetch-response';

const fetchWithCustomError = decorateFetchResponse(async (response) => {
  if (response.status === 404) {
    return new Response('custom-error', {
      status: 404,
      statusText: 'Not Found',
    });
  }
  return response;
});

fetchWithCustomError('https://example.com/not-found').then((response) => {
  response.text().then((body) => {
    console.log(response.status); // 404
    console.log(body); // 'custom-error'
  });
});

API

decorateFetchResponse(decorator, fetchImpl?)

function decorateFetchResponse<
  DecoratorReturns = Awaited<ReturnType<typeof fetch>>,
  FetchFunction<Arguments, ResponseType> extends (...args: any) => Promise<any> = typeof fetch,
>(
  decorator: (
    response: FetchReturns<FetchFunction<Arguments, ResponseType>>,
  ) => Promise<DecoratorReturns> | DecoratorReturns,
  fetchImpl?: FetchFunction<Arguments, ResponseType>,
): (...args: FetchParameters<FetchFunction<Arguments, ResponseType>>) => Promise<DecoratorReturns>

Decorates the response of a fetch request with additional properties.

Parameters

  • decorator: The decorator function to apply to the response.
  • fetchImpl: The fetch implementation to use. Defaults to the global fetch function.

Type Parameters

  • DecoratorReturns: The return type of the decorator function. Defaults to Awaited<ReturnType<typeof fetch>>.
  • FetchFunction<Arguments, ResponseType>: The type of the fetch implementation. Defaults to typeof fetch.

Returns

A decorated version of the fetch implementation.

decorateFetchResponseUsingInputs(decorator, fetchImpl?)

Decorates the response of a fetch request with additional properties.

Type parameters

  • FetchFunction<Arguments, ResponseType>: The type of the fetch implementation.
  • DecoratorReturns: The return type of the decorator function.

Parameters

  • decorator: The decorator function to apply to the response.
    • Type: (response: ResponseType, ...arguments_: Arguments) => Promise<DecoratorReturns> | DecoratorReturns
    • The response parameter is the response object returned by the fetch function.
    • The arguments_ parameter is an array of arguments passed to the fetch function.
    • The function should return a new response object or a promise that resolves to a new response object.
  • fetchImpl (optional): The fetch implementation to use.
    • Type: FetchFunction<Arguments, ResponseType>
    • Default: fetch

Returns

A decorated version of the fetch implementation.

Contributing

Contributions are welcome! Please read the contributing guidelines for more information.

License

@windyroad/decorate-fetch-response is lovingly licensed under the MIT License. ❤️