@composite-fetcher/with-caching
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -40,2 +40,3 @@ // src/drivers/InMemoryCacheDriver.ts | ||
return { | ||
url: response.url, | ||
status: response.status, | ||
@@ -60,2 +61,3 @@ statusText: response.statusText, | ||
const item = { | ||
url: response.url, | ||
value: await response.clone().text(), | ||
@@ -105,4 +107,5 @@ headers: Array.from(response.headers.entries()), | ||
// 10 minutes | ||
constructor(cacheDriver, defaultTTL) { | ||
constructor(options = {}) { | ||
super(); | ||
const { cacheDriver, defaultTTL } = options; | ||
this.cacheDriver = cacheDriver || new InMemoryCacheDriver(); | ||
@@ -124,4 +127,3 @@ if (defaultTTL) { | ||
async onPreRequest(context) { | ||
if (context.request.headers.has("x-no-cache")) { | ||
context.next(); | ||
if (context.request.headers.has("x-fetcher-no-cache")) { | ||
return; | ||
@@ -136,8 +138,7 @@ } | ||
} | ||
context.next(); | ||
} | ||
async onPostRequest(context) { | ||
const { response, originalRequest } = context; | ||
if (!originalRequest.headers.has("x-no-cache") && response.ok) { | ||
const cacheTTL = originalRequest.headers.has("x-cache-ttl") ? Number(originalRequest.headers.get("x-cache-ttl")) : this.defaultTTL; | ||
if (!originalRequest.headers.has("x-fetcher-no-cache") && response.ok) { | ||
const cacheTTL = originalRequest.headers.has("x-fetcher-cache-ttl") ? Number(originalRequest.headers.get("x-fetcher-cache-ttl")) : this.defaultTTL; | ||
const cacheKey = await this.generateCacheKey(context.originalRequest); | ||
@@ -150,3 +151,2 @@ await this.cacheDriver.set( | ||
} | ||
context.next(); | ||
} | ||
@@ -153,0 +153,0 @@ }; |
@@ -10,2 +10,6 @@ import { BasePlugin, PluginHandlerContext, PluginLifecycleHook } from '@composite-fetcher/core'; | ||
} | ||
interface withCachingOptions { | ||
cacheDriver?: CacheDriver; | ||
defaultTTL?: number; | ||
} | ||
@@ -35,3 +39,3 @@ declare class InMemoryCacheDriver implements CacheDriver { | ||
private readonly defaultTTL; | ||
constructor(cacheDriver?: CacheDriver, defaultTTL?: number); | ||
constructor(options?: withCachingOptions); | ||
private generateCacheKey; | ||
@@ -43,2 +47,2 @@ private getCacheTTL; | ||
export { CacheDriver, InMemoryCacheDriver, SessionStorageDriver, withCachingPlugin as withCaching }; | ||
export { CacheDriver, InMemoryCacheDriver, SessionStorageDriver, withCachingPlugin as withCaching, withCachingOptions }; |
@@ -40,2 +40,3 @@ // src/drivers/InMemoryCacheDriver.ts | ||
return { | ||
url: response.url, | ||
status: response.status, | ||
@@ -60,2 +61,3 @@ statusText: response.statusText, | ||
const item = { | ||
url: response.url, | ||
value: await response.clone().text(), | ||
@@ -105,4 +107,5 @@ headers: Array.from(response.headers.entries()), | ||
// 10 minutes | ||
constructor(cacheDriver, defaultTTL) { | ||
constructor(options = {}) { | ||
super(); | ||
const { cacheDriver, defaultTTL } = options; | ||
this.cacheDriver = cacheDriver || new InMemoryCacheDriver(); | ||
@@ -124,4 +127,3 @@ if (defaultTTL) { | ||
async onPreRequest(context) { | ||
if (context.request.headers.has("x-no-cache")) { | ||
context.next(); | ||
if (context.request.headers.has("x-fetcher-no-cache")) { | ||
return; | ||
@@ -136,8 +138,7 @@ } | ||
} | ||
context.next(); | ||
} | ||
async onPostRequest(context) { | ||
const { response, originalRequest } = context; | ||
if (!originalRequest.headers.has("x-no-cache") && response.ok) { | ||
const cacheTTL = originalRequest.headers.has("x-cache-ttl") ? Number(originalRequest.headers.get("x-cache-ttl")) : this.defaultTTL; | ||
if (!originalRequest.headers.has("x-fetcher-no-cache") && response.ok) { | ||
const cacheTTL = originalRequest.headers.has("x-fetcher-cache-ttl") ? Number(originalRequest.headers.get("x-fetcher-cache-ttl")) : this.defaultTTL; | ||
const cacheKey = await this.generateCacheKey(context.originalRequest); | ||
@@ -150,3 +151,2 @@ await this.cacheDriver.set( | ||
} | ||
context.next(); | ||
} | ||
@@ -153,0 +153,0 @@ }; |
{ | ||
"name": "@composite-fetcher/with-caching", | ||
"description": "withCaching is a simple fetcher core plugin to manage request caching with different drivers", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Teofanis Papadopulos", | ||
@@ -33,3 +33,3 @@ "type": "module", | ||
"dependencies": { | ||
"@composite-fetcher/core": "^0.1.4" | ||
"@composite-fetcher/core": "^0.1.5" | ||
}, | ||
@@ -36,0 +36,0 @@ "scripts": { |
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
20331
515