import {z} from 'zod' export type GetApiV1ShoppersMeCartsActiveArgs = {token?: string | undefined, expand?: string | undefined, fields?: string | undefined}; export type GetApiV1ShoppersMeCartsActiveOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const defaultCustomAttributes = z.object({attribute: z.array(z.object({name: z.string().optional(), value: z.string().optional(), type: z.string().optional()})).optional()}); export const defaultProduct = z.object({uri: z.string().optional(), parentProduct: z.object({uri: z.string().optional()}).optional(), id: z.string().optional(), name: z.string().optional(), displayName: z.string().optional(), shortDescription: z.string().optional(), longDescription: z.string().optional(), productType: z.string().optional(), sku: z.string().optional(), externalReferenceId: z.string().optional(), companyId: z.string().optional(), displayableProduct: z.string().optional(), purchasable: z.string().optional(), manufacturerName: z.string().optional(), manufacturerPartNumber: z.string().optional(), thumbnailImage: z.string().optional(), productImage: z.string().optional(), keywords: z.string().optional(), customAttributes: defaultCustomAttributes.optional()}); export const defaultLineItemPrice = z.object({listPrice: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), listPriceWithQuantity: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), salePriceWithQuantity: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedListPrice: z.string().optional(), formattedListPriceWithQuantity: z.string().optional(), formattedSalePriceWithQuantity: z.string().optional(), formattedCommitmentPrice: z.string().optional(), commitmentPrice: z.object({currency: z.string().optional(), value: z.string().optional()}).optional(), productTax: z.object({currency: z.string().optional(), value: z.string().optional()}).optional(), shippingTax: z.object({currency: z.string().optional(), value: z.string().optional()}).optional(), feeTax: z.object({currency: z.string().optional(), value: z.string().optional()}).optional(), taxRate: z.string().optional(), importTax: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedImportTax: z.string().optional(), importDuty: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedImportDuty: z.string().optional()}); export const defaultCurrency = z.object({currency: z.string().optional(), value: z.string().optional()}); export const lineItemFeePricing = z.object({name: z.string().optional(), typeCode: z.string().optional(), listPrice: defaultCurrency.optional(), listPriceWithQuantity: defaultCurrency.optional(), salePriceWithQuantity: defaultCurrency.optional(), formattedListPrice: z.string().optional(), formattedListPriceWithQuantity: z.string().optional(), formattedSalePriceWithQuantity: z.string().optional(), tax: z.object({currency: z.string().optional(), value: z.string().optional()}).optional()}); export const expandedLineItemPrice = z.object({listPrice: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), listPriceWithQuantity: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), salePrice: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), salePriceWithQuantity: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedListPrice: z.string().optional(), formattedListPriceWithQuantity: z.string().optional(), formattedSalePrice: z.string().optional(), formattedSalePriceWithQuantity: z.string().optional(), totalDiscountWithQuantity: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedTotalDiscountWithQuantity: z.string().optional(), formattedCommitmentPrice: z.string().optional(), commitmentPrice: z.object({currency: z.string().optional(), value: z.string().optional()}).optional(), productTax: z.object({currency: z.string().optional(), value: z.string().optional()}).optional(), shippingTax: z.object({currency: z.string().optional(), value: z.string().optional()}).optional(), feeTax: z.object({currency: z.string().optional(), value: z.string().optional()}).optional(), taxRate: z.string().optional(), discountDescription: z.string().optional(), importTax: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedImportTax: z.string().optional(), importDuty: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedImportDuty: z.string().optional(), feePricing: z.object({fee: z.array(lineItemFeePricing).optional()}).optional()}); export const componentsBlock = z.object({lineItem: z.array(z.object({id: z.string().optional(), quantity: z.string().optional(), product: z.object({uri: z.string().optional(), displayName: z.string().optional(), thumbnailImage: z.string().optional()}).optional(), pricing: expandedLineItemPrice.optional()})).optional()}); export const groupDefaultObject = z.object({id: z.string().optional(), relationType: z.string().optional(), offer: z.object({uri: z.string().optional()}).optional()}); export const groupDefaultArray = z.object({group: z.array(groupDefaultObject).optional()}); export const defaultLineItem = z.object({uri: z.string().optional(), id: z.string().optional(), quantity: z.string().optional(), product: defaultProduct.optional(), pricing: defaultLineItemPrice.optional(), components: componentsBlock.optional(), groups: z.array(groupDefaultArray).optional()}); export const paymentMethodObjectPaymentSourceCharge = z.object({chargeId: z.string().optional(), amount: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), status: z.enum(["pending", "capturable", "processing", "complete", "cancelled", "failed"]).optional(), createdTime: z.string().optional(), updatedTime: z.string().optional()}); export const paymentMethodObjectSupplementaryPaymentSourceCharge = z.object({chargeId: z.string().optional(), amount: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), status: z.enum(["pending", "capturable", "processing", "complete", "cancelled", "failed"]).optional(), createdTime: z.string().optional(), updatedTime: z.string().optional()}); export const paymentMethodObjectSupplementaryPaymentSource = z.object({type: z.string().optional(), sourceId: z.string().optional(), sourceClientSecret: z.string().optional(), customerCredit: z.object({flow: z.enum(["standard", "redirect", "receiver"]).optional(), reusable: z.string().optional()}).optional(), charges: z.array(paymentMethodObjectSupplementaryPaymentSourceCharge).optional(), amountContributed: z.object({currency: z.string().optional(), value: z.number().optional()}).optional()}); export const paymentMethodObjectPaymentSource = z.object({type: z.string().optional(), sourceId: z.string().optional(), sourceClientSecret: z.string().optional(), creditCard: z.object({expirationYear: z.string().optional(), lastFourDigits: z.string().optional(), clientSecret: z.string().optional(), expirationMonth: z.string().optional(), fundingSource: z.string().optional(), brand: z.string().optional(), reusable: z.string().optional()}).optional(), amountContributed: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), charges: z.array(paymentMethodObjectPaymentSourceCharge).optional(), supplementaryPaymentMethods: z.array(paymentMethodObjectSupplementaryPaymentSource).optional()}); export const paymentSessionWithAmount = z.object({id: z.string().optional(), status: z.string().optional(), clientSecret: z.string().optional(), redirectUrl: z.string().optional(), amountContributed: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), amountRemainingToBeContributed: z.object({currency: z.string().optional(), value: z.number().optional()}).optional()}); export const cartLandedCostState = z.string(); export const cartOrderLevelDefaultPrice = z.object({subtotal: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), discount: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), shippingAndHandling: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), importTaxAndDuty: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), tax: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), orderTotal: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedSubtotal: z.string().optional(), formattedDiscount: z.string().optional(), formattedShippingAndHandling: z.string().optional(), formattedImportTaxAndDuty: z.string().optional(), formattedTax: z.string().optional(), formattedOrderTotal: z.string().optional()}); export const taxRegistrations = z.array(z.object({key: z.string().optional(), value: z.string().optional()})); export const couponCodeNotEligibleWarning = z.object({errors: z.object({warning: z.array(z.object({code: z.literal("couponcode_not_eligible").optional(), description: z.literal("The relevant discount triggered by {coupon code} will be applied to the cart once it meets the offer criteria...").optional()})).optional()}).optional()}); export const defaultCart = z.object({uri: z.string().optional(), paymentMethods: z.object({uri: z.string().optional()}).optional(), applyPaymentMethod: z.object({uri: z.string().optional()}).optional(), submitCart: z.object({uri: z.string().optional()}).optional(), webCheckout: z.object({uri: z.string().optional()}).optional(), id: z.string().optional(), lineItems: z.object({uri: z.string().optional(), lineItem: z.array(defaultLineItem).optional()}).optional(), totalItemsInCart: z.string().optional(), businessEntityCode: z.string().optional(), billingAddress: z.object({uri: z.string().optional()}).optional(), shippingAddress: z.object({uri: z.string().optional()}).optional(), paymentMethod: paymentMethodObjectPaymentSource.optional(), payment: z.object({name: z.string().optional(), displayableNumber: z.string().optional(), expirationMonth: z.number().int().optional(), expirationYear: z.number().int().optional()}).optional(), paymentSession: paymentSessionWithAmount.optional(), shippingOptions: z.object({uri: z.string().optional()}).optional(), taxInclusive: z.boolean().optional(), landedCostState: cartLandedCostState.optional(), pricing: cartOrderLevelDefaultPrice.optional(), termsOfSalesAcceptance: z.string().optional(), chargeType: z.string().optional(), customerType: z.string().optional(), taxRegistrations: taxRegistrations.optional(), organizationId: z.string().optional(), warnings: couponCodeNotEligibleWarning.optional()}); export const productInventoryStatus = z.object({uri: z.string().optional(), availableQuantity: z.number().int().optional(), availableQuantityIsEstimated: z.string().optional(), productIsInStock: z.string().optional(), productIsAllowsBackorders: z.string().optional(), productIsTracked: z.string().optional(), requestedQuantityAvailable: z.string().optional(), status: z.string().optional(), statusIsEstimated: z.string().optional(), expectedInStockDate: z.string().optional(), customStockMessage: z.string().optional()}); export const expandedProduct = z.object({uri: z.string().optional(), parentProduct: z.object({uri: z.string().optional(), id: z.string().optional(), name: z.string().optional(), displayName: z.string().optional(), shortDescription: z.string().optional(), longDescription: z.string().optional(), productType: z.string().optional(), sku: z.string().optional(), companyId: z.string().optional(), displayableProduct: z.string().optional(), purchasable: z.string().optional(), manufacturerPartNumber: z.string().optional(), thumbnailImage: z.string().optional(), productImage: z.string().optional(), baseProduct: z.string().optional(), variationAttributes: z.object({attribute: z.object({name: z.string().optional(), domainValues: z.array(z.string()).optional()}).optional()}).optional()}).optional(), id: z.string().optional(), name: z.string().optional(), displayName: z.string().optional(), shortDescription: z.string().optional(), longDescription: z.string().optional(), productType: z.string().optional(), sku: z.string().optional(), companyId: z.string().optional(), displayableProduct: z.string().optional(), purchasable: z.string().optional(), thumbnailImage: z.string().optional(), productImage: z.string().optional(), baseProduct: z.string().optional(), customAttributes: defaultCustomAttributes.optional(), inventoryStatus: productInventoryStatus.optional()}); export const groupOfferExpandObject = z.object({uri: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), policyName: z.string().optional(), type: z.string().optional(), image: z.string().optional(), trigger: z.string().optional(), salesPitch: z.array(z.string()).optional(), customAttributes: defaultCustomAttributes.optional()}); export const groupExpandObject = z.object({id: z.string().optional(), relationType: z.string().optional(), offer: groupOfferExpandObject.optional()}); export const groupExpandArray = z.object({group: z.array(groupExpandObject).optional(), customAttributes: defaultCustomAttributes.optional()}); export const expandedLineItem = z.object({uri: z.string().optional(), id: z.string().optional(), quantity: z.number().int().optional(), product: expandedProduct.optional(), pricing: expandedLineItemPrice.optional(), components: componentsBlock.optional(), groups: z.array(groupExpandArray).optional()}); export const billingAddress = z.object({uri: z.string().optional(), relation: z.string().optional(), id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), companyName: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), line3: z.string().optional(), city: z.string().optional(), countrySubdivision: z.string().optional(), postalCode: z.string().optional(), country: z.string().optional(), countryName: z.string().optional(), phoneNumber: z.string().optional(), emailAddress: z.string().optional(), countyName: z.string().optional(), phoneticFirstName: z.string().optional(), phoneticLastName: z.string().optional(), division: z.string().optional(), title: z.string().optional()}); export const shippingAddress = z.object({uri: z.string().optional(), relation: z.string().optional(), id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), companyName: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), line3: z.string().optional(), city: z.string().optional(), countrySubdivision: z.string().optional(), postalCode: z.string().optional(), country: z.string().optional(), countryName: z.string().optional(), phoneNumber: z.string().optional(), countyName: z.string().optional(), emailAddress: z.string().optional(), phoneticFirstName: z.string().optional(), phoneticLastName: z.string().optional(), division: z.string().optional()}); export const defaultShippingOption = z.object({shippingOption: z.object({uri: z.string().optional(), id: z.number().int().optional(), description: z.string().optional(), cost: defaultCurrency.optional(), formattedCost: z.string().optional(), applyShippingOptionToCart: z.object({uri: z.string().optional()}).optional()}).optional()}); export const cartOrderLevelExpandPrice = z.object({subtotal: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), subtotalWithDiscount: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), discount: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), shippingAndHandling: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), importTaxAndDuty: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), tax: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), orderTotal: z.object({currency: z.string().optional(), value: z.number().optional()}).optional(), formattedSubtotal: z.string().optional(), formattedDiscount: z.string().optional(), formattedShippingAndHandling: z.string().optional(), formattedImportTaxAndDuty: z.string().optional(), formattedTax: z.string().optional(), formattedOrderTotal: z.string().optional()}); export const expandedCart = z.object({uri: z.string().optional(), paymentMethods: z.object({uri: z.string().optional()}).optional(), webCheckout: z.object({uri: z.string().optional()}).optional(), id: z.string().optional(), lineItems: z.object({uri: z.string().optional(), lineItem: z.array(expandedLineItem).optional()}).optional(), testOrder: z.string().optional(), sendEmail: z.string().optional(), totalItemsInCart: z.string().optional(), businessEntityCode: z.string().optional(), billingAddress: billingAddress.optional(), shippingAddress: shippingAddress.optional(), paymentMethod: paymentMethodObjectPaymentSource.optional(), shippingMethod: z.object({code: z.string().optional(), description: z.string().optional()}).optional(), shippingOptions: z.object({uri: z.string().optional(), shippingOption: defaultShippingOption.optional()}).optional(), paymentSession: paymentSessionWithAmount.optional(), taxInclusive: z.boolean().optional(), landedCostState: cartLandedCostState.optional(), pricing: cartOrderLevelExpandPrice.optional(), customAttributes: defaultCustomAttributes.optional(), errors: z.object({error: z.object({code: z.string().optional(), subcode: z.string().optional(), description: z.string().optional()}).optional()}).optional(), termsOfSalesAcceptance: z.string().optional(), chargeType: z.string().optional(), customerType: z.string().optional(), taxRegistrations: taxRegistrations.optional(), organizationId: z.string().optional(), warnings: couponCodeNotEligibleWarning.optional()}); export const getApiV1ShoppersMeCartsActiveResponse = z.union([defaultCart, expandedCart]); 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 the current cart * * GET /v1/shoppers/me/carts/active */ export const getApiV1ShoppersMeCartsActive = async (args: GetApiV1ShoppersMeCartsActiveArgs, options?: GetApiV1ShoppersMeCartsActiveOptions) => { const {baseUrl = "https://api.digitalriver.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.token != null) query.append("token", String(args.token)) if (args.expand != null) query.append("expand", String(args.expand)) if (args.fields != null) query.append("fields", String(args.fields)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/shoppers/me/carts/active${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(getApiV1ShoppersMeCartsActiveResponse, await res.json()) };