import {z} from 'zod' export type PromoterProfile = {id?: number | undefined, first_name?: string | undefined, last_name?: string | undefined, website?: (string | null) | undefined, company_name?: (string | null) | undefined, company_number?: (string | null) | undefined, phone_number?: (string | null) | undefined, vat_id?: (string | null) | undefined, country?: (string | null) | undefined, address?: (string | null) | undefined, avatar?: string | undefined, w8_form_url?: (string | null) | undefined, w9_form_url?: (string | null) | undefined, description?: (string | null) | undefined, invoice_details_validation_errors?: (Record | null) | undefined, should_validate_invoice_details?: boolean | undefined, instagram_url?: (string | null) | undefined, youtube_url?: (string | null) | undefined, linkedin_url?: (string | null) | undefined, facebook_url?: (string | null) | undefined, twitter_url?: (string | null) | undefined, twitch_url?: (string | null) | undefined, tiktok_url?: (string | null) | undefined}; export type PromoterUpdate = {email?: string | undefined, find_by?: ('email' | 'cust_id' | 'auth_token' | 'ref_token' | 'promo_code') | undefined, cust_id?: (string | null) | undefined, custom_fields?: Array | undefined, profile?: PromoterProfile | undefined, 'profile[_destroy_w8form]'?: boolean | undefined, 'profile[_destroy_w9form]'?: boolean | undefined}; export type UpdateApiPromotersIdArgs = {'Account-ID': string, id: number, body: PromoterUpdate}; export type UpdateApiPromotersIdOptions = {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 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 }; /** * Update promoter * * PUT /promoters/{id} */ export const updateApiPromotersId = async (args: UpdateApiPromotersIdArgs, options?: UpdateApiPromotersIdOptions) => { const {baseUrl = "https://api.firstpromoter.com/api/v2/company", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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(/\/+$/, '')}/promoters/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(promoter, await res.json()) };