import {z} from 'zod' export type CreditServiceEntityType = 'business_data' | 'prospect_data' | 'business_event_data' | 'prospect_event_data' | 'custom_signals_data' | 'business_data_free_first_visit' | 'prospect_data_free_first_visit'; export type CreditServiceOperationType = 'consume_data' | 'business_match' | 'prospect_match' | 'business_enrich_level_1' | 'business_enrich_level_2' | 'business_enrich_level_3' | 'prospect_enrich_level_1' | 'prospect_enrich_level_2' | 'prospect_enrich_level_3' | 'prospect_enrich_bc_email' | 'prospect_enrich_bc_phone' | 'business_full_fetch' | 'business_partial_fetch' | 'prospect_full_fetch' | 'prospect_partial_fetch' | 'business_read_event' | 'prospect_read_event' | 'consume_signal' | 'business_enrich_free_first_visit' | 'prospect_enrich_free_first_visit' | 'business_full_fetch_premium' | 'prospect_full_fetch_premium' | 'business_enrich_level_1_sample' | 'business_enrich_level_2_sample' | 'business_enrich_level_3_sample' | 'business_enrich_free_first_visit_sample' | 'prospect_enrich_level_1_sample' | 'prospect_enrich_level_2_sample' | 'prospect_enrich_level_3_sample' | 'prospect_enrich_free_first_visit_sample' | 'business_full_fetch_sample' | 'prospect_full_fetch_sample' | 'business_full_fetch_premium_sample' | 'prospect_full_fetch_premium_sample' | 'business_stats' | 'prospect_stats' | 'autocomplete' | 'mcp_business_match_sample' | 'mcp_prospect_match_sample' | 'mcp_business_read_event_sample' | 'mcp_prospect_read_event_sample' | 'mcp_business_push_event_sample' | 'mcp_prospect_push_event_sample' | 'mcp_business_stats_sample' | 'mcp_prospect_stats_sample' | 'mcp_business_fixed_sample' | 'mcp_prospect_fixed_sample' | 'lock_credits' | 'settle_credit_lock' | 'release_credit_lock' | 'expire_credit_lock'; export type V2CreditLockRequest = {entity_type: CreditServiceEntityType, operation_types: Array, entities_amount: number, endpoint_name?: string | undefined, ttl_hours?: number | undefined}; export type CreateApiV2CreditsLocksArgs = {body: V2CreditLockRequest}; export type CreateApiV2CreditsLocksOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const creditLockResponse = z.object({credit_lock_id: z.string(), status: z.string(), credit_cost: z.number().int().optional(), credit_costs: z.record(z.string(), z.number().int()).optional(), locked_credits: z.number().int(), consumed_credits: z.number().int().optional(), released_credits: z.number().int().optional(), remaining_credits: z.number().int().optional(), expires_at: 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 }; /** * Create Credit Lock * * POST /v2/credits/locks * * Also accepts auth: APIKeyHeader */ export const createApiV2CreditsLocks = async (args: CreateApiV2CreditsLocksArgs, options?: CreateApiV2CreditsLocksOptions) => { const {baseUrl = "https://api.explorium.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("api_key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v2/credits/locks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(creditLockResponse, await res.json()) };