Skip to content

Commit a495fd3

Browse files
committed
feat: camel case only
1 parent cec28d3 commit a495fd3

25 files changed

+514
-291
lines changed

lib/client.ts

Lines changed: 117 additions & 105 deletions
Large diffs are not rendered by default.

lib/models/Fee.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export interface Fee {
22
fee: string;
33
currency: string;
44
amount: string;
5-
networkCode: string;
5+
networkFee: string;
66
}

lib/models/OrderBook.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
export interface WSOrderBook {
2+
timestamp: number;
3+
sequence: number;
4+
asks: OrderBookLevel[];
5+
bids: OrderBookLevel[];
6+
}
7+
8+
export interface WSOrderBookRaw {
29
/**
310
* timetsmap
411
*/
@@ -20,14 +27,41 @@ export interface WSOrderBook {
2027
export type OrderBookLevel = [price: number, amount: number];
2128

2229
export interface OrderBookTop {
30+
timestamp: number;
31+
bestAsk: string;
32+
bestAskQuantity: string;
33+
bestBid: string;
34+
bestBidQuantity: string;
35+
}
36+
37+
export interface OrderBookTopRaw {
38+
/**
39+
* timestamp
40+
*/
2341
t: number;
42+
/**
43+
* best ask
44+
*/
2445
a: string;
46+
/**
47+
* best ask quantity
48+
*/
2549
A: string;
50+
/**
51+
* best bid
52+
*/
2653
b: string;
54+
/**
55+
* best bid quantity
56+
*/
2757
B: string;
2858
}
2959

3060
export interface PriceRate {
61+
timestamp: number,
62+
rate: string
63+
}
64+
export interface PriceRateRaw {
3165
/**
3266
* timestamp
3367
*/

lib/models/WSCandle.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
export interface WSCandle {
2+
timestamp: number;
3+
openPrice: string;
4+
closePrice: string;
5+
highPrice: string;
6+
lowPrice: string;
7+
baseVolume: string;
8+
quoteVolume: string;
9+
}
10+
11+
export type MiniTicker = WSCandle;
12+
13+
export interface WSCandleRaw {
214
/**
315
* timestamp
416
*/
@@ -28,5 +40,3 @@ export interface WSCandle {
2840
*/
2941
q: string;
3042
}
31-
32-
export type MiniTicker = WSCandle;

lib/models/WSTicker.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
export interface WSTicker {
2+
timestamp: number;
3+
bestAsk: string;
4+
bestAskQuantity: string;
5+
bestBid: string;
6+
bestBidQuantity: string;
7+
closePrice: string;
8+
openPrice: string;
9+
highPrice: string;
10+
lowPrice: string;
11+
baseVolume: string;
12+
quoteVolume: string;
13+
priceChange: string;
14+
PriceChangePercent: string;
15+
lastTradeId: number;
16+
}
17+
18+
export interface WSTickerRaw {
219
/**
320
* timestamp
421
*/

lib/models/WSTrades.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { SIDE } from "../constants";
22

33
export interface WSTrade {
4+
timestamp: number;
5+
id: number;
6+
price: string;
7+
quantity: string;
8+
side: SIDE;
9+
}
10+
11+
export interface WSTradeRaw {
412
/**
513
* timestamp
614
*/

lib/paramStyleConverter.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export const fromCamelCaseToSnakeCase = (obj: any): any => {
2+
if (isNull(obj)) {
3+
return obj
4+
}
25
if (isArray(obj)) {
36
return (obj as any[]).map(val => fromCamelCaseToSnakeCase(val));
47
}
@@ -16,6 +19,9 @@ const convertObjectKeysToSnakeCase = (obj: { [x: string]: any }): { [x: string]:
1619
}
1720

1821
export const fromSnakeCaseToCamelCase = (obj: any): any => {
22+
if (isNull(obj)) {
23+
return obj
24+
}
1925
if (isArray(obj)) {
2026
return (obj as any[]).map(val => fromSnakeCaseToCamelCase(val));
2127
}
@@ -38,7 +44,7 @@ const camelCaseToSnakeCase = (camelCase: string) => {
3844
}
3945

4046
const snakeCaseToCamelCase = (value: string) =>
41-
value.toLowerCase().replace(/([_][a-z])/g, _letter => _letter.replace('_', '').toUpperCase()
47+
value.replace(/([_][a-z])/g, _letter => _letter.replace('_', '').toUpperCase()
4248
);
4349

4450
const isObject = (value: any) => {
@@ -50,3 +56,7 @@ const isArray = (value: any) => {
5056
}
5157

5258

59+
function isNull(obj: any) {
60+
return obj === undefined || obj === null
61+
}
62+

lib/websocket/DataParser.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { NOTIFICATION_TYPE } from "../constants";
2+
import { OrderBookTop, OrderBookTopRaw, PriceRate, PriceRateRaw, WSCandle, WSCandleRaw, WSOrderBook, WSOrderBookRaw, WSTicker, WSTickerRaw, WSTrade, WSTradeRaw } from "../models";
3+
4+
type JsonObject<T> = { [x: string]: T };
5+
type MapNotificationCallback<T> = (notification: JsonObject<T>, notificationType: NOTIFICATION_TYPE) => any;
6+
type ParseFn<TRaw, T> = (raw: TRaw) => T
7+
8+
export const parseMapListInterceptor = <TRaw, T>(
9+
callback: MapNotificationCallback<T[]>, parseFn: ParseFn<TRaw, T>) => (
10+
notification: JsonObject<TRaw[]>, notificationType: NOTIFICATION_TYPE) => {
11+
const parsedEntries = Object.entries(notification).map(([key, rawValueList]) => [key, rawValueList.map(parseFn)])
12+
const parsedNotification = Object.fromEntries(parsedEntries)
13+
return callback(parsedNotification, notificationType)
14+
}
15+
16+
export const parseMapInterceptor = <TRaw, T>(
17+
callback: MapNotificationCallback<T>, parseFn: ParseFn<TRaw, T>) => (
18+
notification: JsonObject<TRaw>, notificationType: NOTIFICATION_TYPE) => {
19+
const parsedEntries = Object.entries(notification).map(([key, rawValue]) => [key, parseFn(rawValue)])
20+
const parsedNotification = Object.fromEntries(parsedEntries)
21+
return callback(parsedNotification, notificationType)
22+
}
23+
24+
25+
export const parseWSTrade = (wsTradeRaw: WSTradeRaw): WSTrade => {
26+
return {
27+
timestamp: wsTradeRaw.t,
28+
id: wsTradeRaw.i,
29+
price: wsTradeRaw.p,
30+
quantity: wsTradeRaw.q,
31+
side: wsTradeRaw.s,
32+
}
33+
}
34+
35+
export const parseWSCandle = (wsCandleRaw: WSCandleRaw): WSCandle => {
36+
return {
37+
timestamp: wsCandleRaw.t,
38+
openPrice: wsCandleRaw.o,
39+
closePrice: wsCandleRaw.c,
40+
highPrice: wsCandleRaw.h,
41+
lowPrice: wsCandleRaw.l,
42+
baseVolume: wsCandleRaw.v,
43+
quoteVolume: wsCandleRaw.q,
44+
}
45+
}
46+
47+
48+
export const parseWSTicker = (wsTickerRaw: WSTickerRaw): WSTicker => {
49+
return {
50+
timestamp: wsTickerRaw.t,
51+
bestAsk: wsTickerRaw.a,
52+
bestAskQuantity: wsTickerRaw.A,
53+
bestBid: wsTickerRaw.b,
54+
bestBidQuantity: wsTickerRaw.B,
55+
closePrice: wsTickerRaw.c,
56+
openPrice: wsTickerRaw.o,
57+
highPrice: wsTickerRaw.h,
58+
lowPrice: wsTickerRaw.l,
59+
baseVolume: wsTickerRaw.v,
60+
quoteVolume: wsTickerRaw.q,
61+
priceChange: wsTickerRaw.p,
62+
PriceChangePercent: wsTickerRaw.P,
63+
lastTradeId: wsTickerRaw.L,
64+
}
65+
}
66+
67+
68+
export const parseWSOrderbook = (wsOrderbookRaw: WSOrderBookRaw): WSOrderBook => {
69+
return {
70+
timestamp: wsOrderbookRaw.t,
71+
sequence: wsOrderbookRaw.s,
72+
asks: wsOrderbookRaw.a,
73+
bids: wsOrderbookRaw.b,
74+
}
75+
}
76+
77+
export const parseOrderbookTop = (orderbookTopRaw: OrderBookTopRaw): OrderBookTop => {
78+
return {
79+
timestamp: orderbookTopRaw.t,
80+
bestAsk: orderbookTopRaw.a,
81+
bestAskQuantity: orderbookTopRaw.A,
82+
bestBid: orderbookTopRaw.b,
83+
bestBidQuantity: orderbookTopRaw.B,
84+
}
85+
}
86+
87+
export const parsePriceRate = (priceRateRaw: PriceRateRaw): PriceRate => {
88+
return {
89+
timestamp: priceRateRaw.t,
90+
rate: priceRateRaw.r,
91+
}
92+
}

lib/websocket/authClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { WSClientBase } from "./clientBase";
33

44
interface AuthPayload {
55
type: string
6-
api_Key: string
6+
api_key: string
77
timestamp: number
88
window?: number
99
signature: string
@@ -53,7 +53,7 @@ export class AuthClient extends WSClientBase {
5353
const timestamp = Math.floor(Date.now());
5454
const payload: AuthPayload = {
5555
type: "HS256",
56-
api_Key: this.apiKey,
56+
api_key: this.apiKey,
5757
timestamp: timestamp,
5858
signature: "",
5959
};

lib/websocket/clientBase.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventEmitter } from "events";
22
import WebSocket from "ws";
33
import { ResponsePromiseFactory } from "./ResponsePromiseFactory";
44
import { WSResponse } from "./WSResponse";
5+
import { fromCamelCaseToSnakeCase } from "../paramStyleConverter";
56

67
export class WSClientBase {
78
private uri: string;
@@ -101,13 +102,15 @@ export class WSClientBase {
101102
return response as Boolean;
102103
}
103104

104-
protected request({ method, params = {} }: { method: any; params?: {}; }): Promise<unknown> {
105+
protected request({ method, params: paramsRaw = {} }: { method: any; params?: {}; }): Promise<unknown> {
106+
const params = fromCamelCaseToSnakeCase(paramsRaw)
105107
const { id, promise } = this.multiResponsePromiseFactory.newOneResponsePromise();
106108
const payload = { method, params, id };
107109
this.ws.send(JSON.stringify(payload));
108110
return withTimeout(this.requestTimeoutMs, promise);
109111
}
110-
protected requestList({ method, params = {}, responseCount = 1 }: { method: any; params?: {}; responseCount?: number; }): Promise<unknown> {
112+
protected requestList({ method, params: paramsRaw = {}, responseCount = 1 }: { method: any; params?: {}; responseCount?: number; }): Promise<unknown> {
113+
const params = fromCamelCaseToSnakeCase(paramsRaw)
111114
const { id, promise } = this.multiResponsePromiseFactory.newMultiResponsePromise(responseCount);
112115
const payload = { method, params, id };
113116
this.ws.send(JSON.stringify(payload));

0 commit comments

Comments
 (0)