This repository was archived by the owner on Jun 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add purgeCache helper
#433
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
656cfb5
feat: add `purgeCache` helper
eduardoboucas 9eff5eb
fix: oops
eduardoboucas f6368ca
fix: use correct environment variable
eduardoboucas 51998f3
refactor: accept additional parameters
eduardoboucas 7782a3b
refactor: check response status
eduardoboucas e70b66f
refactor: adjust parameters
eduardoboucas 542c520
chore: add tests
eduardoboucas 514d90d
chore: skip tests on Node <18
eduardoboucas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { env } from 'process' | ||
|
|
||
| interface BasePurgeCacheOptions { | ||
| apiURL?: string | ||
| tags?: string[] | ||
| token?: string | ||
| } | ||
|
|
||
| interface PurgeCacheOptionsWithSiteID extends BasePurgeCacheOptions { | ||
| deployAlias?: string | ||
| siteID?: string | ||
| } | ||
|
|
||
| interface PurgeCacheOptionsWithSiteSlug extends BasePurgeCacheOptions { | ||
| deployAlias?: string | ||
| siteSlug: string | ||
| } | ||
|
|
||
| interface PurgeCacheOptionsWithDomain extends BasePurgeCacheOptions { | ||
| domain: string | ||
| } | ||
|
|
||
| type PurgeCacheOptions = PurgeCacheOptionsWithSiteID | PurgeCacheOptionsWithSiteSlug | PurgeCacheOptionsWithDomain | ||
|
|
||
| interface PurgeAPIPayload { | ||
| cache_tags?: string[] | ||
| deploy_alias?: string | ||
| domain?: string | ||
| site_id?: string | ||
| site_slug?: string | ||
| } | ||
|
|
||
| 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.", | ||
| ) | ||
| } | ||
|
|
||
| const payload: PurgeAPIPayload = { | ||
| cache_tags: options.tags, | ||
| site_id: env.SITE_ID, | ||
| } | ||
| const token = env.NETLIFY_PURGE_API_TOKEN || options.token | ||
|
|
||
| if ('siteSlug' in options) { | ||
| payload.deploy_alias = options.deployAlias | ||
| payload.site_slug = options.siteSlug | ||
| } else if ('domain' in options) { | ||
| payload.domain = options.domain | ||
| } else { | ||
| // The `siteID` from `options` takes precedence over the one from the | ||
| // environment. | ||
| if (options.siteID) { | ||
| payload.site_id = options.siteID | ||
| } | ||
|
|
||
| payload.deploy_alias = options.deployAlias | ||
| } | ||
|
|
||
| if (!payload.site_id) { | ||
| throw new Error( | ||
| 'The Netlify site ID was not found in the execution environment. Please supply it manually using the `siteID` property.', | ||
| ) | ||
| } | ||
|
|
||
| if (!token) { | ||
| throw new Error( | ||
| 'The cache purge API token was not found in the execution environment. Please supply it manually using the `token` property.', | ||
| ) | ||
| } | ||
|
|
||
| const apiURL = options.apiURL || 'https://api.netlify.com' | ||
| 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}`) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.