import {z} from 'zod' export type GetApiV1BatchesArgs = {batchTypes?: ('INTERNAL_TRANSFER' | 'BANK_TRANSFER' | 'INTERNATIONAL_TRANSFER' | 'NEW_PAYEE') | undefined, orderBy?: 'DATE' | undefined, order?: ('DESC' | 'ASC') | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiV1BatchesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const batchRequest = z.object({batchItemUuid: z.string().optional(), type: z.enum(["INTERNAL_TRANSFER", "BANK_TRANSFER", "NEW_PAYEE", "DEPRECATED_LOGIN", "DEPRECATED_MANUALLY_ADDED_NEW_PAYEE", "DEPRECATED_NEW_USER", "INTERNATIONAL_TRANSFER"]).optional(), status: z.enum(["PENDING_APPROVAL", "REJECTED", "COMPLETE", "OPEN", "CANCELLED", "PENDING_PARENT_BATCH_APPROVAL", "READY_FOR_PROCESSING"]).optional(), sourceName: z.string().optional(), batchName: z.string().optional(), jobNumber: z.string().optional(), currency: z.string().optional(), numberOfItemsSubmitted: z.number().int().optional(), valueOfItemsSubmitted: z.number().int().optional(), numberOfItemsFailed: z.number().int().optional(), valueOfItemsFailed: z.number().int().optional(), numberOfItemsSucceeded: z.number().int().optional(), valueOfItemsSucceeded: z.number().int().optional(), lastUpdated: z.string().optional(), dateCreated: z.string().optional()}); export const getApiV1BatchesResponse = z.object({total: z.number().int().optional(), batchRequests: z.array(batchRequest).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 all batches * * GET /v1/batches */ export const getApiV1Batches = async (args: GetApiV1BatchesArgs, options?: GetApiV1BatchesOptions) => { const {baseUrl = "https://api.fire.com/business", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.batchTypes != null) query.append("batchTypes", String(args.batchTypes)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.order != null) query.append("order", String(args.order)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/batches${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(getApiV1BatchesResponse, await res.json()) };