import {z} from 'zod' export type GetApiReferralsArgs = {'Account-ID': string, q?: string | undefined, 'ids[]'?: Array | undefined, 'filters[type]'?: ('lead' | 'customer') | undefined, 'filters[promoter_id]'?: string | undefined, 'filters[state]'?: ('subscribed' | 'signup' | 'active' | 'cancelled' | 'refunded' | 'denied' | 'pending' | 'moved') | undefined, 'filters[customer_since][from]'?: string | undefined, 'filters[customer_since][to]'?: string | undefined, 'filters[created_at][from]'?: string | undefined, 'filters[created_at][to]'?: string | undefined, 'filters[cancelled_at][from]'?: string | undefined, 'filters[cancelled_at][to]'?: string | undefined}; export type GetApiReferralsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const promoter = z.object({id: z.number().int().optional(), status: z.string().optional(), cust_id: z.string().optional(), email: z.string().optional(), created_at: z.string().optional(), temp_password: z.string().nullable().optional(), default_promotion_id: z.number().int().optional(), pref: z.string().optional(), default_ref_id: z.string().optional(), note: z.string().nullable().optional(), w8_form_url: z.string().nullable().optional(), w9_form_url: z.string().nullable().optional(), parent_promoter_id: z.number().int().optional(), earnings_balance: z.object({cash: z.number().int().optional()}).optional(), current_balance: z.object({cash: z.number().int().optional()}).optional(), paid_balance: z.object({cash: z.number().int().optional()}).optional(), auth_token: z.string().optional()}); export const campaign = z.object({id: z.number().int().optional(), name: z.string().optional(), color: z.string().nullable().optional()}); export const promoterCampaign = z.object({id: z.number().int().optional(), campaign_id: z.number().int().optional(), promoter_id: z.number().int().optional(), created_at: z.string().optional(), promoter: promoter.optional(), campaign: campaign.optional()}); export const referral = z.object({id: z.number().int().optional(), email: z.string().optional(), uid: z.string().optional(), state: z.enum(["subscribed", "signup", "active", "cancelled", "refunded", "denied", "pending", "moved"]).optional(), metadata: z.object({}).optional(), entry_source: z.enum(["api", "coupon", "cookie", "manual_admin", "manual_affiliate"]).optional(), created_at: z.string().optional(), customer_since: z.string().nullable().optional(), promoter_campaign: promoterCampaign.optional(), fraud_check: z.enum(["no_suspicion", "same_ip_suspicion", "same_promoter_email", "ad_source"]).optional(), created_by_user_email: z.string().nullable().optional(), username: z.string().nullable().optional(), split_details: z.array(z.object({promoter_campaign_id: z.number().int().optional(), promoter_id: z.number().int().optional(), percentage: z.number().int().optional()})).nullable().optional(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), website: z.string().nullable().optional(), comment: z.string().nullable().optional(), is_expired: z.boolean().optional()}); export const getApiReferralsResponse = z.array(referral); 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 }; /** * Get available referrals * * GET /referrals */ export const getApiReferrals = async (args: GetApiReferralsArgs, options?: GetApiReferralsOptions) => { const {baseUrl = "https://api.firstpromoter.com/api/v2/company", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Account-ID'] != null) headers.set("Account-ID", String(args['Account-ID'])) if (args.q != null) query.append("q", String(args.q)) for (const item of args['ids[]'] ?? []) query.append("ids[]", String(item)) if (args['filters[type]'] != null) query.append("filters[type]", String(args['filters[type]'])) if (args['filters[promoter_id]'] != null) query.append("filters[promoter_id]", String(args['filters[promoter_id]'])) if (args['filters[state]'] != null) query.append("filters[state]", String(args['filters[state]'])) if (args['filters[customer_since][from]'] != null) query.append("filters[customer_since][from]", String(args['filters[customer_since][from]'])) if (args['filters[customer_since][to]'] != null) query.append("filters[customer_since][to]", String(args['filters[customer_since][to]'])) if (args['filters[created_at][from]'] != null) query.append("filters[created_at][from]", String(args['filters[created_at][from]'])) if (args['filters[created_at][to]'] != null) query.append("filters[created_at][to]", String(args['filters[created_at][to]'])) if (args['filters[cancelled_at][from]'] != null) query.append("filters[cancelled_at][from]", String(args['filters[cancelled_at][from]'])) if (args['filters[cancelled_at][to]'] != null) query.append("filters[cancelled_at][to]", String(args['filters[cancelled_at][to]'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/referrals${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(getApiReferralsResponse, await res.json()) };