import {z} from 'zod' export type GetApiAffiliatesAlloffersArgs = {page?: number | undefined, page_size?: number | undefined, search?: string | undefined, category_id?: number | undefined}; export type GetApiAffiliatesAlloffersOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAffiliatesAlloffersResponse = z.object({offers: z.array(z.object({network_offer_id: z.number().int().optional(), network_id: z.number().int().optional(), name: z.string().optional(), offer_status: z.string().optional(), currency_id: z.string().optional(), visibility: z.string().optional(), thumbnail_url: z.string().optional(), html_description: z.string().optional(), is_description_plain_text: z.boolean().optional(), preview_url: z.string().optional(), tracking_url: z.string().optional(), impression_tracking_url: z.string().optional(), network_category_id: z.number().int().optional(), network_tracking_domain_id: z.number().int().optional(), app_identifier: z.string().optional(), is_caps_enabled: z.boolean().optional(), caps_timezone_id: z.number().int().optional(), daily_conversion_cap: z.number().int().optional(), weekly_conversion_cap: z.number().int().optional(), monthly_conversion_cap: z.number().int().optional(), global_conversion_cap: z.number().int().optional(), daily_click_cap: z.number().int().optional(), weekly_click_cap: z.number().int().optional(), monthly_click_cap: z.number().int().optional(), global_click_cap: z.number().int().optional(), daily_payout_cap: z.number().optional(), weekly_payout_cap: z.number().optional(), monthly_payout_cap: z.number().optional(), global_payout_cap: z.number().optional(), is_force_terms_and_conditions: z.boolean().optional(), is_using_explicit_terms_and_conditions: z.boolean().optional(), terms_and_conditions: z.string().optional(), is_use_direct_linking: z.boolean().optional(), is_using_suppression_list: z.boolean().optional(), suppression_list_id: z.number().int().optional(), date_live_until: z.string().optional(), time_created: z.number().int().optional(), time_saved: z.number().int().optional(), relationship: z.object({offer_affiliate_status: z.string().optional()}).optional()})).optional(), paging: z.object({page: z.number().int().optional(), page_size: z.number().int().optional(), total_count: z.number().int().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 Visible Offers * * GET /affiliates/alloffers */ export const getApiAffiliatesAlloffers = async (args: GetApiAffiliatesAlloffersArgs, options?: GetApiAffiliatesAlloffersOptions) => { 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.search != null) query.append("search", String(args.search)) if (args.category_id != null) query.append("category_id", String(args.category_id)) if (APIKey !== undefined) headers.set("X-Eflow-Api-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/affiliates/alloffers${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(getApiAffiliatesAlloffersResponse, await res.json()) };