Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ beforeAll(async () => {

const siteID = '12345'
const key = '54321'
const complexKey = '/artista/canção'
const value = 'some value'
const apiToken = 'some token'
const signedURL = 'https://signed.url/123456789'
Expand Down Expand Up @@ -55,6 +56,17 @@ describe('get', () => {
response: new Response(value),
url: signedURL,
})
.get({
headers: { authorization: `Bearer ${apiToken}` },
response: new Response(JSON.stringify({ url: signedURL })),
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${encodeURIComponent(
complexKey,
)}?context=production`,
})
.get({
response: new Response(value),
url: signedURL,
})

const blobs = new Blobs({
authentication: {
Expand All @@ -70,6 +82,9 @@ describe('get', () => {
const stream = await blobs.get(key, { type: 'stream' })
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)

const string2 = await blobs.get(complexKey)
expect(string2).toBe(value)

expect(store.fulfilled).toBeTruthy()
})

Expand Down Expand Up @@ -293,6 +308,19 @@ describe('set', () => {
response: new Response(null),
url: signedURL,
})
.put({
headers: { authorization: `Bearer ${apiToken}` },
response: new Response(JSON.stringify({ url: signedURL })),
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${encodeURIComponent(
complexKey,
)}?context=production`,
})
.put({
body: value,
headers: { 'cache-control': 'max-age=0, stale-while-revalidate=60' },
response: new Response(null),
url: signedURL,
})

const blobs = new Blobs({
authentication: {
Expand All @@ -303,6 +331,7 @@ describe('set', () => {
})

await blobs.set(key, value)
await blobs.set(complexKey, value)

expect(store.fulfilled).toBeTruthy()
})
Expand Down Expand Up @@ -536,12 +565,19 @@ describe('set', () => {

describe('With context credentials', () => {
test('Writes to the blob store', async () => {
const store = new MockFetch().put({
body: value,
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
response: new Response(null),
url: `${edgeURL}/${siteID}/production/${key}`,
})
const store = new MockFetch()
.put({
body: value,
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
response: new Response(null),
url: `${edgeURL}/${siteID}/production/${key}`,
})
.put({
body: value,
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
response: new Response(null),
url: `${edgeURL}/${siteID}/production/${encodeURIComponent(complexKey)}`,
})

const blobs = new Blobs({
authentication: {
Expand All @@ -553,6 +589,7 @@ describe('set', () => {
})

await blobs.set(key, value)
await blobs.set(complexKey, value)

expect(store.fulfilled).toBeTruthy()
})
Expand Down Expand Up @@ -752,6 +789,17 @@ describe('delete', () => {
response: new Response(null),
url: signedURL,
})
.delete({
headers: { authorization: `Bearer ${apiToken}` },
response: new Response(JSON.stringify({ url: signedURL })),
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${encodeURIComponent(
complexKey,
)}?context=production`,
})
.delete({
response: new Response(null),
url: signedURL,
})

const blobs = new Blobs({
authentication: {
Expand All @@ -762,6 +810,7 @@ describe('delete', () => {
})

await blobs.delete(key)
await blobs.delete(complexKey)

expect(store.fulfilled).toBeTruthy()
})
Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,18 @@ export class Blobs {
}

private async getFinalRequest(key: string, method: string) {
const encodedKey = encodeURIComponent(key)

if ('contextURL' in this.authentication) {
return {
headers: {
authorization: `Bearer ${this.authentication.token}`,
},
url: `${this.authentication.contextURL}/${this.siteID}/${this.context}/${key}`,
url: `${this.authentication.contextURL}/${this.siteID}/${this.context}/${encodedKey}`,
}
}

const apiURL = `${this.authentication.apiURL}/api/v1/sites/${this.siteID}/blobs/${key}?context=${this.context}`
const apiURL = `${this.authentication.apiURL}/api/v1/sites/${this.siteID}/blobs/${encodedKey}?context=${this.context}`
const headers = { authorization: `Bearer ${this.authentication.token}` }
const res = await this.fetcher(apiURL, { headers, method })

Expand Down