forked from mtvkand/invest-nodejs-grpc-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk.ts
98 lines (83 loc) · 3.07 KB
/
sdk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { createMetadata, createMetadataCredentials, createSSLCredentials, makeChannel } from './utils';
import { createClientFactory } from 'nice-grpc';
import * as stopOrders from './generated/stoporders';
import { API_URL, API_SANDBOX_URL } from './constants';
import * as instruments from './generated/instruments';
import * as marketData from './generated/marketdata';
import * as operations from './generated/operations';
import * as orders from './generated/orders';
import * as users from './generated/users';
import * as sandbox from './generated/sandbox';
import { getMiddleware, TypeLoggerCb } from './middlewares/response';
const { SandboxServiceDefinition } = sandbox;
const { UsersServiceDefinition, AccountType, AccountStatus, AccessLevel } = users;
const { OrdersServiceDefinition, OrdersStreamServiceDefinition, OrderDirection, OrderType } = orders;
const { StopOrdersServiceDefinition } = stopOrders;
const { InstrumentsServiceDefinition, InstrumentStatus, InstrumentIdType } = instruments;
const {
MarketDataServiceDefinition,
MarketDataStreamServiceDefinition,
CandleInterval,
MarketDataRequest,
SubscriptionAction,
SubscriptionInterval,
} = marketData;
const {
OperationsServiceDefinition,
OperationsStreamServiceDefinition,
OperationState,
OperationType,
PortfolioSubscriptionStatus,
PositionsAccountSubscriptionStatus,
} = operations;
export const createSdk = (
token: string,
appName?: string,
loggerCb?: TypeLoggerCb,
options?: {
isSandbox?: boolean;
},
) => {
const metadata = createMetadata(token, appName);
const metadataCred = createMetadataCredentials(metadata);
const sslCred = createSSLCredentials(metadataCred);
const url = options?.isSandbox ? API_SANDBOX_URL : API_URL;
const channel = makeChannel(url, sslCred);
const clientFactory = createClientFactory().use(getMiddleware(loggerCb));
return {
instruments: clientFactory.create(InstrumentsServiceDefinition, channel),
marketData: clientFactory.create(MarketDataServiceDefinition, channel),
marketDataStream: clientFactory.create(MarketDataStreamServiceDefinition, channel),
operations: clientFactory.create(OperationsServiceDefinition, channel),
operationsStream: clientFactory.create(OperationsStreamServiceDefinition, channel),
orders: clientFactory.create(OrdersServiceDefinition, channel),
ordersStream: clientFactory.create(OrdersStreamServiceDefinition, channel),
sandbox: clientFactory.create(SandboxServiceDefinition, channel),
stopOrders: clientFactory.create(StopOrdersServiceDefinition, channel),
users: clientFactory.create(UsersServiceDefinition, channel),
structures: {
users,
stopOrders,
instruments,
marketData,
operations,
orders,
sandbox,
},
CandleInterval,
InstrumentStatus,
InstrumentIdType,
MarketDataRequest,
SubscriptionAction,
SubscriptionInterval,
OrderDirection,
OrderType,
AccountType,
AccountStatus,
AccessLevel,
OperationState,
OperationType,
PortfolioSubscriptionStatus,
PositionsAccountSubscriptionStatus,
};
};