Skip to content

Commit e2bdfff

Browse files
committed
refactor: move store factory to separate file
1 parent 2586939 commit e2bdfff

File tree

3 files changed

+73
-71
lines changed

3 files changed

+73
-71
lines changed

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { getDeployStore, getStore } from './store.ts'
1+
export { getDeployStore, getStore } from './store_factory.ts'

src/store.ts

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

54
interface BaseStoreOptions {
@@ -26,7 +25,7 @@ interface SetOptions {
2625

2726
const EXPIRY_HEADER = 'x-nf-expires-at'
2827

29-
class Store {
28+
export class Store {
3029
private client: Client
3130
private name: string
3231

@@ -137,70 +136,3 @@ class Store {
137136
})
138137
}
139138
}
140-
141-
/**
142-
* Gets a reference to a deploy-scoped store.
143-
*/
144-
export const getDeployStore = (options: Partial<ClientOptions> = {}): Store => {
145-
const context = getEnvironmentContext()
146-
const { deployID } = context
147-
148-
if (!deployID) {
149-
throw new MissingBlobsEnvironmentError(['deployID'])
150-
}
151-
152-
const clientOptions = getClientOptions(options, context)
153-
const client = new Client(clientOptions)
154-
155-
return new Store({ client, deployID })
156-
}
157-
158-
interface GetStoreOptions extends Partial<ClientOptions> {
159-
deployID?: string
160-
name?: string
161-
}
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-
*/
168-
export const getStore: {
169-
(name: string): Store
170-
(options: GetStoreOptions): Store
171-
} = (input) => {
172-
if (typeof input === 'string') {
173-
const clientOptions = getClientOptions({})
174-
const client = new Client(clientOptions)
175-
176-
return new Store({ client, name: input })
177-
}
178-
179-
if (typeof input.name === 'string') {
180-
const { name } = input
181-
const clientOptions = getClientOptions(input)
182-
183-
if (!name) {
184-
throw new MissingBlobsEnvironmentError(['name'])
185-
}
186-
187-
const client = new Client(clientOptions)
188-
189-
return new Store({ client, name })
190-
}
191-
192-
if (typeof input.deployID === 'string') {
193-
const clientOptions = getClientOptions(input)
194-
const { deployID } = input
195-
196-
if (!deployID) {
197-
throw new MissingBlobsEnvironmentError(['deployID'])
198-
}
199-
200-
const client = new Client(clientOptions)
201-
202-
return new Store({ client, deployID })
203-
}
204-
205-
throw new Error('`getStore()` requires a `name` or `siteID` properties.')
206-
}

src/store_factory.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { Client, ClientOptions, getClientOptions } from './client.ts'
2+
import { getEnvironmentContext, MissingBlobsEnvironmentError } from './environment.ts'
3+
import { Store } from './store.ts'
4+
5+
/**
6+
* Gets a reference to a deploy-scoped store.
7+
*/
8+
export const getDeployStore = (options: Partial<ClientOptions> = {}): Store => {
9+
const context = getEnvironmentContext()
10+
const { deployID } = context
11+
12+
if (!deployID) {
13+
throw new MissingBlobsEnvironmentError(['deployID'])
14+
}
15+
16+
const clientOptions = getClientOptions(options, context)
17+
const client = new Client(clientOptions)
18+
19+
return new Store({ client, deployID })
20+
}
21+
22+
interface GetStoreOptions extends Partial<ClientOptions> {
23+
deployID?: string
24+
name?: string
25+
}
26+
27+
/**
28+
* Gets a reference to a store.
29+
*
30+
* @param input Either a string containing the store name or an options object
31+
*/
32+
export const getStore: {
33+
(name: string): Store
34+
(options: GetStoreOptions): Store
35+
} = (input) => {
36+
if (typeof input === 'string') {
37+
const clientOptions = getClientOptions({})
38+
const client = new Client(clientOptions)
39+
40+
return new Store({ client, name: input })
41+
}
42+
43+
if (typeof input.name === 'string') {
44+
const { name } = input
45+
const clientOptions = getClientOptions(input)
46+
47+
if (!name) {
48+
throw new MissingBlobsEnvironmentError(['name'])
49+
}
50+
51+
const client = new Client(clientOptions)
52+
53+
return new Store({ client, name })
54+
}
55+
56+
if (typeof input.deployID === 'string') {
57+
const clientOptions = getClientOptions(input)
58+
const { deployID } = input
59+
60+
if (!deployID) {
61+
throw new MissingBlobsEnvironmentError(['deployID'])
62+
}
63+
64+
const client = new Client(clientOptions)
65+
66+
return new Store({ client, deployID })
67+
}
68+
69+
throw new Error('`getStore()` requires a `name` or `siteID` properties.')
70+
}

0 commit comments

Comments
 (0)