1
1
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'
3
3
import { fetchAndRetry } from './retry.ts'
4
4
import { BlobInput , Fetcher , HTTPMethod } from './types.ts'
5
5
@@ -37,14 +37,15 @@ export class Client {
37
37
38
38
private async getFinalRequest ( storeName : string , key : string , method : string , metadata ?: Metadata ) {
39
39
const encodedKey = encodeURIComponent ( key )
40
+ const encodedMetadata = encodeMetadata ( metadata )
40
41
41
42
if ( this . edgeURL ) {
42
43
const headers : Record < string , string > = {
43
44
authorization : `Bearer ${ this . token } ` ,
44
45
}
45
46
46
- if ( metadata ) {
47
- headers [ METADATA_HEADER_EXTERNAL ] = encodeMetadata ( metadata )
47
+ if ( encodedMetadata ) {
48
+ headers [ METADATA_HEADER_EXTERNAL ] = encodedMetadata
48
49
}
49
50
50
51
return {
@@ -53,25 +54,27 @@ export class Client {
53
54
}
54
55
}
55
56
56
- let apiURL = `${ this . apiURL ?? 'https://api.netlify.com' } /api/v1/sites/${
57
+ const apiURL = `${ this . apiURL ?? 'https://api.netlify.com' } /api/v1/sites/${
57
58
this . siteID
58
59
} /blobs/${ encodedKey } ?context=${ storeName } `
60
+ const apiHeaders : Record < string , string > = { authorization : `Bearer ${ this . token } ` }
59
61
60
- if ( metadata ) {
61
- apiURL += `&metadata= ${ encodeMetadata ( metadata ) } `
62
+ if ( encodedMetadata ) {
63
+ apiHeaders [ METADATA_HEADER_EXTERNAL ] = encodedMetadata
62
64
}
63
65
64
- const headers = { authorization : `Bearer ${ this . token } ` }
65
66
const fetch = this . fetch ?? globalThis . fetch
66
- const res = await fetch ( apiURL , { headers, method } )
67
+ const res = await fetch ( apiURL , { headers : apiHeaders , method } )
67
68
68
69
if ( res . status !== 200 ) {
69
70
throw new Error ( `${ method } operation has failed: API returned a ${ res . status } response` )
70
71
}
71
72
72
73
const { url } = await res . json ( )
74
+ const userHeaders = encodedMetadata ? { [ METADATA_HEADER_INTERNAL ] : encodedMetadata } : undefined
73
75
74
76
return {
77
+ headers : userHeaders ,
75
78
url,
76
79
}
77
80
}
0 commit comments