cf-workers-query
Version:
Automatically cache and revalidate data in Cloudflare Workers. Using the Cache API and Execution Context
28 lines (26 loc) • 771 B
JavaScript
import {
createQuery
} from "../chunk-X36KAMCL.js";
import {
__name
} from "../chunk-SHUYVCID.js";
// src/lib/hono.ts
import { HTTPException } from "hono/http-exception";
var cache = /* @__PURE__ */ __name(({ cacheKey, handler, revalidate, ...options }) => async (ctx) => {
const { data: response, error } = await createQuery({
...options,
queryKey: typeof cacheKey === "function" ? cacheKey(ctx) : cacheKey,
queryFn: /* @__PURE__ */ __name(() => Promise.resolve(handler(ctx)), "queryFn"),
throwOnError: true,
...revalidate ? {
revalidate: typeof revalidate === "boolean" ? revalidate : revalidate(ctx)
} : {}
});
if (!response || error) {
throw new HTTPException(500);
}
return response;
}, "cache");
export {
cache
};