import { type NextRequest, NextResponse } from 'next/server.js'; import { revalidatePath, revalidateTag } from "next/cache.js"; import { type ClientFactory, type IOptiGraphClient, OptiCmsSchema, RouteResolver, type Route } from '@remkoj/optimizely-graph-client'; export { type NextRequest, NextResponse } from 'next/server.js'; export type PublishApiHandler = (req: NextRequest) => Promise> | NextResponse; export type PublishScopes = Parameters[1]; export type DynamicScopes = NonNullable[1]>; export type PublishHookData = { timestamp: string; tenantId: string; } & ({ type: { subject: "bulk"; action: string; }; data: { journalId: string; items: { [itemId: string]: "indexed" | "deleted"; }; docId: never; }; } | { type: { subject: "doc"; action: string; }; data: { docId: string; journalId: never; items: never; }; }); type PartialRouteResolver = { getContentInfoById: (...args: Parameters['getContentInfoById']>) => Promise>; }; export type PublishApiOptions = { /** * The list of paths that your implementation uses with Optimizely CMS * managed content. If the optimized publishing is enabled, this list will * not be used at all. */ paths: Array[0]>; /** * A list fo paths that must always be purged, regardless of optimization * of purging is enabled. This is normally used for search-results etc.. */ additionalPaths: Array[0]>; /** * The scopes for which to revalidate the cache */ scopes: Array; /** * The scopes for which to revalidate the cache for dynamic routes */ dynamicScopes: Array; /** * The tags to revalidate the cache for */ tags: Parameters[0] | Array[0]> | ((data: PublishHookData | null | undefined) => Promise[0]>>); /** * If set to true, the handler will only publish the paths of the items * that are changed. However this comes at the expense of needing a GraphQL * query for every received hook to resolve the paths. */ optimizePublish: boolean; /** * The Optimizely Graph client to use for Graph Operations needed to publish * content */ client: ClientFactory | IOptiGraphClient; /** * The router to use when the publishing optimization is enabled */ router: (() => PartialRouteResolver) | { urlBase?: URL | string; resolverMode?: OptiCmsSchema; }; /** * A filtering function for the received webhooks, only hooks for which this method * returns true will trigger a cache invalidation. * * @param hookType * @returns */ hookDataFilter: (hookType: PublishHookData['type']) => boolean; /** * Take the item identifier as reported by Optimizely Graph and turn that into a * key and locale. The default implementation assumes [key]_[locale]_[status] * * @param itemId The Item ID to parse * @param subject The Hook Subject, to make the processing different for single or bulk. Typical values are "bulk" or "doc" * @returns An array, with the first item being the key, second locale */ itemIdToKeyAndLocale: (itemId: string, subject?: PublishHookData['type']['subject']) => { key: string; locale: string; version?: string; status?: string; } | undefined; /** * Basic filter for bulk operations, to only select those items in a bulk operation * that are actually needed for processing. * * @param bulkItemStatus * @returns */ bulkItemStatusFilter: (bulkItemStatus: string) => boolean; }; export type PublishApiResponse = { revalidated: { paths: Array; scopes?: Array; tags: Array[0]>; }; optimized: boolean; error?: never; } | { revalidated?: never; optimized?: boolean; error: string; }; /** * Create the default handler for webhooks received from Optimizely Graph. * * @param options The configuration fo the API * @returns The created API Handler */ export declare function createPublishApi(options?: Partial): PublishApiHandler; export default createPublishApi;