From 1c7484bca5b2b56da8e174186bf62c362624c474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Mon, 24 Feb 2025 08:23:15 -0800 Subject: [PATCH] feat: add `getContext` method --- package.json | 2 -- packages/cache/src/bootstrap/cache.test.ts | 16 +++------- packages/cache/src/bootstrap/cache.ts | 33 ++++++++++----------- packages/cache/src/bootstrap/environment.ts | 13 +++++--- packages/cache/src/bootstrap/main.ts | 7 +---- packages/cache/src/fetchwithcache.test.ts | 4 +-- 6 files changed, 31 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 87147a12..a0308dfb 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,6 @@ "prepare": "husky install", "lint": "eslint --cache --report-unused-disable-directives --ignore-path .gitignore --max-warnings=0 . --fix", "lint:ci": "eslint --cache --report-unused-disable-directives --ignore-path .gitignore --max-warnings=0 .", - "build": "npm run build", - "test": "npm run test", "format": "prettier --write .", "format:ci": "prettier --check ." }, diff --git a/packages/cache/src/bootstrap/cache.test.ts b/packages/cache/src/bootstrap/cache.test.ts index edb9b1d6..36ddf102 100644 --- a/packages/cache/src/bootstrap/cache.test.ts +++ b/packages/cache/src/bootstrap/cache.test.ts @@ -37,9 +37,7 @@ describe('Cache API', () => { }) const cache = new NetlifyCache({ base64Encode, - getHost: () => host, - getToken: () => token, - getURL: () => url, + getContext: () => ({ host, token, url }), name: 'my-cache', userAgent, }) @@ -86,9 +84,7 @@ describe('Cache API', () => { }) const cache = new NetlifyCache({ base64Encode, - getHost: () => host, - getToken: () => token, - getURL: () => url, + getContext: () => ({ host, token, url }), name: 'my-cache', userAgent, }) @@ -129,9 +125,7 @@ describe('Cache API', () => { }) const cache = new NetlifyCache({ base64Encode, - getHost: () => host, - getToken: () => token, - getURL: () => url, + getContext: () => ({ host, token, url }), name: 'my-cache', userAgent, }) @@ -172,9 +166,7 @@ describe('Cache API', () => { }) const cache = new NetlifyCache({ base64Encode, - getHost: () => host, - getToken: () => token, - getURL: () => url, + getContext: () => ({ host, token, url }), name: 'my-cache', userAgent, }) diff --git a/packages/cache/src/bootstrap/cache.ts b/packages/cache/src/bootstrap/cache.ts index 3ee2a365..917026ef 100644 --- a/packages/cache/src/bootstrap/cache.ts +++ b/packages/cache/src/bootstrap/cache.ts @@ -1,4 +1,4 @@ -import type { Base64Encoder, EnvironmentOptions, Factory } from './environment.js' +import type { Base64Encoder, EnvironmentOptions, RequestContext, RequestContextFactory } from './environment.js' import * as HEADERS from '../headers.js' @@ -17,28 +17,24 @@ const serializeResourceHeaders = Symbol('serializeResourceHeaders') export class NetlifyCache implements Cache { #base64Encode: Base64Encoder - #getHost?: Factory - #getToken: Factory - #getURL: Factory + #getContext: RequestContextFactory #name: string #userAgent?: string - constructor({ base64Encode, getHost, getToken, getURL, name, userAgent }: NetlifyCacheOptions) { + constructor({ base64Encode, getContext, name, userAgent }: NetlifyCacheOptions) { this.#base64Encode = base64Encode - this.#getHost = getHost - this.#getToken = getToken - this.#getURL = getURL + this.#getContext = getContext this.#name = name this.#userAgent = userAgent } - private [getInternalHeaders]() { + private [getInternalHeaders](requestContext: RequestContext) { + const { host, token } = requestContext const headers: Record = { - Authorization: `Bearer ${this.#getToken()}`, + Authorization: `Bearer ${token}`, [HEADERS.ResourceStore]: this.#name, } - const host = this.#getHost?.() if (host) { headers[HEADERS.NetlifyForwardedHost] = host } @@ -82,10 +78,11 @@ export class NetlifyCache implements Cache { // eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars async delete(request: RequestInfo) { + const context = this.#getContext() const resourceURL = extractAndValidateURL(request) - await fetch(`${this.#getURL()}/${toCacheKey(resourceURL)}`, { - headers: this[getInternalHeaders](), + await fetch(`${context.url}/${toCacheKey(resourceURL)}`, { + headers: this[getInternalHeaders](context), method: 'DELETE', }) @@ -100,10 +97,11 @@ export class NetlifyCache implements Cache { async match(request: RequestInfo) { try { + const context = this.#getContext() const resourceURL = extractAndValidateURL(request) - const cacheURL = `${this.#getURL()}/${toCacheKey(resourceURL)}` + const cacheURL = `${context.url}/${toCacheKey(resourceURL)}` const response = await fetch(cacheURL, { - headers: this[getInternalHeaders](), + headers: this[getInternalHeaders](context), method: 'GET', }) @@ -145,12 +143,13 @@ export class NetlifyCache implements Cache { throw new TypeError("Cannot cache response with 'Vary: *' header.") } + const context = this.#getContext() const resourceURL = extractAndValidateURL(request) - await fetch(`${this.#getURL()}/${toCacheKey(resourceURL)}`, { + await fetch(`${context.url}/${toCacheKey(resourceURL)}`, { body: response.body, headers: { - ...this[getInternalHeaders](), + ...this[getInternalHeaders](context), [HEADERS.ResourceHeaders]: this[serializeResourceHeaders](response.headers), [HEADERS.ResourceStatus]: response.status.toString(), }, diff --git a/packages/cache/src/bootstrap/environment.ts b/packages/cache/src/bootstrap/environment.ts index 18e1d37c..e721a0e7 100644 --- a/packages/cache/src/bootstrap/environment.ts +++ b/packages/cache/src/bootstrap/environment.ts @@ -1,10 +1,15 @@ export type Base64Encoder = (input: string) => string -export type Factory = () => T export interface EnvironmentOptions { base64Encode: Base64Encoder - getHost: Factory - getToken: Factory - getURL: Factory + getContext: RequestContextFactory userAgent?: string } + +export type RequestContextFactory = () => RequestContext + +export interface RequestContext { + host: string + token: string + url: string +} diff --git a/packages/cache/src/bootstrap/main.ts b/packages/cache/src/bootstrap/main.ts index d46b216e..ba934aaa 100644 --- a/packages/cache/src/bootstrap/main.ts +++ b/packages/cache/src/bootstrap/main.ts @@ -1,8 +1,3 @@ -import { Base64Encoder, Factory } from './environment.ts' - -export type { Base64Encoder } -export type HostFactory = Factory -export type TokenFactory = Factory -export type URLFactory = Factory +export type { Base64Encoder, RequestContextFactory } from './environment.ts' export { NetlifyCache } from './cache.ts' export { NetlifyCacheStorage } from './cachestorage.ts' diff --git a/packages/cache/src/fetchwithcache.test.ts b/packages/cache/src/fetchwithcache.test.ts index a4da18e0..e1e945db 100644 --- a/packages/cache/src/fetchwithcache.test.ts +++ b/packages/cache/src/fetchwithcache.test.ts @@ -20,9 +20,7 @@ let originalCaches = globalThis.caches beforeAll(() => { globalThis.caches = new NetlifyCacheStorage({ base64Encode, - getHost: () => host, - getToken: () => token, - getURL: () => url, + getContext: () => ({ host, token, url }), }) })