import {z} from 'zod' export type GetApiV1DispatchRequestsArgs = {status?: ('unstarted' | 'started' | 'submitted' | 'completed' | 'cancelled' | null) | undefined, assigned?: (boolean | null) | undefined, reference_key?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1DispatchRequestsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dispatchRequestExternalSchema = z.object({id: z.string(), location_id: z.string(), visit_type_slug: z.string(), work_order_id: z.string().nullable(), prefill_scope_id: z.string().nullable(), target_scope_id: z.string().nullable(), assignee_email: z.string().nullable(), reference_key: z.string().nullable(), status: z.enum(["unstarted", "started", "submitted", "completed", "cancelled"]), created_at: z.string(), assigned_at: z.string().nullable(), started_at: z.string().nullable(), submitted_at: z.string().nullable(), completed_at: z.string().nullable(), cancelled_at: z.string().nullable(), metadata: z.object({}), deeplink_url: z.string()}); export const listDispatchRequestsResponse = z.object({dispatch_requests: z.array(dispatchRequestExternalSchema), next_cursor: z.string().nullable()}); 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 dispatch requests * * GET /v1/dispatch_requests */ export const getApiV1DispatchRequests = async (args: GetApiV1DispatchRequestsArgs, options?: GetApiV1DispatchRequestsOptions) => { const {baseUrl = "https://api.navigateai.co", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.assigned != null) query.append("assigned", String(args.assigned)) if (args.reference_key != null) query.append("reference_key", String(args.reference_key)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dispatch_requests${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(listDispatchRequestsResponse, await res.json()) };