import {z} from 'zod' export type GetApiV1PaymentLinksOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1PaymentLinksResponse = z.object({data: z.array(z.object({active: z.boolean().optional(), amount: z.string().nullable().optional(), configurations: z.object({paymentlimit: z.number().optional()}).nullable().optional(), createdAt: z.string().optional(), createdChannel: z.string().optional(), currency: z.string().optional(), description: z.string().optional(), id: z.string().optional(), imageUrl: z.string().nullable().optional(), inactiveMessage: z.string().nullable().optional(), metadata: z.object({}).nullable().optional(), name: z.string().optional(), network: z.string().optional(), redirectUrl: z.string().nullable().optional(), slug: z.string().optional(), successMessage: z.unknown(), type: z.string().optional(), updatedAt: z.string().optional()})).optional(), message: z.string().optional(), meta: z.object({currentPage: z.number().optional(), itemCount: z.number().optional(), itemsPerPage: z.number().optional(), totalItems: z.number().optional(), totalPages: z.number().optional()}).optional(), statusCode: z.number().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 All * * GET /v1/payment_links */ export const getApiV1PaymentLinks = async (options?: GetApiV1PaymentLinksOptions) => { const {baseUrl = "https://api.blockradar.co", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiV1PaymentLinksResponse, await res.json()) };