import {z} from 'zod' export type CustomStatusCreateInput = {active?: boolean | undefined, agent_label?: string | undefined, description?: string | undefined, end_user_description?: string | undefined, end_user_label?: string | undefined, status_category?: ('new' | 'open' | 'pending' | 'hold' | 'solved') | undefined}; export type CustomStatusCreateRequest = {custom_status?: CustomStatusCreateInput | undefined}; export type CreateApiApiV2CustomStatusesArgs = {body: CustomStatusCreateRequest}; export type CreateApiApiV2CustomStatusesOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const customStatusObject = z.object({active: z.boolean().optional(), agent_label: z.string(), created_at: z.string().optional(), default: z.boolean().optional(), description: z.string().optional(), end_user_description: z.string().optional(), end_user_label: z.string().optional(), id: z.number().int().optional(), raw_agent_label: z.string().optional(), raw_description: z.string().optional(), raw_end_user_description: z.string().optional(), raw_end_user_label: z.string().optional(), status_category: z.enum(["new", "open", "pending", "hold", "solved"]), updated_at: z.string().optional()}); export const customStatusResponse = z.object({custom_status: customStatusObject.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 Custom Ticket Status * * POST /api/v2/custom_statuses */ export const createApiApiV2CustomStatuses = async (args: CreateApiApiV2CustomStatusesArgs, options?: CreateApiApiV2CustomStatusesOptions) => { 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_statuses${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(customStatusResponse, await res.json()) };