import {z} from 'zod' export type CreateApiConsentV3SiteIdTcstringArgs = {siteId: number, consentUUID?: string | undefined, body: {euconsent: string}}; export type CreateApiConsentV3SiteIdTcstringOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const mergeConsent = z.object({userConsentRecord: z.object({categories: z.array(z.string()).optional(), legIntCategories: z.array(z.string()).optional(), vendors: z.array(z.string()).optional(), legIntVendors: z.array(z.string()).optional(), id: z.string().optional(), consentUUID: z.string().optional(), siteId: z.number().int().optional(), euconsent: z.string().optional(), vendorListId: z.string().optional(), fromNetId: z.boolean().optional(), dateCreated: z.string().optional(), __v: z.number().int().optional()}).optional(), appendedConsentUUID: z.string().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 }; /** * Merge end-user consent status with identity provider * * POST /consent/v3/{siteId}/tcstring */ export const createApiConsentV3SiteIdTcstring = async (args: CreateApiConsentV3SiteIdTcstringArgs, options?: CreateApiConsentV3SiteIdTcstringOptions) => { const {baseUrl = "https://cdn.privacy-mgmt.com/consent/tcfv2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.consentUUID != null) query.append("consentUUID", String(args.consentUUID)) const url = `${baseUrl.replace(/\/+$/, '')}/consent/v3/${encodeURIComponent(String(args.siteId))}/tcstring${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(mergeConsent, await res.json()) };