import {z} from 'zod' export type GetApiApiV2JobStatusesArgs = {'page[before]'?: string | undefined, 'page[after]'?: string | undefined, 'page[size]'?: number | undefined}; export type GetApiApiV2JobStatusesOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const jobStatusResultObject = z.union([z.object({id: z.number().int(), index: z.number().int()}).and(z.record(z.string(), z.unknown())), z.object({action: z.string(), id: z.number().int(), status: z.string(), success: z.boolean()}).and(z.record(z.string(), z.unknown())), z.object({action: z.string(), details: z.string(), error: z.string(), id: z.number().int(), success: z.boolean()}).and(z.record(z.string(), z.unknown()))]); export const jobStatusObject = z.object({id: z.string().optional(), job_type: z.string().optional(), message: z.string().nullable().optional(), progress: z.number().int().nullable().optional(), results: z.union([z.array(jobStatusResultObject).nullable(), z.object({success: z.boolean()})]).optional(), status: z.string().optional(), total: z.number().int().nullable().optional(), url: z.string().optional()}); export const jobStatusesResponse = z.object({job_statuses: z.array(jobStatusObject)}); 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 }; /** * List Job Statuses * * GET /api/v2/job_statuses */ export const getApiApiV2JobStatuses = async (args: GetApiApiV2JobStatusesArgs, options?: GetApiApiV2JobStatusesOptions) => { 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['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/job_statuses${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(jobStatusesResponse, await res.json()) };