warframe-nexus-query 2.1.0 → 2.2.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.
- package/README.md +367 -27
- package/index.js +25 -6
- package/lib/AttachmentCreator.js +80 -7
- package/lib/market/v2/.eslintrc.json +6 -0
- package/lib/market/v2/MarketFetcherV2.js +418 -0
- package/lib/market/v2/constants.js +60 -0
- package/lib/market/v2/index.js +12 -0
- package/lib/market/v2/models/Item.js +135 -0
- package/lib/market/v2/models/Order.js +197 -0
- package/lib/market/v2/models/Summary.js +165 -0
- package/lib/market/v2/types.d.ts +137 -0
- package/lib/market/v2/utils/cache.js +189 -0
- package/lib/market/v2/utils/http.js +246 -0
- package/lib/market/v2/utils/i18n.js +40 -0
- package/lib/market/v2/utils/statistics.js +185 -0
- package/package.json +9 -79
- package/lib/market/v1/MarketFetcher.js +0 -148
- package/lib/market/v1/summary.js +0 -65
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User information embedded in orders
|
|
3
|
+
*/
|
|
4
|
+
class OrderUser {
|
|
5
|
+
constructor(data) {
|
|
6
|
+
this.id = data.id;
|
|
7
|
+
this.ingameName = data.ingameName;
|
|
8
|
+
this.avatar = data.avatar;
|
|
9
|
+
this.reputation = data.reputation || 0;
|
|
10
|
+
this.locale = data.locale;
|
|
11
|
+
this.platform = data.platform;
|
|
12
|
+
this.crossplay = data.crossplay || false;
|
|
13
|
+
this.status = data.status; // 'online' | 'ingame' | 'offline' | 'invisible'
|
|
14
|
+
this.activity = data.activity; // { type, details, startedAt }
|
|
15
|
+
this.lastSeen = data.lastSeen ? new Date(data.lastSeen) : null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Check if user is online
|
|
20
|
+
* @returns {boolean}
|
|
21
|
+
*/
|
|
22
|
+
isOnline() {
|
|
23
|
+
return this.status === 'online' || this.status === 'ingame';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get activity description
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
getActivityDescription() {
|
|
31
|
+
if (!this.activity || !this.activity.type) return null;
|
|
32
|
+
|
|
33
|
+
const { type, details } = this.activity;
|
|
34
|
+
|
|
35
|
+
switch (type) {
|
|
36
|
+
case 'ON_MISSION':
|
|
37
|
+
return details ? `On Mission: ${details}` : 'On Mission';
|
|
38
|
+
case 'IN_DOJO':
|
|
39
|
+
return 'In Dojo';
|
|
40
|
+
case 'IN_ORBITER':
|
|
41
|
+
return 'In Orbiter';
|
|
42
|
+
case 'IN_RELAY':
|
|
43
|
+
return details ? `In Relay: ${details}` : 'In Relay';
|
|
44
|
+
case 'IDLE':
|
|
45
|
+
return 'Idle';
|
|
46
|
+
default:
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get status emoji
|
|
53
|
+
* @returns {string}
|
|
54
|
+
*/
|
|
55
|
+
getStatusEmoji() {
|
|
56
|
+
switch (this.status) {
|
|
57
|
+
case 'online':
|
|
58
|
+
return '🟢';
|
|
59
|
+
case 'ingame':
|
|
60
|
+
return '🎮';
|
|
61
|
+
case 'offline':
|
|
62
|
+
return '⚫';
|
|
63
|
+
case 'invisible':
|
|
64
|
+
return '👻';
|
|
65
|
+
default:
|
|
66
|
+
return '❓';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Convert to JSON
|
|
72
|
+
* @returns {Object}
|
|
73
|
+
*/
|
|
74
|
+
toJSON() {
|
|
75
|
+
return {
|
|
76
|
+
id: this.id,
|
|
77
|
+
ingameName: this.ingameName,
|
|
78
|
+
avatar: this.avatar,
|
|
79
|
+
reputation: this.reputation,
|
|
80
|
+
locale: this.locale,
|
|
81
|
+
platform: this.platform,
|
|
82
|
+
crossplay: this.crossplay,
|
|
83
|
+
status: this.status,
|
|
84
|
+
activity: this.activity,
|
|
85
|
+
lastSeen: this.lastSeen?.toISOString(),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Order model for Warframe Market API v2
|
|
92
|
+
* Represents a buy or sell order with optional user information
|
|
93
|
+
*/
|
|
94
|
+
export default class Order {
|
|
95
|
+
/**
|
|
96
|
+
* @param {Object} data - Raw order data from API
|
|
97
|
+
*/
|
|
98
|
+
constructor(data) {
|
|
99
|
+
this.id = data.id;
|
|
100
|
+
this.type = data.type; // 'buy' | 'sell'
|
|
101
|
+
this.platinum = data.platinum;
|
|
102
|
+
this.quantity = data.quantity;
|
|
103
|
+
this.perTrade = data.perTrade;
|
|
104
|
+
|
|
105
|
+
// Item modifiers
|
|
106
|
+
this.rank = data.rank;
|
|
107
|
+
this.charges = data.charges;
|
|
108
|
+
this.subtype = data.subtype;
|
|
109
|
+
this.amberStars = data.amberStars;
|
|
110
|
+
this.cyanStars = data.cyanStars;
|
|
111
|
+
|
|
112
|
+
// Metadata
|
|
113
|
+
this.visible = data.visible;
|
|
114
|
+
this.createdAt = data.createdAt ? new Date(data.createdAt) : null;
|
|
115
|
+
this.updatedAt = data.updatedAt ? new Date(data.updatedAt) : null;
|
|
116
|
+
this.itemId = data.itemId;
|
|
117
|
+
this.group = data.group;
|
|
118
|
+
|
|
119
|
+
// User data (if OrderWithUser)
|
|
120
|
+
this.user = data.user ? new OrderUser(data.user) : null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Check if order is from an online user
|
|
125
|
+
* @returns {boolean}
|
|
126
|
+
*/
|
|
127
|
+
isUserOnline() {
|
|
128
|
+
if (!this.user) return false;
|
|
129
|
+
return this.user.status === 'online' || this.user.status === 'ingame';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Check if order is buy type
|
|
134
|
+
* @returns {boolean}
|
|
135
|
+
*/
|
|
136
|
+
isBuyOrder() {
|
|
137
|
+
return this.type === 'buy';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Check if order is sell type
|
|
142
|
+
* @returns {boolean}
|
|
143
|
+
*/
|
|
144
|
+
isSellOrder() {
|
|
145
|
+
return this.type === 'sell';
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Get order age in hours
|
|
150
|
+
* @returns {number}
|
|
151
|
+
*/
|
|
152
|
+
getAgeHours() {
|
|
153
|
+
if (!this.updatedAt) return 0;
|
|
154
|
+
return (Date.now() - this.updatedAt.getTime()) / (1000 * 60 * 60);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Convert to JSON
|
|
159
|
+
* @returns {Object}
|
|
160
|
+
*/
|
|
161
|
+
toJSON() {
|
|
162
|
+
return {
|
|
163
|
+
id: this.id,
|
|
164
|
+
type: this.type,
|
|
165
|
+
platinum: this.platinum,
|
|
166
|
+
quantity: this.quantity,
|
|
167
|
+
perTrade: this.perTrade,
|
|
168
|
+
rank: this.rank,
|
|
169
|
+
charges: this.charges,
|
|
170
|
+
subtype: this.subtype,
|
|
171
|
+
amberStars: this.amberStars,
|
|
172
|
+
cyanStars: this.cyanStars,
|
|
173
|
+
visible: this.visible,
|
|
174
|
+
createdAt: this.createdAt?.toISOString(),
|
|
175
|
+
updatedAt: this.updatedAt?.toISOString(),
|
|
176
|
+
itemId: this.itemId,
|
|
177
|
+
group: this.group,
|
|
178
|
+
user: this.user?.toJSON(),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Convert to string representation
|
|
184
|
+
* @returns {string}
|
|
185
|
+
*/
|
|
186
|
+
toString() {
|
|
187
|
+
const userStr = this.user ? ` by ${this.user.ingameName}` : '';
|
|
188
|
+
const modifiers = [];
|
|
189
|
+
if (this.rank != null) modifiers.push(`R${this.rank}`);
|
|
190
|
+
if (this.subtype) modifiers.push(this.subtype);
|
|
191
|
+
const modStr = modifiers.length > 0 ? ` (${modifiers.join(', ')})` : '';
|
|
192
|
+
|
|
193
|
+
return `${this.type.toUpperCase()}: ${this.platinum}p x${this.quantity}${modStr}${userStr}`;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export { OrderUser };
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Summary model for v2 API
|
|
3
|
+
* Compatible with v1 Summary but with v2 data structures
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ASSETS_BASE_URL } from '../constants.js';
|
|
7
|
+
|
|
8
|
+
export default class SummaryV2 {
|
|
9
|
+
/**
|
|
10
|
+
* Create a summary from v2 Item and Order data
|
|
11
|
+
* @param {Item} item - Item object
|
|
12
|
+
* @param {Object} orders - Orders object with { buy, sell }
|
|
13
|
+
* @param {Object} statistics - Statistics object with { buy, sell }
|
|
14
|
+
*/
|
|
15
|
+
constructor(item, orders, statistics) {
|
|
16
|
+
// Basic item info
|
|
17
|
+
this.name = item.name;
|
|
18
|
+
this.slug = item.slug;
|
|
19
|
+
this.thumbnail = item.getThumbUrl(ASSETS_BASE_URL);
|
|
20
|
+
this.partThumb = item.subIcon ? `${ASSETS_BASE_URL}${item.subIcon}` : this.thumbnail;
|
|
21
|
+
this.icon = item.getIconUrl(ASSETS_BASE_URL);
|
|
22
|
+
|
|
23
|
+
// Item metadata
|
|
24
|
+
this.tradingTax = item.tradingTax || 0;
|
|
25
|
+
this.ducats = item.ducats || 0;
|
|
26
|
+
this.vosfor = item.vosfor || 0;
|
|
27
|
+
this.masteryLevel = item.reqMasteryRank;
|
|
28
|
+
this.tradable = item.tradable;
|
|
29
|
+
this.vaulted = item.vaulted;
|
|
30
|
+
|
|
31
|
+
// Links and descriptions
|
|
32
|
+
this.wikiUrl = item.wikiLink;
|
|
33
|
+
this.description = item.description || '';
|
|
34
|
+
this.codex = this.description; // v1 compatibility
|
|
35
|
+
this.url = `https://warframe.market/items/${item.slug}`;
|
|
36
|
+
|
|
37
|
+
// Tags
|
|
38
|
+
this.tags = item.tags || [];
|
|
39
|
+
|
|
40
|
+
// Prices and statistics (from v2 orders)
|
|
41
|
+
this.prices = {
|
|
42
|
+
soldCount: statistics.sell.orderCount,
|
|
43
|
+
soldPrice: statistics.sell.median,
|
|
44
|
+
minimum: statistics.sell.min,
|
|
45
|
+
maximum: statistics.sell.max,
|
|
46
|
+
average: statistics.sell.avg,
|
|
47
|
+
volume: statistics.sell.volume,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Store full orders and statistics
|
|
51
|
+
this.orders = orders;
|
|
52
|
+
this.statistics = statistics;
|
|
53
|
+
|
|
54
|
+
// Type marker
|
|
55
|
+
this.type = 'market-v2';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get best online sellers
|
|
60
|
+
* @param {number} limit - Number of sellers to return
|
|
61
|
+
* @returns {Array<{ingameName: string, platinum: number, quantity: number, status: string, activity: string}>}
|
|
62
|
+
*/
|
|
63
|
+
getOnlineSellers(limit = 5) {
|
|
64
|
+
return this.orders.sell
|
|
65
|
+
.filter((order) => order.isUserOnline())
|
|
66
|
+
.slice(0, limit)
|
|
67
|
+
.map((order) => ({
|
|
68
|
+
ingameName: order.user.ingameName,
|
|
69
|
+
platinum: order.platinum,
|
|
70
|
+
quantity: order.quantity,
|
|
71
|
+
status: order.user.status,
|
|
72
|
+
activity: order.user.getActivityDescription(),
|
|
73
|
+
reputation: order.user.reputation,
|
|
74
|
+
platform: order.user.platform,
|
|
75
|
+
crossplay: order.user.crossplay,
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Get best online buyers
|
|
81
|
+
* @param {number} limit - Number of buyers to return
|
|
82
|
+
* @returns {Array<{ingameName: string, platinum: number, quantity: number, status: string, activity: string}>}
|
|
83
|
+
*/
|
|
84
|
+
getOnlineBuyers(limit = 5) {
|
|
85
|
+
return this.orders.buy
|
|
86
|
+
.filter((order) => order.isUserOnline())
|
|
87
|
+
.slice(0, limit)
|
|
88
|
+
.map((order) => ({
|
|
89
|
+
ingameName: order.user.ingameName,
|
|
90
|
+
platinum: order.platinum,
|
|
91
|
+
quantity: order.quantity,
|
|
92
|
+
status: order.user.status,
|
|
93
|
+
activity: order.user.getActivityDescription(),
|
|
94
|
+
reputation: order.user.reputation,
|
|
95
|
+
platform: order.user.platform,
|
|
96
|
+
crossplay: order.user.crossplay,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* String representation (v1 compatible)
|
|
102
|
+
* @param {string} opt - Option: 'codex', 'item', 'location', 'all'
|
|
103
|
+
* @returns {string}
|
|
104
|
+
*/
|
|
105
|
+
toString(opt = 'item') {
|
|
106
|
+
let value = `**${this.name}**\n`;
|
|
107
|
+
|
|
108
|
+
if (opt === 'codex' || opt === 'all') {
|
|
109
|
+
value += `\`\`\`\nCodex: ${this.codex}\n\`\`\`\n`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (opt === 'item' || opt === 'all') {
|
|
113
|
+
value += '```\n';
|
|
114
|
+
if (this.masteryLevel) {
|
|
115
|
+
value += `Requires: MR ${this.masteryLevel} | `;
|
|
116
|
+
}
|
|
117
|
+
value += `Tax: ${this.tradingTax}cr\n`;
|
|
118
|
+
value += `Item is ${this.tradable ? '' : 'not '}tradable`;
|
|
119
|
+
if (this.vaulted) {
|
|
120
|
+
value += ' (VAULTED)';
|
|
121
|
+
}
|
|
122
|
+
value += '\n\n';
|
|
123
|
+
|
|
124
|
+
// Sell statistics
|
|
125
|
+
value += `${this.prices.soldCount} sellers (${this.prices.volume} items)\n`;
|
|
126
|
+
value += `Median: ${this.prices.soldPrice}p | `;
|
|
127
|
+
value += `Range: ${this.prices.minimum}p - ${this.prices.maximum}p\n`;
|
|
128
|
+
|
|
129
|
+
// Buy statistics (if available)
|
|
130
|
+
if (this.statistics.buy.orderCount > 0) {
|
|
131
|
+
value += `\n${this.statistics.buy.orderCount} buyers\n`;
|
|
132
|
+
value += `Median: ${this.statistics.buy.median}p | `;
|
|
133
|
+
value += `Range: ${this.statistics.buy.min}p - ${this.statistics.buy.max}p\n`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
value += '```\n';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (opt === 'traders' || opt === 'all') {
|
|
140
|
+
const sellers = this.getOnlineSellers(3);
|
|
141
|
+
if (sellers.length > 0) {
|
|
142
|
+
value += '\n**Online Sellers:**\n';
|
|
143
|
+
sellers.forEach((seller) => {
|
|
144
|
+
const statusEmoji = seller.status === 'ingame' ? '🎮' : '🟢';
|
|
145
|
+
const activity = seller.activity ? ` (${seller.activity})` : '';
|
|
146
|
+
value += `${statusEmoji} ${seller.ingameName} - ${seller.platinum}p x${seller.quantity}${activity}\n`;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
value += `\n${this.wikiUrl || this.url}`;
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Padding helper (v1 compatibility)
|
|
157
|
+
* @param {string} str - String to pad
|
|
158
|
+
* @param {number} length - Target length
|
|
159
|
+
* @param {string} character - Padding character
|
|
160
|
+
* @returns {string}
|
|
161
|
+
*/
|
|
162
|
+
pad(str, length = 10, character = ' ') {
|
|
163
|
+
return str.padEnd(length, character);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript type definitions for Warframe Market API v2
|
|
3
|
+
* @module types
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface ItemData {
|
|
7
|
+
id: string;
|
|
8
|
+
slug: string;
|
|
9
|
+
gameRef?: string;
|
|
10
|
+
tags?: string[];
|
|
11
|
+
tradingTax?: number;
|
|
12
|
+
ducats?: number;
|
|
13
|
+
vosfor?: number;
|
|
14
|
+
masteryLevel?: number;
|
|
15
|
+
maxRank?: number;
|
|
16
|
+
i18n: {
|
|
17
|
+
[lang: string]: {
|
|
18
|
+
name: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
icon?: string;
|
|
21
|
+
thumb?: string;
|
|
22
|
+
subIcon?: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface OrderUserData {
|
|
28
|
+
id: string;
|
|
29
|
+
ingameName: string;
|
|
30
|
+
lastSeen?: string;
|
|
31
|
+
reputation?: number;
|
|
32
|
+
locale?: string;
|
|
33
|
+
avatar?: string;
|
|
34
|
+
status?: 'invisible' | 'offline' | 'online' | 'ingame';
|
|
35
|
+
activity?: {
|
|
36
|
+
type: string;
|
|
37
|
+
string: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface OrderData {
|
|
42
|
+
id: string;
|
|
43
|
+
platinum: number;
|
|
44
|
+
quantity: number;
|
|
45
|
+
orderType: 'buy' | 'sell';
|
|
46
|
+
platform: string;
|
|
47
|
+
creationDate: string;
|
|
48
|
+
lastUpdate: string;
|
|
49
|
+
visible: boolean;
|
|
50
|
+
user: OrderUserData;
|
|
51
|
+
region?: string;
|
|
52
|
+
rank?: number;
|
|
53
|
+
modRank?: number;
|
|
54
|
+
charges?: number;
|
|
55
|
+
amberStars?: number;
|
|
56
|
+
subtype?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface StatisticsData {
|
|
60
|
+
median?: number;
|
|
61
|
+
mean?: number;
|
|
62
|
+
min?: number;
|
|
63
|
+
max?: number;
|
|
64
|
+
volume?: number;
|
|
65
|
+
orderCount?: number;
|
|
66
|
+
stdDev?: number;
|
|
67
|
+
range?: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface SummaryData {
|
|
71
|
+
name: string;
|
|
72
|
+
slug: string;
|
|
73
|
+
thumbnail: string;
|
|
74
|
+
icon?: string;
|
|
75
|
+
url: string;
|
|
76
|
+
color: string;
|
|
77
|
+
tradingTax: number;
|
|
78
|
+
ducats?: number;
|
|
79
|
+
vosfor?: number;
|
|
80
|
+
masteryLevel?: number;
|
|
81
|
+
type: 'market-v2';
|
|
82
|
+
prices: {
|
|
83
|
+
soldCount: number;
|
|
84
|
+
soldPrice: number;
|
|
85
|
+
minimum: number;
|
|
86
|
+
maximum: number;
|
|
87
|
+
volume: number;
|
|
88
|
+
};
|
|
89
|
+
orders: {
|
|
90
|
+
buy: OrderData[];
|
|
91
|
+
sell: OrderData[];
|
|
92
|
+
};
|
|
93
|
+
statistics: {
|
|
94
|
+
buy: StatisticsData;
|
|
95
|
+
sell: StatisticsData;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface QueryOptions {
|
|
100
|
+
platform: string;
|
|
101
|
+
successfulQuery?: () => void;
|
|
102
|
+
rank?: number;
|
|
103
|
+
rankLt?: number;
|
|
104
|
+
charges?: number;
|
|
105
|
+
chargesLt?: number;
|
|
106
|
+
amberStars?: number;
|
|
107
|
+
amberStarsLt?: number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface TraderInfo {
|
|
111
|
+
ingameName: string;
|
|
112
|
+
platinum: number;
|
|
113
|
+
status?: string;
|
|
114
|
+
activity?: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface MarketFetcherV2Options {
|
|
118
|
+
locale?: string;
|
|
119
|
+
timeout?: number;
|
|
120
|
+
logger?: Console;
|
|
121
|
+
cacheSize?: number;
|
|
122
|
+
cacheTTL?: number;
|
|
123
|
+
baseURL?: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface HttpClientOptions {
|
|
127
|
+
baseURL?: string;
|
|
128
|
+
timeout?: number;
|
|
129
|
+
locale?: string;
|
|
130
|
+
logger?: Console;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface CacheOptions {
|
|
134
|
+
maxSize?: number;
|
|
135
|
+
ttl?: number;
|
|
136
|
+
logger?: Console;
|
|
137
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { API_BASE_URL, VERSION_CHECK_INTERVAL, CACHE_TTL } from '../constants.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Version-based cache system for Warframe Market API v2
|
|
5
|
+
* Uses version hashes from /v2/versions endpoint to invalidate cache
|
|
6
|
+
*/
|
|
7
|
+
export default class VersionedCache {
|
|
8
|
+
#cache;
|
|
9
|
+
#versions;
|
|
10
|
+
#lastVersionCheck;
|
|
11
|
+
|
|
12
|
+
constructor({ maxSize = 100, ttl = CACHE_TTL } = {}) {
|
|
13
|
+
this.#cache = new Map();
|
|
14
|
+
this.maxSize = maxSize;
|
|
15
|
+
this.ttl = ttl;
|
|
16
|
+
this.#versions = undefined;
|
|
17
|
+
this.#lastVersionCheck = undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get item from cache or fetch it
|
|
22
|
+
* @param {string} key - Cache key
|
|
23
|
+
* @param {string} collection - Collection name (items, rivens, etc.)
|
|
24
|
+
* @param {function} fetchFn - Function to fetch data if cache miss
|
|
25
|
+
* @returns {Promise<*>}
|
|
26
|
+
*/
|
|
27
|
+
async get(key, collection, fetchFn) {
|
|
28
|
+
// Check if we should refresh based on version
|
|
29
|
+
if (collection && (await this.shouldRefresh(collection))) {
|
|
30
|
+
return this.refresh(key, collection, fetchFn);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Try to get from cache
|
|
34
|
+
const cached = this.#get(key);
|
|
35
|
+
if (cached !== undefined) {
|
|
36
|
+
return cached;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Cache miss - fetch and store
|
|
40
|
+
const data = await fetchFn();
|
|
41
|
+
this.set(key, data, collection);
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Set item in cache
|
|
47
|
+
* @param {string} key - Cache key
|
|
48
|
+
* @param {*} value - Value to cache
|
|
49
|
+
* @param {string} [collection] - Collection name for version tracking
|
|
50
|
+
*/
|
|
51
|
+
set(key, value, collection) {
|
|
52
|
+
// LRU eviction if cache is full
|
|
53
|
+
if (this.#cache.size >= this.maxSize) {
|
|
54
|
+
const firstKey = this.#cache.keys().next().value;
|
|
55
|
+
this.#cache.delete(firstKey);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.#cache.set(key, {
|
|
59
|
+
value,
|
|
60
|
+
timestamp: Date.now(),
|
|
61
|
+
collection,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Store collection version if provided
|
|
65
|
+
if (collection && this.#versions) {
|
|
66
|
+
const versionKey = `_version_${collection}`;
|
|
67
|
+
const collectionVersion = this.#versions.data?.collections?.[collection];
|
|
68
|
+
if (collectionVersion) {
|
|
69
|
+
this.#cache.set(versionKey, {
|
|
70
|
+
value: collectionVersion,
|
|
71
|
+
timestamp: Date.now(),
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Get item from cache (internal)
|
|
79
|
+
* @param {string} key - Cache key
|
|
80
|
+
* @returns {*|undefined}
|
|
81
|
+
* @private
|
|
82
|
+
*/
|
|
83
|
+
#get(key) {
|
|
84
|
+
const cached = this.#cache.get(key);
|
|
85
|
+
if (!cached) return undefined;
|
|
86
|
+
|
|
87
|
+
// Check TTL
|
|
88
|
+
if (Date.now() - cached.timestamp > this.ttl) {
|
|
89
|
+
this.#cache.delete(key);
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return cached.value;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Check if collection should be refreshed
|
|
98
|
+
* @param {string} collection - Collection name
|
|
99
|
+
* @returns {Promise<boolean>}
|
|
100
|
+
*/
|
|
101
|
+
async shouldRefresh(collection) {
|
|
102
|
+
try {
|
|
103
|
+
const currentVersions = await this.checkVersions();
|
|
104
|
+
const cachedVersion = this.#get(`_version_${collection}`);
|
|
105
|
+
const serverVersion = currentVersions.data?.collections?.[collection];
|
|
106
|
+
|
|
107
|
+
return serverVersion && cachedVersion !== serverVersion;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
// If version check fails, don't force refresh
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Refresh cache entry
|
|
116
|
+
* @param {string} key - Cache key
|
|
117
|
+
* @param {string} collection - Collection name
|
|
118
|
+
* @param {function} fetchFn - Function to fetch fresh data
|
|
119
|
+
* @returns {Promise<*>}
|
|
120
|
+
*/
|
|
121
|
+
async refresh(key, collection, fetchFn) {
|
|
122
|
+
const data = await fetchFn();
|
|
123
|
+
this.set(key, data, collection);
|
|
124
|
+
return data;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Check versions from server
|
|
129
|
+
* @returns {Promise<Object>}
|
|
130
|
+
*/
|
|
131
|
+
async checkVersions() {
|
|
132
|
+
// Check every VERSION_CHECK_INTERVAL (5 minutes)
|
|
133
|
+
if (this.#versions && this.#lastVersionCheck && Date.now() - this.#lastVersionCheck < VERSION_CHECK_INTERVAL) {
|
|
134
|
+
return this.#versions;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
const response = await fetch(`${API_BASE_URL}/versions`);
|
|
139
|
+
if (!response.ok) {
|
|
140
|
+
throw new Error(`Version check failed: ${response.status}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
this.#versions = await response.json();
|
|
144
|
+
this.#lastVersionCheck = Date.now();
|
|
145
|
+
|
|
146
|
+
return this.#versions;
|
|
147
|
+
} catch (error) {
|
|
148
|
+
// If fetch fails and we have cached versions, return them
|
|
149
|
+
if (this.#versions) {
|
|
150
|
+
return this.#versions;
|
|
151
|
+
}
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Clear entire cache
|
|
158
|
+
*/
|
|
159
|
+
clear() {
|
|
160
|
+
this.#cache.clear();
|
|
161
|
+
this.#versions = undefined;
|
|
162
|
+
this.#lastVersionCheck = undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Delete specific key from cache
|
|
167
|
+
* @param {string} key - Cache key
|
|
168
|
+
*/
|
|
169
|
+
delete(key) {
|
|
170
|
+
this.#cache.delete(key);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get cache size
|
|
175
|
+
* @returns {number}
|
|
176
|
+
*/
|
|
177
|
+
size() {
|
|
178
|
+
return this.#cache.size;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Check if key exists in cache (and is valid)
|
|
183
|
+
* @param {string} key - Cache key
|
|
184
|
+
* @returns {boolean}
|
|
185
|
+
*/
|
|
186
|
+
has(key) {
|
|
187
|
+
return this.#get(key) !== undefined;
|
|
188
|
+
}
|
|
189
|
+
}
|