import {z} from 'zod' export type GetApiPaymentsArgs = {'x-simple-workspace': string, 'x-piriod-test-mode'?: boolean | undefined, page?: number | undefined, page_size?: number | undefined, ordering?: string | undefined, customer?: string | undefined, invoice?: string | undefined, source?: number | undefined, sources?: number | undefined, status?: ('cancelled' | 'failed' | 'incomplete' | 'processing' | 'requires_payment_method' | 'succeeded') | undefined, amount?: number | undefined, balance?: number | undefined, currency?: string | undefined, date?: string | undefined, payment_date?: string | undefined, description?: string | undefined}; export type GetApiPaymentsOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const payment = z.object({id: z.number().int().optional(), invoice: z.string().nullable().optional(), subscription: z.string().nullable().optional(), customer: z.string().nullable().optional(), amount: z.number(), balance: z.number().optional(), currency: z.string(), date: z.string(), payment_date: z.string().nullable().optional(), description: z.string().max(256), source: z.number().int().nullable().optional(), sources: z.array(z.number().int()).optional(), status: z.enum(["cancelled", "failed", "incomplete", "processing", "requires_payment_method", "succeeded"]).optional(), failed_reason: z.string().nullable().optional(), return_url: z.string().nullable().optional(), enable_checkout: z.boolean().optional(), metadata: z.object({}).optional(), test_mode: z.boolean().optional(), created: z.string().optional(), updated: z.string().optional()}); export const getApiPaymentsResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(payment)}); 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 payments * * GET /payments/ */ export const getApiPayments = async (args: GetApiPaymentsArgs, options?: GetApiPaymentsOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-simple-workspace'] != null) headers.set("x-simple-workspace", String(args['x-simple-workspace'])) if (args['x-piriod-test-mode'] != null) headers.set("x-piriod-test-mode", String(args['x-piriod-test-mode'])) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.customer != null) query.append("customer", String(args.customer)) if (args.invoice != null) query.append("invoice", String(args.invoice)) if (args.source != null) query.append("source", String(args.source)) if (args.sources != null) query.append("sources", String(args.sources)) if (args.status != null) query.append("status", String(args.status)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.balance != null) query.append("balance", String(args.balance)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.date != null) query.append("date", String(args.date)) if (args.payment_date != null) query.append("payment_date", String(args.payment_date)) if (args.description != null) query.append("description", String(args.description)) if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${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(getApiPaymentsResponse, await res.json()) };