Skip to content

Commit 4017d6e

Browse files
feat: add logger option (#96)
Release-As: 1.7.0
1 parent ecc737e commit 4017d6e

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

packages/cache/src/bootstrap/cache.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ describe('Cache API', () => {
256256
})
257257

258258
test('logs a message when the response is not added to the cache', async () => {
259-
const consoleWarn = vi.spyOn(globalThis.console, 'warn')
259+
const logger = vi.fn()
260260
const mockFetch = getMockFetch({
261261
responses: {
262262
'https://example.netlify/.netlify/cache/https%3A%2F%2Fnetlify.com%2F': [
@@ -274,6 +274,7 @@ describe('Cache API', () => {
274274
const cache = new NetlifyCache({
275275
base64Encode,
276276
getContext: () => ({ host, token, url }),
277+
logger,
277278
name: 'my-cache',
278279
userAgent,
279280
})
@@ -288,8 +289,7 @@ describe('Cache API', () => {
288289

289290
mockFetch.restore()
290291

291-
expect(consoleWarn).toHaveBeenCalledWith(`Failed to write to the cache: ${ERROR_CODES.no_ttl}`)
292-
consoleWarn.mockRestore()
292+
expect(logger).toHaveBeenCalledWith(`Failed to write to the cache: ${ERROR_CODES.no_ttl}`)
293293

294294
expect(mockFetch.requests.length).toBe(1)
295295
})

packages/cache/src/bootstrap/cache.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Base64Encoder, EnvironmentOptions, RequestContext, Operation, RequestContextFactory } from './environment.js'
1+
import {
2+
Base64Encoder,
3+
EnvironmentOptions,
4+
Logger,
5+
RequestContext,
6+
Operation,
7+
RequestContextFactory,
8+
} from './environment.js'
29

310
import { ERROR_CODES, GENERIC_ERROR } from './errors.js'
411
import * as HEADERS from '../headers.js'
@@ -19,12 +26,14 @@ const serializeResourceHeaders = Symbol('serializeResourceHeaders')
1926
export class NetlifyCache implements Cache {
2027
#base64Encode: Base64Encoder
2128
#getContext: RequestContextFactory
29+
#logger?: Logger
2230
#name: string
2331
#userAgent?: string
2432

25-
constructor({ base64Encode, getContext, name, userAgent }: NetlifyCacheOptions) {
33+
constructor({ base64Encode, getContext, logger, name, userAgent }: NetlifyCacheOptions) {
2634
this.#base64Encode = base64Encode
2735
this.#getContext = getContext
36+
this.#logger = logger
2837
this.#name = name
2938
this.#userAgent = userAgent
3039
}
@@ -176,7 +185,7 @@ export class NetlifyCache implements Cache {
176185
const errorDetail = cacheResponse.headers.get(HEADERS.ErrorDetail) ?? ''
177186
const errorMessage = ERROR_CODES[errorDetail as keyof typeof ERROR_CODES] || GENERIC_ERROR
178187

179-
console.warn(`Failed to write to the cache: ${errorMessage}`)
188+
this.#logger?.(`Failed to write to the cache: ${errorMessage}`)
180189
}
181190
}
182191
}

packages/cache/src/bootstrap/environment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
export type Base64Encoder = (input: string) => string
22

3+
export type Logger = (...args: any[]) => void
4+
35
export interface EnvironmentOptions {
46
base64Encode: Base64Encoder
57
getContext: RequestContextFactory
8+
logger?: Logger
69
userAgent?: string
710
}
811

0 commit comments

Comments
 (0)