@0xarchive/sdk 0.3.3 → 0.3.5

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/dist/index.js CHANGED
@@ -81,6 +81,25 @@ var OxArchiveError = class extends Error {
81
81
  };
82
82
 
83
83
  // src/http.ts
84
+ function snakeToCamel(str) {
85
+ return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
86
+ }
87
+ function transformKeys(obj) {
88
+ if (obj === null || obj === void 0) {
89
+ return obj;
90
+ }
91
+ if (Array.isArray(obj)) {
92
+ return obj.map(transformKeys);
93
+ }
94
+ if (typeof obj === "object") {
95
+ const result = {};
96
+ for (const [key, value] of Object.entries(obj)) {
97
+ result[snakeToCamel(key)] = transformKeys(value);
98
+ }
99
+ return result;
100
+ }
101
+ return obj;
102
+ }
84
103
  var HttpClient = class {
85
104
  baseUrl;
86
105
  apiKey;
@@ -108,7 +127,11 @@ var HttpClient = class {
108
127
  if (params) {
109
128
  for (const [key, value] of Object.entries(params)) {
110
129
  if (value !== void 0 && value !== null) {
111
- url.searchParams.set(key, String(value));
130
+ if (value instanceof Date) {
131
+ url.searchParams.set(key, String(value.getTime()));
132
+ } else {
133
+ url.searchParams.set(key, String(value));
134
+ }
112
135
  }
113
136
  }
114
137
  }
@@ -124,22 +147,25 @@ var HttpClient = class {
124
147
  signal: controller.signal
125
148
  });
126
149
  clearTimeout(timeoutId);
127
- const data = await response.json();
150
+ const rawData = await response.json();
151
+ const data = transformKeys(rawData);
128
152
  if (!response.ok) {
129
153
  const error = data;
154
+ const apiResponse = data;
130
155
  throw new OxArchiveError(
131
156
  error.error || `Request failed with status ${response.status}`,
132
157
  response.status,
133
- data.meta?.request_id
158
+ apiResponse.meta?.requestId
134
159
  );
135
160
  }
136
161
  if (this.validate && schema) {
137
162
  const result = schema.safeParse(data);
138
163
  if (!result.success) {
164
+ const apiResponse = data;
139
165
  throw new OxArchiveError(
140
166
  `Response validation failed: ${result.error.message}`,
141
167
  422,
142
- data.meta?.request_id
168
+ apiResponse.meta?.requestId
143
169
  );
144
170
  }
145
171
  return result.data;
@@ -165,8 +191,8 @@ var HttpClient = class {
165
191
  var import_zod = require("zod");
166
192
  var ApiMetaSchema = import_zod.z.object({
167
193
  count: import_zod.z.number(),
168
- next_cursor: import_zod.z.string().optional(),
169
- request_id: import_zod.z.string()
194
+ nextCursor: import_zod.z.string().optional(),
195
+ requestId: import_zod.z.string()
170
196
  });
171
197
  var ApiResponseSchema = (dataSchema) => import_zod.z.object({
172
198
  success: import_zod.z.boolean(),
@@ -183,17 +209,12 @@ var OrderBookSchema = import_zod.z.object({
183
209
  timestamp: import_zod.z.string(),
184
210
  bids: import_zod.z.array(PriceLevelSchema),
185
211
  asks: import_zod.z.array(PriceLevelSchema),
186
- mid_price: import_zod.z.string().optional(),
212
+ midPrice: import_zod.z.string().optional(),
187
213
  spread: import_zod.z.string().optional(),
188
- spread_bps: import_zod.z.string().optional()
214
+ spreadBps: import_zod.z.string().optional()
189
215
  });
190
216
  var TradeSideSchema = import_zod.z.enum(["A", "B"]);
191
- var TradeDirectionSchema = import_zod.z.enum([
192
- "Open Long",
193
- "Open Short",
194
- "Close Long",
195
- "Close Short"
196
- ]);
217
+ var TradeDirectionSchema = import_zod.z.string();
197
218
  var DataSourceSchema = import_zod.z.enum(["s3", "ws", "api", "live"]);
198
219
  var TradeSchema = import_zod.z.object({
199
220
  coin: import_zod.z.string(),
@@ -201,19 +222,19 @@ var TradeSchema = import_zod.z.object({
201
222
  price: import_zod.z.string(),
202
223
  size: import_zod.z.string(),
203
224
  timestamp: import_zod.z.string(),
204
- tx_hash: import_zod.z.string().optional(),
205
- trade_id: import_zod.z.number().optional(),
206
- order_id: import_zod.z.number().optional(),
225
+ txHash: import_zod.z.string().optional(),
226
+ tradeId: import_zod.z.number().optional(),
227
+ orderId: import_zod.z.number().optional(),
207
228
  crossed: import_zod.z.boolean().optional(),
208
229
  fee: import_zod.z.string().optional(),
209
- fee_token: import_zod.z.string().optional(),
210
- closed_pnl: import_zod.z.string().optional(),
230
+ feeToken: import_zod.z.string().optional(),
231
+ closedPnl: import_zod.z.string().optional(),
211
232
  direction: TradeDirectionSchema.optional(),
212
- start_position: import_zod.z.string().optional(),
233
+ startPosition: import_zod.z.string().optional(),
213
234
  source: DataSourceSchema.optional(),
214
- user_address: import_zod.z.string().optional(),
215
- maker_address: import_zod.z.string().optional(),
216
- taker_address: import_zod.z.string().optional()
235
+ userAddress: import_zod.z.string().optional(),
236
+ makerAddress: import_zod.z.string().optional(),
237
+ takerAddress: import_zod.z.string().optional()
217
238
  });
218
239
  var InstrumentTypeSchema = import_zod.z.enum(["perp", "spot"]);
219
240
  var InstrumentSchema = import_zod.z.object({
@@ -227,20 +248,20 @@ var InstrumentSchema = import_zod.z.object({
227
248
  var FundingRateSchema = import_zod.z.object({
228
249
  coin: import_zod.z.string(),
229
250
  timestamp: import_zod.z.string(),
230
- funding_rate: import_zod.z.string(),
251
+ fundingRate: import_zod.z.string(),
231
252
  premium: import_zod.z.string().optional()
232
253
  });
233
254
  var OpenInterestSchema = import_zod.z.object({
234
255
  coin: import_zod.z.string(),
235
256
  timestamp: import_zod.z.string(),
236
- open_interest: import_zod.z.string(),
237
- mark_price: import_zod.z.string().optional(),
238
- oracle_price: import_zod.z.string().optional(),
239
- day_ntl_volume: import_zod.z.string().optional(),
240
- prev_day_price: import_zod.z.string().optional(),
241
- mid_price: import_zod.z.string().optional(),
242
- impact_bid_price: import_zod.z.string().optional(),
243
- impact_ask_price: import_zod.z.string().optional()
257
+ openInterest: import_zod.z.string(),
258
+ markPrice: import_zod.z.string().optional(),
259
+ oraclePrice: import_zod.z.string().optional(),
260
+ dayNtlVolume: import_zod.z.string().optional(),
261
+ prevDayPrice: import_zod.z.string().optional(),
262
+ midPrice: import_zod.z.string().optional(),
263
+ impactBidPrice: import_zod.z.string().optional(),
264
+ impactAskPrice: import_zod.z.string().optional()
244
265
  });
245
266
  var WsChannelSchema = import_zod.z.enum(["orderbook", "trades", "ticker", "all_tickers"]);
246
267
  var WsConnectionStateSchema = import_zod.z.enum(["connecting", "connected", "disconnected", "reconnecting"]);
@@ -277,17 +298,17 @@ var WsReplayStartedSchema = import_zod.z.object({
277
298
  });
278
299
  var WsReplayPausedSchema = import_zod.z.object({
279
300
  type: import_zod.z.literal("replay_paused"),
280
- current_timestamp: import_zod.z.number()
301
+ currentTimestamp: import_zod.z.number()
281
302
  });
282
303
  var WsReplayResumedSchema = import_zod.z.object({
283
304
  type: import_zod.z.literal("replay_resumed"),
284
- current_timestamp: import_zod.z.number()
305
+ currentTimestamp: import_zod.z.number()
285
306
  });
286
307
  var WsReplayCompletedSchema = import_zod.z.object({
287
308
  type: import_zod.z.literal("replay_completed"),
288
309
  channel: WsChannelSchema,
289
310
  coin: import_zod.z.string(),
290
- snapshots_sent: import_zod.z.number()
311
+ snapshotsSent: import_zod.z.number()
291
312
  });
292
313
  var WsReplayStoppedSchema = import_zod.z.object({
293
314
  type: import_zod.z.literal("replay_stopped")
@@ -308,7 +329,7 @@ var WsStreamStartedSchema = import_zod.z.object({
308
329
  });
309
330
  var WsStreamProgressSchema = import_zod.z.object({
310
331
  type: import_zod.z.literal("stream_progress"),
311
- snapshots_sent: import_zod.z.number()
332
+ snapshotsSent: import_zod.z.number()
312
333
  });
313
334
  var TimestampedRecordSchema = import_zod.z.object({
314
335
  timestamp: import_zod.z.number(),
@@ -324,11 +345,11 @@ var WsStreamCompletedSchema = import_zod.z.object({
324
345
  type: import_zod.z.literal("stream_completed"),
325
346
  channel: WsChannelSchema,
326
347
  coin: import_zod.z.string(),
327
- snapshots_sent: import_zod.z.number()
348
+ snapshotsSent: import_zod.z.number()
328
349
  });
329
350
  var WsStreamStoppedSchema = import_zod.z.object({
330
351
  type: import_zod.z.literal("stream_stopped"),
331
- snapshots_sent: import_zod.z.number()
352
+ snapshotsSent: import_zod.z.number()
332
353
  });
333
354
  var WsServerMessageSchema = import_zod.z.discriminatedUnion("type", [
334
355
  WsSubscribedSchema,
@@ -438,7 +459,7 @@ var TradesResource = class {
438
459
  );
439
460
  return {
440
461
  data: response.data,
441
- nextCursor: response.meta.next_cursor
462
+ nextCursor: response.meta.nextCursor
442
463
  };
443
464
  }
444
465
  /**
@@ -666,11 +687,11 @@ function transformTrade(coin, raw) {
666
687
  price: px ?? "0",
667
688
  size: sz ?? "0",
668
689
  timestamp: time ? new Date(time).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
669
- tx_hash: hash,
670
- trade_id: tid,
671
- maker_address,
672
- taker_address,
673
- user_address
690
+ txHash: hash,
691
+ tradeId: tid,
692
+ makerAddress: maker_address,
693
+ takerAddress: taker_address,
694
+ userAddress: user_address
674
695
  };
675
696
  }
676
697
  function transformTrades(coin, rawTrades) {
@@ -695,25 +716,25 @@ function transformOrderbook(coin, raw) {
695
716
  asks.push({ px: level.px, sz: level.sz, n: level.n });
696
717
  }
697
718
  }
698
- let mid_price;
719
+ let midPrice;
699
720
  let spread;
700
- let spread_bps;
721
+ let spreadBps;
701
722
  if (bids.length > 0 && asks.length > 0) {
702
723
  const bestBid = parseFloat(bids[0].px);
703
724
  const bestAsk = parseFloat(asks[0].px);
704
725
  const mid = (bestBid + bestAsk) / 2;
705
- mid_price = mid.toString();
726
+ midPrice = mid.toString();
706
727
  spread = (bestAsk - bestBid).toString();
707
- spread_bps = ((bestAsk - bestBid) / mid * 1e4).toFixed(2);
728
+ spreadBps = ((bestAsk - bestBid) / mid * 1e4).toFixed(2);
708
729
  }
709
730
  return {
710
731
  coin,
711
732
  timestamp: time ? new Date(time).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
712
733
  bids,
713
734
  asks,
714
- mid_price,
735
+ midPrice,
715
736
  spread,
716
- spread_bps
737
+ spreadBps
717
738
  };
718
739
  }
719
740
  var OxArchiveWs = class {