Skip to content

Commit

Permalink
refactor: adjust parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Oct 11, 2023
1 parent 7782a3b commit e70b66f
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/lib/purge_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import { env } from 'process'

interface BasePurgeCacheOptions {
apiURL?: string
deployAlias?: string
tags?: string[]
token?: string
}

interface PurgeCacheOptionsWithSiteID extends BasePurgeCacheOptions {
deployAlias?: string
siteID?: string
}

interface PurgeCacheOptionsWithSiteSlug extends BasePurgeCacheOptions {
deployAlias?: string
siteSlug: string
}

Expand All @@ -39,29 +38,26 @@ export const purgeCache = async (options: PurgeCacheOptions = {}) => {

const payload: PurgeAPIPayload = {
cache_tags: options.tags,
site_id: env.SITE_ID,
deploy_alias: options.deployAlias,
}
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
}
const siteID = options.siteID || env.SITE_ID

payload.deploy_alias = options.deployAlias
}
if (!siteID) {
throw new Error(
'The Netlify site ID was not found in the execution environment. Please supply it manually using the `siteID` property.',
)
}

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.',
)
payload.site_id = siteID
}

if (!token) {
Expand Down

0 comments on commit e70b66f

Please sign in to comment.