UNPKG

wretch

Version:

A tiny wrapper built around fetch with an intuitive syntax.

1 lines 3.97 kB
{"version":3,"file":"progress.min.cjs","sources":["../../../src/addons/progress.ts"],"sourcesContent":["import type { ConfiguredMiddleware, WretchAddon, WretchResponseChain } from \"../types.js\"\n\nexport interface ProgressResolver {\n /**\n * Provides a way to register a callback to be invoked one or multiple times during the download.\n * The callback receives the current progress as two arguments, the number of bytes loaded and the total number of bytes to load.\n *\n * _Under the hood: this method adds a middleware to the chain that will intercept the response and replace the body with a new one that will emit the progress event._\n *\n * ```js\n * import ProgressAddon from \"wretch/addons/progress\"\n *\n * wretch(\"some_url\")\n * // Register the addon\n * .addon(ProgressAddon())\n * .get()\n * // Log the progress as a percentage of completion\n * .progress((loaded, total) => console.log(`${(loaded / total * 100).toFixed(0)}%`))\n * ```\n *\n * @param onProgress - A callback that will be called one or multiple times with the number of bytes loaded and the total number of bytes to load.\n */\n progress: <T, C extends ProgressResolver, R>(\n this: C & WretchResponseChain<T, C, R>,\n onProgress: (loaded: number, total: number) => void\n ) => this\n}\n\n/**\n * Adds the ability to monitor progress when downloading a response.\n *\n * _Compatible with all platforms implementing the [TransformStream WebAPI](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream#browser_compatibility)._\n *\n * ```js\n * import ProgressAddon from \"wretch/addons/progress\"\n *\n * wretch(\"some_url\")\n * // Register the addon\n * .addon(ProgressAddon())\n * .get()\n * // Log the progress as a percentage of completion\n * .progress((loaded, total) => console.log(`${(loaded / total * 100).toFixed(0)}%`))\n * ```\n */\nconst progress: () => WretchAddon<unknown, ProgressResolver> = () => {\n const cb = {\n ref: null\n }\n\n const transformMiddleware: ConfiguredMiddleware = next => (url, opts) => {\n let loaded = 0\n let total = 0\n return next(url, opts).then(response => {\n try {\n const contentLength = response.headers.get(\"content-length\")\n total = contentLength ? +contentLength : null\n const transform = new TransformStream({\n transform(chunk, controller) {\n loaded += chunk.length\n if (total < loaded) {\n total = loaded\n }\n if (cb.ref) {\n cb.ref(loaded, total)\n }\n controller.enqueue(chunk)\n }\n })\n return new Response(response.body.pipeThrough(transform), response)\n } catch (e) {\n return response\n }\n })\n }\n\n return {\n beforeRequest(wretch) {\n return wretch._middlewares.push(transformMiddleware)\n },\n resolver: {\n progress(onProgress: (loaded: number, total: number) => void) {\n cb.ref = onProgress\n return this\n }\n },\n }\n}\n\nexport default progress\n"],"names":["cb","ref","transformMiddleware","next","url","opts","loaded","then","response","contentLength","headers","get","total","transform","TransformStream","chunk","controller","enqueue","Response","body","pipeThrough","e","beforeRequest","wretch","_middlewares","push","resolver","progress","onProgress","this"],"mappings":"aA2CG,MAAAA,EAAA,CACGC,IAAA,MAEFC,EAASC,GAAA,CAAAC,EAAAC,KACV,IAAAC,EAAA,MAGC,OAAIH,EAASC,EAACC,GAAAE,MAAAC,IACV,IACO,MAAIC,EAAaD,EAAWE,QAAAC,IAAA,kBACjCC,EAAAH,GAAAA,EAAA,KACF,MAAMI,EAAa,IAAGC,gBAAgB,CACtCD,UAAqBE,EAAAC,GACfV,GAAYS,SACPH,EAAMN,IACPM,EAASN,GAEbN,EAAAC,KACDD,EAAAC,IAAAK,EAAAM,GAECI,EAAOC,QAAQF,EAChB,IAEF,OAAA,IAAAG,SAAAV,EAAAW,KAAAC,YAAAP,GAAAL,EAGH,CADA,MAAOa,GACR,OAAAb,CAAC,IACA,EAEJ,MAAE,CACHc,cAAAC,GAEMA,EAAAC,aAAAC,KAAAvB,GAEHwB,SAAA,CACDC,SAAAC,GAEC,OADM5B,EAAEC,IAAA2B,EACCC,IACP,GAED,SAGPF"}