import {z} from 'zod' export type GetApiApiV1PaymentlinksArgs = {page?: number | undefined}; export type GetApiApiV1PaymentlinksOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyEnum = z.enum(["KES", "GHS", "NGN", "UGX", "TZS", "XAF", "XOF"]); export const mobileTarrifEnum = z.enum(["BUSINESS-PAYS", "CUSTOMER-PAYS"]); export const cardTarrifEnum = z.enum(["BUSINESS-PAYS", "CUSTOMER-PAYS"]); export const paymentLinksSer = z.object({id: z.string().optional(), title: z.string().max(140), is_active: z.boolean().optional(), redirect_url: z.string().max(400).nullable().optional(), amount: z.number().int().gte(0).optional(), usage_limit: z.number().int().gte(0).optional(), qrcode_file: z.string().nullable(), url: z.string(), currency: currencyEnum.optional(), mobile_tarrif: mobileTarrifEnum.optional(), card_tarrif: cardTarrifEnum.optional(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const paginatedPaymentLinksSerList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(paymentLinksSer)}); 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 Paymentlinks * * GET /api/v1/paymentlinks/ */ export const getApiApiV1Paymentlinks = async (args: GetApiApiV1PaymentlinksArgs, options?: GetApiApiV1PaymentlinksOptions) => { 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.page != null) query.append("page", String(args.page)) if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/paymentlinks/${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(paginatedPaymentLinksSerList, await res.json()) };