From b0152f133eb12e79e24199acfc767157e1ff034f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 18 Dec 2024 14:01:00 +0000 Subject: [PATCH 1/2] feat: accept `userAgent` property in `purgeCache` method --- src/lib/purge_cache.ts | 15 +++++++++++---- test/unit/purge_cache.js | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/lib/purge_cache.ts b/src/lib/purge_cache.ts index af7b74aa..f88c905a 100644 --- a/src/lib/purge_cache.ts +++ b/src/lib/purge_cache.ts @@ -5,6 +5,7 @@ interface BasePurgeCacheOptions { deployAlias?: string tags?: string[] token?: string + userAgent?: string } interface PurgeCacheOptionsWithSiteID extends BasePurgeCacheOptions { @@ -73,13 +74,19 @@ export const purgeCache = async (options: PurgeCacheOptions = {}) => { ) } + const headers: Record = { + 'Content-Type': 'application/json; charset=utf8', + Authorization: `Bearer ${token}`, + } + + if (options.userAgent) { + headers['user-agent'] = options.userAgent + } + const apiURL = options.apiURL || 'https://api.netlify.com' const response = await fetch(`${apiURL}/api/v1/purge`, { method: 'POST', - headers: { - 'Content-Type': 'application/json; charset=utf8', - Authorization: `Bearer ${token}`, - }, + headers, body: JSON.stringify(payload), }) diff --git a/test/unit/purge_cache.js b/test/unit/purge_cache.js index 6fd0ef7f..d31bd3f0 100644 --- a/test/unit/purge_cache.js +++ b/test/unit/purge_cache.js @@ -104,7 +104,7 @@ test.serial('Ignores purgeCache if in local dev with no token or site', async (t const mockAPI = new MockFetch().post({ body: () => { t.fail() - } + }, }) const myFunction = async () => { await purgeCache() @@ -116,3 +116,40 @@ test.serial('Ignores purgeCache if in local dev with no token or site', async (t t.is(response, undefined) }) + +test.serial('Accepts a custom user agent', async (t) => { + if (!hasFetchAPI) { + console.warn('Skipping test requires the fetch API') + + return t.pass() + } + + const userAgent = 'Netlify' + const mockSiteID = '123456789' + const mockToken = '1q2w3e4r5t6y7u8i9o0p' + + process.env.NETLIFY_PURGE_API_TOKEN = mockToken + process.env.SITE_ID = mockSiteID + + const mockAPI = new MockFetch().post({ + body: (payload) => { + const data = JSON.parse(payload) + + t.is(data.site_id, mockSiteID) + }, + headers: { Authorization: `Bearer ${mockToken}`, 'user-agent': userAgent }, + method: 'post', + response: new Response(null, { status: 202 }), + url: `https://api.netlify.com/api/v1/purge`, + }) + const myFunction = async () => { + await purgeCache({ userAgent }) + } + + globalThis.fetch = mockAPI.fetcher + + const response = await invokeLambda(myFunction) + + t.is(response, undefined) + t.true(mockAPI.fulfilled) +}) From fbc518879301b9db6a6e906fda8a1bf3d818f0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 18 Dec 2024 21:10:05 +0000 Subject: [PATCH 2/2] chore: add test --- src/lib/purge_cache.test.ts | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/lib/purge_cache.test.ts b/src/lib/purge_cache.test.ts index f8bb2266..04871c56 100644 --- a/src/lib/purge_cache.test.ts +++ b/src/lib/purge_cache.test.ts @@ -123,3 +123,41 @@ test('Ignores purgeCache if in local dev with no token or site', async () => { expect(response).toBeUndefined() }) + +test('Accepts a custom user-agent', async () => { + if (!hasFetchAPI) { + console.warn('Skipping test requires the fetch API') + + return + } + + const userAgent = 'Netlify' + const mockSiteID = '123456789' + const mockToken = '1q2w3e4r5t6y7u8i9o0p' + + process.env.NETLIFY_PURGE_API_TOKEN = mockToken + process.env.SITE_ID = mockSiteID + + const mockAPI = new MockFetch().post({ + body: (payload: string) => { + const data = JSON.parse(payload) + + expect(data.site_id).toBe(mockSiteID) + }, + headers: { Authorization: `Bearer ${mockToken}`, 'user-agent': userAgent }, + method: 'post', + response: new Response(null, { status: 202 }), + url: `https://api.netlify.com/api/v1/purge`, + }) + + const myFunction = async () => { + await purgeCache({ userAgent }) + } + + globalThis.fetch = mockAPI.fetcher + + const response = await invokeLambda(myFunction) + + expect(response).toBeUndefined() + expect(mockAPI.fulfilled).toBeTruthy() +})