import {z} from 'zod' export type GetApiPaymentLinksLinksIdOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentLink = z.object({id: z.number().int().optional(), name: z.string().max(56), publishable_key: z.string().optional(), cta: z.enum(["book", "donate", "pay"]).optional(), amount: z.number().nullable().optional(), amount_min: z.number().nullable().optional(), amount_max: z.number().nullable().optional(), amount_preset: z.number().nullable().optional(), amount_scheme: z.enum(["custom", "fixed"]).optional(), currency: z.string(), customer_type: z.enum(["individual", "business", "both"]).optional(), description: z.string().nullable().optional(), end_description: z.string().nullable().optional(), hosted_page_title: z.string().max(32).nullable().optional(), hosted_page_description: z.string().nullable().optional(), hosted_page_layout: z.enum(["vertical", "horizontal"]).optional(), image: z.string().nullable().optional(), return_url: z.string().nullable().optional(), create_invoice: z.boolean().optional(), document: z.number().int().nullable().optional(), status: z.enum(["active", "archived"]).optional(), test_mode: z.boolean().optional(), created: z.string().optional(), updated: 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 }; /** * Retrieve payment link * * GET /payment_links/links/{id}/ */ export const getApiPaymentLinksLinksId = async (options?: GetApiPaymentLinksLinksIdOptions) => { 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 (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/payment_links/links/${encodeURIComponent(String(args.id))}/${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(paymentLink, await res.json()) };