import {z} from 'zod' export type GetApiDemandAccountsAccountIdProfilingTemplatesProfileTemplateIdArgs = {locales?: string | undefined}; export type GetApiDemandAccountsAccountIdProfilingTemplatesProfileTemplateIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const profileTemplateID = z.string(); export const localeCode = z.string(); export const questionID = z.number().int(); export const templateSelectionConditionV3 = z.object({object: z.literal("selection"), question_id: questionID, option: z.string(), text: z.string().optional(), text_translated: z.string().optional()}); export const profileTargetsRangeConditionsV3 = z.object({object: z.literal("range"), question_id: questionID, min: z.number().int().gte(1).lte(99), max: z.number().int().gte(1).lte(99)}); export const profileTargetsOpenEndedConditionsV3 = z.union([z.object({object: z.literal("open_ended"), question_id: questionID, allow_all_values: z.literal(true), open_ended_values: z.array(z.string()).nullable()}), z.object({object: z.literal("open_ended"), question_id: questionID, allow_all_values: z.literal(false), open_ended_values: z.array(z.string())})]); export const templateProfileConditionsV3 = z.union([templateSelectionConditionV3, profileTargetsRangeConditionsV3, profileTargetsOpenEndedConditionsV3]); export const templateQuotaDetailsV3 = z.object({conditions: z.array(templateProfileConditionsV3), quota: z.object({name: z.string().optional(), completes_goal_percentage: z.number().gte(0).lte(100).multipleOf(0.01)}).optional()}); export const templatesQuotaV3 = z.array(templateQuotaDetailsV3); export const templateRegularProfiles = z.object({object: z.literal("regular"), name: z.string().optional(), description: z.string().optional(), quotas_enabled: z.boolean(), targets: templatesQuotaV3}); export const profilingTemplate = z.object({profile_adjustment_type: z.literal("percentage"), profiles: z.array(templateRegularProfiles)}); export const profileTemplateResponse = z.object({id: profileTemplateID.optional(), name: z.string().optional(), description: z.string().optional(), label: z.string().optional(), locales: z.array(localeCode).optional(), scope: z.enum(["global", "account"]).optional(), profiling: profilingTemplate.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 a profile template * * GET /demand/accounts/{account_id}/profiling-templates/{profile_template_id} */ export const getApiDemandAccountsAccountIdProfilingTemplatesProfileTemplateId = async (args: GetApiDemandAccountsAccountIdProfilingTemplatesProfileTemplateIdArgs, options?: GetApiDemandAccountsAccountIdProfilingTemplatesProfileTemplateIdOptions) => { const {baseUrl = "https://api.cint.com/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.locales != null) query.append("locales", String(args.locales)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/demand/accounts/${encodeURIComponent(String(args.account_id))}/profiling-templates/${encodeURIComponent(String(args.profile_template_id))}${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(profileTemplateResponse, await res.json()) };