Cookie scanner, phase 1: declarations, database lookup, review list
ipalKit({ cookieScanner }) (opt-in) adds the cookie-review collection, a
"Scan cookies" box above its list and GET/POST /api/ipal/cookie-scan. A scan
lists the cookies of every integration the project switches on (consent,
admin login, locale, stats, GA4, GTM, Meta Pixel, Clarity, LinkedIn, TikTok,
Turnstile), the project's own declarations, and extra names looked up in the
bundled Open Cookie Database, as drafts. Editors accept (→ Cookie Settings →
Cookies, every locale, only empty text filled), edit or reject. A rescan never
deletes, never changes status and never overwrites edited text.
Everything is overridable (access, collectionOverrides, integrations,
declarations, databaseCategories, labels, syncAccepted) and the building
blocks are exported. Admin labels in en + pl. Docs: cookie-scanner.md,
consent.md, AGENT.md, ipal-reference, ipal-consent-check.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ab45cf48be
commit
a0545b6716
@@ -0,0 +1,54 @@
|
||||
import type { Endpoint, PayloadRequest } from 'payload'
|
||||
|
||||
import type { CookieScanContext, CookieScannerOptions } from './types.js'
|
||||
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
import { DEFAULT_COOKIE_INTEGRATIONS } from './integrations.js'
|
||||
import { cookieScanContext, enabledCookieIntegrations, parseCookieNames, runCookieScan } from './scan.js'
|
||||
|
||||
export const COOKIE_SCAN_PATH = '/ipal/cookie-scan'
|
||||
|
||||
/**
|
||||
* GET /api/ipal/cookie-scan → `{ integrations: [{ key, label }] }` — what a scan would cover.
|
||||
* POST /api/ipal/cookie-scan, body `{ names?: string[] | string }` → runs a scan,
|
||||
* `{ ok, summary: { created, updated, unchanged, unknown }, integrations }`.
|
||||
* Editors and up by default (`cookieScanner.access`).
|
||||
*/
|
||||
export function buildCookieScanEndpoints(options: CookieScannerOptions, plugin: CookieScanContext['plugin']): Endpoint[] {
|
||||
const allowed = async (req: PayloadRequest) =>
|
||||
options.access ? Boolean(await options.access({ req })) : userMeetsRole(req, 'editor')
|
||||
const integrations = options.integrations ? options.integrations(DEFAULT_COOKIE_INTEGRATIONS) : DEFAULT_COOKIE_INTEGRATIONS
|
||||
const list = (items: { key: string; label: string }[]) => items.map(({ key, label }) => ({ key, label }))
|
||||
|
||||
return [
|
||||
{
|
||||
handler: async (req) => {
|
||||
if (!(await allowed(req))) {return Response.json({ ok: false }, { status: 403 })}
|
||||
const enabled = await enabledCookieIntegrations(integrations, await cookieScanContext(req.payload, plugin, req))
|
||||
return Response.json({ integrations: list(enabled), ok: true })
|
||||
},
|
||||
method: 'get',
|
||||
path: COOKIE_SCAN_PATH,
|
||||
},
|
||||
{
|
||||
handler: async (req) => {
|
||||
if (!(await allowed(req))) {return Response.json({ ok: false }, { status: 403 })}
|
||||
let body: { names?: unknown } = {}
|
||||
try {
|
||||
body = ((await (req as unknown as Request).json()) ?? {}) as { names?: unknown }
|
||||
} catch {
|
||||
// empty body = scan without extra names
|
||||
}
|
||||
try {
|
||||
const { enabled, summary } = await runCookieScan({ names: parseCookieNames(body.names), options, payload: req.payload, plugin, req })
|
||||
return Response.json({ integrations: list(enabled), ok: true, summary })
|
||||
} catch (err) {
|
||||
req.payload.logger.error(`[ipal] cookie scan failed: ${err instanceof Error ? err.message : String(err)}`)
|
||||
return Response.json({ ok: false }, { status: 500 })
|
||||
}
|
||||
},
|
||||
method: 'post',
|
||||
path: COOKIE_SCAN_PATH,
|
||||
},
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user