import {z} from 'zod' export type AffiliatePatchInput = {network_affiliate_ids: Array, fields: Array<{field_type: 'network_employee_id' | 'account_executive_id' | 'referrer_id' | 'labels' | 'affiliate_name' | 'account_status' | 'default_currency_id' | 'has_notifications' | 'enable_media_cost_tracking_links' | 'network_traffic_source_id' | 'internal_notes' | 'billing_payment_method' | 'billing_billing_frequency' | 'billing_weekly' | 'billing_bimonthly' | 'billing_monthly' | 'billing_two_months' | 'billing_quarterly' | 'billing_is_invoice_creation_auto' | 'billing_invoice_amount_threshold' | 'billing_auto_invoice_start_date' | 'billing_invoice_generation_days_delay' | 'billing_default_payment_terms' | 'billing_default_invoice_is_hidden' | 'referral_is_enabled' | 'referral_method' | 'referral_value' | 'referral_commission_basis' | 'referral_minimum_threshold' | 'referral_duration', field_value: unknown, operator: 'overwrite' | 'append' | 'delete' | 'clear'}>}; export type CreateApiNetworksPatchAffiliatesSubmitArgs = {body: AffiliatePatchInput}; export type CreateApiNetworksPatchAffiliatesSubmitOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const affiliatePatchResponse = z.object({changes: z.array(z.object({network_affiliate: z.object({network_affiliate_id: z.number().int().optional(), network_id: z.number().int().optional(), name: z.string().optional(), account_status: z.string().optional()}).optional(), change_status: z.string().optional(), current_field: z.object({field_type: z.string().optional(), field_value: z.unknown(), operator: z.string().optional()}).optional(), changed_field: z.object({field_type: z.string().optional(), field_value: z.unknown(), operator: z.string().optional()}).optional(), error_message: z.string().optional(), related_changes: z.string().optional()})).optional(), resource_errors: z.array(z.object({id: z.number().int().optional(), label: z.string().optional(), message: z.string().optional()})).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 }; /** * Affiliate Bulk Edit Preview * * POST /networks/patch/affiliates/submit */ export const createApiNetworksPatchAffiliatesSubmit = async (args: CreateApiNetworksPatchAffiliatesSubmitArgs, options?: CreateApiNetworksPatchAffiliatesSubmitOptions) => { 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/patch/affiliates/submit${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(affiliatePatchResponse, await res.json()) };