Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/cache-tags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sql } from '@vercel/postgres';
import { createHash } from 'crypto';
import { DocumentNode, print } from 'graphql';
import { CacheTag } from './types';
import { type DocumentNode, print } from 'graphql';
import { type CacheTag } from './types';

/**
* Converts the value of DatoCMS's `X-Cache-Tags` header into an array of strings typed as `CacheTag`.
Expand All @@ -10,14 +10,8 @@ import { CacheTag } from './types';
* @param string String value of the `X-Cache-Tags` header
* @returns Array of strings typed as `CacheTag`
*/

export function parseXCacheTagsResponseHeader(string?: null | string) {
if (!string) {
return [];
}

return (string.split(' ') || []).map((tag) => tag as CacheTag);
}
export const parseXCacheTagsResponseHeader = (string?: null | string) =>
(string?.split(' ') ?? []).map((tag) => tag as CacheTag);

/**
* Generates a unique query ID based on the query document and its variables.
Expand All @@ -26,7 +20,6 @@ export function parseXCacheTagsResponseHeader(string?: null | string) {
* @param {TVariables} variables Query variables
* @returns Unique query ID
*/

export const generateQueryId = <TVariables = unknown>(document: DocumentNode, variables?: TVariables): string => {
return createHash('sha1')
.update(print(document))
Expand All @@ -41,7 +34,6 @@ export const generateQueryId = <TVariables = unknown>(document: DocumentNode, va
* @param {CacheTag[]} cacheTags Array of cache tags
* @param {string} tableId Database table ID
*/

export const storeQueryCacheTags = async (queryId: string, cacheTags: CacheTag[], tableId: string) => {
if (!cacheTags?.length) {
return;
Expand All @@ -60,7 +52,6 @@ export const storeQueryCacheTags = async (queryId: string, cacheTags: CacheTag[]
* @param {string} tableId Database table ID
* @returns Array of query IDs
*/

export const queriesReferencingCacheTags = async (cacheTags: CacheTag[], tableId: string): Promise<string[]> => {
if (!cacheTags?.length) {
return [];
Expand All @@ -82,7 +73,6 @@ export const queriesReferencingCacheTags = async (cacheTags: CacheTag[], tableId
* @param {string} queryId Unique query ID
* @param {string} tableId Database table ID
*/

export const deleteQueries = async (queryIds: string[], tableId: string) => {
if (!queryIds?.length) {
return;
Expand All @@ -97,7 +87,6 @@ export const deleteQueries = async (queryIds: string[], tableId: string) => {
*
* @param {string} tableId Database table ID
*/

export async function truncateCacheTags(tableId: string) {
await sql.query(`DELETE FROM ${tableId}`);
}
1 change: 0 additions & 1 deletion src/classnames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
* @param classNames Array of class names
* @returns Clean string to be used for class name
*/

export const classNames = (...classNames: (string | undefined | boolean)[]): string => classNames.filter(Boolean).join(' ');
1 change: 0 additions & 1 deletion src/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @param phoneNumber Phone number
* @returns `tel:` link for the phone number
*/

export const getTelLink = (phoneNumber: string): string => {
if (typeof phoneNumber !== 'string') {
throw new Error('Phone number must be a string.');
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
/**
* A branded type for cache tags. This is created by intersecting `string`
* with `{ readonly _: unique symbol }`, making it a unique type.
* Although it is fundamentally a string, it is treated as a distinct type
* due to the unique symbol.
*/
export type CacheTag = string & { readonly _: unique symbol };

/**
* A type representing the structure of a webhook payload for cache tag invalidation.
* It includes the entity type, event type, and the entity details which contain
* the cache tags to be invalidated.
*/
export type CacheTagsInvalidateWebhook = {
entity_type: 'cda_cache_tags';
event_type: 'invalidate';
Expand Down