import {z} from 'zod' export type ServiceStrategy = 'CUSTOM' | 'IMMEDIATELY'; export type ServiceConfiguration = {date?: string | undefined, strategy?: ServiceStrategy | undefined}; export type BillingStrategy = 'CUSTOM' | 'NEXT_BILLING_CYCLE' | 'UPON_SERVICE_ACTIVATION'; export type BillingConfiguration = {date?: number | undefined, strategy?: BillingStrategy | undefined}; export type CustomPriceType = 'RESELLER_PURCHASE_PRICE' | 'MARKETPLACE_RESELLER_PURCHASE_PRICE'; export type PricingStrategy = 'FREE' | 'FLAT' | 'UNIT' | 'TIERED' | 'VOLUME' | 'VARIABLE_PRICE_STRATEGIES' | 'UNLIMITED' | 'CUSTOM'; export type PriceRange = {min: number, max?: number | undefined, price: number, salePrice?: unknown | undefined, wholesalePrice?: unknown | undefined}; export type Definition = {price?: number | undefined, priceRanges?: Array | undefined}; export type CustomPrice = {costId: string, priceType?: CustomPriceType | undefined, pricingStrategy?: PricingStrategy | undefined, definition?: Definition | undefined}; export type Unit = {quantity?: number | undefined, unit?: string | undefined}; export type CustomAttributes = {[key: string]: unknown}; export type ItemCustomAttributes = {vendorRequiredFields?: CustomAttributes | undefined, subscription?: CustomAttributes | undefined}; export type MinimumServiceLengthUnit = 'DAILY' | 'MONTHLY' | 'QUARTERLY' | 'SIX_MONTHS' | 'YEARLY' | 'TWO_YEARS' | 'THREE_YEARS'; export type GracePeriodUnit = 'NONE' | 'INFINITY' | 'DAY' | 'MONTH'; export type CancellationPeriodUnit = 'NONE' | 'DAY'; export type UnitContractTerms = {blockContractIncrease?: boolean | undefined, blockContractDecrease?: boolean | undefined, blockOriginalContractDecrease?: boolean | undefined}; export type TerminationFeeType = 'FIXED' | 'PERCENTAGE'; export type TerminationFee = {feeType?: TerminationFeeType | undefined, amount?: number | undefined, description?: string | undefined}; export type CustomContractConfiguration = {minimumServiceLength: number, minimumServiceLengthUnit: MinimumServiceLengthUnit, endOfContractGracePeriod?: number | undefined, endOfContractGracePeriodUnit?: GracePeriodUnit | undefined, contractCancellationPeriodLimit?: number | undefined, contractCancellationPeriodUnit?: CancellationPeriodUnit | undefined, continueWithoutContract?: boolean | undefined, autoExtensionPricingId?: string | undefined, terminationFeeGracePeriod?: number | undefined, terminationFeeGracePeriodUnit?: GracePeriodUnit | undefined, blockContractUpgrades?: boolean | undefined, blockContractDowngrades?: boolean | undefined, blockSwitchToShorterContract?: boolean | undefined, keepContractDateOnPlanChange?: boolean | undefined, alignWithParentCycleStartDate?: boolean | undefined, unitTerms?: {[key: string]: UnitContractTerms} | undefined, sourceId?: string | undefined, terminationFee?: TerminationFee | undefined, contractFee?: number | undefined}; export type ShoppingCartItem = {id?: string | undefined, referenceId?: string | undefined, subscriptionId?: string | undefined, pricingPlanId?: string | undefined, serviceConfiguration?: ServiceConfiguration | undefined, billingConfiguration?: BillingConfiguration | undefined, discountCode?: string | undefined, discountId?: string | undefined, customPrices?: Array | undefined, units?: Array | undefined, customAttributes?: ItemCustomAttributes | undefined, customContractConfiguration?: CustomContractConfiguration | undefined}; export type CreateApiV3CheckoutShoppingCartsShoppingCartIdItemsArgs = {shoppingCartId: string, body: ShoppingCartItem}; export type CreateApiV3CheckoutShoppingCartsShoppingCartIdItemsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceStrategy = z.enum(["CUSTOM", "IMMEDIATELY"]); export const serviceConfiguration = z.object({date: z.string().optional(), strategy: serviceStrategy.optional()}); export const billingStrategy = z.enum(["CUSTOM", "NEXT_BILLING_CYCLE", "UPON_SERVICE_ACTIVATION"]); export const billingConfiguration = z.object({date: z.number().optional(), strategy: billingStrategy.optional()}); export const customPriceType = z.enum(["RESELLER_PURCHASE_PRICE", "MARKETPLACE_RESELLER_PURCHASE_PRICE"]); 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 definition = z.object({price: z.number().optional(), priceRanges: z.array(priceRange).optional()}); export const customPrice = z.object({costId: z.string(), priceType: customPriceType.optional(), pricingStrategy: pricingStrategy.optional(), definition: definition.optional()}); export const unit = z.object({quantity: z.number().optional(), unit: z.string().optional()}); export const customAttributes = z.record(z.string(), z.unknown()); export const itemCustomAttributes = z.object({vendorRequiredFields: customAttributes.optional(), subscription: customAttributes.optional()}); export const minimumServiceLengthUnit = z.enum(["DAILY", "MONTHLY", "QUARTERLY", "SIX_MONTHS", "YEARLY", "TWO_YEARS", "THREE_YEARS"]); export const gracePeriodUnit = z.enum(["NONE", "INFINITY", "DAY", "MONTH"]); export const cancellationPeriodUnit = z.enum(["NONE", "DAY"]); export const unitContractTerms = z.object({blockContractIncrease: z.boolean().optional(), blockContractDecrease: z.boolean().optional(), blockOriginalContractDecrease: z.boolean().optional()}); export const terminationFeeType = z.enum(["FIXED", "PERCENTAGE"]); export const terminationFee = z.object({feeType: terminationFeeType.optional(), amount: z.number().optional(), description: z.string().optional()}); export const customContractConfiguration = z.object({minimumServiceLength: z.number().int().gte(1), minimumServiceLengthUnit: minimumServiceLengthUnit, endOfContractGracePeriod: z.number().int().optional(), endOfContractGracePeriodUnit: gracePeriodUnit.optional(), contractCancellationPeriodLimit: z.number().int().optional(), contractCancellationPeriodUnit: cancellationPeriodUnit.optional(), continueWithoutContract: z.boolean().optional(), autoExtensionPricingId: z.string().optional(), terminationFeeGracePeriod: z.number().int().optional(), terminationFeeGracePeriodUnit: gracePeriodUnit.optional(), blockContractUpgrades: z.boolean().optional(), blockContractDowngrades: z.boolean().optional(), blockSwitchToShorterContract: z.boolean().optional(), keepContractDateOnPlanChange: z.boolean().optional(), alignWithParentCycleStartDate: z.boolean().optional(), unitTerms: z.record(z.string(), unitContractTerms).optional(), sourceId: z.string().optional(), terminationFee: terminationFee.optional(), contractFee: z.number().optional()}); export const shoppingCartItem = z.object({id: z.string().optional(), referenceId: z.string().optional(), subscriptionId: z.string().optional(), pricingPlanId: z.string().optional(), serviceConfiguration: serviceConfiguration.optional(), billingConfiguration: billingConfiguration.optional(), discountCode: z.string().optional(), discountId: z.string().optional(), customPrices: z.array(customPrice).optional(), units: z.array(unit).optional(), customAttributes: itemCustomAttributes.optional(), customContractConfiguration: customContractConfiguration.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 }; /** * Add items to cart * * POST /v3/checkout/shoppingCarts/{shoppingCartId}/items */ export const createApiV3CheckoutShoppingCartsShoppingCartIdItems = async (args: CreateApiV3CheckoutShoppingCartsShoppingCartIdItemsArgs, options?: CreateApiV3CheckoutShoppingCartsShoppingCartIdItemsOptions) => { 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(/\/+$/, '')}/v3/checkout/shoppingCarts/${encodeURIComponent(String(args.shoppingCartId))}/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(shoppingCartItem, await res.json()) };