import {z} from 'zod' export type GetApiAffiliatesPixelsArgs = {network_offer_id?: number | undefined, pixel_status?: ('active' | 'inactive') | undefined, pixel_type?: ('conversion' | 'post_conversion' | 'cpc') | undefined, delivery_method?: ('html' | 'postback' | 'facebook') | undefined}; export type GetApiAffiliatesPixelsOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const postback = z.object({network_pixel_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(), network_offer_payout_revenue_id: z.number().int().optional(), delivery_method: z.string().optional(), pixel_level: z.string().optional(), pixel_status: z.string().optional(), pixel_type: z.string().optional(), postback_url: z.string().optional(), html_code: z.string().optional(), delay_ms: z.number().int().optional(), time_created: z.number().int().optional(), time_saved: z.number().int().optional(), facebook_pixel: z.object({pixel_id: z.string().optional(), event_name: z.string().optional(), access_token: z.string().optional()}).nullable().optional(), tiktok_pixel: z.object({}).nullable().optional(), snapchat_pixel: z.object({}).nullable().optional(), rumble_pixel: z.object({}).nullable().optional(), relationship: z.object({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(), network_employee_id: z.number().int().optional(), internal_notes: z.string().optional(), has_notifications: z.boolean().optional(), network_traffic_source_id: z.number().int().optional(), account_executive_id: z.number().int().optional(), adress_id: z.number().int().optional(), default_currency_id: z.string().optional(), is_contact_address_enabled: z.boolean().optional(), enable_media_cost_tracking_links: z.boolean().optional(), time_created: z.number().int().optional(), time_saved: z.number().int().optional(), referrer_id: z.number().int().optional()}).optional(), offer: z.object({network_offer_id: z.number().int().optional(), network_id: z.number().int().optional(), network_advertiser_id: z.number().int().optional(), network_offer_group_id: z.number().int().optional(), name: z.string().optional(), offer_status: z.string().optional(), network_tracking_domain_id: z.number().int().optional(), visibility: z.string().optional(), currency_id: z.string().optional()}).optional(), payout_revenue: z.object({}).optional()}).optional()}); export const getApiAffiliatesPixelsResponse = z.object({pixels: z.array(postback).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 }; /** * Find All Postbacks * * GET /affiliates/pixels */ export const getApiAffiliatesPixels = async (args: GetApiAffiliatesPixelsArgs, options?: GetApiAffiliatesPixelsOptions) => { const {baseUrl = "https://api.eflow.team/v1", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.network_offer_id != null) query.append("network_offer_id", String(args.network_offer_id)) if (args.pixel_status != null) query.append("pixel_status", String(args.pixel_status)) if (args.pixel_type != null) query.append("pixel_type", String(args.pixel_type)) if (args.delivery_method != null) query.append("delivery_method", String(args.delivery_method)) if (APIKey !== undefined) headers.set("X-Eflow-Api-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/affiliates/pixels${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiAffiliatesPixelsResponse, await res.json()) };