import {z} from 'zod' export type Description = {defaultDescription?: string | undefined, localizedDescriptions?: {[key: string]: string} | undefined, params?: {[key: string]: string} | undefined}; export type RevenueShare = {appdirect?: number | undefined, channel?: number | undefined, vendor?: number | undefined}; export type Fee = {id?: string | undefined, externalId?: string | undefined, createdOn?: string | undefined, key?: string | undefined, currency?: string | undefined, amount?: number | undefined, description?: Description | undefined, revenueShare?: RevenueShare | undefined, itemIds?: Array | undefined, metadata?: {[key: string]: string} | undefined}; export type CreateApiV3CheckoutShoppingCartsShoppingCartIdFeesArgs = {shoppingCartId: string, 'AD-ShoppingCart-Partner': string, body: Fee}; export type CreateApiV3CheckoutShoppingCartsShoppingCartIdFeesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const description = z.object({defaultDescription: z.string().optional(), localizedDescriptions: z.record(z.string(), z.string()).optional(), params: z.record(z.string(), z.string()).optional()}); export const revenueShare = z.object({appdirect: z.number().optional(), channel: z.number().optional(), vendor: z.number().optional()}); export const fee = z.object({id: z.string().optional(), externalId: z.string().optional(), createdOn: z.string().optional(), key: z.string().optional(), currency: z.string().optional(), amount: z.number().optional(), description: description.optional(), revenueShare: revenueShare.optional(), itemIds: z.array(z.string()).optional(), metadata: z.record(z.string(), z.string()).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 }; /** * Create cart fees * * POST /v3/checkout/shoppingCarts/{shoppingCartId}/fees */ export const createApiV3CheckoutShoppingCartsShoppingCartIdFees = async (args: CreateApiV3CheckoutShoppingCartsShoppingCartIdFeesArgs, options?: CreateApiV3CheckoutShoppingCartsShoppingCartIdFeesOptions) => { 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") if (args['AD-ShoppingCart-Partner'] != null) headers.set("AD-ShoppingCart-Partner", String(args['AD-ShoppingCart-Partner'])) const url = `${baseUrl.replace(/\/+$/, '')}/v3/checkout/shoppingCarts/${encodeURIComponent(String(args.shoppingCartId))}/fees${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(fee, await res.json()) };