import {z} from 'zod' export type Def21 = {gpc?: number | undefined}; export type Def22 = {subject: {verifiedId?: string | undefined, anonymousId?: string | undefined}, compliance?: Def21 | undefined, attributes: {[key: string]: unknown}, jurisdiction?: string | undefined}; export type CreateApiV2ConsentsGpcArgs = {'x-country-code-override'?: string | undefined, 'x-region-code-override'?: string | undefined, body: Def22}; export type CreateApiV2ConsentsGpcOptions = {baseUrl?: string | undefined, ucApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2ConsentsGpcResponse = z.object({gpcActions: z.array(z.object({target: z.string(), vendor: z.string(), action: z.string(), jurisdiction: 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 }; /** * Create and insert a gpc consent * * POST /v2/consents/gpc */ export const createApiV2ConsentsGpc = async (args: CreateApiV2ConsentsGpcArgs, options?: CreateApiV2ConsentsGpcOptions) => { const {baseUrl = "https://uc.api.osano.com", fetch: fetchFn = fetch, ucApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-country-code-override'] != null) headers.set("x-country-code-override", String(args['x-country-code-override'])) if (args['x-region-code-override'] != null) headers.set("x-region-code-override", String(args['x-region-code-override'])) if (ucApiKey !== undefined) headers.set("x-uc-api-key", ucApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/consents/gpc${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(createApiV2ConsentsGpcResponse, await res.json()) };