import {z} from 'zod' export type CustomCapInput = {network_affiliate_id: number, network_offer_id: number, name?: string | undefined, daily_conversion_cap?: number | undefined, weekly_conversion_cap?: number | undefined, monthly_conversion_cap?: number | undefined, global_conversion_cap?: number | undefined, daily_payout_cap?: number | undefined, weekly_payout_cap?: number | undefined, monthly_payout_cap?: number | undefined, global_payout_cap?: number | undefined, daily_click_cap?: number | undefined, weekly_click_cap?: number | undefined, monthly_click_cap?: number | undefined, global_click_cap?: number | undefined, daily_revenue_cap?: number | undefined, weekly_revenue_cap?: number | undefined, monthly_revenue_cap?: number | undefined, global_revenue_cap?: number | undefined, is_soft_cap?: boolean | undefined}; export type CreateApiNetworksCustomCapsArgs = {body: CustomCapInput}; export type CreateApiNetworksCustomCapsOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const customCap = z.object({network_custom_cap_setting_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(), name: z.string().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_payout_cap: z.number().optional(), weekly_payout_cap: z.number().optional(), monthly_payout_cap: z.number().optional(), global_payout_cap: z.number().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_revenue_cap: z.number().optional(), weekly_revenue_cap: z.number().optional(), monthly_revenue_cap: z.number().optional(), global_revenue_cap: z.number().optional(), time_created: z.number().int().optional(), time_saved: z.number().int().optional(), is_soft_cap: z.boolean().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()}).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()}).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 }; /** * Create Custom Cap * * POST /networks/custom/caps */ export const createApiNetworksCustomCaps = async (args: CreateApiNetworksCustomCapsArgs, options?: CreateApiNetworksCustomCapsOptions) => { 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/custom/caps${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(customCap, await res.json()) };