import {z} from 'zod' export type GetApiConsentV3HistorySiteIdArgs = {siteId: number, consentUUID?: string | undefined, authId?: string | undefined, latest?: boolean | undefined}; export type GetApiConsentV3HistorySiteIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const iABAndCustomVendors = z.object({id: z.string().optional(), name: z.string().optional(), vendorType: z.string().optional()}); export const googleATPVendors = z.object({id: z.string().optional(), name: z.string().optional(), vendorType: z.string().optional(), googleId: z.number().int().optional()}); export const iABPurposes = z.object({id: z.string().optional(), name: z.string().optional(), iabPurposeRef: z.object({name: z.string().optional(), iabId: z.number().int().optional()}).optional()}); export const customPurposes = z.object({id: z.string().optional(), name: z.string().optional()}); export const getConsentResponse = z.array(z.object({_id: z.string().optional(), siteId: z.number().optional(), dateCreated: z.string().optional(), consentUUID: z.string().optional(), messageId: z.number().int().optional(), vendorListId: z.string().optional(), vendors: z.array(z.union([iABAndCustomVendors, googleATPVendors])).optional(), legIntVendors: z.array(z.union([iABAndCustomVendors, googleATPVendors])).optional(), categories: z.array(z.union([iABPurposes, customPurposes])).optional(), legIntCategories: z.array(z.union([iABPurposes, customPurposes])).optional()})); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get end-user consent status * * GET /consent/v3/history/{siteId} */ export const getApiConsentV3HistorySiteId = async (args: GetApiConsentV3HistorySiteIdArgs, options?: GetApiConsentV3HistorySiteIdOptions) => { const {baseUrl = "https://cdn.privacy-mgmt.com/consent/tcfv2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.consentUUID != null) query.append("consentUUID", String(args.consentUUID)) if (args.authId != null) query.append("authId", String(args.authId)) if (args.latest != null) query.append("latest", String(args.latest)) const url = `${baseUrl.replace(/\/+$/, '')}/consent/v3/history/${encodeURIComponent(String(args.siteId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getConsentResponse, await res.json()) };