From d01c58909fb52a403c7f3d7ec2d27e355899aaed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Thu, 9 Nov 2023 13:56:51 +0000 Subject: [PATCH] feat!: remove `fresh` property from metadata result --- README.md | 4 ++-- src/main.test.ts | 23 ++++++++++++----------- src/store.ts | 4 +--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5e5d06b..a2f0dd8 100644 --- a/README.md +++ b/README.md @@ -221,9 +221,9 @@ const cachedETag = getFromMockCache('my-key') // Get entry from the blob store only if its ETag is different from the one you // have locally, which means the entry has changed since you last obtained it -const { data, etag, fresh } = await store.getWithMetadata('some-key', { etag: cachedETag }) +const { data, etag } = await store.getWithMetadata('some-key', { etag: cachedETag }) -if (fresh) { +if (etag === cachedETag) { // `data` is `null` because the local blob is fresh } else { // `data` contains the new blob, store it locally alongside the new ETag diff --git a/src/main.test.ts b/src/main.test.ts index 4e70eb8..769f5ff 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -574,7 +574,10 @@ describe('getWithMetadata', () => { cool: true, functions: ['edge', 'serverless'], } - const etags = ['"thewrongetag"', '"therightetag"'] + const etags = { + right: '"therightetag"', + wrong: '"thewrongetag"', + } const metadataHeaders = { 'x-amz-meta-user': `b64;${base64Encode(mockMetadata)}`, } @@ -585,8 +588,8 @@ describe('getWithMetadata', () => { url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`, }) .get({ - headers: { 'if-none-match': etags[0] }, - response: new Response(value, { headers: { ...metadataHeaders, etag: etags[0] }, status: 200 }), + headers: { 'if-none-match': etags.wrong }, + response: new Response(value, { headers: { ...metadataHeaders, etag: etags.right }, status: 200 }), url: `${signedURL}b`, }) .get({ @@ -595,8 +598,8 @@ describe('getWithMetadata', () => { url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`, }) .get({ - headers: { 'if-none-match': etags[1] }, - response: new Response(null, { headers: { ...metadataHeaders, etag: etags[0] }, status: 304 }), + headers: { 'if-none-match': etags.right }, + response: new Response(null, { headers: { ...metadataHeaders, etag: etags.right }, status: 304 }), url: `${signedURL}a`, }) @@ -608,16 +611,14 @@ describe('getWithMetadata', () => { siteID, }) - const staleEntry = await blobs.getWithMetadata(key, { etag: etags[0] }) + const staleEntry = await blobs.getWithMetadata(key, { etag: etags.wrong }) expect(staleEntry?.data).toBe(value) - expect(staleEntry?.etag).toBe(etags[0]) - expect(staleEntry?.fresh).toBe(false) + expect(staleEntry?.etag).toBe(etags.right) expect(staleEntry?.metadata).toEqual(mockMetadata) - const freshEntry = await blobs.getWithMetadata(key, { etag: etags[1], type: 'text' }) + const freshEntry = await blobs.getWithMetadata(key, { etag: etags.right, type: 'text' }) expect(freshEntry?.data).toBe(null) - expect(freshEntry?.etag).toBe(etags[0]) - expect(freshEntry?.fresh).toBe(true) + expect(freshEntry?.etag).toBe(etags.right) expect(freshEntry?.metadata).toEqual(mockMetadata) expect(mockStore.fulfilled).toBeTruthy() diff --git a/src/store.ts b/src/store.ts index 76213f4..6d38ef0 100644 --- a/src/store.ts +++ b/src/store.ts @@ -26,7 +26,6 @@ interface GetWithMetadataOptions { interface GetWithMetadataResult { etag?: string - fresh: boolean metadata: Metadata } @@ -219,12 +218,11 @@ export class Store { const metadata = getMetadataFromResponse(res) const result: GetWithMetadataResult = { etag: responseETag, - fresh: false, metadata, } if (res.status === 304 && requestETag) { - return { data: null, ...result, fresh: true } + return { data: null, ...result } } if (type === undefined || type === 'text') {