@netlify/blobs
Advanced tools
Comparing version
@@ -54,2 +54,5 @@ var HTTPMethod; | ||
const res = await this.fetcher(apiURL, { headers, method }); | ||
if (res.status !== 200) { | ||
throw new Error(`${method} operation has failed: API returned a ${res.status} response`); | ||
} | ||
const { url } = await res.json(); | ||
@@ -56,0 +59,0 @@ return { |
@@ -72,2 +72,22 @@ import { version as nodeVersion } from 'process'; | ||
}); | ||
test('Throws when the API returns a non-200 status code', async () => { | ||
const fetcher = async (...args) => { | ||
const [url, options] = args; | ||
const headers = options?.headers; | ||
expect(options?.method).toBe('get'); | ||
if (url === `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`) { | ||
expect(headers.authorization).toBe(`Bearer ${apiToken}`); | ||
return new Response(null, { status: 401, statusText: 'Unauthorized' }); | ||
} | ||
throw new Error(`Unexpected fetch call: ${url}`); | ||
}; | ||
const blobs = new Blobs({ | ||
authentication: { | ||
token: apiToken, | ||
}, | ||
fetcher, | ||
siteID, | ||
}); | ||
expect(async () => await blobs.get(key)).rejects.toThrowError('get operation has failed: API returned a 401 response'); | ||
}); | ||
test('Throws when a pre-signed URL returns a non-200 status code', async () => { | ||
@@ -183,2 +203,22 @@ const fetcher = async (...args) => { | ||
}); | ||
test('Throws when the API returns a non-200 status code', async () => { | ||
const fetcher = async (...args) => { | ||
const [url, options] = args; | ||
const headers = options?.headers; | ||
expect(options?.method).toBe('put'); | ||
if (url === `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`) { | ||
expect(headers.authorization).toBe(`Bearer ${apiToken}`); | ||
return new Response(null, { status: 401 }); | ||
} | ||
throw new Error(`Unexpected fetch call: ${url}`); | ||
}; | ||
const blobs = new Blobs({ | ||
authentication: { | ||
token: apiToken, | ||
}, | ||
fetcher, | ||
siteID, | ||
}); | ||
expect(async () => await blobs.set(key, 'value')).rejects.toThrowError('put operation has failed: API returned a 401 response'); | ||
}); | ||
}); | ||
@@ -212,2 +252,25 @@ describe('delete', () => { | ||
}); | ||
test('Throws when the API returns a non-200 status code', async () => { | ||
const fetcher = async (...args) => { | ||
const [url, options] = args; | ||
const headers = options?.headers; | ||
expect(options?.method).toBe('delete'); | ||
if (url === `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`) { | ||
expect(headers.authorization).toBe(`Bearer ${apiToken}`); | ||
return new Response(null, { status: 401 }); | ||
} | ||
if (url === signedURL) { | ||
return new Response('Something went wrong', { status: 401 }); | ||
} | ||
throw new Error(`Unexpected fetch call: ${url}`); | ||
}; | ||
const blobs = new Blobs({ | ||
authentication: { | ||
token: apiToken, | ||
}, | ||
fetcher, | ||
siteID, | ||
}); | ||
expect(async () => await blobs.delete(key)).rejects.toThrowError('delete operation has failed: API returned a 401 response'); | ||
}); | ||
}); |
{ | ||
"name": "@netlify/blobs", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A JavaScript client for the Netlify Blob Store", | ||
@@ -5,0 +5,0 @@ "type": "module", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
26178
12.86%459
16.79%0
-100%