Skip to content

feat!: remove fresh property from metadata result #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
}
Expand All @@ -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({
Expand All @@ -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`,
})

Expand All @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ interface GetWithMetadataOptions {

interface GetWithMetadataResult {
etag?: string
fresh: boolean
metadata: Metadata
}

Expand Down Expand Up @@ -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') {
Expand Down