a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
at trunk 30 lines 831 B view raw
1/** fetch handler function */ 2export type FetchHandler = (pathname: string, init: RequestInit) => Promise<Response>; 3 4/** fetch handler in an object */ 5export interface FetchHandlerObject { 6 handle(this: FetchHandlerObject, pathname: string, init: RequestInit): Promise<Response>; 7} 8 9export const buildFetchHandler = (handler: FetchHandler | FetchHandlerObject): FetchHandler => { 10 if (typeof handler === 'object') { 11 return handler.handle.bind(handler); 12 } 13 14 return handler; 15}; 16 17export interface SimpleFetchHandlerOptions { 18 service: string | URL; 19 fetch?: typeof globalThis.fetch; 20} 21 22export const simpleFetchHandler = ({ 23 service, 24 fetch: _fetch = fetch, 25}: SimpleFetchHandlerOptions): FetchHandler => { 26 return async (pathname, init) => { 27 const url = new URL(pathname, service); 28 return await _fetch(url.href, init); 29 }; 30};