import {z} from 'zod' export type GetApiApiV1InvoicesIdArgs = {id: string}; export type GetApiApiV1InvoicesIdOptions = {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 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 }; /** * Retrieve Invoices * * GET /api/v1/invoices/{id}/ */ export const getApiApiV1InvoicesId = async (args: GetApiApiV1InvoicesIdArgs, options?: GetApiApiV1InvoicesIdOptions) => { 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 (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/invoices/${encodeURIComponent(String(args.id))}/${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(invoicesSer, await res.json()) };