import {z} from 'zod' export type TriggerActionObject = {field?: string | undefined, value?: (string | number | Array) | undefined}; export type TriggerConditionObject = {field?: string | undefined, operator?: string | undefined, value?: (string | number | Array) | undefined}; export type TriggerConditionsObject = {all?: (Array | null) | undefined, any?: (Array | null) | undefined}; export type ObjectTriggerObject = {actions: Array, active?: boolean | undefined, conditions: TriggerConditionsObject, created_at?: string | undefined, default?: boolean | undefined, description?: string | undefined, id?: number | undefined, position?: number | undefined, raw_title?: string | undefined, title: string, updated_at?: string | undefined, url?: string | undefined}; export type ObjectTriggerRequest = {trigger?: ObjectTriggerObject | undefined}; export type CreateApiApiV2CustomObjectsCustomObjectKeyTriggersArgs = {body: ObjectTriggerRequest}; export type CreateApiApiV2CustomObjectsCustomObjectKeyTriggersOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const triggerActionObject = z.object({field: z.string().optional(), value: z.union([z.string(), z.number().int(), z.array(z.union([z.string(), z.number().int()]))]).optional()}); export const triggerConditionObject = z.object({field: z.string().optional(), operator: z.string().optional(), value: z.union([z.string(), z.number().int(), z.array(z.union([z.string(), z.number().int()]))]).optional()}); export const triggerConditionsObject = z.object({all: z.array(triggerConditionObject).nullable().optional(), any: z.array(triggerConditionObject).nullable().optional()}); export const objectTriggerObject = z.object({actions: z.array(triggerActionObject), active: z.boolean().optional(), conditions: triggerConditionsObject, created_at: z.string().optional(), default: z.boolean().optional(), description: z.string().optional(), id: z.number().int().optional(), position: z.number().int().optional(), raw_title: z.string().optional(), title: z.string(), updated_at: z.string().optional(), url: z.string().optional()}); export const objectTriggerResponse = z.object({trigger: objectTriggerObject.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 Object Trigger * * POST /api/v2/custom_objects/{custom_object_key}/triggers */ export const createApiApiV2CustomObjectsCustomObjectKeyTriggers = async (args: CreateApiApiV2CustomObjectsCustomObjectKeyTriggersArgs, options?: CreateApiApiV2CustomObjectsCustomObjectKeyTriggersOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/custom_objects/${encodeURIComponent(String(args.custom_object_key))}/triggers${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(objectTriggerResponse, await res.json()) };