UNPKG

@sudowealth/schwab-api

Version:

TypeScript client for Charles Schwab API with OAuth support, market data, trading functionality, and complete type safety

23 lines (22 loc) 998 B
export function compose(...mws) { return (initial) => { // Ensure fetch is available in the global scope or polyfilled const effectiveFetch = typeof fetch !== 'undefined' ? fetch : globalThis.fetch; if (!effectiveFetch) { throw new Error('Global fetch is not available. Please provide a fetch polyfill.'); } // Create a fetch function to use as the final handler in the middleware chain const fetchHandler = (req) => { return effectiveFetch(req); }; // Build middleware chain from right to left (last to first) const handler = mws.reduceRight((nextHandler, middleware) => { // Create a new handler that applies this middleware with the next handler return (req) => { return middleware(req, nextHandler); }; }, fetchHandler); // Execute the composed handler chain with the initial request return handler(initial); }; }