import {z} from 'zod' export type GetApiV1BatchesBatchUuidArgs = {batchUuid: string}; export type GetApiV1BatchesBatchUuidOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const payeeCheck = z.object({countFullMatch: z.number().int().optional(), countPartialMatch: z.number().int().optional(), countNoMatch: z.number().int().optional(), countUnableToMatch: z.number().int().optional(), countPending: z.number().int().optional()}); export const batch = z.object({batchUuid: z.string().optional(), type: z.enum(["INTERNAL_TRANSFER", "BANK_TRANSFER", "INTERNATIONAL_TRANSFER", "NEW_PAYEE"]).optional(), status: z.enum(["PENDING_APPROVAL", "REJECTED", "COMPLETE", "OPEN", "CANCELLED", "PENDING_PARENT_BATCH_APPROVAL", "READY_FOR_PROCESSING", "PROCESSING"]).optional(), sourceName: z.string().optional(), batchName: z.string().optional(), jobNumber: z.string().optional(), callbackUrl: 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(), payeeChecks: payeeCheck.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 }; /** * Get the details of a batch * * GET /v1/batches/{batchUuid} */ export const getApiV1BatchesBatchUuid = async (args: GetApiV1BatchesBatchUuidArgs, options?: GetApiV1BatchesBatchUuidOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/batches/${encodeURIComponent(String(args.batchUuid))}${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(batch, await res.json()) };