import {z} from 'zod' export type GetApiAffiliatesTrafficblockingArgs = {network_offer_id?: number | undefined, traffic_blocking_status?: ('active' | 'inactive') | undefined}; export type GetApiAffiliatesTrafficblockingOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAffiliatesTrafficblockingResponse = z.object({traffic_blocking: z.array(z.object({network_offer_id: z.number().int().optional(), relationship: z.object({variables: z.object({total: z.number().int().optional(), entries: z.array(z.object({comparison_method: z.string().optional(), variable: z.string().optional(), variable_value: z.string().optional()})).optional()}).optional()}).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 }; /** * Find All Blocked Offer Sources * * GET /affiliates/trafficblocking */ export const getApiAffiliatesTrafficblocking = async (args: GetApiAffiliatesTrafficblockingArgs, options?: GetApiAffiliatesTrafficblockingOptions) => { 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.traffic_blocking_status != null) query.append("traffic_blocking_status", String(args.traffic_blocking_status)) if (APIKey !== undefined) headers.set("X-Eflow-Api-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/affiliates/trafficblocking${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(getApiAffiliatesTrafficblockingResponse, await res.json()) };