import {z} from 'zod' export type DiscountCodeType = 'percentage' | 'fixed'; export type UpdateCampaignDiscountCodeRequest = {items?: (Array | null) | undefined, include_future?: boolean | undefined, amount: number, type?: DiscountCodeType | undefined, active?: boolean | undefined, code?: string | undefined, uses?: (number | null) | undefined, starts_at?: (string | null) | undefined, expires_at?: (string | null) | undefined}; export type UpdateApiV1CampaignsCampaignDiscountCodesDiscountCodeArgs = {campaign: number, discountCode: number, body: UpdateCampaignDiscountCodeRequest}; export type UpdateApiV1CampaignsCampaignDiscountCodesDiscountCodeOptions = {baseUrl?: string | undefined, http?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignDiscountCodeResource = z.object({id: z.string(), campaign_id: z.string(), items: z.string().optional(), applies_to: z.object({item_ids: z.string()}).optional(), code: z.string(), type: z.string(), amount: z.number(), used: z.string(), uses: z.string(), active: z.string(), all_items: z.string().optional(), include_future: z.string().optional(), total_discount: z.string(), starts_at: z.string(), expires_at: z.string(), created_at: z.string(), updated_at: z.string()}); 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 discount code * * PUT /v1/campaigns/{campaign}/discount-codes/{discountCode} */ export const updateApiV1CampaignsCampaignDiscountCodesDiscountCode = async (args: UpdateApiV1CampaignsCampaignDiscountCodesDiscountCodeArgs, options?: UpdateApiV1CampaignsCampaignDiscountCodesDiscountCodeOptions) => { const {baseUrl = "https://api.givebutter.com", fetch: fetchFn = fetch, http} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (http !== undefined) headers.set('authorization', `Bearer ${http}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/campaigns/${encodeURIComponent(String(args.campaign))}/discount-codes/${encodeURIComponent(String(args.discountCode))}${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(campaignDiscountCodeResource, await res.json()) };