in lib/recipes-data/src/lib/fastly.ts [40:98]
export async function sendFastlyPurgeRequest({
contentPath,
apiKey,
contentPrefix,
purgeType,
}: {
contentPath: string;
apiKey: string;
contentPrefix: string;
purgeType?: PurgeType;
}) {
if (!apiKey || apiKey == '') {
throw new Error('Cannot purge because Fastly API key is not set');
}
const urlToPurge = [
'https://api.fastly.com/purge',
removeLeadingAndTrailingSlash(contentPrefix),
removeLeadingAndTrailingSlash(contentPath),
].join('/');
const baseHeaders: Record<string, string> = {
'FASTLY-KEY': apiKey,
Accept: 'application/json',
};
const headers =
purgeType == 'hard'
? baseHeaders
: {
'Fastly-Soft-Purge': '1',
...baseHeaders,
};
if (DebugLogsEnabled) console.debug('urlToPurge is ', urlToPurge);
const response = await fetch(urlToPurge, {
method: 'POST',
headers,
});
const content = await response.text();
switch (response.status) {
case 200:
if (DebugLogsEnabled) {
console.log(`Purge of ${contentPath} successful: ${content}`);
}
break;
case 404:
console.warn(
`Fastly could not purge ${contentPath}, api returned Not Found.`,
);
break;
default:
console.error(
`Unable to purge ${contentPath}, Fastly returned ${response.status}: ${content}`,
);
throw new FastlyError(`Fastly returned ${response.status}`);
}
}