import {z} from 'zod' export type GetApiApiV1PaymentLinksArgs = {page?: number | undefined, limit?: number | undefined, sort?: string | undefined, status?: ('active' | 'used' | 'expired' | 'cancelled') | undefined, merchant_id?: string | undefined, search?: string | undefined}; export type GetApiApiV1PaymentLinksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentLinkListResponse = z.object({success: z.boolean(), data: z.array(z.object({id: z.string(), merchant: z.object({id: z.string(), brand_name: z.string()}), amount: z.string(), currency: z.string(), payment_reference: z.string(), order_id: z.string().nullable(), status: z.enum(["active", "used", "expired", "cancelled"]), link_type: z.enum(["single_use", "reusable"]), views_count: z.number().int(), first_viewed_at: z.string().nullable(), expires_at: z.string(), created_at: z.string(), transaction_id: z.string().nullable()})), pagination: z.object({total: z.number().int(), page: z.number().int(), limit: z.number().int(), totalPages: z.number().int(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean()})}); 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 payment links with context-aware access control * * GET /api/v1/payment-links */ export const getApiApiV1PaymentLinks = async (args: GetApiApiV1PaymentLinksArgs, options?: GetApiApiV1PaymentLinksOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch, bearerAuth} = 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 (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.status != null) query.append("status", String(args.status)) if (args.merchant_id != null) query.append("merchant_id", String(args.merchant_id)) if (args.search != null) query.append("search", String(args.search)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment-links${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(paymentLinkListResponse, await res.json()) };