import {z} from 'zod' export type ActionObject = {field?: string | undefined, value?: string | undefined}; export type MacroInput = {actions: Array, active?: boolean | undefined, description?: (string | null) | undefined, restriction?: ({id?: number | undefined, ids?: Array | undefined, type?: string | undefined} | {[key: string]: unknown}) | undefined, title: string}; export type CreateApiApiV2MacrosArgs = {body: {macro?: MacroInput | undefined}}; export type CreateApiApiV2MacrosOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const actionObject = z.object({field: z.string().optional(), value: z.string().optional()}); export const macroObject = z.object({actions: z.array(actionObject), active: z.boolean().optional(), created_at: z.string().optional(), default: z.boolean().optional(), description: z.string().nullable().optional(), id: z.number().int().optional(), position: z.number().int().optional(), restriction: z.record(z.string(), z.unknown()).nullable().optional(), title: z.string(), updated_at: z.string().optional(), url: z.string().optional(), app_installation: z.string().nullable().optional(), categories: z.string().nullable().optional(), permissions: z.string().nullable().optional(), usage_1h: z.number().int().optional(), usage_7d: z.number().int().optional(), usage_24h: z.number().int().optional(), usage_30d: z.number().int().optional()}); export const createApiApiV2MacrosResponse = z.object({macro: macroObject.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 Macro * * POST /api/v2/macros */ export const createApiApiV2Macros = async (args: CreateApiApiV2MacrosArgs, options?: CreateApiApiV2MacrosOptions) => { 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/macros${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(createApiApiV2MacrosResponse, await res.json()) };