Skip to content

Commit 7b24ef8

Browse files
feat: support named deploy-scoped stores (#181)
1 parent a765139 commit 7b24ef8

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

src/main.test.ts

+74
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,80 @@ describe('Deploy scope', () => {
13841384
expect(mockStore.fulfilled).toBeTruthy()
13851385
})
13861386

1387+
test('Returns a named deploy-scoped store if `getDeployStore` receives a string parameter', async () => {
1388+
const mockToken = 'some-token'
1389+
const mockStoreName = 'my-store'
1390+
const mockStore = new MockFetch()
1391+
.get({
1392+
headers: { authorization: `Bearer ${mockToken}` },
1393+
response: new Response(value),
1394+
url: `${edgeURL}/${siteID}/deploy:${deployID}:${mockStoreName}/${key}`,
1395+
})
1396+
.get({
1397+
headers: { authorization: `Bearer ${mockToken}` },
1398+
response: new Response(value),
1399+
url: `${edgeURL}/${siteID}/deploy:${deployID}:${mockStoreName}/${key}`,
1400+
})
1401+
1402+
globalThis.fetch = mockStore.fetch
1403+
1404+
const context = {
1405+
deployID,
1406+
edgeURL,
1407+
siteID,
1408+
token: mockToken,
1409+
}
1410+
1411+
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')
1412+
1413+
const deployStore = getDeployStore(mockStoreName)
1414+
1415+
const string = await deployStore.get(key)
1416+
expect(string).toBe(value)
1417+
1418+
const stream = await deployStore.get(key, { type: 'stream' })
1419+
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
1420+
1421+
expect(mockStore.fulfilled).toBeTruthy()
1422+
})
1423+
1424+
test('Returns a named deploy-scoped store if `getDeployStore` receives an object with a `name` property', async () => {
1425+
const mockToken = 'some-token'
1426+
const mockStoreName = 'my-store'
1427+
const mockStore = new MockFetch()
1428+
.get({
1429+
headers: { authorization: `Bearer ${mockToken}` },
1430+
response: new Response(value),
1431+
url: `${edgeURL}/${siteID}/deploy:${deployID}:${mockStoreName}/${key}`,
1432+
})
1433+
.get({
1434+
headers: { authorization: `Bearer ${mockToken}` },
1435+
response: new Response(value),
1436+
url: `${edgeURL}/${siteID}/deploy:${deployID}:${mockStoreName}/${key}`,
1437+
})
1438+
1439+
globalThis.fetch = mockStore.fetch
1440+
1441+
const context = {
1442+
deployID,
1443+
edgeURL,
1444+
siteID,
1445+
token: mockToken,
1446+
}
1447+
1448+
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')
1449+
1450+
const deployStore = getDeployStore({ name: mockStoreName })
1451+
1452+
const string = await deployStore.get(key)
1453+
expect(string).toBe(value)
1454+
1455+
const stream = await deployStore.get(key, { type: 'stream' })
1456+
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
1457+
1458+
expect(mockStore.fulfilled).toBeTruthy()
1459+
})
1460+
13871461
test('Throws if the deploy ID fails validation', async () => {
13881462
const mockToken = 'some-token'
13891463
const mockStore = new MockFetch()

src/store.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface BaseStoreOptions {
1616

1717
interface DeployStoreOptions extends BaseStoreOptions {
1818
deployID: string
19+
name?: string
1920
}
2021

2122
interface NamedStoreOptions extends BaseStoreOptions {
@@ -74,7 +75,13 @@ export class Store {
7475
if ('deployID' in options) {
7576
Store.validateDeployID(options.deployID)
7677

77-
this.name = DEPLOY_STORE_PREFIX + options.deployID
78+
let name = DEPLOY_STORE_PREFIX + options.deployID
79+
80+
if (options.name) {
81+
name += `:${options.name}`
82+
}
83+
84+
this.name = name
7885
} else if (options.name.startsWith(LEGACY_STORE_INTERNAL_PREFIX)) {
7986
const storeName = options.name.slice(LEGACY_STORE_INTERNAL_PREFIX.length)
8087

src/store_factory.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ type ExperimentalRegion =
1111

1212
interface GetDeployStoreOptions extends Partial<ClientOptions> {
1313
deployID?: string
14+
name?: string
1415
experimentalRegion?: ExperimentalRegion
1516
}
1617

1718
/**
1819
* Gets a reference to a deploy-scoped store.
1920
*/
20-
export const getDeployStore = (options: GetDeployStoreOptions = {}): Store => {
21+
export const getDeployStore = (input: GetDeployStoreOptions | string = {}): Store => {
2122
const context = getEnvironmentContext()
23+
const options = typeof input === 'string' ? { name: input } : input
2224
const deployID = options.deployID ?? context.deployID
2325

2426
if (!deployID) {
@@ -47,7 +49,7 @@ export const getDeployStore = (options: GetDeployStoreOptions = {}): Store => {
4749

4850
const client = new Client(clientOptions)
4951

50-
return new Store({ client, deployID })
52+
return new Store({ client, deployID, name: options.name })
5153
}
5254

5355
interface GetStoreOptions extends Partial<ClientOptions> {

0 commit comments

Comments
 (0)