export async function publishRecipeContent()

in lib/recipes-data/src/lib/s3.ts [46:111]


export async function publishRecipeContent({
	recipe,
	staticBucketName,
	fastlyApiKey,
	contentPrefix,
	attempt,
}: {
	recipe: RecipeReference;
	staticBucketName: string;
	fastlyApiKey: string;
	contentPrefix: string;
	attempt?: number;
}): Promise<void> {
	const realAttempt = attempt ?? 1;
	if (!recipe.checksum) {
		throw new Error(
			'publishRecipeContent: Cannot output recipe data without a checksum',
		);
	}

	const Key = `content/${recipe.checksum}`;

	const req = new PutObjectCommand({
		Bucket: staticBucketName,
		Key,
		Body: recipe.jsonBlob,
		ContentType: 'application/json',
		//ChecksumSHA256: recipe.checksum,  //This is commented out because the format is wrong. Left here because we want to fix it but not hold up PR approval.
		CacheControl: DefaultCacheControlParams,
	});

	try {
		await s3Client.send(req);
		//TODO - check if "hard" or "soft" purging is the right option here
		await sendFastlyPurgeRequestWithRetries({
			contentPath: Key,
			apiKey: fastlyApiKey,
			contentPrefix,
			purgeType: 'hard',
		});
	} catch (err) {
		if (err instanceof S3ServiceException) {
			console.warn(`Unable to write to S3 on attempt ${realAttempt}: `, err);
			if (
				MaximumRetries &&
				!isNaN(MaximumRetries) &&
				realAttempt < MaximumRetries
			) {
				await awaitableDelay();
				return publishRecipeContent({
					recipe,
					staticBucketName: staticBucketName,
					fastlyApiKey,
					contentPrefix,
					attempt: realAttempt + 1,
				});
			} else {
				throw new Error('Could not write to S3, see logs for details.');
			}
		} else if (err instanceof FastlyError) {
			console.warn(`Unable to flush Fastly cache: `, err);
		} else {
			throw err;
		}
	}
}