Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ function defaultCacheOptions() {
*/
export function defineCachedHandler<E extends HTTPEvent = HTTPEvent>(
handler: EventHandler<E>,
opts: CachedEventHandlerOptions<E> = defaultCacheOptions() as CachedEventHandlerOptions<E>,
opts: CachedEventHandlerOptions<E> = {},
): EventHandler<E> {
opts = { ...defaultCacheOptions(), ...opts };

const variableHeaderNames = (opts.varies || [])
.filter(Boolean)
.map((h) => h.toLowerCase())
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,16 @@ describe("defineCachedHandler", () => {
expect(res.status).toBe(200);
});

it("merges default options when partial opts are provided", async () => {
const path = uniquePath();
const handler = defineCachedHandler(() => new Response("ok"), { maxAge: 60 });

const res = (await handler(makeEvent(path))) as Response;
const cc = res.headers.get("cache-control")!;
expect(cc).toContain("s-maxage=60");
expect(cc).toContain("stale-while-revalidate");
});

it("works with generic event type", async () => {
interface CustomEvent extends HTTPEvent {
custom: string;
Expand Down
Loading