import {z} from 'zod' export type GetApiOrderFormsLagoIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orderFormStatusEnum = z.enum(["generated", "signed", "expired", "voided"]); export const orderFormVoidReasonEnum = z.enum(["null", "manual", "expired", "invalid"]).nullable(); export const orderFormObject = z.object({lago_id: z.string(), number: z.string(), status: orderFormStatusEnum, void_reason: orderFormVoidReasonEnum, expires_at: z.string().nullable(), signed_at: z.string().nullable(), voided_at: z.string().nullable(), signed_document_url: z.string().nullable(), lago_organization_id: z.string(), lago_customer_id: z.string(), lago_quote_id: z.string(), lago_quote_version_id: z.string(), created_at: z.string(), updated_at: z.string()}); export const orderForm = z.object({order_form: orderFormObject}); 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 an order form * * GET /order_forms/{lago_id} */ export const getApiOrderFormsLagoId = async (options?: GetApiOrderFormsLagoIdOptions) => { const {baseUrl = "https://api.getlago.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/order_forms/${encodeURIComponent(String(args.lago_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(orderForm, await res.json()) };