UNPKG

@composite-fetcher/core

Version:

Fetcher core

54 lines (49 loc) 2.19 kB
declare class PluginManager { private plugins; private requestIdCounter; private modifiedRequest; private processingContext; private modifiedResponse; private processedHooks; addPlugins(plugin: Plugin | Plugin[]): void; private hasBeenProcessed; private isPreRequestContext; private isPostRequestContext; private processPlugins; runPreRequestHooks(requestId: number, request: Request): Promise<Request | Response>; runPostRequestHooks(requestId: number, response: Response, originalRequest: Request): Promise<Response>; getModifiedRequest(requestId: number): Request; getModifiedResponse(requestId: number): Response; getPlugins(): Plugin[]; generateNewRequestId(): number; clearProcessedPlugins(requestId: number): void; } declare enum PluginLifecycleHook { PRE_REQUEST = "preRequest", POST_REQUEST = "postRequest" } type PreRequestPluginHandlerContext = { request: Request; originalRequest: Request; pluginManager: PluginManager; }; type PostRequestPluginHandlerContext = { response: Response; originalRequest: Request; pluginManager: PluginManager; }; type PluginHandlerContext<T> = T extends PluginLifecycleHook.PRE_REQUEST ? PreRequestPluginHandlerContext : T extends PluginLifecycleHook.POST_REQUEST ? PostRequestPluginHandlerContext : never; interface Plugin { onPreRequest?: (context: PluginHandlerContext<PluginLifecycleHook.PRE_REQUEST>) => Promise<void | Response>; onPostRequest?: (context: PluginHandlerContext<PluginLifecycleHook.POST_REQUEST>) => Promise<void | Response>; } declare class Fetcher { private pluginManager; use(plugin: Plugin | Plugin[]): void; fetch(input: RequestInfo, init?: RequestInit): Promise<Response>; } declare abstract class BasePlugin implements Plugin { onPreRequest(context: PluginHandlerContext<PluginLifecycleHook.PRE_REQUEST>): Promise<void | Response>; onPostRequest(context: PluginHandlerContext<PluginLifecycleHook.POST_REQUEST>): Promise<void | Response>; } export { BasePlugin, Fetcher, Plugin, PluginHandlerContext, PluginLifecycleHook, PostRequestPluginHandlerContext, PreRequestPluginHandlerContext };