import {z} from 'zod' export type GetApiApiV1InvoicesArgs = {account?: string | undefined, api_ref?: string | undefined, page?: number | undefined, provider_ref?: string | undefined, record_id?: string | undefined, state?: ('CANCELED' | 'COMPLETE' | 'FAILED' | 'PARTIAL' | 'PENDING' | 'PROCESSING' | 'RETRY') | undefined, wallet_id?: string | undefined}; export type GetApiApiV1InvoicesOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoicesSerStateEnum = z.enum(["PENDING", "PROCESSING", "FAILED", "CANCELED", "PARTIAL", "COMPLETE", "RETRY"]); export const provider906Enum = z.enum(["M-PESA", "PESALINK", "CARD-PAYMENT", "GOOGLE-PAY", "APPLE-PAY", "BITCOIN", "BANK-ACH", "COOP_B2B"]); export const invoicesSer = z.object({invoice_id: z.string(), state: invoicesSerStateEnum.optional(), provider: provider906Enum, charges: z.string().optional(), net_amount: z.string(), currency: z.string(), value: z.string(), account: z.string().max(140).nullable().optional(), api_ref: z.string().max(140).nullable().optional(), clearing_status: z.string(), mpesa_reference: z.string(), host: z.string().max(200).nullable().optional(), card_info: z.string(), retry_count: z.number().int().gte(-2147483648).lte(2147483647).optional(), failed_reason: z.string().nullable().optional(), failed_code: z.string().max(45).nullable().optional(), failed_code_link: z.string(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const paginatedInvoicesSerList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(invoicesSer)}); 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 Invoices * * GET /api/v1/invoices/ */ export const getApiApiV1Invoices = async (args: GetApiApiV1InvoicesArgs, options?: GetApiApiV1InvoicesOptions) => { const {baseUrl = "https://api.intasend.com", fetch: fetchFn = fetch, IntaSendBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account != null) query.append("account", String(args.account)) if (args.api_ref != null) query.append("api_ref", String(args.api_ref)) if (args.page != null) query.append("page", String(args.page)) if (args.provider_ref != null) query.append("provider_ref", String(args.provider_ref)) if (args.record_id != null) query.append("record_id", String(args.record_id)) if (args.state != null) query.append("state", String(args.state)) if (args.wallet_id != null) query.append("wallet_id", String(args.wallet_id)) if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/invoices/${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(paginatedInvoicesSerList, await res.json()) };