import {z} from 'zod' export type GetApiTerminalsArgs = {Authorization: string, 'Sub-Account'?: string | undefined, status?: ('connected' | 'disconnected' | 'unknown' | 'pending_configuration' | 'archived') | undefined, terminal_id?: string | undefined, provider_id?: string | undefined, terminal_order_id?: string | undefined, verified_after?: string | undefined, verified_before?: string | undefined, verified_on?: string | undefined}; export type GetApiTerminalsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const terminal = z.object({id: z.string().optional(), account_id: z.string().optional(), platform_account_id: z.string().optional(), provider: z.string().optional(), status: z.string().optional(), provider_id: z.string().optional(), provider_serial_number: z.string().optional(), nickname: z.string().optional(), verified_at: z.string().optional(), model_name: z.string().optional(), terminal_order_created_at: z.string().optional(), status_last_requested_at: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const pageInfo = z.object({end_cursor: z.string().optional(), has_next: z.boolean().optional(), has_previous: z.boolean().optional(), start_cursor: z.string().optional()}); export const getApiTerminalsResponse = z.object({id: z.number().optional(), type: z.string().optional(), data: z.array(terminal).optional(), page_info: pageInfo.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 }; /** * List Terminals * * GET /terminals */ export const getApiTerminals = async (args: GetApiTerminalsArgs, options?: GetApiTerminalsOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Sub-Account'] != null) headers.set("Sub-Account", String(args['Sub-Account'])) if (args.status != null) query.append("status", String(args.status)) if (args.terminal_id != null) query.append("terminal_id", String(args.terminal_id)) if (args.provider_id != null) query.append("provider_id", String(args.provider_id)) if (args.terminal_order_id != null) query.append("terminal_order_id", String(args.terminal_order_id)) if (args.verified_after != null) query.append("verified_after", String(args.verified_after)) if (args.verified_before != null) query.append("verified_before", String(args.verified_before)) if (args.verified_on != null) query.append("verified_on", String(args.verified_on)) const url = `${baseUrl.replace(/\/+$/, '')}/terminals${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(getApiTerminalsResponse, await res.json()) };