mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-14 06:02:36 +03:00
Added errorCallback
This commit is contained in:
6
package-lock.json
generated
6
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@orionprotocol/sdk",
|
||||
"version": "0.17.26",
|
||||
"version": "0.17.27",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@orionprotocol/sdk",
|
||||
"version": "0.17.26",
|
||||
"version": "0.17.27",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.21.0",
|
||||
@@ -31,7 +31,7 @@
|
||||
"ts-node": "^10.9.1",
|
||||
"uuid": "^9.0.0",
|
||||
"ws": "^8.12.1",
|
||||
"zod": "^3.21.2"
|
||||
"zod": "3.21.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.21.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@orionprotocol/sdk",
|
||||
"version": "0.17.27",
|
||||
"version": "0.17.28",
|
||||
"description": "Orion Protocol SDK",
|
||||
"main": "./lib/esm/index.js",
|
||||
"module": "./lib/esm/index.js",
|
||||
@@ -92,7 +92,7 @@
|
||||
"ts-node": "^10.9.1",
|
||||
"uuid": "^9.0.0",
|
||||
"ws": "^8.12.1",
|
||||
"zod": "^3.21.2"
|
||||
"zod": "3.21.2"
|
||||
},
|
||||
"homepage": "https://github.com/orionprotocol/sdk#readme",
|
||||
"files": [
|
||||
|
||||
@@ -16,9 +16,30 @@ describe('Price Feed', () => {
|
||||
clearTimeout(timeout);
|
||||
unsubscribe()
|
||||
resolve(true);
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('Handle error', async () => {
|
||||
const orion = new Orion('testing');
|
||||
const bscUnit = orion.getUnit('bsc')
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error('Timeout'));
|
||||
}, 10000);
|
||||
const { unsubscribe } = bscUnit.priceFeed.ws.subscribe('ticker', {
|
||||
payload: 'SGERGEWRGWERG',
|
||||
callback: () => null,
|
||||
errorCallback: (error) => {
|
||||
expect(error.message).toContain('Can\'t recognize PriceFeed "ticker" subscription message "{"message":"Wrong pair"}"')
|
||||
clearTimeout(timeout);
|
||||
unsubscribe()
|
||||
resolve(true);
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,33 +5,7 @@ import priceFeedSubscriptions from './priceFeedSubscriptions';
|
||||
import { tickerInfoSchema, candleSchema } from './schemas';
|
||||
import priceSchema from './schemas/priceSchema';
|
||||
import type { AnyJSON } from '../../../types';
|
||||
|
||||
type TickerInfo = {
|
||||
pairName: string
|
||||
lastPrice: string
|
||||
openPrice: string
|
||||
highPrice: string
|
||||
lowPrice: string
|
||||
volume24h: string
|
||||
}
|
||||
|
||||
const allTickersSchema = z.unknown().array()
|
||||
.transform((tickers) => {
|
||||
const data = [...tickers];
|
||||
data.shift();
|
||||
const parsedData = tickerInfoSchema.array().parse(data);
|
||||
return parsedData.reduce<
|
||||
Partial<
|
||||
Record<
|
||||
string,
|
||||
TickerInfo
|
||||
>
|
||||
>
|
||||
>((prev, pairData) => ({
|
||||
...prev,
|
||||
[pairData.pairName]: pairData,
|
||||
}), {});
|
||||
});
|
||||
import allTickersSchema from './schemas/allTickersSchema';
|
||||
|
||||
export const subscriptions = {
|
||||
[priceFeedSubscriptions.ALL_TICKERS]: {
|
||||
@@ -59,9 +33,11 @@ export type Subscription<
|
||||
> = typeof subscriptions[T] extends { payload: true }
|
||||
? {
|
||||
callback: (data: Schema) => void
|
||||
errorCallback?: (error: Error) => void
|
||||
payload: string
|
||||
} : {
|
||||
callback: (data: Schema) => void
|
||||
errorCallback?: (error: Error) => void
|
||||
}
|
||||
|
||||
export default class PriceFeedSubscription<T extends SubscriptionType = SubscriptionType> {
|
||||
@@ -69,6 +45,8 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
|
||||
|
||||
private readonly callback: Subscription<T>['callback'];
|
||||
|
||||
private readonly errorCallback?: Subscription<T>['errorCallback'];
|
||||
|
||||
private readonly payload?: string;
|
||||
|
||||
private ws?: WebSocket;
|
||||
@@ -100,6 +78,7 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
|
||||
this.payload = params.payload;
|
||||
}
|
||||
this.callback = params.callback;
|
||||
this.errorCallback = params.errorCallback;
|
||||
this.onOpen = onOpen;
|
||||
this.init();
|
||||
}
|
||||
@@ -146,9 +125,13 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
|
||||
const parseResult = subscription.schema.safeParse(json);
|
||||
if (!parseResult.success) {
|
||||
const errorsMessage = parseResult.error.errors.map((error) => `[${error.path.join('.')}] ${error.message}`).join(', ');
|
||||
throw new Error(`Can't recognize PriceFeed "${type}" subscription message "${dataString}": ${errorsMessage}`);
|
||||
const error = new Error(`Can't recognize PriceFeed "${type}" subscription message "${dataString}": ${errorsMessage}`);
|
||||
if (this.errorCallback !== undefined) {
|
||||
this.errorCallback(error);
|
||||
} else throw error;
|
||||
} else {
|
||||
this.callback(parseResult.data);
|
||||
}
|
||||
this.callback(parseResult.data);
|
||||
};
|
||||
|
||||
this.ws.onclose = () => {
|
||||
|
||||
24
src/services/PriceFeed/ws/schemas/allTickersSchema.ts
Normal file
24
src/services/PriceFeed/ws/schemas/allTickersSchema.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { z } from 'zod';
|
||||
import tickerInfoSchema from './tickerInfoSchema';
|
||||
|
||||
type TickerInfo = z.infer<typeof tickerInfoSchema>
|
||||
|
||||
const allTickersSchema = z.unknown().array()
|
||||
.transform((tickers) => {
|
||||
const data = [...tickers];
|
||||
data.shift();
|
||||
const parsedData = tickerInfoSchema.array().parse(data);
|
||||
return parsedData.reduce<
|
||||
Partial<
|
||||
Record<
|
||||
string,
|
||||
TickerInfo
|
||||
>
|
||||
>
|
||||
>((prev, pairData) => ({
|
||||
...prev,
|
||||
[pairData.pairName]: pairData,
|
||||
}), {});
|
||||
});
|
||||
|
||||
export default allTickersSchema;
|
||||
@@ -1,2 +1,4 @@
|
||||
export { default as tickerInfoSchema } from './tickerInfoSchema';
|
||||
export { default as candleSchema } from './candleSchema';
|
||||
export { default as priceSchema } from './priceSchema';
|
||||
export { default as allTickersSchema } from './allTickersSchema';
|
||||
|
||||
Reference in New Issue
Block a user