import {z} from 'zod' export type GetApiTicketListsArgs = {user_id?: number | undefined, status?: ('open' | 'pending' | 'close' | 'awaiting_reply') | undefined, priority?: ('low' | 'medium' | 'high' | 'emergency') | undefined, support_department?: string | undefined, assignee_by?: number | undefined}; export type GetApiTicketListsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiResponse = z.object({success: z.boolean().optional()}).and(z.record(z.string(), z.unknown())); 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 support tickets * * GET /ticket/lists */ export const getApiTicketLists = async (args: GetApiTicketListsArgs, options?: GetApiTicketListsOptions) => { const {baseUrl = "https://demo.onlinebillingform.com/api/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.status != null) query.append("status", String(args.status)) if (args.priority != null) query.append("priority", String(args.priority)) if (args.support_department != null) query.append("support_department", String(args.support_department)) if (args.assignee_by != null) query.append("assignee_by", String(args.assignee_by)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/ticket/lists${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(apiResponse, await res.json()) };