import {z} from 'zod' export type GetApiBillingV1OrdersArgs = {startDateFrom?: number | undefined, startDateTo?: number | undefined, status?: ('INITIALIZED' | 'PENDING_USER_APPROVAL' | 'PENDING_REMOTE_CREATION' | 'PENDING_MANUAL_RECOVERY' | 'PENDING_ASYNCHRONOUS_CREATION' | 'FREE_TRIAL' | 'ACTIVE' | 'FINISHED' | 'ONE_TIME' | 'CANCELLED' | 'SUSPENDED' | 'FREE_TRIAL_EXPIRED' | 'FREE_TRIAL_CANCELLED' | 'DELETED' | 'FAILED' | 'UPCOMING' | 'PENDING_MIGRATION_ACTIVATION' | 'PENDING_DELAYED_PROVISIONING') | undefined, currency?: ('USD' | 'CAD' | 'EUR' | 'JPY' | 'GBP' | 'KRW' | 'CHF' | 'SEK' | 'SGD' | 'MYR' | 'AUD' | 'MXN' | 'INR' | 'BRL' | 'DKK' | 'NZD' | 'NOK' | 'ZAR' | 'PHP' | 'CNY' | 'SAR' | 'GTQ' | 'IDR' | 'ARS' | 'COP' | 'PEN') | undefined, type?: ('NEW' | 'MIGRATION' | 'ADOPTION' | 'FREE_TRIAL_ACTIVATION' | 'TERMINATION_FEE' | 'END_OF_CONTRACT_MIGRATION' | 'REFUND' | 'CREDIT' | 'ONE_TIME_FEE' | 'ADDITIONAL_FEE' | 'END_OF_DISCOUNT_MIGRATION' | 'METERED_USAGE') | undefined, frequency?: ('ONE_TIME' | 'MONTHLY' | 'QUARTERLY' | 'SIX_MONTHS' | 'YEARLY' | 'TWO_YEARS' | 'THREE_YEARS' | 'DAILY') | undefined, orderReferenceCode?: string | undefined, applicationIds?: number | undefined, fromCreationDate?: number | undefined, toCreationDate?: number | undefined, page?: number | undefined, referenceCode?: string | undefined, size?: number | undefined, sortField?: ('DATE' | 'ORDER_ID' | 'TOTAL') | undefined, sortOrder?: ('ASC' | 'DESC') | undefined}; export type GetApiBillingV1OrdersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string().optional(), rel: z.string().optional()}).nullable(); export const linkWS = z.object({href: z.string().optional(), id: z.string().optional()}).nullable(); export const orderListingWS = z.object({links: z.array(link).optional(), _embedded: z.record(z.string(), z.object({}).nullable()).nullable().optional(), company: linkWS.optional(), currency: z.string().nullable().optional(), endDate: z.number().nullable().optional(), endOfDiscountDate: z.number().nullable().optional(), frequency: z.string().nullable().optional(), id: z.number().nullable().optional(), parentSubscriptionId: z.number().nullable().optional(), serviceStartDate: z.number().nullable().optional(), nextBillingDate: z.number().nullable().optional(), referenceCode: z.string().nullable().optional(), salesSupportUser: linkWS.optional(), salesSupportCompany: linkWS.optional(), startDate: z.number().nullable().optional(), status: z.string().nullable().optional(), totalPrice: z.number().nullable().optional(), type: z.string().nullable().optional(), user: linkWS.optional(), paymentPlan: linkWS.optional()}).nullable(); 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 all purchase orders * * GET /billing/v1/orders */ export const getApiBillingV1Orders = async (args: GetApiBillingV1OrdersArgs, options?: GetApiBillingV1OrdersOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDateFrom != null) query.append("startDateFrom", String(args.startDateFrom)) if (args.startDateTo != null) query.append("startDateTo", String(args.startDateTo)) if (args.status != null) query.append("status", String(args.status)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.type != null) query.append("type", String(args.type)) if (args.frequency != null) query.append("frequency", String(args.frequency)) if (args.orderReferenceCode != null) query.append("orderReferenceCode", String(args.orderReferenceCode)) if (args.applicationIds != null) query.append("applicationIds", String(args.applicationIds)) if (args.fromCreationDate != null) query.append("fromCreationDate", String(args.fromCreationDate)) if (args.toCreationDate != null) query.append("toCreationDate", String(args.toCreationDate)) if (args.page != null) query.append("page", String(args.page)) if (args.referenceCode != null) query.append("referenceCode", String(args.referenceCode)) if (args.size != null) query.append("size", String(args.size)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) const url = `${baseUrl.replace(/\/+$/, '')}/billing/v1/orders${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(orderListingWS, await res.json()) };