import {z} from 'zod' export type CheckoutItemsDetailsRequest = {ownerUserId: string, ownerCompanyId: string, buyerUserId: string, buyerCompanyId: string, currency: string, productIds: Array, pricingPlanIds?: Array | undefined, bundleIds?: Array | undefined, subscriptionIds?: Array | undefined}; export type CreateApiApiV3CheckoutItemsArgs = {body: CheckoutItemsDetailsRequest}; export type CreateApiApiV3CheckoutItemsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const checkoutItemType = z.enum(["PRODUCT", "ADDON", "BUNDLE"]); export const branding = z.object({name: z.string().optional(), vendor: z.string().optional(), desc: z.string().optional(), iconUrl: z.string().optional(), href: z.string().optional(), storefrontUrl: z.string().optional()}); export const revenueModel = z.enum(["FREE", "ONE_TIME", "RECURRING", "TIERED"]); export const freeTrialDurationPeriod = z.enum(["DAY", "MONTH"]); export const freeTrialTerms = z.object({freeTrialDuration: z.number().int().optional(), freeTrialDurationPeriod: freeTrialDurationPeriod.optional(), freeTrialGracePeriod: z.number().int().optional()}); export const pricingPeriod = z.enum(["ONE_TIME", "DAILY", "MONTHLY", "QUARTERLY", "SIX_MONTHS", "YEARLY", "TWO_YEARS", "THREE_YEARS"]); export const pricingSourceType = z.enum(["PURCHASE_CHANNEL", "PURCHASE_RESELLER_COMPANY", "RESELLER_COMPANY_CATALOG", "CHANNEL_CATALOG", "BASE_CATALOG"]); export const costTypeCategory = z.enum(["RECURRING", "ONE_TIME"]); export const costType = z.enum(["RECURRING_FLAT", "ONE_TIME_FLAT", "INCLUDED", "SETUP_PER_UNIT", "RECURRING_PER_UNIT", "ONE_TIME_PER_UNIT", "SETUP_FEE", "CONTRACT_FEE", "DISCOUNT_PER_UNIT", "DISCOUNT_FLAT", "METERED_USAGE"]); export const pricingStrategy = z.enum(["FREE", "FLAT", "UNIT", "TIERED", "VOLUME", "VARIABLE_PRICE_STRATEGIES", "UNLIMITED", "CUSTOM"]); export const priceRange = z.object({min: z.number(), max: z.number().optional(), price: z.number(), salePrice: z.string().optional(), wholesalePrice: z.string().optional()}); export const pricingDefinition = z.object({costTypeCategory: costTypeCategory.optional(), costType: costType.optional(), pricingStrategy: pricingStrategy.optional(), unit: z.string().optional(), priceRanges: z.array(priceRange).optional(), unitAllowsDecimals: z.boolean().optional(), increment: z.number().optional(), allowsMeteredUsage: z.boolean().optional(), displaysPricePerIncrement: z.boolean().optional()}); export const pricingSource = z.object({source: pricingSourceType.optional(), pricingDefinitions: z.array(pricingDefinition).optional()}); export const editionPricingPlan = z.object({id: z.string().optional(), currency: z.string().optional(), pricingPeriod: pricingPeriod.optional(), pricingSources: z.array(pricingSource).optional()}); export const edition = z.object({id: z.string().optional(), name: z.string().optional(), revenueModel: revenueModel.optional(), freeTrialTerms: freeTrialTerms.optional(), pricingPlans: z.array(editionPricingPlan).optional()}); export const checkoutItemDetails = z.object({id: z.string().optional(), type: checkoutItemType.optional(), stacked: z.boolean().optional(), branding: branding.optional(), editions: z.array(edition).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 }; /** * Get item details * * POST /api/v3/checkout/items */ export const createApiApiV3CheckoutItems = async (args: CreateApiApiV3CheckoutItemsArgs, options?: CreateApiApiV3CheckoutItemsOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", 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/v3/checkout/items${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(checkoutItemDetails, await res.json()) };