@@ -6,9 +6,10 @@ import { BlobInput, Fetcher, HTTPMethod } from './types.ts'
6
6
interface MakeStoreRequestOptions {
7
7
body ?: BlobInput | null
8
8
headers ?: Record < string , string >
9
- key : string
9
+ key ? : string
10
10
metadata ?: Metadata
11
11
method : HTTPMethod
12
+ parameters ?: Record < string , string >
12
13
storeName : string
13
14
}
14
15
@@ -20,6 +21,14 @@ export interface ClientOptions {
20
21
token : string
21
22
}
22
23
24
+ interface GetFinalRequestOptions {
25
+ key : string | undefined
26
+ metadata ?: Metadata
27
+ method : string
28
+ parameters ?: Record < string , string >
29
+ storeName : string
30
+ }
31
+
23
32
export class Client {
24
33
private apiURL ?: string
25
34
private edgeURL ?: string
@@ -41,7 +50,7 @@ export class Client {
41
50
}
42
51
}
43
52
44
- private async getFinalRequest ( storeName : string , key : string , method : string , metadata ?: Metadata ) {
53
+ private async getFinalRequest ( { key , metadata , method, parameters = { } , storeName } : GetFinalRequestOptions ) {
45
54
const encodedMetadata = encodeMetadata ( metadata )
46
55
47
56
if ( this . edgeURL ) {
@@ -53,38 +62,72 @@ export class Client {
53
62
headers [ METADATA_HEADER_EXTERNAL ] = encodedMetadata
54
63
}
55
64
65
+ const path = key ? `/${ this . siteID } /${ storeName } /${ key } ` : `/${ this . siteID } /${ storeName } `
66
+ const url = new URL ( path , this . edgeURL )
67
+
68
+ for ( const key in parameters ) {
69
+ url . searchParams . set ( key , parameters [ key ] )
70
+ }
71
+
56
72
return {
57
73
headers,
58
- url : ` ${ this . edgeURL } / ${ this . siteID } / ${ storeName } / ${ key } ` ,
74
+ url : url . toString ( ) ,
59
75
}
60
76
}
61
77
62
- const apiURL = `${ this . apiURL ?? 'https://api.netlify.com' } /api/v1/sites/${
63
- this . siteID
64
- } /blobs/${ key } ?context=${ storeName } `
65
78
const apiHeaders : Record < string , string > = { authorization : `Bearer ${ this . token } ` }
79
+ const url = new URL ( `/api/v1/sites/${ this . siteID } /blobs` , this . apiURL ?? 'https://api.netlify.com' )
80
+
81
+ for ( const key in parameters ) {
82
+ url . searchParams . set ( key , parameters [ key ] )
83
+ }
84
+
85
+ url . searchParams . set ( 'context' , storeName )
86
+
87
+ if ( key === undefined ) {
88
+ return {
89
+ headers : apiHeaders ,
90
+ url : url . toString ( ) ,
91
+ }
92
+ }
93
+
94
+ url . pathname += `/${ key } `
66
95
67
96
if ( encodedMetadata ) {
68
97
apiHeaders [ METADATA_HEADER_EXTERNAL ] = encodedMetadata
69
98
}
70
99
71
- const res = await this . fetch ( apiURL , { headers : apiHeaders , method } )
100
+ const res = await this . fetch ( url . toString ( ) , { headers : apiHeaders , method } )
72
101
73
102
if ( res . status !== 200 ) {
74
- throw new Error ( `${ method } operation has failed: API returned a ${ res . status } response` )
103
+ throw new Error ( `Netlify Blobs has generated an internal error: ${ res . status } response` )
75
104
}
76
105
77
- const { url } = await res . json ( )
106
+ const { url : signedURL } = await res . json ( )
78
107
const userHeaders = encodedMetadata ? { [ METADATA_HEADER_INTERNAL ] : encodedMetadata } : undefined
79
108
80
109
return {
81
110
headers : userHeaders ,
82
- url,
111
+ url : signedURL ,
83
112
}
84
113
}
85
114
86
- async makeRequest ( { body, headers : extraHeaders , key, metadata, method, storeName } : MakeStoreRequestOptions ) {
87
- const { headers : baseHeaders = { } , url } = await this . getFinalRequest ( storeName , key , method , metadata )
115
+ async makeRequest ( {
116
+ body,
117
+ headers : extraHeaders ,
118
+ key,
119
+ metadata,
120
+ method,
121
+ parameters,
122
+ storeName,
123
+ } : MakeStoreRequestOptions ) {
124
+ const { headers : baseHeaders = { } , url } = await this . getFinalRequest ( {
125
+ key,
126
+ metadata,
127
+ method,
128
+ parameters,
129
+ storeName,
130
+ } )
88
131
const headers : Record < string , string > = {
89
132
...baseHeaders ,
90
133
...extraHeaders ,
@@ -106,17 +149,7 @@ export class Client {
106
149
options . duplex = 'half'
107
150
}
108
151
109
- const res = await fetchAndRetry ( this . fetch , url , options )
110
-
111
- if ( res . status === 404 && method === HTTPMethod . GET ) {
112
- return null
113
- }
114
-
115
- if ( res . status !== 200 && res . status !== 304 ) {
116
- throw new Error ( `${ method } operation has failed: store returned a ${ res . status } response` )
117
- }
118
-
119
- return res
152
+ return fetchAndRetry ( this . fetch , url , options )
120
153
}
121
154
}
122
155
0 commit comments