8000 fix: couple of small fixes (#72) · netlify/blobs@edadf1c · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit edadf1c

Browse files
fix: couple of small fixes (#72)
1 parent 9b2a4df commit edadf1c

File tree

2 files changed

+103
-27
lines changed

2 files changed

+103
-27
lines changed

src/main.test.ts

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -916,19 +916,9 @@ describe('delete', () => {
916916
})
917917

918918
describe('Deploy scope', () => {
919-
test('Returns a deploy-scoped store if the `deployID` parameter is supplied', async () => {
919+
test('Returns a deploy-scoped store if the `deployID` parameter is supplied and the environment context is present', async () => {
920920
const mockToken = 'some-token'
921921
const mockStore = new MockFetch()
922-
.get({
923-
headers: { authorization: `Bearer ${mockToken}` },
924-
response: new Response(value),
925-
url: `${edgeURL}/${siteID}/images/${key}`,
926-
})
927-
.get({
928-
headers: { authorization: `Bearer ${mockToken}` },
929-
response: new Response(value),
930-
url: `${edgeURL}/${siteID}/images/${key}`,
931-
})
932922
.get({
933923
headers: { authorization: `Bearer ${mockToken}` },
934924
response: new Response(value),
@@ -950,26 +940,52 @@ describe('Deploy scope', () => {
950940

951941
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')
952942

953-
const siteStore = getStore('images')
943+
const deployStore = getStore({ deployID })
954944

955-
const string1 = await siteStore.get(key)
956-
expect(string1).toBe(value)
945+
const string = await deployStore.get(key)
946+
expect(string).toBe(value)
957947

958-
const stream1 = await siteStore.get(key, { type: 'stream' })
959-
expect(await streamToString(stream1 as unknown as NodeJS.ReadableStream)).toBe(value)
948+
const stream = await deployStore.get(key, { type: 'stream' })
949+
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
960950

961-
const deployStore = getStore({ deployID })
951+
expect(mockStore.fulfilled).toBeTruthy()
952+
})
953+
954+
test('Returns a deploy-scoped store if the `deployID` parameter is supplied and the environment context is not present', async () => {
955+
const mockStore = new MockFetch()
956+
.get({
957+
headers: { authorization: `Bearer ${apiToken}` },
958+
response: new Response(JSON.stringify({ url: signedURL })),
959+
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=deploy:${deployID}`,
960+
})
961+
.get({
962+
response: new Response(value),
963+
url: signedURL,
964+
})
965+
.get({
966+
headers: { authorization: `Bearer ${apiToken}` },
967+
response: new Response(JSON.stringify({ url: signedURL })),
968+
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=deploy:${deployID}`,
969+
})
970+
.get({
971+
response: new Response(value),
972+
url: signedURL,
973+
})
974+
975+
globalThis.fetch = mockStore.fetch
962976

963-
const string2 = await deployStore.get(key)
964-
expect(string2).toBe(value)
977+
const deployStore = getStore({ deployID, siteID, token: apiToken })
965978

966-
const stream2 = await deployStore.get(key, { type: 'stream' })
967-
expect(await streamToString(stream2 as unknown as NodeJS.ReadableStream)).toBe(value)
979+
const string = await deployStore.get(key)
980+
expect(string).toBe(value)
981+
982+
const stream = await deployStore.get(key, { type: 'stream' })
983+
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
968984

969985
expect(mockStore.fulfilled).toBeTruthy()
970986
})
971987

972-
test('Returns a deploy-scoped store if the `getDeployStore` method is called', async () => {
988+
test('Returns a deploy-scoped store if the `getDeployStore` method is called and the environment context is present', async () => {
973989
const mockToken = 'some-token'
974990
const mockStore = new MockFetch()
975991
.get({
@@ -1004,6 +1020,40 @@ describe('Deploy scope', () => {
10041020

10051021
expect(mockStore.fulfilled).toBeTruthy()
10061022
})
1023+
1024+
test('Returns a deploy-scoped store if the `getDeployStore` method is called and the environment context is not present', async () => {
1025+
const mockStore = new MockFetch()
1026+
.get({
1027+
headers: { authorization: `Bearer ${apiToken}` },
1028+
response: new Response(JSON.stringify({ url: signedURL })),
1029+
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=deploy:${deployID}`,
1030+
})
1031+
.get({
1032+
response: new Response(value),
1033+
url: signedURL,
1034+
})
1035+
.get({
1036+
headers: { authorization: `Bearer ${apiToken}` },
1037+
response: new Response(JSON.stringify({ url: signedURL })),
1038+
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=deploy:${deployID}`,
1039+
})
1040+
.get({
1041+
response: new Response(value),
1042+
url: signedURL,
1043+
})
1044+
1045+
globalThis.fetch = mockStore.fetch
1046+
1047+
const deployStore = getDeployStore({ deployID, siteID, token: apiToken })
1048+
1049+
const string = await deployStore.get(key)
1050+
expect(string).toBe(value)
1051+
1052+
const stream = await deployStore.get(key, { type: 'stream' })
1053+
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
1054+
1055+
expect(mockStore.fulfilled).toBeTruthy()
1056+
})
10071057
})
10081058

10091059
describe('Custom `fetch`', () => {
@@ -1050,4 +1100,24 @@ describe(`getStore`, () => {
10501100
}),
10511101
).toThrowError(MissingBlobsEnvironmentError)
10521102
})
1103+
1104+
test('Throws when the name of the store is not provided', async () => {
1105+
const { fetch } = new MockFetch()
1106+
1107+
globalThis.fetch = fetch
1108+
1109+
// @ts-expect-error Ignoring types, which expect an argument
1110+
expect(() => getStore()).toThrowError(
1111+
'The `getStore` method requires the name of the store as a string or as the `name` property of an options object',
1112+
)
1113+
1114+
expect(() =>
1115+
getStore({
1116+
token: apiToken,
1117+
siteID,
1118+
}),
1119+
).toThrowError(
1120+
'The `getStore` method requires the name of the store as a string or as the `name` property of an options object',
1121+
)
1122+
})
10531123
})

src/store_factory.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { Client, ClientOptions, getClientOptions } from './client.ts'
22
import { getEnvironmentContext, MissingBlobsEnvironmentError } from './environment.ts'
33
import { Store } from './store.ts'
44

5+
interface GetDeployStoreOptions extends Partial<ClientOptions> {
6+
deployID?: string
7+
}
8+
59
/**
610
* Gets a reference to a deploy-scoped store.
711
*/
8-
export const getDeployStore = (options: Partial<ClientOptions> = {}): Store => {
12+
export const getDeployStore = (options: GetDeployStoreOptions = {}): Store => {
913
const context = getEnvironmentContext()
10-
const { deployID } = context
14+
const deployID = options.deployID ?? context.deployID
1115

1216
if (!deployID) {
1317
throw new MissingBlobsEnvironmentError(['deployID'])
@@ -40,7 +44,7 @@ export const getStore: {
4044
return new Store({ client, name: input })
4145
}
4246

43-
if (typeof input.name === 'string') {
47+
if (typeof input?.name === 'string') {
4448
const { name } = input
4549
const clientOptions = getClientOptions(input)
4650

@@ -53,7 +57,7 @@ export const getStore: {
5357
return new Store({ client, name })
5458
}
5559

56-
if (typeof input.deployID === 'string') {
60+
if (typeof input?.deployID === 'string') {
5761
const clientOptions = getClientOptions(input)
5862
const { deployID } = input
5963

@@ -66,5 +70,7 @@ export const getStore: {
6670
return new Store({ client, deployID })
6771
}
6872

69-
throw new Error('`getStore()` requires a `name` or `siteID` properties.')
73+
throw new Error(
74+
'The `getStore` method requires the name of the store as a string or as the `name` property of an options object',
75+
)
7076
}

0 commit comments

Comments
 (0)
0