@composite-fetcher/core
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -37,3 +37,3 @@ // src/interfaces/index.ts | ||
const isPostRequestContext = this.isPostRequestContext(context); | ||
const next = () => { | ||
const next = async () => { | ||
if (index >= this.plugins.length) { | ||
@@ -64,6 +64,13 @@ if (isPreRequestContext) { | ||
}; | ||
Promise.resolve(plugin[method]?.(context)).catch((error) => { | ||
const result = await plugin[method]?.(context).catch((error) => { | ||
console.error(`Error in plugin: ${error}`); | ||
clearTimeout(timeout); | ||
context.next(); | ||
}); | ||
if (result instanceof Response) { | ||
clearTimeout(timeout); | ||
resolve(result); | ||
return; | ||
} | ||
context.next(); | ||
}; | ||
@@ -124,3 +131,2 @@ next(); | ||
} | ||
// test | ||
async fetch(input, init) { | ||
@@ -131,2 +137,5 @@ const originalRequest = new Request(input, init); | ||
); | ||
if (modifiedRequest instanceof Response) { | ||
return modifiedRequest; | ||
} | ||
const response = await fetch(modifiedRequest); | ||
@@ -133,0 +142,0 @@ const modifiedResponse = await this.pluginManager.runPostRequestHooks( |
@@ -11,3 +11,3 @@ declare class PluginManager { | ||
private processPlugins; | ||
runPreRequestHooks(request: Request): Promise<Request>; | ||
runPreRequestHooks(request: Request): Promise<Request | Response>; | ||
runPostRequestHooks(response: Response, originalRequest: Request): Promise<Response>; | ||
@@ -38,4 +38,4 @@ getModifiedRequest(): Request; | ||
pluginTimeout?: number; | ||
onPreRequest?: (context: PluginHandlerContext<PluginLifecycleHook.PRE_REQUEST>) => Promise<void>; | ||
onPostRequest?: (context: PluginHandlerContext<PluginLifecycleHook.POST_REQUEST>) => Promise<void>; | ||
onPreRequest?: (context: PluginHandlerContext<PluginLifecycleHook.PRE_REQUEST>) => Promise<void | Response>; | ||
onPostRequest?: (context: PluginHandlerContext<PluginLifecycleHook.POST_REQUEST>) => Promise<void | Response>; | ||
} | ||
@@ -51,6 +51,6 @@ | ||
pluginTimeout?: number; | ||
onPreRequest(context: PluginHandlerContext<PluginLifecycleHook.PRE_REQUEST>): Promise<void>; | ||
onPostRequest(context: PluginHandlerContext<PluginLifecycleHook.POST_REQUEST>): Promise<void>; | ||
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 }; |
@@ -37,3 +37,3 @@ // src/interfaces/index.ts | ||
const isPostRequestContext = this.isPostRequestContext(context); | ||
const next = () => { | ||
const next = async () => { | ||
if (index >= this.plugins.length) { | ||
@@ -64,6 +64,13 @@ if (isPreRequestContext) { | ||
}; | ||
Promise.resolve(plugin[method]?.(context)).catch((error) => { | ||
const result = await plugin[method]?.(context).catch((error) => { | ||
console.error(`Error in plugin: ${error}`); | ||
clearTimeout(timeout); | ||
context.next(); | ||
}); | ||
if (result instanceof Response) { | ||
clearTimeout(timeout); | ||
resolve(result); | ||
return; | ||
} | ||
context.next(); | ||
}; | ||
@@ -124,3 +131,2 @@ next(); | ||
} | ||
// test | ||
async fetch(input, init) { | ||
@@ -131,2 +137,5 @@ const originalRequest = new Request(input, init); | ||
); | ||
if (modifiedRequest instanceof Response) { | ||
return modifiedRequest; | ||
} | ||
const response = await fetch(modifiedRequest); | ||
@@ -133,0 +142,0 @@ const modifiedResponse = await this.pluginManager.runPostRequestHooks( |
{ | ||
"name": "@composite-fetcher/core", | ||
"description": "Fetcher core", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"author": "Teofanis Papadopulos", | ||
@@ -6,0 +6,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21273
557