UNPKG

@fly/cdn

Version:
42 lines (41 loc) 1.19 kB
/** * A library for composing `fetch` generators into a single pipeline. * * @preferred * @module HTTP */ import { FetchGenerator, FetchFunction } from "./fetch"; /** * PipeplineStage can either be a FetchGenerator function, or a tuple of * FetchGenerator + args. */ export declare type PipelineStage = FetchGenerator | [FetchGenerator, any[]]; /** * Combine multiple fetches into a single function. Allows middleware type functionality * * Example: * * ```javascript * import { pipeline } from "@fly/fetch/pipeline" * * const addHeader = function(fetch){ * return function(req, init){ * if(typeof req === "string") req = new Request(req, init) * req.headers.set("Superfly-Header", "shazam") * return fetch(req, init) * } * } * * const p = pipeline(fetch, addHeader) * * fly.http.respondWith(p) * * @param stages fetch generator functions that apply additional logic * @returns a combinedfunction that can be used anywhere that wants `fetch` */ export declare function pipeline(...stages: PipelineStage[]): ((fetch: FetchFunction) => FetchFunction & { stages: PipelineStage[]; }) & { stages: PipelineStage[]; }; export default pipeline;