diff --git a/src/cache-tags.ts b/src/cache-tags.ts index 280cfab..d3baef4 100644 --- a/src/cache-tags.ts +++ b/src/cache-tags.ts @@ -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`. @@ -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. @@ -26,7 +20,6 @@ export function parseXCacheTagsResponseHeader(string?: null | string) { * @param {TVariables} variables Query variables * @returns Unique query ID */ - export const generateQueryId = (document: DocumentNode, variables?: TVariables): string => { return createHash('sha1') .update(print(document)) @@ -41,7 +34,6 @@ export const generateQueryId = (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; @@ -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 => { if (!cacheTags?.length) { return []; @@ -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; @@ -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}`); } diff --git a/src/classnames.ts b/src/classnames.ts index d81e642..9ea16ec 100644 --- a/src/classnames.ts +++ b/src/classnames.ts @@ -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(' '); diff --git a/src/links.ts b/src/links.ts index 941565e..343c6ce 100644 --- a/src/links.ts +++ b/src/links.ts @@ -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.'); diff --git a/src/types.ts b/src/types.ts index e6cdedc..5cae59b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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';