import {z} from 'zod' export type DiscountParametersUpdate = {name?: string | undefined, code?: string | undefined, discount_type?: ('code' | 'automatic' | 'sale') | undefined, discount_method?: ('percentage' | 'fixed' | 'shipping' | 'bogo') | undefined, percent?: string | undefined, amount?: string | undefined, currency?: string | undefined, applies_to?: ('all_products' | 'specific_products' | 'specific_variants' | 'specific_collections') | undefined, apply_from?: string | undefined, apply_until?: (string | null) | undefined, expired?: boolean | undefined, limit_1_per_customer?: boolean | undefined, require_minimum_spend?: boolean | undefined, require_minimum_spend_amount?: number | undefined, discounts_per_redemption?: number | undefined, product_ids?: Array | undefined, variant_ids?: Array | undefined, products_collection_ids?: Array | undefined}; export type PatchApiDiscountsIdArgs = {id: string, body: {discount?: DiscountParametersUpdate | undefined}}; export type PatchApiDiscountsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const discountAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), workspace_id: z.number().int().optional(), name: z.string().optional(), code: z.string().optional(), discount_type: z.enum(["code", "automatic", "sale"]).optional(), discount_method: z.enum(["percentage", "fixed", "shipping", "bogo"]).optional(), amount: z.string().nullable().optional(), percent: z.string().nullable().optional(), currency: z.string().optional(), applies_to: z.enum(["all_products", "specific_products", "specific_variants", "specific_collections"]).optional(), apply_from: z.string().optional(), apply_until: z.string().nullable().optional(), limit_1_per_customer: z.boolean().optional(), require_minimum_spend: z.boolean().optional(), require_minimum_spend_amount: z.number().int().nullable().optional(), discounts_per_redemption: z.number().int().nullable().optional(), redemptions_count: z.number().int().optional(), product_ids: z.array(z.number().int()).optional(), variant_ids: z.array(z.number().int()).optional(), products_collection_ids: z.array(z.number().int()).optional(), active: z.boolean().optional(), expired: z.boolean().optional(), scheduled: z.boolean().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().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 }; /** * Update Discount * * PATCH /discounts/{id} * * Also accepts auth: OAuth2 */ export const patchApiDiscountsId = async (args: PatchApiDiscountsIdArgs, options?: PatchApiDiscountsIdOptions) => { const {baseUrl = "https://myworkspace.myclickfunnels.com/api/v2", 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(/\/+$/, '')}/discounts/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(discountAttributes, await res.json()) };