Skip to content

Commit a6573b2

Browse files
feat: throw when API returns an error code (#12)
* feat: throw when API returns an error code * chore: fix tests * chore: fix test
1 parent 1c8299c commit a6573b2

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/main.test.ts

+91
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ describe('get', () => {
9393
expect(await blobs.get(key)).toBeNull()
9494
})
9595

96+
test('Throws when the API returns a non-200 status code', async () => {
97+
const fetcher = async (...args: Parameters<typeof globalThis.fetch>) => {
98+
const [url, options] = args
99+
const headers = options?.headers as Record<string, string>
100+
101+
expect(options?.method).toBe('get')
102+
103+
if (url === `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`) {
104+
expect(headers.authorization).toBe(`Bearer ${apiToken}`)
105+
106+
return new Response(null, { status: 401, statusText: 'Unauthorized' })
107+
}
108+
109+
throw new Error(`Unexpected fetch call: ${url}`)
110+
}
111+
112+
const blobs = new Blobs({
113+
authentication: {
114+
token: apiToken,
115+
},
116+
fetcher,
117+
siteID,
118+
})
119+
120+
expect(async () => await blobs.get(key)).rejects.toThrowError(
121+
'get operation has failed: API returned a 401 response',
122+
)
123+
})
124+
96125
test('Throws when a pre-signed URL returns a non-200 status code', async () => {
97126
const fetcher = async (...args: Parameters<typeof globalThis.fetch>) => {
98127
const [url, options] = args
@@ -242,6 +271,35 @@ describe('set', () => {
242271

243272
await blobs.set(key, value, { ttl })
244273
})
274+
275+
test('Throws when the API returns a non-200 status code', async () => {
276+
const fetcher = async (...args: Parameters<typeof globalThis.fetch>) => {
277+
const [url, options] = args
278+
const headers = options?.headers as Record<string, string>
279+
280+
expect(options?.method).toBe('put')
281+
282+
if (url === `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`) {
283+
expect(headers.authorization).toBe(`Bearer ${apiToken}`)
284+
285+
return new Response(null, { status: 401 })
286+
}
287+
288+
throw new Error(`Unexpected fetch call: ${url}`)
289+
}
290+
291+
const blobs = new Blobs({
292+
authentication: {
293+
token: apiToken,
294+
},
295+
fetcher,
296+
siteID,
297+
})
298+
299+
expect(async () => await blobs.set(key, 'value')).rejects.toThrowError(
300+
'put operation has failed: API returned a 401 response',
301+
)
302+
})
245303
})
246304

247305
describe('delete', () => {
@@ -281,4 +339,37 @@ describe('delete', () => {
281339

282340
await blobs.delete(key)
283341
})
342+
343+
test('Throws when the API returns a non-200 status code', async () => {
344+
const fetcher = async (...args: Parameters<typeof globalThis.fetch>) => {
345+
const [url, options] = args
346+
const headers = options?.headers as Record<string, string>
347+
348+
expect(options?.method).toBe('delete')
349+
350+
if (url === `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`) {
351+
expect(headers.authorization).toBe(`Bearer ${apiToken}`)
352+
353+
return new Response(null, { status: 401 })
354+
}
355+
356+
if (url === signedURL) {
357+
return new Response('Something went wrong', { status: 401 })
358+
}
359+
360+
throw new Error(`Unexpected fetch call: ${url}`)
361+
}
362+
363+
const blobs = new Blobs({
364+
authentication: {
365+
token: apiToken,
366+
},
367+
fetcher,
368+
siteID,
369+
})
370+
371+
expect(async () => await blobs.delete(key)).rejects.toThrowError(
372+
'delete operation has failed: API returned a 401 response',
373+
)
374+
})
284375
})

src/main.ts

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export class Blobs {
7878
const apiURL = `${this.authentication.apiURL}/api/v1/sites/${this.siteID}/blobs/${key}?context=${this.context}`
7979
const headers = { authorization: `Bearer ${this.authentication.token}` }
8080
const res = await this.fetcher(apiURL, { headers, method })
81+
82+
if (res.status !== 200) {
83+
throw new Error(`${method} operation has failed: API returned a ${res.status} response`)
84+
}
85+
8186
const { url } = await res.json()
8287

8388
return {

0 commit comments

Comments
 (0)