From 67cca9da31ac48e7517bb1383675e6abb4a29edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Mon, 4 Mar 2024 15:49:28 +0000 Subject: [PATCH] feat: use `accept` header for signed URL --- src/client.ts | 5 ++++- src/main.test.ts | 12 ++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/client.ts b/src/client.ts index 259db44..97563c8 100644 --- a/src/client.ts +++ b/src/client.ts @@ -134,7 +134,10 @@ export class Client { } } - const res = await this.fetch(url.toString(), { headers: apiHeaders, method }) + const res = await this.fetch(url.toString(), { + headers: { ...apiHeaders, accept: `application/json;type=signed-url` }, + method, + }) if (res.status !== 200) { throw new Error(`Netlify Blobs has generated an internal error: ${res.status} response`) diff --git a/src/main.test.ts b/src/main.test.ts index 0f66745..224238e 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -45,7 +45,7 @@ describe('get', () => { test('Reads from the blob store', async () => { const mockStore = new MockFetch() .get({ - headers: { authorization: `Bearer ${apiToken}` }, + headers: { accept: 'application/json;type=signed-url', authorization: `Bearer ${apiToken}` }, response: new Response(JSON.stringify({ url: signedURL })), url: `https://api.netlify.com/api/v1/blobs/${siteID}/site:production/${key}`, }) @@ -54,7 +54,7 @@ describe('get', () => { url: signedURL, }) .get({ - headers: { authorization: `Bearer ${apiToken}` }, + headers: { accept: 'application/json;type=signed-url', authorization: `Bearer ${apiToken}` }, response: new Response(JSON.stringify({ url: signedURL })), url: `https://api.netlify.com/api/v1/blobs/${siteID}/site:production/${key}`, }) @@ -63,7 +63,7 @@ describe('get', () => { url: signedURL, }) .get({ - headers: { authorization: `Bearer ${apiToken}` }, + headers: { accept: 'application/json;type=signed-url', authorization: `Bearer ${apiToken}` }, response: new Response(JSON.stringify({ url: signedURL })), url: `https://api.netlify.com/api/v1/blobs/${siteID}/site:production/${complexKey}`, }) @@ -95,7 +95,7 @@ describe('get', () => { test('Returns `null` when the pre-signed URL returns a 404', async () => { const mockStore = new MockFetch() .get({ - headers: { authorization: `Bearer ${apiToken}` }, + headers: { accept: 'application/json;type=signed-url', authorization: `Bearer ${apiToken}` }, response: new Response(JSON.stringify({ url: signedURL })), url: `https://api.netlify.com/api/v1/blobs/${siteID}/site:production/${key}`, }) @@ -118,7 +118,7 @@ describe('get', () => { test('Throws when the API returns a non-200 status code', async () => { const mockStore = new MockFetch().get({ - headers: { authorization: `Bearer ${apiToken}` }, + headers: { accept: 'application/json;type=signed-url', authorization: `Bearer ${apiToken}` }, response: new Response(null, { status: 401 }), url: `https://api.netlify.com/api/v1/blobs/${siteID}/site:production/${key}`, }) @@ -140,7 +140,7 @@ describe('get', () => { test('Throws when a pre-signed URL returns a non-200 status code', async () => { const mockStore = new MockFetch() .get({ - headers: { authorization: `Bearer ${apiToken}` }, + headers: { accept: 'application/json;type=signed-url', authorization: `Bearer ${apiToken}` }, response: new Response(JSON.stringify({ url: signedURL })), url: `https://api.netlify.com/api/v1/blobs/${siteID}/site:production/${key}`, })