Skip to content

Commit

Permalink
refactor: check response status
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Oct 11, 2023
1 parent 51998f3 commit 7782a3b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/lib/purge_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface PurgeAPIPayload {
site_slug?: string
}

export const purgeCache = (options: PurgeCacheOptions = {}) => {
export const purgeCache = async (options: PurgeCacheOptions = {}) => {
if (globalThis.fetch === undefined) {
throw new Error(
"`fetch` is not available. Please ensure you're using Node.js version 18.0.0 or above. Refer to https://ntl.fyi/functions-runtime for more information.",
Expand Down Expand Up @@ -71,13 +71,16 @@ export const purgeCache = (options: PurgeCacheOptions = {}) => {
}

const apiURL = options.apiURL || 'https://api.netlify.com'

return fetch(`${apiURL}/api/v1/purge`, {
const response = await fetch(`${apiURL}/api/v1/purge`, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf8',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(payload),
})

if (!response.ok) {
throw new Error(`Cache purge API call returned an unexpected status code: ${response.status}`)
}
}

0 comments on commit 7782a3b

Please sign in to comment.