|
1 | 1 | import { EnvironmentContext, getEnvironmentContext, MissingBlobsEnvironmentError } from './environment.ts' |
| 2 | +import { encodeMetadata, Metadata, METADATA_HEADER_EXTERNAL, METADATA_HEADER_INTERNAL } from './metadata.ts' |
2 | 3 | import { fetchAndRetry } from './retry.ts' |
3 | 4 | import { BlobInput, Fetcher, HTTPMethod } from './types.ts' |
4 | 5 |
|
5 | 6 | interface MakeStoreRequestOptions { |
6 | 7 | body?: BlobInput | null |
7 | 8 | headers?: Record<string, string> |
8 | 9 | key: string |
| 10 | + metadata?: Metadata |
9 | 11 | method: HTTPMethod |
10 | 12 | storeName: string |
11 | 13 | } |
@@ -33,38 +35,52 @@ export class Client { |
33 | 35 | this.token = token |
34 | 36 | } |
35 | 37 |
|
36 | | - private async getFinalRequest(storeName: string, key: string, method: string) { |
| 38 | + private async getFinalRequest(storeName: string, key: string, method: string, metadata?: Metadata) { |
37 | 39 | const encodedKey = encodeURIComponent(key) |
| 40 | + const encodedMetadata = encodeMetadata(metadata) |
38 | 41 |
|
39 | 42 | if (this.edgeURL) { |
| 43 | + const headers: Record<string, string> = { |
| 44 | + authorization: `Bearer ${this.token}`, |
| 45 | + } |
| 46 | + |
| 47 | + if (encodedMetadata) { |
| 48 | + headers[METADATA_HEADER_EXTERNAL] = encodedMetadata |
| 49 | + } |
| 50 | + |
40 | 51 | return { |
41 | | - headers: { |
42 | | - authorization: `Bearer ${this.token}`, |
43 | | - }, |
| 52 | + headers, |
44 | 53 | url: `${this.edgeURL}/${this.siteID}/${storeName}/${encodedKey}`, |
45 | 54 | } |
46 | 55 | } |
47 | 56 |
|
48 | 57 | const apiURL = `${this.apiURL ?? 'https://api.netlify.com'}/api/v1/sites/${ |
49 | 58 | this.siteID |
50 | 59 | }/blobs/${encodedKey}?context=${storeName}` |
51 | | - const headers = { authorization: `Bearer ${this.token}` } |
| 60 | + const apiHeaders: Record<string, string> = { authorization: `Bearer ${this.token}` } |
| 61 | + |
| 62 | + if (encodedMetadata) { |
| 63 | + apiHeaders[METADATA_HEADER_EXTERNAL] = encodedMetadata |
| 64 | + } |
| 65 | + |
52 | 66 | const fetch = this.fetch ?? globalThis.fetch |
53 | | - const res = await fetch(apiURL, { headers, method }) |
| 67 | + const res = await fetch(apiURL, { headers: apiHeaders, method }) |
54 | 68 |
|
55 | 69 | if (res.status !== 200) { |
56 | 70 | throw new Error(`${method} operation has failed: API returned a ${res.status} response`) |
57 | 71 | } |
58 | 72 |
|
59 | 73 | const { url } = await res.json() |
| 74 | + const userHeaders = encodedMetadata ? { [METADATA_HEADER_INTERNAL]: encodedMetadata } : undefined |
60 | 75 |
|
61 | 76 | return { |
| 77 | + headers: userHeaders, |
62 | 78 | url, |
63 | 79 | } |
64 | 80 | } |
65 | 81 |
|
66 | | - async makeRequest({ body, headers: extraHeaders, key, method, storeName }: MakeStoreRequestOptions) { |
67 | | - const { headers: baseHeaders = {}, url } = await this.getFinalRequest(storeName, key, method) |
| 82 | + async makeRequest({ body, headers: extraHeaders, key, metadata, method, storeName }: MakeStoreRequestOptions) { |
| 83 | + const { headers: baseHeaders = {}, url } = await this.getFinalRequest(storeName, key, method, metadata) |
68 | 84 | const headers: Record<string, string> = { |
69 | 85 | ...baseHeaders, |
70 | 86 | ...extraHeaders, |
|
0 commit comments