fix linter configuration & jest type dependency

This commit is contained in:
tiagosiebler
2023-02-17 13:51:40 +00:00
parent 7669c037c8
commit 8e54ecbaf5
34 changed files with 1600 additions and 1710 deletions

View File

@@ -1,14 +1,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
import { signMessage } from './node-support';
import {
RestClientOptions,
serializeParams,
RestClientType,
REST_CLIENT_TYPE_ENUM,
APIID,
REST_CLIENT_TYPE_ENUM,
RestClientOptions,
RestClientType,
getRestBaseUrl,
serializeParams,
} from './requestUtils';
import { signMessage } from './node-support';
// axios.interceptors.request.use((request) => {
// console.log(new Date(), 'Starting Request', JSON.stringify(request, null, 2));
@@ -48,7 +49,7 @@ interface SignedRequestContext {
}
interface SignedRequest<T> {
originalParams: T & SignedRequestContext;
originalParams: (T & SignedRequestContext) | SignedRequestContext;
paramsWithSign?: T & SignedRequestContext & { sign: string };
serializedParams: string;
sign: string;
@@ -65,12 +66,19 @@ type SignMethod = 'keyInBody' | 'usdc';
export default abstract class BaseRestClient {
private timeOffset: number | null = null;
private syncTimePromise: null | Promise<any> = null;
private options: RestClientOptions;
private baseUrl: string;
private globalRequestOptions: AxiosRequestConfig;
private key: string | undefined;
private secret: string | undefined;
private clientType: RestClientType;
/** Function that calls exchange API to query & resolve server time, used by time sync, disabled by default */
@@ -159,13 +167,15 @@ export default abstract class BaseRestClient {
params?: TParams,
isPublicApi?: true
): Promise<UnsignedRequest<TParams>>;
private async prepareSignParams<TParams = any>(
method: Method,
signMethod: SignMethod,
params?: TParams,
isPublicApi?: false | undefined
): Promise<SignedRequest<TParams>>;
private async prepareSignParams<TParams = any>(
private async prepareSignParams<TParams extends SignedRequestContext = any>(
method: Method,
signMethod: SignMethod,
params?: TParams,
@@ -186,7 +196,7 @@ export default abstract class BaseRestClient {
await this.syncTime();
}
return this.signRequest(params, method, signMethod);
return this.signRequest(params || {}, method, signMethod);
}
/** Returns an axios request object. Handles signing process automatically if this is a private API call */
@@ -336,7 +346,7 @@ export default abstract class BaseRestClient {
/**
* @private sign request and set recv window
*/
private async signRequest<T = {}>(
private async signRequest<T extends SignedRequestContext | {} = {}>(
data: T,
method: Method,
signMethod: SignMethod
@@ -409,10 +419,13 @@ export default abstract class BaseRestClient {
encodeValues
);
res.sign = await signMessage(res.serializedParams, this.secret);
// @ts-ignore
res.paramsWithSign = {
...res.originalParams,
sign: res.sign,
};
return res;
}

View File

@@ -16,7 +16,8 @@ type WsTopic = string;
/**
* A "Set" is used to ensure we only subscribe to a topic once (tracking a list of unique topics we're expected to be connected to)
* Note: Accurate duplicate tracking only works for plaintext topics. E.g. JSON objects may not be seen as duplicates if keys are in different orders. If that's needed, check the FTX implementation.
* Note: Accurate duplicate tracking only works for plaintext topics.
* E.g. JSON objects may not be seen as duplicates if keys are in different orders. If that's needed, check the FTX implementation.
*/
type WsTopicList = Set<WsTopic>;
@@ -39,6 +40,7 @@ interface WsStoredState {
export default class WsStore {
private wsState: Record<string, WsStoredState>;
private logger: typeof DefaultLogger;
constructor(logger: typeof DefaultLogger) {
@@ -48,7 +50,9 @@ export default class WsStore {
/** Get WS stored state for key, optionally create if missing */
get(key: WsKey, createIfMissing?: true): WsStoredState;
get(key: WsKey, createIfMissing?: false): WsStoredState | undefined;
get(key: WsKey, createIfMissing?: boolean): WsStoredState | undefined {
if (this.wsState[key]) {
return this.wsState[key];

View File

@@ -3,6 +3,7 @@ export async function signMessage(
secret: string
): Promise<string> {
const encoder = new TextEncoder();
// eslint-disable-next-line no-undef
const key = await window.crypto.subtle.importKey(
'raw',
encoder.encode(secret),
@@ -11,6 +12,7 @@ export async function signMessage(
['sign']
);
// eslint-disable-next-line no-undef
const signature = await window.crypto.subtle.sign(
'HMAC',
key,
@@ -18,8 +20,10 @@ export async function signMessage(
);
return Array.prototype.map
.call(new Uint8Array(signature), (x: any) =>
('00' + x.toString(16)).slice(-2)
.call(
new Uint8Array(signature),
(x: { toString: (arg0: number) => string }) =>
('00' + x.toString(16)).slice(-2)
)
.join('');
}

View File

@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type LogParams = null | any;
export const DefaultLogger = {

View File

@@ -91,6 +91,7 @@ export function getRestBaseUrl(
return exchangeBaseUrls.livenet;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isWsPong(msg: any): boolean {
if (!msg) {
return false;

View File

@@ -298,7 +298,7 @@ export function getWsKeyForTopic(
: WS_KEY_MAP.contractUSDTPublic;
}
default: {
throw neverGuard(market, `getWsKeyForTopic(): Unhandled market`);
throw neverGuard(market, 'getWsKeyForTopic(): Unhandled market');
}
}
}
@@ -322,7 +322,7 @@ export function getMaxTopicsPerSubscribeEvent(
return 10;
}
default: {
throw neverGuard(market, `getWsKeyForTopic(): Unhandled market`);
throw neverGuard(market, 'getWsKeyForTopic(): Unhandled market');
}
}
}