import {z} from 'zod' export type DateFilterDto = {isnull?: boolean | undefined, notnull?: boolean | undefined, gt?: string | undefined, gte?: string | undefined, lt?: string | undefined, lte?: string | undefined}; export type GetApiApiV1PaymentLinksArgs = {page?: number | undefined, limit?: number | undefined, createdAt?: DateFilterDto | undefined, ids?: string | undefined, isActive?: boolean | undefined}; export type GetApiApiV1PaymentLinksOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dateFilterDto = z.object({isnull: z.boolean().optional(), notnull: z.boolean().optional(), gt: z.string().optional(), gte: z.string().optional(), lt: z.string().optional(), lte: z.string().optional()}); export const paginationFilterDto = z.object({page: z.number().gte(1).optional(), limit: z.number().gte(1).lte(200).optional(), createdAt: dateFilterDto.optional()}); export const linkType = z.enum(["one_time", "multiple"]); export const cryptoCurrency = z.enum(["usdc", "usdt", "pyusd", "eurc", "brett", "dai", "btc", "eth", "matic", "bnb", "sol", "busd", "trx", "food", "bonk", "wif", "goat", "spx", "truf", "pengu"]); export const customFieldType = z.enum(["text", "numeric", "dropdown"]); export const customFieldLabelType = z.literal("custom"); export const customFieldLabelDto = z.object({type: customFieldLabelType.optional(), custom: z.string()}); export const paymentLinkCustomFieldsDto = z.object({key: z.string(), type: customFieldType, label: customFieldLabelDto, optional: z.boolean()}); export const paymentLinkCustomFieldDto = z.object({fields: z.array(paymentLinkCustomFieldsDto).optional()}); export const submitType = z.enum(["pay", "book", "donate"]); export const afterCompletionLink = z.enum(["redirect", "hosted_confirmation"]); export const paymentLinkChainDto = z.object({chainId: z.number(), isPreferred: z.boolean()}); export const interval = z.enum(["day", "week", "month", "year"]); export const priceType = z.enum(["one_time", "recurring", "streaming"]); export const getApiApiV1PaymentLinksResponse = z.object({page: z.number(), limit: z.number(), count: z.number(), hasMore: z.boolean(), query: paginationFilterDto.optional(), data: z.array(z.object({id: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), organizationId: z.string(), type: linkType, amount: z.number().int().optional(), currency: cryptoCurrency.optional(), currencies: z.array(cryptoCurrency).optional(), preferredCurrency: cryptoCurrency.optional(), title: z.string(), description: z.string().optional(), image: z.string().optional(), publicImage: z.string().optional(), tags: z.array(z.string()), isActive: z.boolean().optional(), nameCollection: z.boolean(), emailCollection: z.boolean(), phoneNumberCollection: z.boolean(), shippingAddressCollection: z.boolean(), billingAddressCollection: z.boolean(), customFields: paymentLinkCustomFieldDto.optional(), submitType: submitType, afterCompletion: afterCompletionLink, afterCompletionRedirectUrl: z.string().optional(), afterCompletionConfirmMsg: z.string().optional(), suggestedCurrency: cryptoCurrency.optional(), suggestedAmounts: z.array(z.string()).optional(), productId: z.string().optional(), allowedChains: z.array(paymentLinkChainDto).optional(), interval: interval.optional(), intervalCount: z.number().optional(), priceType: priceType, allowPromotionCodes: z.boolean().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 }; /** * List of payment links * * GET /api/v1/payment-links */ export const getApiApiV1PaymentLinks = async (args: GetApiApiV1PaymentLinksArgs, options?: GetApiApiV1PaymentLinksOptions) => { const {baseUrl = "https://api.copperx.dev", fetch: fetchFn = fetch, bearer} = 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.createdAt != null) query.append("createdAt", JSON.stringify(args.createdAt)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.isActive != null) query.append("isActive", String(args.isActive)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) 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(getApiApiV1PaymentLinksResponse, await res.json()) };