• Adds the ability to measure requests using the Performance Timings API.

    Uses the Performance API (browsers & node.js) to expose timings related to the underlying request.

    Browser timings are very accurate, node.js only contains raw measures.

    import PerfsAddon from "wretch/addons/perfs"

    // Use perfs() before the response types (text, json, ...)
    wretch("...")
    .addon(PerfsAddon())
    .get()
    .perfs((timings) => {
    // Will be called when the timings are ready.
    console.log(timings.startTime);
    })
    .res();

    For node.js, there is a little extra work to do :

    // Node.js only
    const { performance, PerformanceObserver } = require("perf_hooks");

    wretch.polyfills({
    fetch: function (url, opts) {
    performance.mark(url + " - begin");
    return fetch(url, opts).then(res => {
    performance.mark(url + " - end");
    setTimeout(() => performance.measure(res.url, url + " - begin", url + " - end"), 0);
    return res;
    });
    },
    // other polyfills…
    performance: performance,
    PerformanceObserver: PerformanceObserver,
    });

    Returns WretchAddon<unknown, PerfsAddon>

Generated using TypeDoc