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

wretch-middlewares

v0.1.13

Published

Middlewares for the wretch library

Downloads

3,020

Readme

Installation

Prerequisite: install Wretch

Npm

npm i wretch-middlewares

Cdn

<!-- Global variable name : window.wretchMiddlewares -->
<script src="https://unpkg.com/wretch-middlewares"></script>

Middlewares

| Dedupe | RetryThrottling cacheDelay | |-----|-----|-----|-----|

Dedupe

Prevents having multiple identical requests on the fly at the same time.

Options

  • skip : (url, opts) => boolean

If skip returns true, then the dedupe check is skipped.

  • key : (url, opts) => string

Returns a key that is used to identify the request.

  • resolver : (response: Response) => Response

This function is called when resolving the fetch response from duplicate calls. By default it clones the response to allow reading the body from multiple sources.

Usage

import wretch from 'wretch'
import { dedupe } from 'wretch-middlewares'

wretch().middlewares([
    dedupe({
        /* Options - defaults below */
        skip: (url, opts) => opts.skipDedupe || opts.method !== 'GET',
        key: (url, opts) => opts.method + '@' + url,
        resolver: response => response.clone()
    })
])./* ... */

Retry

Retries a request multiple times in case of an error (or until a custom condition is true).

Options

  • delayTimer : milliseconds

The timer between each attempt.

  • delayRamp : (delay, nbOfAttempts) => milliseconds

The custom function that is used to calculate the actual delay based on the the timer & the number of attemps.

  • maxAttempts : number

The maximum number of retries before resolving the promise with the last error. Specifying 0 means infinite retries.

  • until : (fetchResponse, error) => boolean | Promise<boolean>

The request will be retried until that condition is satisfied.

  • onRetry : ({ response, error, url, options }) => { url?, options? } || Promise<{url?, options?}>

Callback that will get executed before retrying the request. If this function returns an object having url and/or options properties, they will override existing values in the retried request. If it returns a Promise, it will be awaited before retrying the request.

  • retryOnNetworkError : boolean

If true, will retry the request if a network error was thrown. Will also provide an 'error' argument to the onRetry and until methods.

  • resolver : (response: Response) => Response

This function is called when resolving the fetch response from duplicate calls. By default it clones the response to allow reading the body from multiple sources.

Usage

import wretch from 'wretch'
import { retry } from 'wretch-middlewares'

wretch().middlewares([
    retry({
        /* Options - defaults below */
        delayTimer: 500,
        delayRamp: (delay, nbOfAttempts) => delay * nbOfAttempts,
        maxAttempts: 10,
        until: (response, error) => response && response.ok,
        onRetry: null,
        retryOnNetworkError: false,
        resolver: response => response.clone()
    })
])./* ... */

// You can also return a Promise, which is useful if you want to inspect the body:
wretch().middlewares([
    retry({
        until: response =>
            response.json().then(body =>
                body.field === 'something'
            )
    })
])

Throttling cache

A throttling cache which stores and serves server responses for a certain amount of time.

Options

  • throttle : milliseconds

The response will be stored for this amount of time before being deleted from the cache.

  • skip : (url, opts) => boolean

If skip returns true, then the dedupe check is skipped.

  • key : (url, opts) => string

Returns a key that is used to identify the request.

  • clear (url, opts) => boolean

Clears the cache if true.

  • invalidate (url, opts) => string | RegExp | Array<string | RegExp> | null

Removes url(s) matching the string or RegExp from the cache. Can use an array to invalidate multiple values.

  • condition response => boolean

If false then the response will not be added to the cache.

  • flagResponseOnCacheHit string

If set, a Response returned from the cache whill be flagged with a property name equal to this option.

Usage

import wretch from 'wretch'
import { throttlingCache } from 'wretch-middlewares'

wretch().middlewares([
    throttlingCache({
        /* Options - defaults below */
        throttle: 1000,
        skip: (url, opts) => opts.skipCache || opts.method !== 'GET',
        key: (url, opts) => opts.method + '@' + url,
        clear: (url, opts) => false,
        invalidate: (url, opts) => null,
        condition: response => response.ok,
        flagResponseOnCacheHit: '__cached'
    })
])./* ... */

Delay

Delays the request by a specific amount of time.

Options

  • time : milliseconds

The request will be delayed by that amount of time.

Usage

import wretch from 'wretch'
import { delay } from 'wretch-middlewares'

wretch().middlewares([
    delay(1000)
])./* ... */