import {z} from 'zod' export type InvoiceItem = {product_id: number, product_name: string, product_sku?: (string | null) | undefined, quantity: number, price: number}; export type CreateInvoiceRequest = {order_id: string, amount: number, description?: string | undefined, phone?: (string | null) | undefined, name?: (string | null) | undefined, customer_id?: (string | null) | undefined, location_id?: (string | null) | undefined, success_url?: (string | null) | undefined, failure_url?: (string | null) | undefined, items?: Array | undefined}; export type CreateApiApiV1PaylinkInvoiceArgs = {body: CreateInvoiceRequest}; export type CreateApiApiV1PaylinkInvoiceOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createInvoiceResponse = z.object({payment_url: z.string(), invoice_uid: z.string()}); 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 Paylink Invoice * * POST /api/v1/paylink/invoice */ export const createApiApiV1PaylinkInvoice = async (args: CreateApiApiV1PaylinkInvoiceArgs, options: CreateApiApiV1PaylinkInvoiceOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/paylink/invoice${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(createInvoiceResponse, await res.json()) };