import {z} from 'zod' export type GetApiV2ConsentProfilesHashedSubjectIdArgs = {configId: string, hashedSubjectId: string, 'x-country-code-override'?: string | undefined, 'x-region-code-override'?: string | undefined}; export type GetApiV2ConsentProfilesHashedSubjectIdOptions = {baseUrl?: string | undefined, ucApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const def0 = z.object({privacyPolicy: z.object({version: z.string().optional(), url: z.string()}).optional(), gpc: z.number().int().gte(0).lte(1).optional()}); export const def4 = z.object({subjectId: z.string(), brandId: z.string(), channelIds: z.array(z.string()).optional(), compliance: def0.optional(), actions: z.array(z.object({target: z.string(), vendor: z.string(), action: z.string()})), attributes: z.unknown(), lastUpdateDate: z.string().optional(), lastConflictDate: z.string().optional()}); export const def3 = z.object({type: z.string().optional(), resolution: z.string().optional(), actionsInConflict: z.array(z.object({target: z.string().optional(), vendor: z.string().optional(), action: z.string().optional(), consentId: z.string().nullable().optional(), channelId: z.string().nullable().optional(), createdAt: z.string().nullable().optional()})).optional()}); export const getApiV2ConsentProfilesHashedSubjectIdResponse = z.object({unifiedConsent: def4.optional(), conflicts: z.array(def3).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 }; /** * Retrieves the unified consent for a subject. * * GET /v2/consent-profiles/{hashedSubjectId} */ export const getApiV2ConsentProfilesHashedSubjectId = async (args: GetApiV2ConsentProfilesHashedSubjectIdArgs, options?: GetApiV2ConsentProfilesHashedSubjectIdOptions) => { const {baseUrl = "https://uc.api.osano.com", fetch: fetchFn = fetch, ucApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.configId != null) query.append("configId", String(args.configId)) 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/consent-profiles/${encodeURIComponent(String(args.hashedSubjectId))}${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(getApiV2ConsentProfilesHashedSubjectIdResponse, await res.json()) };