import {z} from 'zod' export type ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiPromotionLimits = {maximumUse?: number | undefined, oneUsePerCustomer: boolean}; export type Money = {value: number, currency: string}; export type ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiPromotionRequirementsShop = {minimumOrderValue?: Money | undefined}; export type ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiUpdatePromotionAppliesToEntireOrder = {type: string}; export type ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiUpdatePromotionAppliesToSelectedProducts = {productIds: Array, oncePerOrder?: boolean | undefined}; export type ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiUpdatePromotionRequest = {limits?: ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiPromotionLimits | undefined, requirements?: ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiPromotionRequirementsShop | undefined, appliesTo?: (ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiUpdatePromotionAppliesToEntireOrder | ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiUpdatePromotionAppliesToSelectedProducts) | undefined, status?: ('LIVE' | 'ENDED') | undefined}; export type UpdateApiOpenApiV10PromotionsPromotionIdArgs = {promotionId: string, body: ComFourthwallAppOpenapiEndpointModelRequestsPromotionsOpenApiUpdatePromotionRequest}; export type UpdateApiOpenApiV10PromotionsPromotionIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const money = z.object({value: z.number().gte(0), currency: z.string()}); export const comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FixedAmount = z.object({type: z.literal("FLAT_RATE"), money: money, freeShipping: z.boolean()}); export const comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProductsAutoAddedFreeProduct = z.object({variantId: z.string(), quantity: z.number().int()}); export const comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProductsTypeAutoAdded = z.object({type: z.literal("AUTO_ADDED"), products: z.array(comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProductsAutoAddedFreeProduct)}); export const comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProductsManuallyAddedFreeProduct = z.object({availableVariantIds: z.array(z.string()), quantity: z.number().int()}); export const comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProductsTypeManuallyAdded = z.object({type: z.literal("MANUALLY_ADDED"), products: z.array(comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProductsManuallyAddedFreeProduct)}); export const comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProducts = z.object({type: z.literal("FREE_PRODUCTS"), freeProductsType: z.union([comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProductsTypeAutoAdded, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProductsTypeManuallyAdded]), freeShipping: z.boolean()}); export const comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeShipping = z.object({type: z.literal("FREE_SHIPPING"), lowestOnly: z.boolean()}); export const comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1Percentage = z.object({type: z.literal("PERCENTAGE"), percentage: z.number(), shippingOption: z.enum(["Included", "Excluded", "Free", "FreeLowestOnly"]).optional()}); export const comFourthwallOpenapiModelPromotionsPromotionRequirementsV1Shop = z.object({minimumOrderValue: money.optional()}); export const comFourthwallOpenapiModelPromotionsPromotionLimitsV1 = z.object({maximumUsesNumber: z.number().int().optional(), oneUsePerCustomer: z.boolean().optional()}); export const comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1EntireOrder = z.object({type: z.literal("ENTIRE_ORDER")}); export const comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1EntireOrderWithExcludedProducts = z.object({type: z.literal("ENTIRE_ORDER_WITH_EXCLUDED_PRODUCTS"), excludedProducts: z.array(z.string())}); export const comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1SelectedProducts = z.object({type: z.literal("SELECTED_PRODUCTS"), products: z.array(z.string()), oncePerOrder: z.boolean()}); export const comFourthwallOpenapiModelPromotionsAutoApplyingShopPromotionV1 = z.object({type: z.literal("SHOP_AUTO_APPLYING"), id: z.string(), title: z.string(), code: z.string(), discount: z.union([comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FixedAmount, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProducts, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeShipping, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1Percentage]), status: z.enum(["Live", "Ended", "Archived", "AllUsed"]), requirements: comFourthwallOpenapiModelPromotionsPromotionRequirementsV1Shop, usageCount: z.number().int(), limits: comFourthwallOpenapiModelPromotionsPromotionLimitsV1, appliesTo: z.union([comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1EntireOrder, comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1EntireOrderWithExcludedProducts, comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1SelectedProducts])}); export const comFourthwallOpenapiModelPromotionsMultiPromoSubCodeV1 = z.object({id: z.string(), code: z.string(), usageCount: z.number().int(), status: z.enum(["Live", "Ended", "Archived", "AllUsed"])}); export const comFourthwallOpenapiModelPromotionsPromotionMembershipsDiscountTypeV1Percentage = z.object({type: z.literal("MEMBERSHIPS_PERCENTAGE"), percentage: z.number()}); export const comFourthwallOpenapiModelPromotionsPromotionRequirementsV1Memberships = z.object({newMembersOnly: z.boolean()}); export const comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1AllMembers = z.object({type: z.literal("ALL")}); export const comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1AnnualMembersOnly = z.object({type: z.literal("ANNUAL")}); export const comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1MonthlyMembersOnly = z.object({type: z.literal("MONTHLY")}); export const comFourthwallOpenapiModelPromotionsPromotionMembershipsTiersAppliesToV1AllTiers = z.object({type: z.literal("ALL")}); export const comFourthwallOpenapiModelPromotionsPromotionMembershipsTiersAppliesToV1SelectedTiers = z.object({type: z.literal("SELECTED"), tiersIds: z.array(z.string())}); export const comFourthwallOpenapiModelPromotionsMultiMembershipsPromotionV1 = z.object({type: z.literal("MEMBERSHIPS_MULTI"), id: z.string(), title: z.string(), codes: z.array(comFourthwallOpenapiModelPromotionsMultiPromoSubCodeV1), discount: comFourthwallOpenapiModelPromotionsPromotionMembershipsDiscountTypeV1Percentage, status: z.enum(["Live", "Ended", "Archived", "AllUsed"]), requirements: comFourthwallOpenapiModelPromotionsPromotionRequirementsV1Memberships, usageCount: z.number().int(), subscriptionType: z.union([comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1AllMembers, comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1AnnualMembersOnly, comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1MonthlyMembersOnly]), tiers: z.union([comFourthwallOpenapiModelPromotionsPromotionMembershipsTiersAppliesToV1AllTiers, comFourthwallOpenapiModelPromotionsPromotionMembershipsTiersAppliesToV1SelectedTiers])}); export const comFourthwallOpenapiModelPromotionsMultiShopPromotionV1 = z.object({type: z.literal("SHOP_MULTI"), id: z.string(), title: z.string(), codes: z.array(comFourthwallOpenapiModelPromotionsMultiPromoSubCodeV1), discount: z.union([comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FixedAmount, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProducts, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeShipping, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1Percentage]), status: z.enum(["Live", "Ended", "Archived", "AllUsed"]), requirements: comFourthwallOpenapiModelPromotionsPromotionRequirementsV1Shop, limits: comFourthwallOpenapiModelPromotionsPromotionLimitsV1, usageCount: z.number().int(), appliesTo: z.union([comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1EntireOrder, comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1EntireOrderWithExcludedProducts, comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1SelectedProducts])}); export const comFourthwallOpenapiModelPromotionsSingleMembershipsPromotionV1 = z.object({type: z.literal("MEMBERSHIPS_SINGLE"), id: z.string(), code: z.string(), discount: comFourthwallOpenapiModelPromotionsPromotionMembershipsDiscountTypeV1Percentage, status: z.enum(["Live", "Ended", "Archived", "AllUsed"]), requirements: comFourthwallOpenapiModelPromotionsPromotionRequirementsV1Memberships, usageCount: z.number().int(), subscriptionType: z.union([comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1AllMembers, comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1AnnualMembersOnly, comFourthwallOpenapiModelPromotionsPromotionMembershipsSubscriptionTypeAppliesToV1MonthlyMembersOnly]), tiers: z.union([comFourthwallOpenapiModelPromotionsPromotionMembershipsTiersAppliesToV1AllTiers, comFourthwallOpenapiModelPromotionsPromotionMembershipsTiersAppliesToV1SelectedTiers])}); export const comFourthwallOpenapiModelPromotionsSingleShopPromotionV1 = z.object({type: z.literal("SHOP_SINGLE"), id: z.string(), code: z.string(), discount: z.union([comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FixedAmount, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeProducts, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1FreeShipping, comFourthwallOpenapiModelPromotionsPromotionShopDiscountTypeV1Percentage]), status: z.enum(["Live", "Ended", "Archived", "AllUsed"]), requirements: comFourthwallOpenapiModelPromotionsPromotionRequirementsV1Shop, usageCount: z.number().int(), limits: comFourthwallOpenapiModelPromotionsPromotionLimitsV1, appliesTo: z.union([comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1EntireOrder, comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1EntireOrderWithExcludedProducts, comFourthwallOpenapiModelPromotionsPromotionShopAppliesToV1SelectedProducts])}); export const updateApiOpenApiV10PromotionsPromotionIdResponse = z.union([comFourthwallOpenapiModelPromotionsAutoApplyingShopPromotionV1, comFourthwallOpenapiModelPromotionsMultiMembershipsPromotionV1, comFourthwallOpenapiModelPromotionsMultiShopPromotionV1, comFourthwallOpenapiModelPromotionsSingleMembershipsPromotionV1, comFourthwallOpenapiModelPromotionsSingleShopPromotionV1]); 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 }; /** * Update a promotion * * PUT /open-api/v1.0/promotions/{promotionId} * * Also accepts auth: basicAuth */ export const updateApiOpenApiV10PromotionsPromotionId = async (args: UpdateApiOpenApiV10PromotionsPromotionIdArgs, options?: UpdateApiOpenApiV10PromotionsPromotionIdOptions) => { const {baseUrl = "https://api.fourthwall.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/open-api/v1.0/promotions/${encodeURIComponent(String(args.promotionId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiOpenApiV10PromotionsPromotionIdResponse, await res.json()) };