import {z} from 'zod' export type GetApiV1ShopsShopIdAffiliatesStatsOptions = {baseUrl?: string | undefined, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ShopsShopIdAffiliatesStatsResponse = z.object({total_affiliates: z.number().int().optional(), active_affiliates: z.number().int().optional(), inactive_affiliates: z.number().int().optional(), new_affiliates: z.number().int().optional(), new_affiliates_previous: z.number().int().optional(), commissions_usd: z.number().int().optional(), commissions_usd_previous: z.number().int().optional(), commissions_usd_all_time: z.number().int().optional(), attributed_revenue_usd: z.number().int().optional(), attributed_revenue_usd_previous: z.number().int().optional(), attributed_revenue_usd_all_time: z.number().int().optional(), commissionable_revenue_usd: z.number().int().optional(), commissionable_revenue_usd_previous: z.number().int().optional(), commissionable_revenue_usd_all_time: z.number().int().optional(), attributed_orders: z.number().int().optional(), skipped_commissions: z.number().int().optional(), skipped_commissions_by_reason: z.array(z.unknown()).optional(), average_commission_rate: z.number().int().optional(), top_earner_usd: z.number().int().optional(), top_performers: z.array(z.object({id: z.number().int().optional(), email: z.string().optional(), shop_id: z.number().int().optional(), balance: z.string().optional(), affiliate_balance: z.string().optional(), affiliate_code: z.string().optional(), affiliate_code_set_at: z.string().optional(), affiliate_tier_id: z.string().nullable().optional(), affiliate_referrer_earnings: z.string().optional(), created_at: z.string().optional(), referrals_count: z.number().int().optional(), affiliate_tier: z.string().nullable().optional()})).optional(), balance_mode: z.string().optional(), pending_payout_requests: z.number().int().optional(), window_days: z.number().int().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 }; /** * Affiliate Stats * * GET /v1/shops/{shopId}/affiliates/stats */ export const getApiV1ShopsShopIdAffiliatesStats = async (options?: GetApiV1ShopsShopIdAffiliatesStatsOptions) => { const {baseUrl = "https://api.sellauth.com", fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (defaultValue !== undefined) headers.set('authorization', `Bearer ${defaultValue}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/shops/${encodeURIComponent(String(args.shopId))}/affiliates/stats${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(getApiV1ShopsShopIdAffiliatesStatsResponse, await res.json()) };