import {z} from 'zod' export type CryptoCurrency = 'usdc' | 'usdt' | 'pyusd' | 'eurc' | 'brett' | 'dai' | 'btc' | 'eth' | 'matic' | 'bnb' | 'sol' | 'busd' | 'trx' | 'food' | 'bonk' | 'wif' | 'goat' | 'spx' | 'truf' | 'pengu'; export type Interval = 'day' | 'week' | 'month' | 'year'; export type EntityVisibility = string; export type PriceType = 'one_time' | 'recurring' | 'streaming'; export type ProductDataDto = {name: string, description?: string | undefined, images?: Array | undefined, unitLabel?: string | undefined, url?: string | undefined, metadata?: Record | undefined, visibility?: EntityVisibility | undefined}; export type CreatePriceDto = {currency: CryptoCurrency, interval?: Interval | undefined, intervalCount?: number | undefined, unitAmount?: number | undefined, customPreset?: number | undefined, customUnitAmountSuggestions?: Array | undefined, metadata?: Record | undefined, visibility?: EntityVisibility | undefined, nickname?: string | undefined, type: PriceType, productId?: string | undefined, productData?: ProductDataDto | undefined}; export type CreateApiApiV1PricesArgs = {body: CreatePriceDto}; export type CreateApiApiV1PricesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const billingScheme = z.literal("per_unit"); 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 interval = z.enum(["day", "week", "month", "year"]); export const recurringUsageType = z.literal("licensed"); export const priceType = z.enum(["one_time", "recurring", "streaming"]); export const entityVisibility = z.string(); export const publicProductDto = z.object({id: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), name: z.string(), description: z.string().optional(), isActive: z.boolean().optional(), images: z.array(z.string()).optional(), publicImages: z.array(z.string()).optional(), unitLabel: z.string().optional(), url: z.string().optional(), defaultPriceId: z.string().optional(), metadata: z.object({}).optional(), visibility: entityVisibility.optional()}); export const priceDto = z.object({id: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), billingScheme: billingScheme.optional(), currency: cryptoCurrency, productId: z.string(), interval: interval.optional(), intervalCount: z.number().optional(), isActive: z.boolean().optional(), usageType: recurringUsageType.optional(), type: priceType, unitAmount: z.number().int().optional(), unitAmountDecimal: z.string().optional(), customUnitMax: z.number().int().optional(), customUnitMin: z.number().int().optional(), customPreset: z.number().int().optional(), customUnitAmountSuggestions: z.array(z.string()).optional(), product: publicProductDto, metadata: z.object({}).optional(), visibility: entityVisibility.optional(), nickname: z.string().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 }; /** * Create a price * * POST /api/v1/prices */ export const createApiApiV1Prices = async (args: CreateApiApiV1PricesArgs, options?: CreateApiApiV1PricesOptions) => { const {baseUrl = "https://api.copperx.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/prices${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(priceDto, await res.json()) };