import {z} from 'zod' export type UpdateApiNetworksCouponcodesCouponCodeIdArgs = {couponCodeId: number, body: {network_offer_id: number, network_affiliate_id?: number | undefined, coupon_code: string, coupon_status: 'active' | 'paused', internal_notes?: string | undefined, description?: string | undefined, is_description_plain_text?: boolean | undefined, start_date?: string | undefined, end_date?: string | undefined}}; export type UpdateApiNetworksCouponcodesCouponCodeIdOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const couponCodeWrite = z.object({network_coupon_code_id: z.number().int().optional(), network_id: z.number().int().optional(), network_affiliate_id: z.number().int().optional(), network_offer_id: z.number().int().optional(), coupon_code: z.string().optional(), coupon_status: z.enum(["active", "paused"]).optional(), internal_notes: z.string().optional(), description: z.string().optional(), is_description_plain_text: z.boolean().optional(), start_date: z.string().optional(), end_date: z.string().optional(), time_created: z.number().int().optional(), time_saved: z.number().int().optional(), time_deleted: z.number().int().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 Coupon Code * * PUT /networks/couponcodes/{couponCodeId} */ export const updateApiNetworksCouponcodesCouponCodeId = async (args: UpdateApiNetworksCouponcodesCouponCodeIdArgs, options?: UpdateApiNetworksCouponcodesCouponCodeIdOptions) => { const {baseUrl = "https://api.eflow.team/v1", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-Eflow-Api-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/networks/couponcodes/${encodeURIComponent(String(args.couponCodeId))}${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(couponCodeWrite, await res.json()) };