Skip to content

Commit 4f06edb

Browse files
committed
fix: use correct headers
1 parent 4677bbb commit 4f06edb

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/client.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EnvironmentContext, getEnvironmentContext, MissingBlobsEnvironmentError } from './environment.ts'
2-
import { encodeMetadata, Metadata, METADATA_HEADER_EXTERNAL } from './metadata.ts'
2+
import { encodeMetadata, Metadata, METADATA_HEADER_EXTERNAL, METADATA_HEADER_INTERNAL } from './metadata.ts'
33
import { fetchAndRetry } from './retry.ts'
44
import { BlobInput, Fetcher, HTTPMethod } from './types.ts'
55

@@ -37,14 +37,15 @@ export class Client {
3737

3838
private async getFinalRequest(storeName: string, key: string, method: string, metadata?: Metadata) {
3939
const encodedKey = encodeURIComponent(key)
40+
const encodedMetadata = encodeMetadata(metadata)
4041

4142
if (this.edgeURL) {
4243
const headers: Record<string, string> = {
4344
authorization: `Bearer ${this.token}`,
4445
}
4546

46-
if (metadata) {
47-
headers[METADATA_HEADER_EXTERNAL] = encodeMetadata(metadata)
47+
if (encodedMetadata) {
48+
headers[METADATA_HEADER_EXTERNAL] = encodedMetadata
4849
}
4950

5051
return {
@@ -53,25 +54,27 @@ export class Client {
5354
}
5455
}
5556

56-
let apiURL = `${this.apiURL ?? 'https://api.netlify.com'}/api/v1/sites/${
57+
const apiURL = `${this.apiURL ?? 'https://api.netlify.com'}/api/v1/sites/${
5758
this.siteID
5859
}/blobs/${encodedKey}?context=${storeName}`
60+
const apiHeaders: Record<string, string> = { authorization: `Bearer ${this.token}` }
5961

60-
if (metadata) {
61-
apiURL += `&metadata=${encodeMetadata(metadata)}`
62+
if (encodedMetadata) {
63+
apiHeaders[METADATA_HEADER_EXTERNAL] = encodedMetadata
6264
}
6365

64-
const headers = { authorization: `Bearer ${this.token}` }
6566
const fetch = this.fetch ?? globalThis.fetch
66-
const res = await fetch(apiURL, { headers, method })
67+
const res = await fetch(apiURL, { headers: apiHeaders, method })
6768

6869
if (res.status !== 200) {
6970
throw new Error(`${method} operation has failed: API returned a ${res.status} response`)
7071
}
7172

7273
const { url } = await res.json()
74+
const userHeaders = encodedMetadata ? { [METADATA_HEADER_INTERNAL]: encodedMetadata } : undefined
7375

7476
return {
77+
headers: userHeaders,
7578
url,
7679
}
7780
}

src/main.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,15 @@ describe('set', () => {
498498
const encodedMetadata = `b64;${Buffer.from(JSON.stringify(metadata)).toString('base64')}`
499499
const mockStore = new MockFetch()
500500
.put({
501-
headers: { authorization: `Bearer ${apiToken}` },
501+
headers: { authorization: `Bearer ${apiToken}`, 'netlify-blobs-metadata': encodedMetadata },
502502
response: new Response(JSON.stringify({ url: signedURL })),
503-
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production&metadata=${encodedMetadata}`,
503+
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`,
504504
})
505505
.put({
506506
body: value,
507507
headers: {
508508
'cache-control': 'max-age=0, stale-while-revalidate=60',
509+
'x-amz-meta-user': encodedMetadata,
509510
},
510511
response: new Response(null),
511512
url: signedURL,

src/metadata.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export const METADATA_HEADER_INTERNAL = 'x-amz-meta-user'
77
export const METADATA_HEADER_EXTERNAL = 'netlify-blobs-metadata'
88
const METADATA_MAX_SIZE = 2 * 1024
99

10-
export const encodeMetadata = (metadata: Metadata) => {
10+
export const encodeMetadata = (metadata?: Metadata) => {
11+
if (!metadata) {
12+
return null
13+
}
14+
1115
const encodedObject = Buffer.from(JSON.stringify(metadata)).toString('base64')
1216
const payload = `b64;${encodedObject}`
1317

0 commit comments

Comments
 (0)