1
- import { Client , ClientOptions } from './client.ts'
1
+ import { Client , ClientOptions , getClientOptions } from './client.ts'
2
2
import { getEnvironmentContext , MissingBlobsEnvironmentError } from './environment.ts'
3
3
import { BlobInput , HTTPMethod } from './types.ts'
4
4
@@ -138,21 +138,19 @@ class Store {
138
138
}
139
139
}
140
140
141
+ /**
142
+ * Gets a reference to a deploy-scoped store.
143
+ */
141
144
export const getDeployStore = ( options : Partial < ClientOptions > = { } ) : Store => {
142
145
const context = getEnvironmentContext ( )
143
- const { deployID, ...contextOptions } = context
144
- const clientOptions = {
145
- ...contextOptions ,
146
- ...options ,
147
- siteID : options . siteID ?? context . siteID ,
148
- token : options . token ?? context . token ,
149
- }
146
+ const { deployID } = context
150
147
151
- if ( ! deployID || ! clientOptions . siteID || ! clientOptions . token ) {
152
- throw new MissingBlobsEnvironmentError ( [ 'deployID' , 'siteID' , 'token' ] )
148
+ if ( ! deployID ) {
149
+ throw new MissingBlobsEnvironmentError ( [ 'deployID' ] )
153
150
}
154
151
155
- const client = new Client ( clientOptions as ClientOptions )
152
+ const clientOptions = getClientOptions ( options , context )
153
+ const client = new Client ( clientOptions )
156
154
157
155
return new Store ( { client, deployID } )
158
156
}
@@ -162,59 +160,44 @@ interface GetStoreOptions extends Partial<ClientOptions> {
162
160
name ?: string
163
161
}
164
162
163
+ /**
164
+ * Gets a reference to a store.
165
+ *
166
+ * @param input Either a string containing the store name or an options object
167
+ */
165
168
export const getStore : {
166
169
( name : string ) : Store
167
170
( options : GetStoreOptions ) : Store
168
171
} = ( input ) => {
169
- const context = getEnvironmentContext ( )
170
-
171
172
if ( typeof input === 'string' ) {
172
- if ( ! context . siteID || ! context . token ) {
173
- throw new MissingBlobsEnvironmentError ( [ 'siteID' , 'token' ] )
174
- }
175
-
176
- const client = new Client ( {
177
- apiURL : context . apiURL ,
178
- edgeURL : context . edgeURL ,
179
- siteID : context . siteID ,
180
- token : context . token ,
181
- } )
173
+ const clientOptions = getClientOptions ( { } )
174
+ const client = new Client ( clientOptions )
182
175
183
176
return new Store ( { client, name : input } )
184
177
}
185
178
186
179
if ( typeof input . name === 'string' ) {
187
- const { name, ...options } = input
188
- const clientOptions = {
189
- ...context ,
190
- ...options ,
191
- siteID : options . siteID ?? context . siteID ,
192
- token : options . token ?? context . token ,
193
- }
180
+ const { name } = input
181
+ const clientOptions = getClientOptions ( input )
194
182
195
- if ( ! clientOptions . siteID || ! clientOptions . token ) {
196
- throw new MissingBlobsEnvironmentError ( [ 'siteID' , 'token '] )
183
+ if ( ! name ) {
184
+ throw new MissingBlobsEnvironmentError ( [ 'name ' ] )
197
185
}
198
186
199
- const client = new Client ( clientOptions as ClientOptions )
187
+ const client = new Client ( clientOptions )
200
188
201
189
return new Store ( { client, name } )
202
190
}
203
191
204
192
if ( typeof input . deployID === 'string' ) {
205
- const { deployID, name, ...options } = input
206
- const clientOptions = {
207
- ...context ,
208
- ...options ,
209
- siteID : options . siteID ?? context . siteID ,
210
- token : options . token ?? context . token ,
211
- }
193
+ const clientOptions = getClientOptions ( input )
194
+ const { deployID } = input
212
195
213
- if ( ! clientOptions . siteID || ! clientOptions . token ) {
214
- throw new MissingBlobsEnvironmentError ( [ 'siteID' , 'token '] )
196
+ if ( ! deployID ) {
197
+ throw new MissingBlobsEnvironmentError ( [ 'deployID ' ] )
215
198
}
216
199
217
- const client = new Client ( clientOptions as ClientOptions )
200
+ const client = new Client ( clientOptions )
218
201
219
202
return new Store ( { client, deployID } )
220
203
}
0 commit comments