import {z} from 'zod' export type GetApiMeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMeResponse = z.object({id: z.number().optional(), team: z.object({id: z.number().optional(), name: z.string().optional(), tracking_token: z.string().optional(), days_until_account_inactive: z.number().optional(), total_user_count: z.number().optional(), enabled_user_count: z.number().optional(), paid_user_count: z.number().optional(), free_users: z.number().optional(), subscribed: z.boolean().optional(), payment_type: z.enum(["stripe", "free", "appstack"]).optional(), discount_code: z.string().optional(), discount_type: z.string().optional(), promo_description: z.string().optional(), promo_redeem_by: z.string().optional(), promo_code: z.string().optional(), plan: z.number().optional(), currency: z.object({id: z.number().optional(), html: z.string().optional(), iso: z.string().optional()}).optional(), billing_currency: z.object({id: z.number().optional(), html: z.string().optional(), iso: z.string().optional()}).optional(), billing_frequency: z.enum(["monthly", "annually"]).optional(), creation_date: z.string().optional(), time_zone: z.string().optional(), stripe_customer_id: z.string().optional(), stripe_subscription_date: z.string().optional(), campaign: z.string().optional(), credit_usage: z.number().optional(), credit_quota: z.number().optional(), credit_package: z.enum(["S", "M", "L", "XL"]).optional(), input_token_usage: z.number().optional(), output_token_usage: z.number().optional(), ai_inference_usage: z.number().optional(), ai_inference_quota: z.number().optional(), email_tracking_subdomain: z.string().optional(), email_tracking_domain: z.object({url: z.string(), enabled: z.boolean(), timeZones: z.array(z.string()).optional()}).optional(), automated_tagging: z.boolean().optional(), domain: z.string().optional()}).optional(), group: z.number().optional(), amount_of_trial_days_earned: z.number().optional(), setup_completion_percentage: z.number().optional(), has_received_account_suggestions: z.boolean().optional(), has_archived_contacts: z.boolean().optional(), notification_channel_id: z.string().optional(), needs_password: z.boolean().optional(), disabled: z.boolean().optional(), creation_date: z.string().optional(), modification_date: z.string().optional(), prefix: z.string().optional(), firstname: z.string().optional(), middle: z.string().optional(), lastname: z.string().optional(), suffix: z.string().optional(), name: z.string().optional(), email: z.string().optional(), domain: z.string().optional(), picture: z.string().optional(), type: z.string().optional(), is_admin: z.boolean().optional(), credit_usage: z.number().optional(), is_importing: z.boolean().optional(), sync_status: z.string().optional(), calendar_sync_status: z.string().optional(), first_calendar_sync_done: z.boolean().optional(), trial_expired: z.boolean().optional(), trial_expiry_date: z.string().optional(), trial_extended: z.boolean().optional(), restricted: z.boolean().optional(), restricted_reason: z.string().optional(), team_discount_code: z.string().optional(), is_delinquent: z.boolean().optional(), input_token_usage: z.number().optional(), output_token_usage: z.number().optional(), ai_inference_usage: z.number().optional(), social_profiles: z.array(z.object({id: z.number().optional(), username: z.string().optional(), url: z.string().optional(), type: z.string().optional()})).optional(), phone_numbers: z.array(z.object({id: z.number().optional(), number: z.string().optional(), type: z.string().optional()})).optional(), positions: z.array(z.object({id: z.number().optional(), organisation: z.string().optional(), role: z.string().optional()})).optional(), addresses: z.array(z.object({})).optional(), tags: z.array(z.object({})).optional(), intercom_hash: z.string().optional(), first_day_of_week: z.number().optional(), theme: z.enum(["system", "light", "dark"]).optional(), language: z.enum(["en", "nl", "fr", "de", "pt", "br", "it", "es"]).optional(), am_pm_notation: z.boolean().optional(), auto_find_email_linkedin: z.boolean().optional(), data_sources_to_reconnect: z.array(z.object({id: z.number().optional(), email: z.string().optional(), type: z.string().optional()})).optional(), data_sources: z.array(z.object({id: z.number().optional(), email: z.string().optional(), type: z.string().optional(), sync_status: z.string().optional(), status: z.enum(["ERROR", "RECONNECT", "OK"]).optional(), status_message: z.string().optional()})).optional(), plan_flags: z.object({}).optional(), flags: z.array(z.object({id: z.number().optional(), flag: z.number(), name: z.string(), state: z.boolean(), user: z.number().optional(), plan: z.number().optional(), creation_date: z.string().optional(), modification_date: z.string().optional()})).optional(), role: z.object({id: z.number().optional(), name: z.string().optional(), permissions: z.object({view: z.enum(["CED", "VIEW"]).optional(), ced: z.enum(["CED", "VIEW"]).optional()}).optional()}).optional(), call_log_sync_date: z.string().optional(), two_fa_enabled: z.boolean().optional(), has_recently_failed_workflows: z.boolean().optional(), amount_of_failed_workflows: z.number().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 }; /** * Get current user's details * * GET /me */ export const getApiMe = async (options?: GetApiMeOptions) => { const {baseUrl = "https://api.salesflare.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/me${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(getApiMeResponse, await res.json()) };