Skip to content

Commit 3b792d4

Browse files
committed
refactor: create getClientOptions
1 parent 1e938c5 commit 3b792d4

File tree

2 files changed

+57
-43
lines changed

2 files changed

+57
-43
lines changed

src/client.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EnvironmentContext, getEnvironmentContext, MissingBlobsEnvironmentError } from './environment.ts'
12
import { fetchAndRetry } from './retry.ts'
23
import { BlobInput, Fetcher, HTTPMethod } from './types.ts'
34

@@ -99,3 +100,33 @@ export class Client {
99100
return res
100101
}
101102
}
103+
104+
/**
105+
* Merges a set of options supplied by the user when getting a reference to a
106+
* store with a context object found in the environment.
107+
*
108+
* @param options User-supplied options
109+
* @param contextOverride Context to be used instead of the environment object
110+
*/
111+
export const getClientOptions = (
112+
options: Partial<ClientOptions>,
113+
contextOverride?: EnvironmentContext,
114+
): ClientOptions => {
115+
const context = contextOverride ?? getEnvironmentContext()
116+
const siteID = context.siteID ?? options.siteID
117+
const token = context.token ?? options.token
118+
119+
if (!siteID || !token) {
120+
throw new MissingBlobsEnvironmentError(['siteID', 'token'])
121+
}
122+
123+
const clientOptions = {
124+
apiURL: context.apiURL ?? options.apiURL,
125+
edgeURL: context.edgeURL ?? options.edgeURL,
126+
fetch: options.fetch,
127+
siteID,
128+
token,
129+
}
130+
131+
return clientOptions
132+
}

src/store.ts

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client, ClientOptions } from './client.ts'
1+
import { Client, ClientOptions, getClientOptions } from './client.ts'
22
import { getEnvironmentContext, MissingBlobsEnvironmentError } from './environment.ts'
33
import { BlobInput, HTTPMethod } from './types.ts'
44

@@ -138,21 +138,19 @@ class Store {
138138
}
139139
}
140140

141+
/**
142+
* Gets a reference to a deploy-scoped store.
143+
*/
141144
export const getDeployStore = (options: Partial<ClientOptions> = {}): Store => {
142145
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
150147

151-
if (!deployID || !clientOptions.siteID || !clientOptions.token) {
152-
throw new MissingBlobsEnvironmentError(['deployID', 'siteID', 'token'])
148+
if (!deployID) {
149+
throw new MissingBlobsEnvironmentError(['deployID'])
153150
}
154151

155-
const client = new Client(clientOptions as ClientOptions)
152+
const clientOptions = getClientOptions(options, context)
153+
const client = new Client(clientOptions)
156154

157155
return new Store({ client, deployID })
158156
}
@@ -162,59 +160,44 @@ interface GetStoreOptions extends Partial<ClientOptions> {
162160
name?: string
163161
}
164162

163+
/**
164+
* Gets a reference to a store.
165+
*
166+
* @param input Either a string containing the store name or an options object
167+
*/
165168
export const getStore: {
166169
(name: string): Store
167170
(options: GetStoreOptions): Store
168171
} = (input) => {
169-
const context = getEnvironmentContext()
170-
171172
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)
182175

183176
return new Store({ client, name: input })
184177
}
185178

186179
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)
194182

195-
if (!clientOptions.siteID || !clientOptions.token) {
196-
throw new MissingBlobsEnvironmentError(['siteID', 'token'])
183+
if (!name) {
184+
throw new MissingBlobsEnvironmentError(['name'])
197185
}
198186

199-
const client = new Client(clientOptions as ClientOptions)
187+
const client = new Client(clientOptions)
200188

201189
return new Store({ client, name })
202190
}
203191

204192
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
212195

213-
if (!clientOptions.siteID || !clientOptions.token) {
214-
throw new MissingBlobsEnvironmentError(['siteID', 'token'])
196+
if (!deployID) {
197+
throw new MissingBlobsEnvironmentError(['deployID'])
215198
}
216199

217-
const client = new Client(clientOptions as ClientOptions)
200+
const client = new Client(clientOptions)
218201

219202
return new Store({ client, deployID })
220203
}

0 commit comments

Comments
 (0)