@seekora-ai/search-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,401 @@
1
+ /**
2
+ * Seekora SDK Client
3
+ * High-level wrapper around generated OpenAPI client
4
+ */
5
+ import type { DataTypesIndexConfig, DataTypesEventPayload } from '../generated';
6
+ import { type SeekoraEnvironment } from './config';
7
+ import { Logger, type LogLevel, type LoggerConfig } from './logger';
8
+ export interface SeekoraClientConfig {
9
+ storeId?: string;
10
+ readSecret?: string;
11
+ writeSecret?: string;
12
+ baseUrl?: string;
13
+ environment?: SeekoraEnvironment;
14
+ timeout?: number;
15
+ logLevel?: LogLevel;
16
+ logger?: LoggerConfig;
17
+ configFile?: string;
18
+ /**
19
+ * User identifier (authenticated user ID)
20
+ * If provided, will be used in all analytics events
21
+ */
22
+ userId?: string;
23
+ /**
24
+ * Anonymous identifier (if not provided, will be auto-generated)
25
+ * Required if userId is not provided
26
+ */
27
+ anonId?: string;
28
+ /**
29
+ * Session identifier (if not provided, will be auto-generated per session)
30
+ */
31
+ sessionId?: string;
32
+ /**
33
+ * Automatically track search events when search() is called
34
+ * @default false
35
+ */
36
+ autoTrackSearch?: boolean;
37
+ }
38
+ /**
39
+ * Search context containing identifiers for linking events to searches
40
+ *
41
+ * Returned by search() method and should be passed to tracking methods
42
+ * to link clicks/conversions back to the originating search.
43
+ */
44
+ export interface SearchContext {
45
+ /** Correlation ID for this search journey (generated per search) */
46
+ correlationId: string;
47
+ /** Search ID from backend (if present in response) */
48
+ searchId?: string;
49
+ /** User ID if authenticated */
50
+ userId?: string;
51
+ /** Anonymous user ID */
52
+ anonId: string;
53
+ /** Session ID */
54
+ sessionId: string;
55
+ }
56
+ /**
57
+ * Extended event payload with correlation_id and search_id at top level
58
+ * (per identifier spec requirements)
59
+ *
60
+ * This extends the base DataTypesEventPayload to include:
61
+ * - correlation_id: Required for linking events to search journeys
62
+ * - search_id: Optional, but should be included when available
63
+ *
64
+ * Both fields are set at the top level (not just in metadata) per spec.
65
+ */
66
+ export interface ExtendedEventPayload extends DataTypesEventPayload {
67
+ /** Correlation ID for linking events to search journeys */
68
+ correlation_id?: string;
69
+ /** Search ID at top level (also in metadata for backward compat) */
70
+ search_id?: string;
71
+ }
72
+ export interface SearchOptions {
73
+ q: string;
74
+ per_page?: number;
75
+ page?: number;
76
+ /**
77
+ * Filter string in format: "field:value" or "field1:value1 && field2:value2" for multiple filters.
78
+ * Examples: "Type:variation", "1:western", "Type:variation && Size:Large"
79
+ * Multiple filters use && separator: "field1:value1 && field2:value2"
80
+ */
81
+ filter?: string;
82
+ filter_by?: string;
83
+ facet_by?: string;
84
+ sort?: string;
85
+ sort_by?: string;
86
+ analytics_tags?: string[];
87
+ widget_mode?: boolean;
88
+ search_fields?: string[];
89
+ field_weights?: number[];
90
+ return_fields?: string[];
91
+ omit_fields?: string[];
92
+ snippet_fields?: string[];
93
+ full_snippet_fields?: string[];
94
+ snippet_prefix?: string;
95
+ snippet_suffix?: string;
96
+ snippet_token_limit?: number;
97
+ snippet_min_len?: number;
98
+ include_snippets?: boolean;
99
+ group_field?: string;
100
+ group_size?: number;
101
+ prefix_mode?: 'always' | 'fallback' | 'false';
102
+ infix_mode?: 'always' | 'fallback' | 'false';
103
+ typo_max?: number;
104
+ typo_min_len_1?: number;
105
+ typo_min_len_2?: number;
106
+ typo_timeout_ms?: number;
107
+ search_timeout_ms?: number;
108
+ require_all_terms?: boolean;
109
+ exact_match_boost?: boolean;
110
+ cache_results?: boolean;
111
+ apply_rules?: boolean;
112
+ preset_name?: string;
113
+ facet_search_text?: string;
114
+ include_suggestions?: boolean;
115
+ suggestions_limit?: number;
116
+ max_facet_values?: number;
117
+ stopword_sets?: string[];
118
+ synonym_sets?: string[];
119
+ }
120
+ export interface SearchResponse {
121
+ results: any[];
122
+ totalResults: number;
123
+ searchId?: string;
124
+ /** Search context for linking subsequent events */
125
+ context: SearchContext;
126
+ [key: string]: any;
127
+ }
128
+ /**
129
+ * Full query suggestions API response when returnFullResponse is true.
130
+ * Includes suggestion hits plus dropdown recommendations (trending_searches, top_searches,
131
+ * related_searches, popular_brands, filtered_tabs) when requested.
132
+ */
133
+ export interface QuerySuggestionsFullResponse {
134
+ /** Suggestion hits (same as getSuggestions() default return) */
135
+ suggestions: any[];
136
+ /** Full results array from API (multi-index: first = suggestions, second = extensions when include_dropdown_recommendations/filtered_tabs) */
137
+ results?: any[];
138
+ /** Extensions from the response (e.g. trending_searches, top_searches, related_searches, popular_brands, filtered_tabs) */
139
+ extensions?: Record<string, any>;
140
+ /** Raw API response data */
141
+ raw?: any;
142
+ }
143
+ /**
144
+ * Seekora SDK Client
145
+ *
146
+ * Provides a clean, easy-to-use interface for the Seekora Search API
147
+ */
148
+ export declare class SeekoraClient {
149
+ private config;
150
+ private searchApi;
151
+ private suggestionsApi;
152
+ private suggestionsConfigApi;
153
+ private analyticsApi;
154
+ private storesApi;
155
+ private storeId;
156
+ private readSecret;
157
+ private writeSecret?;
158
+ private logger;
159
+ private userId?;
160
+ private anonId;
161
+ private sessionId;
162
+ private autoTrackSearch;
163
+ constructor(config?: SeekoraClientConfig);
164
+ /**
165
+ * Search for documents
166
+ *
167
+ * Generates a new correlation_id for this search and returns SearchContext
168
+ * for linking subsequent events (clicks, conversions) to this search.
169
+ *
170
+ * @param query - Search query string
171
+ * @param options - Search options (pagination, filters, facets, etc.)
172
+ * @returns SearchResponse with results and SearchContext
173
+ */
174
+ search(query: string, options?: Partial<SearchOptions>): Promise<SearchResponse>;
175
+ /**
176
+ * Get query suggestions
177
+ *
178
+ * Uses POST when filtered_tabs or include_dropdown_recommendations is set (GET does not send include_dropdown_recommendations).
179
+ * With returnFullResponse: true returns full shape including extensions (trending_searches, top_searches, related_searches, popular_brands, filtered_tabs).
180
+ *
181
+ * @param query - Partial query to get suggestions for
182
+ * @param options - Optional parameters for suggestions
183
+ * @returns Suggestion hits array, or QuerySuggestionsFullResponse when returnFullResponse is true
184
+ */
185
+ getSuggestions(query: string, options?: {
186
+ hitsPerPage?: number;
187
+ page?: number;
188
+ analytics_tags?: string | string[];
189
+ tags_match_mode?: 'any' | 'all';
190
+ include_categories?: boolean;
191
+ include_facets?: boolean;
192
+ max_categories?: number;
193
+ max_facets?: number;
194
+ min_popularity?: number;
195
+ time_range?: '7d' | '30d' | '90d';
196
+ disable_typo_tolerance?: boolean;
197
+ include_dropdown_recommendations?: boolean;
198
+ filtered_tabs?: Array<{
199
+ id?: string;
200
+ label: string;
201
+ filter: string;
202
+ count?: number;
203
+ }>;
204
+ /** When true, returns full response with results and extensions (dropdown recommendations, filtered_tabs) */
205
+ returnFullResponse?: boolean;
206
+ }): Promise<any[] | QuerySuggestionsFullResponse>;
207
+ /**
208
+ * Get query suggestions configuration (store-specific).
209
+ * Uses GET /api/v1/stores/{storeId}/query-suggestions/config via SDKQuerySuggestionsConfigApi.
210
+ */
211
+ getSuggestionsConfig(): Promise<any>;
212
+ /**
213
+ * Get store configuration
214
+ * Returns store search configuration and onboarding status
215
+ */
216
+ getConfig(): Promise<DataTypesIndexConfig>;
217
+ /**
218
+ * Get store information
219
+ * Returns store metadata including name, status, and configuration details
220
+ *
221
+ * Note: This extracts information from the store config response.
222
+ * Store name may be derived from config fields if not directly available.
223
+ *
224
+ * @returns Store information object
225
+ */
226
+ getStoreInfo(): Promise<{
227
+ storeId: string;
228
+ storeName?: string;
229
+ onboardingStatus?: string;
230
+ isActive?: boolean;
231
+ orgId?: number;
232
+ queryFields?: string[];
233
+ facetFields?: string[];
234
+ primaryField?: string;
235
+ config?: any;
236
+ }>;
237
+ /**
238
+ * Update store configuration (requires write secret)
239
+ */
240
+ updateConfig(config: Partial<DataTypesIndexConfig>): Promise<DataTypesIndexConfig>;
241
+ /**
242
+ * Update query suggestions configuration (requires write secret).
243
+ * Uses PUT /api/v1/stores/{xStoreID}/query-suggestions/config via SDKQuerySuggestionsConfigApi.
244
+ */
245
+ updateQuerySuggestionsConfig(config: any): Promise<any>;
246
+ /**
247
+ * Get configuration schema
248
+ */
249
+ getConfigSchema(): Promise<any>;
250
+ /**
251
+ * Build event payload with identifiers
252
+ * Ensures user_id or anon_id is present, and sets correlation_id/search_id at top level
253
+ */
254
+ private buildEventPayload;
255
+ /**
256
+ * Track a single analytics event
257
+ *
258
+ * @param event - Event payload (partial, will be enriched with identifiers)
259
+ * @param context - Optional search context for linking events to searches
260
+ */
261
+ trackEvent(event: Partial<DataTypesEventPayload>, context?: SearchContext | Partial<SearchContext>): Promise<void>;
262
+ /**
263
+ * Track multiple analytics events (batch)
264
+ *
265
+ * Note: Events should already have identifiers set. Consider using individual trackEvent()
266
+ * calls or buildEventPayload() to ensure identifiers are properly set.
267
+ *
268
+ * @param events - Array of event payloads (should have identifiers already set)
269
+ */
270
+ trackEvents(events: (Partial<DataTypesEventPayload> & {
271
+ event_name?: string;
272
+ })[], context?: SearchContext): Promise<void>;
273
+ /**
274
+ * Track a search event
275
+ *
276
+ * @param params - Search tracking parameters
277
+ */
278
+ trackSearch(params: {
279
+ query: string;
280
+ resultsCount: number;
281
+ context?: SearchContext;
282
+ analyticsTags?: string[];
283
+ metadata?: Record<string, any>;
284
+ }): Promise<void>;
285
+ /**
286
+ * Track an impression event
287
+ *
288
+ * @param params - Impression tracking parameters
289
+ */
290
+ trackImpression(params: {
291
+ itemId: string;
292
+ position: number;
293
+ listType?: string;
294
+ searchQuery?: string;
295
+ context?: SearchContext;
296
+ metadata?: Record<string, any>;
297
+ }): Promise<void>;
298
+ /**
299
+ * Track a click event
300
+ *
301
+ * @param itemId - Item ID that was clicked
302
+ * @param position - Position in search results (1-based)
303
+ * @param context - Search context (from search() response) for linking to search
304
+ */
305
+ trackClick(itemId: string, position: number, context?: SearchContext): Promise<void>;
306
+ /**
307
+ * Track a click event (backward compatibility)
308
+ *
309
+ * @param itemId - Item ID that was clicked
310
+ * @param position - Position in search results (1-based)
311
+ * @param searchId - Search ID (deprecated, use context instead)
312
+ */
313
+ trackClick(itemId: string, position: number, searchId?: string): Promise<void>;
314
+ /**
315
+ * Track a view event
316
+ *
317
+ * @param itemId - Item ID that was viewed
318
+ * @param context - Search context (from search() response) for linking to search
319
+ */
320
+ trackView(itemId: string, context?: SearchContext): Promise<void>;
321
+ /**
322
+ * Track a view event (backward compatibility)
323
+ *
324
+ * @param itemId - Item ID that was viewed
325
+ * @param searchId - Search ID (deprecated, use context instead)
326
+ */
327
+ trackView(itemId: string, searchId?: string): Promise<void>;
328
+ /**
329
+ * Track a conversion event
330
+ *
331
+ * @param itemId - Item ID that was converted
332
+ * @param value - Conversion value (optional)
333
+ * @param currency - Currency code (optional)
334
+ * @param context - Search context (from search() response) for linking to search
335
+ */
336
+ trackConversion(itemId: string, value?: number, currency?: string, context?: SearchContext): Promise<void>;
337
+ /**
338
+ * Track a conversion event (backward compatibility)
339
+ *
340
+ * @param itemId - Item ID that was converted
341
+ * @param value - Conversion value (optional)
342
+ * @param currency - Currency code (optional)
343
+ * @param searchId - Search ID (deprecated, use context instead)
344
+ */
345
+ trackConversion(itemId: string, value?: number, currency?: string, searchId?: string): Promise<void>;
346
+ /**
347
+ * Track a custom event
348
+ *
349
+ * @param eventName - Custom event name
350
+ * @param payload - Additional event data
351
+ * @param context - Optional search context for linking events to searches
352
+ */
353
+ trackCustom(eventName: string, payload?: Partial<DataTypesEventPayload>, context?: SearchContext): Promise<void>;
354
+ /**
355
+ * Validate an event payload before sending
356
+ * Useful for debugging and ensuring events are properly formatted
357
+ *
358
+ * @param event - Event payload to validate
359
+ * @returns Validation result with success status and any error messages
360
+ */
361
+ validateEvent(event: Partial<DataTypesEventPayload>): Promise<{
362
+ valid: boolean;
363
+ message?: string;
364
+ errors?: any;
365
+ }>;
366
+ /**
367
+ * Get event schema
368
+ * Returns the JSON schema for event payloads, useful for understanding required/optional fields
369
+ *
370
+ * @returns Event schema with field definitions, types, and validation rules
371
+ */
372
+ getEventSchema(): Promise<any>;
373
+ /**
374
+ * Set user ID (call this when user logs in)
375
+ *
376
+ * @param userId - Authenticated user ID
377
+ */
378
+ setUserId(userId: string): void;
379
+ /**
380
+ * Clear user ID (call this when user logs out)
381
+ */
382
+ clearUserId(): void;
383
+ /**
384
+ * Get current identifiers
385
+ * Useful for debugging or custom event tracking
386
+ */
387
+ getIdentifiers(): {
388
+ userId?: string;
389
+ anonId: string;
390
+ sessionId: string;
391
+ };
392
+ /**
393
+ * Handle errors consistently
394
+ */
395
+ private handleError;
396
+ /**
397
+ * Get the logger instance
398
+ */
399
+ getLogger(): Logger;
400
+ }
401
+ export default SeekoraClient;