Git Product home page Git Product logo

ultrafetch's Introduction

ultrafetch

Modular utilities for enhancing fetch behavior. Bring Your Own Fetch implementation supports both node-fetch and unidici's fetch (globally available in node@18+).

withCache

The withCache function enhances fetch with RFC-7234 compliant cache behavior. The default cache is a simple in-memory Map, but custom caches are also supported.

import { withCache } from "ultrafetch";

const enhancedFetch = withCache(fetch);

Any custom cache that adheres to a Map interface is valid.

import { withCache } from "ultrafetch";

class MyCache implements Map {
  clear(): void;
  delete(key: K): boolean;
  get(key: K): V| undefined>;
  has(key: K): boolean;
  set(key: K, value: V): this;
  readonly size: number;
}

const enhancedFetch = withCache(fetch, { cache: new MyCache() });

Custom caches can also use the AsyncMap interface, which is the same as a standard Map but each method is async.

import type { AsyncMap } from "ultrafetch";
import { withCache } from "ultrafetch";

class MyAsyncCache implements AsyncMap {
  clear(): Promise<void>;
  delete(key: K): Promise<boolean>;
  get(key: K): Promise<V | undefined>;
  has(key: K): Promise<boolean>;
  set(key: K, value: V): Promise<this>;
  readonly size: number;
}

const enhancedFetch = withCache(fetch, { cache: new MyAsyncCache() });

isCached

The isCached export can be used to determine if a given Response was returned from the cache or not.

import { withCache, isCached } from "ultrafetch";

const enhancedFetch = withCache(fetch);
const responseA = await enhancedFetch('https://example.com');
isCached(responseA) // false
const responseB = await enhancedFetch('https://example.com');
isCached(responseB) // true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.