import {z} from 'zod' export type GetApiPromoterCampaignsArgs = {'Account-ID': string}; export type GetApiPromoterCampaignsOptions = {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 getApiPromoterCampaignsResponse = z.array(promoterCampaign); 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 promoter campaigns * * GET /promoter_campaigns */ export const getApiPromoterCampaigns = async (args: GetApiPromoterCampaignsArgs, options?: GetApiPromoterCampaignsOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/promoter_campaigns${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(getApiPromoterCampaignsResponse, await res.json()) };