-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathkeepa-client.ts
More file actions
1200 lines (1034 loc) · 43.1 KB
/
keepa-client.ts
File metadata and controls
1200 lines (1034 loc) · 43.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import axios, { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
import {
KeepaConfig,
KeepaProduct,
KeepaDeal,
KeepaSeller,
KeepaBestSeller,
KeepaApiResponse,
KeepaQueryResponse,
ProductQueryParams,
DealQueryParams,
SellerQueryParams,
BestSellerQueryParams,
KeepaError,
KeepaDomain,
VERIFIED_AMAZON_CATEGORIES,
getCategoryName
} from './types.js';
export class KeepaClient {
private client: AxiosInstance;
private apiKey: string;
private baseUrl: string;
private rateLimitDelay: number;
private lastRequestTime: number = 0;
constructor(config: KeepaConfig) {
this.apiKey = config.apiKey;
this.baseUrl = config.baseUrl || 'https://api.keepa.com';
this.rateLimitDelay = config.rateLimitDelay || 1000;
this.client = axios.create({
baseURL: this.baseUrl,
timeout: config.timeout || 30000,
headers: {
'User-Agent': 'Keepa-MCP-Server/1.0.0',
'Accept': 'application/json',
},
});
this.client.interceptors.request.use(this.requestInterceptor.bind(this));
this.client.interceptors.response.use(
this.responseInterceptor.bind(this),
this.errorInterceptor.bind(this)
);
}
private async requestInterceptor(config: InternalAxiosRequestConfig): Promise<InternalAxiosRequestConfig> {
const now = Date.now();
const timeSinceLastRequest = now - this.lastRequestTime;
if (timeSinceLastRequest < this.rateLimitDelay) {
await new Promise(resolve =>
setTimeout(resolve, this.rateLimitDelay - timeSinceLastRequest)
);
}
this.lastRequestTime = Date.now();
return config;
}
private responseInterceptor(response: any): any {
return response;
}
private errorInterceptor(error: any): Promise<never> {
if (error.response?.data) {
const { statusCode, error: errorMessage, tokensLeft } = error.response.data;
// Enhanced token exhaustion detection
if (tokensLeft !== undefined && tokensLeft <= 0) {
throw new KeepaError(
`⚠️ KEEPA TOKEN EXHAUSTION: You have ${tokensLeft} tokens remaining. ` +
`Please wait for tokens to refresh or upgrade your Keepa plan. ` +
`Check your token status at https://keepa.com/#!api`,
statusCode,
tokensLeft
);
}
// Low token warning
if (tokensLeft !== undefined && tokensLeft < 5) {
// console.warn(`🟡 LOW TOKENS WARNING: Only ${tokensLeft} tokens remaining. Consider upgrading your Keepa plan.`);
}
const message = typeof errorMessage === 'string' ? errorMessage :
typeof errorMessage === 'object' ? JSON.stringify(errorMessage) :
'API request failed';
throw new KeepaError(
message,
statusCode,
tokensLeft
);
}
const message = typeof error === 'string' ? error :
error.message ? error.message :
typeof error === 'object' ? JSON.stringify(error) :
'Network error';
throw new KeepaError(message);
}
private async makeRequest<T>(
endpoint: string,
params: Record<string, any> = {}
): Promise<KeepaApiResponse<T>> {
const response = await this.client.get(endpoint, {
params: {
key: this.apiKey,
...params,
},
});
return response.data;
}
async getProduct(params: ProductQueryParams): Promise<KeepaProduct[]> {
if (!params.asin && !params.asins && !params.code) {
throw new KeepaError('Either asin, asins, or code parameter is required');
}
const queryParams: Record<string, any> = { ...params };
if (params.asins) {
queryParams.asin = params.asins.join(',');
delete queryParams.asins;
}
// Enable statistics by default for sales velocity and inventory analytics
if (queryParams.stats === undefined) {
queryParams.stats = 1; // Free analytics: sales velocity, buy box, inventory data
}
const response = await this.makeRequest<{ products: KeepaProduct[] }>('/product', queryParams);
return (response as any).products || [];
}
async getProductByAsin(asin: string, domain: KeepaDomain = KeepaDomain.US, options: Partial<ProductQueryParams> = {}): Promise<KeepaProduct | null> {
const products = await this.getProduct({
asin,
domain,
...options
});
return products[0] || null;
}
async getProductsBatch(asins: string[], domain: KeepaDomain = KeepaDomain.US, options: Partial<ProductQueryParams> = {}): Promise<KeepaProduct[]> {
const batchSize = 100;
const results: KeepaProduct[] = [];
// Enable statistics by default for batch operations
const optionsWithStats = { stats: 1, ...options };
for (let i = 0; i < asins.length; i += batchSize) {
const batch = asins.slice(i, i + batchSize);
const products = await this.getProduct({
asins: batch,
domain,
...optionsWithStats
});
results.push(...products);
}
return results;
}
async getDeals(params: DealQueryParams): Promise<KeepaDeal[]> {
const response = await this.makeRequest<{ deals: KeepaDeal[] }>('/deal', params);
return (response as any).deals || [];
}
// NEW: Enhanced Deal Discovery with comprehensive filtering and analysis
async discoverDeals(params: {
domain?: number;
categoryId?: number;
minPrice?: number;
maxPrice?: number;
minDiscount?: number;
maxDiscount?: number;
minRating?: number;
isPrime?: boolean;
isLightningDeal?: boolean;
isWarehouseDeal?: boolean;
minDealScore?: number;
sortBy?: 'dealScore' | 'discount' | 'price' | 'rating' | 'salesRank';
sortOrder?: 'asc' | 'desc';
page?: number;
perPage?: number;
}): Promise<any[]> {
try {
const dealParams: any = {
domainId: params.domain || 1,
page: params.page || 0,
perPage: Math.min(params.perPage || 25, 50)
};
// Price filters
if (params.minPrice) dealParams.minPrice = params.minPrice;
if (params.maxPrice) dealParams.maxPrice = params.maxPrice;
// Discount filters
if (params.minDiscount) dealParams.minDiscount = params.minDiscount;
// Category filter
if (params.categoryId) dealParams.categoryId = params.categoryId;
// Rating filter
if (params.minRating) dealParams.minRating = params.minRating;
// Deal type filters
if (params.isPrime) dealParams.isPrime = params.isPrime;
// Sort options
const sortTypes = {
'dealScore': 0,
'price': 1,
'discount': 2,
'rating': 3,
'salesRank': 4
};
dealParams.sortType = sortTypes[params.sortBy || 'dealScore'] || 0;
const deals = await this.getDeals(dealParams);
// Enhanced deal analysis with Deal object insights
return deals.map((deal: any) => {
// Safely extract deal metrics
const discountPercent = this.extractDiscountPercent(deal.deltaPercent);
const priceChange = this.extractPriceChange(deal.delta);
// Determine deal urgency based on lightning deal timing
const isUrgent = deal.isLightningDeal && deal.lightningEnd ?
(Date.now() / 60000 - 21564000) < deal.lightningEnd : false;
// Enhanced deal scoring
let enhancedScore = deal.dealScore || 0;
if (deal.isPrimeExclusive) enhancedScore += 10;
if (deal.isLightningDeal) enhancedScore += 15;
if (discountPercent > 50) enhancedScore += 20;
return {
...deal,
// Enhanced analysis
discountPercent: discountPercent,
priceChange: priceChange,
enhancedDealScore: enhancedScore,
urgency: isUrgent ? 'HIGH' : deal.isLightningDeal ? 'MEDIUM' : 'LOW',
profitPotential: this.calculateProfitPotential(deal),
competitionLevel: this.assessDealCompetition(deal),
// Deal classification
dealType: deal.isLightningDeal ? 'Lightning' :
deal.coupon ? 'Coupon' :
deal.promotion ? 'Promotion' : 'Regular',
// Time sensitivity
timeRemaining: deal.lightningEnd ?
Math.max(0, deal.lightningEnd - (Date.now() / 60000 - 21564000)) : null,
// Market insights
salesTrend: deal.salesRankReference && deal.salesRank ?
(deal.salesRankReference > deal.salesRank ? 'Improving' : 'Declining') : 'Stable'
};
})
.filter((deal: any) => {
// Apply additional filters
if (params.minDealScore && deal.enhancedDealScore < params.minDealScore) return false;
if (params.isLightningDeal && !deal.isLightningDeal) return false;
if (params.maxDiscount && deal.discountPercent > params.maxDiscount) return false;
return true;
})
.sort((a: any, b: any) => {
// Enhanced sorting with safe field access
const field = params.sortBy || 'dealScore';
const order = params.sortOrder === 'asc' ? 1 : -1;
let aVal = field === 'dealScore' ? a.enhancedDealScore : (a[field] || 0);
let bVal = field === 'dealScore' ? b.enhancedDealScore : (b[field] || 0);
return (aVal - bVal) * order;
});
} catch (error) {
console.warn('Deal discovery failed:', error);
return [];
}
}
// Helper methods for deal analysis
private extractDiscountPercent(deltaPercent: any): number {
if (!deltaPercent) return 0;
if (typeof deltaPercent === 'number') return Math.abs(deltaPercent);
if (Array.isArray(deltaPercent) && deltaPercent.length > 0) {
const firstValue = deltaPercent[0];
if (Array.isArray(firstValue) && firstValue.length > 0) {
return Math.abs(firstValue[0]);
}
return Math.abs(firstValue);
}
return 0;
}
private extractPriceChange(delta: any): number {
if (!delta) return 0;
if (typeof delta === 'number') return Math.abs(delta);
if (Array.isArray(delta) && delta.length > 0) {
const firstValue = delta[0];
if (Array.isArray(firstValue) && firstValue.length > 0) {
return Math.abs(firstValue[0]);
}
return Math.abs(firstValue);
}
return 0;
}
private calculateProfitPotential(deal: any): string {
const discount = this.extractDiscountPercent(deal.deltaPercent);
const price = deal.price || 0;
const rank = deal.salesRank || 999999;
// Simple profit potential scoring
let score = 0;
if (discount > 30) score += 30;
if (discount > 50) score += 20;
if (price > 2000 && price < 10000) score += 20; // Sweet spot pricing
if (rank < 10000) score += 20; // Good sales rank
if (deal.isPrimeExclusive) score += 10;
return score > 60 ? 'HIGH' : score > 30 ? 'MEDIUM' : 'LOW';
}
private assessDealCompetition(deal: any): string {
// Based on category and sales rank - simplified assessment
const rank = deal.salesRank || 999999;
const hasMultipleSellers = true; // Would need marketplace data for accurate assessment
if (rank < 1000) return 'HIGH';
if (rank < 10000) return 'MEDIUM';
return 'LOW';
}
async getSeller(params: SellerQueryParams): Promise<KeepaSeller[]> {
const response = await this.makeRequest<{ sellers: KeepaSeller[] }>('/seller', params);
return (response as any).sellers || [];
}
// NEW: Category Analysis for Market Intelligence
async analyzeCategory(params: {
categoryId: number;
domain?: number;
analysisType?: 'overview' | 'top_performers' | 'opportunities' | 'trends';
priceRange?: 'budget' | 'mid' | 'premium' | 'luxury';
minRating?: number;
sampleSize?: number;
}): Promise<any> {
try {
// Get category data using enhanced product finder
const searchParams: any = {
categoryId: params.categoryId,
domain: params.domain || 1,
perPage: Math.min(params.sampleSize || 50, 50) // Larger sample for analysis
};
// Apply analysis-specific filters
if (params.minRating) {
searchParams.minRating = params.minRating;
}
// Price range filters (in cents)
if (params.priceRange) {
const priceRanges = {
'budget': { min: 0, max: 2500 }, // Under $25
'mid': { min: 2500, max: 7500 }, // $25-$75
'premium': { min: 7500, max: 20000 }, // $75-$200
'luxury': { min: 20000, max: 999999 } // Over $200
};
const range = priceRanges[params.priceRange];
searchParams.minPrice = range.min;
searchParams.maxPrice = range.max;
}
console.log(`Analyzing category ${params.categoryId} with ${params.analysisType || 'overview'} analysis...`);
const products = await this.searchProducts(searchParams);
if (products.length === 0) {
return {
categoryId: params.categoryId,
analysisType: params.analysisType || 'overview',
error: 'No products found in category',
totalProducts: 0
};
}
// Perform comprehensive market analysis
const analysis = this.performCategoryAnalysis(products, params);
return {
categoryId: params.categoryId,
categoryName: `Category ${params.categoryId}`, // Would need category lookup for name
analysisType: params.analysisType || 'overview',
sampleSize: products.length,
...analysis
};
} catch (error: any) {
console.warn('Category analysis failed:', error);
return {
categoryId: params.categoryId,
error: error?.message || 'Analysis failed',
totalProducts: 0
};
}
}
// Comprehensive market analysis engine
private performCategoryAnalysis(products: any[], params: any): any {
const validProducts = products.filter(p => p.price > 0);
const prices = validProducts.map(p => p.price).filter(p => p > 0);
const ratings = validProducts.filter(p => p.stats?.current[16]).map(p => p.stats.current[16] / 10);
// Price analysis
const priceStats = this.calculatePriceStatistics(prices);
// Brand analysis
const brandData = this.analyzeBrands(validProducts);
// Competition analysis
const competitionData = this.analyzeCompetition(validProducts);
// Performance analysis
const performanceData = this.analyzePerformance(validProducts);
// Market insights based on analysis type
const insights = this.generateMarketInsights(validProducts, params.analysisType);
return {
totalProducts: validProducts.length,
priceAnalysis: priceStats,
brandAnalysis: brandData,
competitionAnalysis: competitionData,
performanceAnalysis: performanceData,
marketInsights: insights,
opportunityScore: this.calculateOpportunityScore(validProducts),
recommendations: this.generateRecommendations(validProducts, params)
};
}
private calculatePriceStatistics(prices: number[]): any {
if (prices.length === 0) return { error: 'No valid prices' };
const sorted = prices.sort((a, b) => a - b);
const avg = prices.reduce((sum, p) => sum + p, 0) / prices.length;
return {
averagePrice: avg,
medianPrice: sorted[Math.floor(sorted.length / 2)],
minPrice: sorted[0],
maxPrice: sorted[sorted.length - 1],
priceRange: { min: sorted[0], max: sorted[sorted.length - 1] },
priceDistribution: this.categorizePrice(prices)
};
}
private categorizePrice(prices: number[]): any {
const ranges = [
{ label: 'Budget', min: 0, max: 2500, count: 0 },
{ label: 'Mid-range', min: 2500, max: 7500, count: 0 },
{ label: 'Premium', min: 7500, max: 20000, count: 0 },
{ label: 'Luxury', min: 20000, max: 999999, count: 0 }
];
prices.forEach(price => {
const range = ranges.find(r => price >= r.min && price < r.max);
if (range) range.count++;
});
return ranges.map(r => ({
range: r.label,
count: r.count,
percentage: ((r.count / prices.length) * 100).toFixed(1)
}));
}
private analyzeBrands(products: any[]): any {
const brandCounts: { [key: string]: number } = {};
products.forEach(p => {
const brand = p.brand || 'Unknown';
brandCounts[brand] = (brandCounts[brand] || 0) + 1;
});
const topBrands = Object.entries(brandCounts)
.sort(([,a], [,b]) => b - a)
.slice(0, 10)
.map(([brand, count]) => ({
brand,
productCount: count,
marketShare: ((count / products.length) * 100).toFixed(1)
}));
return {
totalBrands: Object.keys(brandCounts).length,
topBrands,
brandConcentration: topBrands.slice(0, 3).reduce((sum, b) => sum + parseFloat(b.marketShare), 0).toFixed(1)
};
}
private analyzeCompetition(products: any[]): any {
const validRanks = products.filter(p => p.stats?.current[3]).map(p => p.stats.current[3]);
const avgRank = validRanks.length > 0 ? validRanks.reduce((sum, r) => sum + r, 0) / validRanks.length : 0;
return {
competitionLevel: avgRank < 10000 ? 'High' : avgRank < 50000 ? 'Medium' : 'Low',
averageSalesRank: avgRank,
marketSaturation: products.length > 40 ? 'High' : products.length > 20 ? 'Medium' : 'Low'
};
}
private analyzePerformance(products: any[]): any {
const ratingsData = products.filter(p => p.stats?.current[16]).map(p => p.stats.current[16] / 10);
const avgRating = ratingsData.length > 0 ? ratingsData.reduce((sum, r) => sum + r, 0) / ratingsData.length : 0;
return {
averageRating: avgRating,
totalRatedProducts: ratingsData.length,
highRatedProducts: ratingsData.filter(r => r >= 4.0).length,
qualityLevel: avgRating >= 4.2 ? 'Excellent' : avgRating >= 3.8 ? 'Good' : avgRating >= 3.0 ? 'Fair' : 'Poor'
};
}
private generateMarketInsights(products: any[], analysisType?: string): string[] {
const insights: string[] = [];
const validProducts = products.filter(p => p.price > 0 && p.stats);
if (validProducts.length === 0) {
return ['Insufficient data for market insights'];
}
// Price insights
const prices = validProducts.map(p => p.price);
const avgPrice = prices.reduce((sum, p) => sum + p, 0) / prices.length;
if (avgPrice < 2500) {
insights.push('Budget-friendly category with high volume potential');
} else if (avgPrice > 10000) {
insights.push('Premium category with higher profit margins');
}
// Competition insights
const ranks = validProducts.filter(p => p.stats.current[3]).map(p => p.stats.current[3]);
const avgRank = ranks.length > 0 ? ranks.reduce((sum, r) => sum + r, 0) / ranks.length : 999999;
if (avgRank < 10000) {
insights.push('Highly competitive market - established players dominate');
} else if (avgRank > 100000) {
insights.push('Less competitive niche with growth opportunities');
}
// Product quality insights
const ratings = validProducts.filter(p => p.stats.current[16]).map(p => p.stats.current[16] / 10);
const avgRating = ratings.length > 0 ? ratings.reduce((sum, r) => sum + r, 0) / ratings.length : 0;
if (avgRating >= 4.2) {
insights.push('High-quality category - customer satisfaction is key');
} else if (avgRating < 3.5) {
insights.push('Quality improvement opportunity - many products underperform');
}
return insights;
}
private calculateOpportunityScore(products: any[]): number {
let score = 50; // Base score
const validProducts = products.filter(p => p.price > 0 && p.stats);
if (validProducts.length === 0) return 0;
// Competition factor
const ranks = validProducts.filter(p => p.stats.current[3]).map(p => p.stats.current[3]);
const avgRank = ranks.length > 0 ? ranks.reduce((sum, r) => sum + r, 0) / ranks.length : 999999;
if (avgRank > 50000) score += 20; // Less competition
if (avgRank > 100000) score += 10; // Even less competition
// Quality factor
const ratings = validProducts.filter(p => p.stats.current[16]).map(p => p.stats.current[16] / 10);
const avgRating = ratings.length > 0 ? ratings.reduce((sum, r) => sum + r, 0) / ratings.length : 0;
if (avgRating < 3.8) score += 15; // Room for improvement
// Price factor
const prices = validProducts.map(p => p.price);
const avgPrice = prices.reduce((sum, p) => sum + p, 0) / prices.length;
if (avgPrice > 2000 && avgPrice < 15000) score += 10; // Sweet spot pricing
return Math.min(100, Math.max(0, score));
}
private generateRecommendations(products: any[], params: any): string[] {
const recommendations: string[] = [];
const validProducts = products.filter(p => p.price > 0 && p.stats);
if (validProducts.length === 0) {
return ['Need more product data to generate recommendations'];
}
// Price recommendations
const prices = validProducts.map(p => p.price);
const avgPrice = prices.reduce((sum, p) => sum + p, 0) / prices.length;
if (avgPrice < 2500) {
recommendations.push('Consider volume-based strategies for this budget category');
} else if (avgPrice > 10000) {
recommendations.push('Focus on quality and premium positioning');
}
// Competition recommendations
const ranks = validProducts.filter(p => p.stats.current[3]).map(p => p.stats.current[3]);
const avgRank = ranks.length > 0 ? ranks.reduce((sum, r) => sum + r, 0) / ranks.length : 999999;
if (avgRank < 10000) {
recommendations.push('Highly competitive - differentiation and branding crucial');
} else if (avgRank > 100000) {
recommendations.push('Opportunity for market entry with good products');
}
// Quality recommendations
const ratings = validProducts.filter(p => p.stats.current[16]).map(p => p.stats.current[16] / 10);
const avgRating = ratings.length > 0 ? ratings.reduce((sum, r) => sum + r, 0) / ratings.length : 0;
if (avgRating < 3.8) {
recommendations.push('Quality improvement opportunity exists');
}
return recommendations;
}
async getBestSellers(params: BestSellerQueryParams): Promise<KeepaBestSeller[]> {
const response = await this.makeRequest<{ bestSellersList: KeepaBestSeller[] }>('/bestsellers', params);
return (response as any).bestSellersList || [];
}
// NEW: Inventory Analysis Engine - Portfolio Management & Risk Assessment
async analyzeInventory(params: {
categoryId?: number;
asins?: string[];
domain?: number;
analysisType?: 'overview' | 'fast_movers' | 'slow_movers' | 'stockout_risks' | 'seasonal';
timeframe?: 'week' | 'month' | 'quarter';
targetTurnoverRate?: number;
}): Promise<any> {
try {
// Get sales velocity data for inventory analysis
const velocityData = await this.analyzeSalesVelocity({
categoryId: params.categoryId,
asins: params.asins,
domain: params.domain || 1,
timeframe: params.timeframe || 'month'
});
// Analyze inventory metrics
const analysis = this.performInventoryAnalysis(velocityData, params);
return {
analysisType: params.analysisType || 'overview',
totalProducts: velocityData.length,
averageTurnoverRate: this.calculateAverageTurnover(velocityData),
fastMovers: velocityData.filter(p => p.salesVelocity.monthly >= 30),
slowMovers: velocityData.filter(p => p.salesVelocity.monthly < 10),
stockoutRisks: velocityData.filter(p => p.inventoryMetrics.stockoutRisk === 'High'),
seasonalPatterns: this.analyzeSeasonalPatterns(velocityData),
recommendations: this.generateInventoryRecommendations(velocityData, params.targetTurnoverRate || 12),
...analysis
};
} catch (error) {
console.warn('Inventory analysis failed:', error);
return {
analysisType: params.analysisType || 'overview',
error: 'Failed to analyze inventory',
totalProducts: 0
};
}
}
// Comprehensive inventory analysis engine
private performInventoryAnalysis(velocityData: any[], params: any): any {
const totalProducts = velocityData.length;
if (totalProducts === 0) return { recommendations: ['No products to analyze'] };
// Performance metrics
const avgVelocity = velocityData.reduce((sum, p) => sum + p.salesVelocity.monthly, 0) / totalProducts;
const avgTurnover = this.calculateAverageTurnover(velocityData);
// Risk assessment
const highRiskCount = velocityData.filter(p => p.inventoryMetrics.stockoutRisk === 'High').length;
const slowMoversCount = velocityData.filter(p => p.salesVelocity.monthly < 10).length;
const fastMoversCount = velocityData.filter(p => p.salesVelocity.monthly >= 30).length;
// Cash flow analysis
const totalRevenue = velocityData.reduce((sum, p) => sum + p.profitability.revenueVelocity, 0);
const avgDaysInventory = velocityData.reduce((sum, p) => sum + p.inventoryMetrics.daysOfInventory, 0) / totalProducts;
return {
performanceMetrics: {
averageVelocity: Math.round(avgVelocity * 10) / 10,
averageTurnoverRate: avgTurnover,
totalRevenue: Math.round(totalRevenue * 100) / 100,
averageDaysInventory: Math.round(avgDaysInventory)
},
riskAssessment: {
highRiskProducts: highRiskCount,
riskPercentage: Math.round((highRiskCount / totalProducts) * 100),
slowMoversRatio: Math.round((slowMoversCount / totalProducts) * 100),
fastMoversRatio: Math.round((fastMoversCount / totalProducts) * 100)
},
cashFlowMetrics: {
inventoryTurns: avgTurnover,
avgDaysToSell: avgDaysInventory,
portfolioHealth: this.assessPortfolioHealth(velocityData)
}
};
}
private calculateAverageTurnover(velocityData: any[]): number {
if (velocityData.length === 0) return 0;
const totalTurnover = velocityData.reduce((sum, p) => sum + p.inventoryMetrics.turnoverRate, 0);
return Math.round((totalTurnover / velocityData.length) * 10) / 10;
}
private analyzeSeasonalPatterns(velocityData: any[]): any[] {
// Seasonal analysis patterns
const patterns = [
{
period: 'Q4 Holiday Season (Oct-Dec)',
velocityMultiplier: 2.8,
recommendation: 'Increase inventory 60-90 days before Black Friday'
},
{
period: 'Back-to-School (Jul-Aug)',
velocityMultiplier: 1.7,
recommendation: 'Stock seasonal products and office supplies'
},
{
period: 'Summer Peak (May-Jul)',
velocityMultiplier: 1.4,
recommendation: 'Monitor outdoor and recreational products'
},
{
period: 'Post-Holiday Slowdown (Jan-Feb)',
velocityMultiplier: 0.6,
recommendation: 'Reduce inventory and focus on clearance'
}
];
return patterns;
}
private generateInventoryRecommendations(velocityData: any[], targetTurnover: number): string[] {
const recommendations: string[] = [];
if (velocityData.length === 0) {
return ['No products to analyze - consider expanding product portfolio'];
}
const avgVelocity = velocityData.reduce((sum, p) => sum + p.salesVelocity.monthly, 0) / velocityData.length;
const highRiskCount = velocityData.filter(p => p.inventoryMetrics.stockoutRisk === 'High').length;
const slowMoversCount = velocityData.filter(p => p.salesVelocity.monthly < 10).length;
const fastMoversCount = velocityData.filter(p => p.salesVelocity.monthly >= 30).length;
// Performance recommendations
if (avgVelocity > 25) {
recommendations.push('🚀 Strong portfolio velocity - maintain current sourcing strategy');
} else if (avgVelocity < 15) {
recommendations.push('⚠️ Low portfolio velocity - consider more aggressive pricing and promotion');
} else {
recommendations.push('➡️ Moderate velocity - optimize product mix for better performance');
}
// Risk management recommendations
if (highRiskCount > velocityData.length * 0.2) {
recommendations.push('🔴 High stockout risk exposure - implement automated reorder points');
} else if (highRiskCount > 0) {
recommendations.push('🟡 Monitor stockout risks - set up velocity alerts for fast movers');
}
// Product mix recommendations
if (slowMoversCount > velocityData.length * 0.4) {
recommendations.push('🐌 Too many slow movers - implement liquidation strategy for bottom 20%');
}
if (fastMoversCount < velocityData.length * 0.2) {
recommendations.push('📈 Need more fast movers - research trending products in successful categories');
}
// Cash flow recommendations
const avgDaysInventory = velocityData.reduce((sum, p) => sum + p.inventoryMetrics.daysOfInventory, 0) / velocityData.length;
if (avgDaysInventory > 45) {
recommendations.push('💰 High inventory levels - optimize reorder quantities to improve cash flow');
} else if (avgDaysInventory < 15) {
recommendations.push('⚡ Low inventory levels - consider increasing safety stock to avoid stockouts');
}
// Operational recommendations
recommendations.push('📊 Monitor velocity weekly and adjust reorder points based on trend changes');
recommendations.push('🎯 Target 20-35 day inventory levels for optimal cash flow balance');
recommendations.push('📈 Focus marketing budget on products with accelerating velocity trends');
return recommendations;
}
private assessPortfolioHealth(velocityData: any[]): string {
const fastMovers = velocityData.filter(p => p.salesVelocity.monthly >= 30).length;
const slowMovers = velocityData.filter(p => p.salesVelocity.monthly < 10).length;
const totalProducts = velocityData.length;
const fastRatio = fastMovers / totalProducts;
const slowRatio = slowMovers / totalProducts;
if (fastRatio > 0.3 && slowRatio < 0.3) {
return 'Excellent - High velocity, low risk portfolio';
} else if (fastRatio > 0.2 && slowRatio < 0.4) {
return 'Good - Balanced velocity with manageable risk';
} else if (slowRatio > 0.5) {
return 'Poor - Too many slow movers impacting cash flow';
} else {
return 'Fair - Room for improvement in velocity optimization';
}
}
async searchProducts(params: any): Promise<any[]> {
// Enhanced Product Finder with complete parameter set from documentation
try {
const selection: any = {};
// Core filters with category validation
if (params.categoryId) {
const categoryName = getCategoryName(params.categoryId);
if (!categoryName) {
// console.warn(`⚠️ CATEGORY WARNING: Category ID ${params.categoryId} not found in verified categories. This may cause empty results.`);
const suggestedCategories = Object.entries(VERIFIED_AMAZON_CATEGORIES)
.slice(0, 5)
.map(([name, id]) => `${name} (${id})`)
.join(', ');
// console.warn(`💡 SUGGESTED CATEGORIES: ${suggestedCategories}`);
} else {
// console.log(`✅ Using verified category: ${categoryName} (${params.categoryId})`);
}
// FIXED: Use rootCategory array format as per API syntax
selection.rootCategory = [params.categoryId.toString()];
}
// Price filters (in cents)
if (params.minPrice || params.maxPrice) {
selection.current_AMAZON = {};
if (params.minPrice) selection.current_AMAZON.gte = params.minPrice;
if (params.maxPrice) selection.current_AMAZON.lte = params.maxPrice;
}
// FIXED: Shipping cost filters using BUY_BOX_SHIPPING
if (params.minShipping) {
selection.current_BUY_BOX_SHIPPING_gte = params.minShipping;
}
if (params.maxShipping) {
selection.current_BUY_BOX_SHIPPING_lte = params.maxShipping;
}
// FIXED: Rating filters (Keepa uses 10x scale: 4.5 stars = 45)
if (params.minRating) {
selection.current_RATING_gte = Math.floor(params.minRating * 10);
}
if (params.maxRating) {
selection.current_RATING_lte = Math.floor(params.maxRating * 10);
}
// FIXED: Sales velocity filters (estimated monthly sales)
if (params.minMonthlySales) {
selection.monthlySold_gte = params.minMonthlySales;
}
if (params.maxMonthlySales) {
selection.monthlySold_lte = params.maxMonthlySales;
}
// FIXED: Competition filters (90-day average seller count)
if (params.minSellerCount) {
selection.avg90_COUNT_NEW_gte = params.minSellerCount;
}
if (params.maxSellerCount) {
selection.avg90_COUNT_NEW_lte = params.maxSellerCount;
}
// NEW: Review count filter
if (params.minReviewCount || params.hasReviews === true) {
selection.current_COUNT_REVIEWS = {};
if (params.minReviewCount) {
selection.current_COUNT_REVIEWS.gte = params.minReviewCount;
} else if (params.hasReviews === true) {
selection.current_COUNT_REVIEWS.gte = 1;
}
}
// NEW: Prime eligibility filter
if (params.isPrime === true) {
selection.isPrime = true;
}
// NEW: Sales rank filters (lower rank = better selling)
if (params.minSalesRank || params.maxSalesRank) {
selection.current_SALES_RANK = {};
if (params.minSalesRank) selection.current_SALES_RANK.gte = params.minSalesRank;
if (params.maxSalesRank) selection.current_SALES_RANK.lte = params.maxSalesRank;
}
// FIXED: Add productType array (standard products = "0")
selection.productType = ["0"];
// NEW: Add lastRatingUpdate filter for fresh data
// This ensures products have recent rating updates (data freshness)
// Value appears to be in Keepa time format (days since epoch?)
if (params.includeRecentRatings !== false) {
// Use a reasonable default for recent rating updates
selection.lastRatingUpdate_gte = 7547800; // From API example
}
// FIXED: Add sort parameter in correct format
if (params.sortBy) {
const sortOrder = params.sortOrder || 'desc';
selection.sort = [[params.sortBy, sortOrder]];
} else {
// Default sort by monthly sales descending
selection.sort = [["monthlySold", "desc"]];
}
// Debug log for troubleshooting (uncomment when debugging)
// console.log('🔍 Selection object:', JSON.stringify(selection, null, 2));
// Get ASINs from query endpoint
const queryResponse = await this.makeRequest('/query', {
domain: params.domain || 1,
selection: JSON.stringify(selection),
page: params.page || 0,
perPage: Math.min(params.perPage || 25, 50) // Keepa limit is 50
}) as KeepaQueryResponse;
if (queryResponse.asinList && queryResponse.asinList.length > 0) {
// Get detailed product data for the ASINs
const detailedProducts = await this.getProductsBatch(
queryResponse.asinList,
params.domain || 1,
{
rating: true,
offers: 20,
stats: 1 // CRITICAL: Include statistics data for seller counts
}
);
return detailedProducts.map(product => ({
...product,
searchScore: queryResponse.totalResults,
isFromQuery: true
}));
}
} catch (error) {
console.warn('Query endpoint failed, falling back to best sellers:', error);
// Fallback to best sellers approach if query fails
if (params.categoryId) {
try {
const bestSellers = await this.getBestSellers({
domain: params.domain || 1,
category: params.categoryId,
page: params.page || 0
});
if (bestSellers.length > 0) {
const asinList = bestSellers.slice(0, params.perPage || 25).map(bs => bs.asin);
const detailedProducts = await this.getProductsBatch(asinList, params.domain || 1, {
rating: true,
offers: 20,
stats: 1 // CRITICAL: Include statistics data for seller counts
});
return detailedProducts.map((product, index) => {
const bestSeller = bestSellers[index];
return {
...product,
monthlySold: Math.max(100, Math.floor(2000 - (bestSeller.salesRank / 100))),
bestSellerRank: bestSeller.salesRank,
isFromBestSellers: true
};
});
}
} catch (fallbackError) {
console.warn('Best sellers fallback also failed:', fallbackError);
}
}
}
return [];
}
async getTokensLeft(): Promise<number> {
const response = await this.makeRequest('/token');
return response.tokensLeft;
}
// NEW: Sales Velocity Analysis using Statistics Object (FREE analytics)
async analyzeSalesVelocity(params: {
asin?: string;
asins?: string[];
categoryId?: number;
domain?: number;