import {z} from 'zod' export type GetApiNetworksCampaignsArgs = {page?: number | undefined, page_size?: number | undefined, relationship?: ('reporting' | 'redirects' | 'offers' | 'catch_all_offer_basic') | undefined}; export type GetApiNetworksCampaignsOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const redirect = z.object({network_campaign_redirect_id: z.number().int().optional(), network_campaign_id: z.number().int().optional(), redirect_network_offer_id: z.number().int().optional(), redirect_network_offer_url_id: z.number().int().optional(), routing_value: z.number().int().optional(), ruleset: z.object({}).optional()}); export const smartLink = z.object({network_campaign_id: z.number().int().optional(), network_id: z.number().int().optional(), campaign_name: z.string().optional(), campaign_status: z.enum(["active", "paused", "deleted"]).optional(), network_tracking_domain_id: z.number().int().optional(), is_use_secure_link: z.boolean().optional(), redirect_routing_type: z.enum(["priority", "weight", "kpi"]).optional(), catch_all_network_offer_id: z.number().int().optional(), is_open_to_affiliates: z.boolean().optional(), run_frequency: z.enum(["unknown", "1_hours", "3_hours", "6_hours", "12_hours", "24_hours"]).optional(), metric: z.enum(["unknown", "profit", "cvr", "evr", "conversions", "payout", "revenue", "rpc", "cpc", "epc"]).optional(), optimization_goal: z.number().optional(), data_lookback_window: z.enum(["unknown", "12_hours", "24_hours", "48_hours"]).optional(), data_collection_threshold: z.number().int().optional(), time_created: z.number().int().optional(), time_saved: z.number().int().optional(), relationship: z.object({redirects: z.object({total: z.number().int().optional(), entries: z.array(redirect).optional()}).optional(), labels: z.object({total: z.number().int().optional(), entries: z.array(z.string()).optional()}).optional(), catch_all_offer_basic: z.object({network_offer_id: z.number().int().optional(), network_id: z.number().int().optional(), name: z.string().optional(), offer_status: z.string().optional()}).optional(), encoded_value: z.string().optional()}).optional()}); export const getApiNetworksCampaignsResponse = z.object({campaigns: z.array(smartLink).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 }; /** * List Smart Links * * GET /networks/campaigns */ export const getApiNetworksCampaigns = async (args: GetApiNetworksCampaignsArgs, options?: GetApiNetworksCampaignsOptions) => { 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.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.relationship != null) query.append("relationship", String(args.relationship)) if (APIKey !== undefined) headers.set("X-Eflow-Api-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/networks/campaigns${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(getApiNetworksCampaignsResponse, await res.json()) };