cf-workers-query
Advanced tools
Comparing version 0.5.4 to 0.6.0
@@ -0,1 +1,13 @@ | ||
## 0.6.0 (2024-09-02) | ||
### 🚀 Features | ||
- **cache-api:** better handling receive a response in data ([332f306](https://github.com/anymaniax/cf-workers-query/commit/332f306)) | ||
- **create-query:** add enabled option ([b15c891](https://github.com/anymaniax/cf-workers-query/commit/b15c891)) | ||
### ❤️ Thank You | ||
- Victor Bury | ||
## 0.5.4 (2024-09-02) | ||
@@ -2,0 +14,0 @@ |
{ | ||
"name": "cf-workers-query", | ||
"version": "0.5.4", | ||
"version": "0.6.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Automatically cache and revalidate data in Cloudflare Workers. Using the Cache API and Execution Context", |
@@ -15,5 +15,3 @@ export declare const CACHE_URL = "INTERNAL_CF_WORKERS_QUERY_CACHE_HOSTNAME.local"; | ||
}); | ||
retrieve<Data = unknown>(key: QueryKey, options?: { | ||
raw?: boolean; | ||
}): Promise<CachePayload<Data> | null>; | ||
retrieve<Data = unknown>(key: QueryKey): Promise<CachePayload<Data> | null>; | ||
update<Data = unknown>(key: QueryKey, value: Data | Response, options?: { | ||
@@ -20,0 +18,0 @@ maxAge?: number; |
export const CACHE_URL = 'INTERNAL_CF_WORKERS_QUERY_CACHE_HOSTNAME.local'; | ||
const HEADER_RAW = 'cf-workers-query-raw'; | ||
const getVoidCache = () => { | ||
@@ -26,5 +27,4 @@ console.warn('No caches API available'); | ||
} | ||
async retrieve(key, options) { | ||
async retrieve(key) { | ||
try { | ||
const { raw = false } = options ?? {}; | ||
const cache = await getCache(this.cacheName); | ||
@@ -36,3 +36,7 @@ const cacheKey = key instanceof URL ? key : this.buildCacheKey(key); | ||
} | ||
const raw = response.headers.get(HEADER_RAW) === 'true'; | ||
const data = (raw ? response : await response.json()); | ||
if (raw) { | ||
response.headers.delete(HEADER_RAW); | ||
} | ||
const cacheControlHeader = response.headers.get('cache-control'); | ||
@@ -60,2 +64,3 @@ const dateHeader = response.headers.get('date'); | ||
response.headers.append('cache-control', `max-age=${maxAge}`); | ||
response.headers.set(HEADER_RAW, 'true'); | ||
await cache.put(cacheKey, response); | ||
@@ -62,0 +67,0 @@ return; |
@@ -15,5 +15,5 @@ import { ExecutionContext } from './context'; | ||
throwOnError?: boolean; | ||
raw?: boolean; | ||
enabled?: boolean | ((data: Data) => boolean); | ||
}; | ||
export declare const createQuery: <Data = unknown, Error = unknown>({ queryKey, queryFn, gcTime, staleTime, revalidate, retry, retryDelay, executionCtx, cacheName, throwOnError, raw, }: CreateQuery<Data, Error>) => Promise<{ | ||
export declare const createQuery: <Data = unknown, Error = unknown>({ queryKey, queryFn, gcTime, staleTime, revalidate, retry, retryDelay, executionCtx, cacheName, throwOnError, enabled, }: CreateQuery<Data, Error>) => Promise<{ | ||
data: Data | null; | ||
@@ -20,0 +20,0 @@ error: Error | null; |
import { getCFExecutionContext } from './context'; | ||
import { CacheApiAdaptor } from './cache-api'; | ||
import { nanoid } from 'nanoid'; | ||
export const createQuery = async ({ queryKey, queryFn, gcTime, staleTime, revalidate, retry, retryDelay, executionCtx, cacheName, throwOnError, raw, }) => { | ||
export const createQuery = async ({ queryKey, queryFn, gcTime, staleTime, revalidate, retry, retryDelay, executionCtx, cacheName, throwOnError, enabled = true, }) => { | ||
try { | ||
if (!queryKey) { | ||
if (!queryKey || !enabled) { | ||
const data = await queryFn(); | ||
@@ -13,5 +13,8 @@ return { data, error: null, invalidate: () => undefined }; | ||
const invalidate = () => cache.delete(cacheKey); | ||
const context = executionCtx ?? getCFExecutionContext(); | ||
if (!revalidate) { | ||
const cachedData = await cache.retrieve(cacheKey, { raw }); | ||
const context = (executionCtx ?? getCFExecutionContext()); | ||
if (context && !!staleTime) { | ||
console.warn('Context not found, staleTime will be ignored'); | ||
} | ||
if (!revalidate && staleTime !== 0) { | ||
const cachedData = await cache.retrieve(cacheKey); | ||
if (cachedData?.data) { | ||
@@ -52,3 +55,10 @@ const isStale = staleTime && cachedData.lastModified + staleTime * 1000 < Date.now(); | ||
} | ||
await cache.update(cacheKey, data); | ||
if (typeof enabled !== 'function' || enabled(data)) { | ||
if (context) { | ||
context.waitUntil(cache.update(cacheKey, data)); | ||
} | ||
else { | ||
await cache.update(cacheKey, data); | ||
} | ||
} | ||
return { data, error: null, invalidate }; | ||
@@ -55,0 +65,0 @@ } |
@@ -10,3 +10,2 @@ import { createQuery } from './create-query'; | ||
throwOnError: true, | ||
raw: true, | ||
...(revalidate | ||
@@ -13,0 +12,0 @@ ? { |
23292
357