import {z} from 'zod' export type GetApiApiV2RequestsSearchArgs = {query?: string | undefined}; export type GetApiApiV2RequestsSearchOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const ticketAuditViaObject = z.object({channel: z.string().optional(), source: z.record(z.string(), z.unknown()).optional()}); export const requestObject = z.object({assignee_id: z.number().int().optional(), can_be_solved_by_me: z.boolean().optional(), collaborator_ids: z.array(z.number().int()).optional(), created_at: z.string().optional(), custom_fields: z.array(z.object({id: z.number().int().optional(), value: z.string().optional()})).optional(), custom_status_id: z.number().int().optional(), description: z.string().optional(), due_at: z.string().optional(), email_cc_ids: z.array(z.number().int()).optional(), followup_source_id: z.number().int().optional(), group_id: z.number().int().optional(), id: z.number().int().optional(), is_public: z.boolean().optional(), organization_id: z.number().int().optional(), priority: z.string().optional(), recipient: z.string().optional(), requester_id: z.number().int().optional(), solved: z.boolean().optional(), status: z.string().optional(), subject: z.string(), ticket_form_id: z.number().int().optional(), type: z.string().optional(), updated_at: z.string().optional(), url: z.string().optional(), via: ticketAuditViaObject.optional()}); export const requestsResponse = z.object({requests: z.array(requestObject).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 }; /** * Search Requests * * GET /api/v2/requests/search */ export const getApiApiV2RequestsSearch = async (args: GetApiApiV2RequestsSearchArgs, options?: GetApiApiV2RequestsSearchOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/requests/search${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(requestsResponse, await res.json()) };