@tastytrade/api 0.0.1
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/.env +5 -0
- package/.eslintrc.cjs +18 -0
- package/.vscode/launch.json +24 -0
- package/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/account-streamer.d.ts +92 -0
- package/dist/account-streamer.js +394 -0
- package/dist/models/tastytrade-session.d.ts +5 -0
- package/dist/models/tastytrade-session.js +23 -0
- package/dist/quote-streamer.d.ts +11 -0
- package/dist/quote-streamer.js +63 -0
- package/dist/services/account-status-service.d.ts +6 -0
- package/dist/services/account-status-service.js +64 -0
- package/dist/services/accounts-and-customers-service.d.ts +10 -0
- package/dist/services/accounts-and-customers-service.js +116 -0
- package/dist/services/balances-and-positions-service.d.ts +8 -0
- package/dist/services/balances-and-positions-service.js +93 -0
- package/dist/services/instruments-service.d.ts +28 -0
- package/dist/services/instruments-service.js +370 -0
- package/dist/services/margin-requirements-service.d.ts +7 -0
- package/dist/services/margin-requirements-service.js +76 -0
- package/dist/services/market-metrics-service.d.ts +8 -0
- package/dist/services/market-metrics-service.js +91 -0
- package/dist/services/net-liquidating-value-history-service.d.ts +7 -0
- package/dist/services/net-liquidating-value-history-service.js +77 -0
- package/dist/services/orders-service.d.ts +17 -0
- package/dist/services/orders-service.js +208 -0
- package/dist/services/risk-parameters-service.d.ts +7 -0
- package/dist/services/risk-parameters-service.js +76 -0
- package/dist/services/session-service.d.ts +9 -0
- package/dist/services/session-service.js +113 -0
- package/dist/services/symbol-search-service.d.ts +6 -0
- package/dist/services/symbol-search-service.js +63 -0
- package/dist/services/tastytrade-http-client.d.ts +13 -0
- package/dist/services/tastytrade-http-client.js +137 -0
- package/dist/services/transactions-service.d.ts +8 -0
- package/dist/services/transactions-service.js +91 -0
- package/dist/services/watchlists-service.d.ts +14 -0
- package/dist/services/watchlists-service.js +170 -0
- package/dist/tastytrade-api.d.ts +40 -0
- package/dist/tastytrade-api.js +56 -0
- package/dist/utils/json-util.d.ts +14 -0
- package/dist/utils/json-util.js +43 -0
- package/dist/utils/response-util.d.ts +1 -0
- package/dist/utils/response-util.js +21 -0
- package/lib/account-streamer.ts +394 -0
- package/lib/models/tastytrade-session.ts +13 -0
- package/lib/quote-streamer.ts +40 -0
- package/lib/services/account-status-service.ts +15 -0
- package/lib/services/accounts-and-customers-service.ts +35 -0
- package/lib/services/balances-and-positions-service.ts +29 -0
- package/lib/services/instruments-service.ts +138 -0
- package/lib/services/margin-requirements-service.ts +19 -0
- package/lib/services/market-metrics-service.ts +24 -0
- package/lib/services/net-liquidating-value-history-service.ts +20 -0
- package/lib/services/orders-service.ts +80 -0
- package/lib/services/risk-parameters-service.ts +19 -0
- package/lib/services/session-service.ts +35 -0
- package/lib/services/symbol-search-service.ts +14 -0
- package/lib/services/tastytrade-http-client.ts +65 -0
- package/lib/services/transactions-service.ts +27 -0
- package/lib/services/watchlists-service.ts +58 -0
- package/lib/tastytrade-api.ts +65 -0
- package/lib/utils/json-util.ts +44 -0
- package/lib/utils/response-util.ts +15 -0
- package/package.json +35 -0
- package/tsconfig.json +71 -0
package/.env
ADDED
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
|
|
3
|
+
parser: '@typescript-eslint/parser',
|
|
4
|
+
plugins: ['@typescript-eslint'],
|
|
5
|
+
root: true,
|
|
6
|
+
rules: {
|
|
7
|
+
"@typescript-eslint/no-inferrable-types": "warn",
|
|
8
|
+
"@typescript-eslint/no-unused-vars": [
|
|
9
|
+
"error",
|
|
10
|
+
{
|
|
11
|
+
"argsIgnorePattern": "^_",
|
|
12
|
+
"varsIgnorePattern": "^_",
|
|
13
|
+
"caughtErrorsIgnorePattern": "^_"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"@typescript-eslint/no-non-null-assertion": "off"
|
|
17
|
+
}
|
|
18
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
|
3
|
+
// Hover to view descriptions of existing attributes.
|
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"type": "node",
|
|
9
|
+
"request": "launch",
|
|
10
|
+
"name": "Integration Tests",
|
|
11
|
+
"skipFiles": ["<node_internals>/**"],
|
|
12
|
+
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
|
|
13
|
+
"args": ["-i", "--", "tests/integration/service/instruments-service.test.ts"]
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"type": "node",
|
|
17
|
+
"request": "launch",
|
|
18
|
+
"name": "Unit Tests",
|
|
19
|
+
"skipFiles": ["<node_internals>/**"],
|
|
20
|
+
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
|
|
21
|
+
"args": ["-i", "--", "tests/unit/service/session-service.test.ts"]
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 tastytrade
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Tastytrade Api Javascript SDK
|
|
2
|
+
## Building Locally
|
|
3
|
+
`npm run build`
|
|
4
|
+
Outputs everything to `dist/`
|
|
5
|
+
|
|
6
|
+
## Running example app
|
|
7
|
+
```sh
|
|
8
|
+
npm run build
|
|
9
|
+
cd examples/
|
|
10
|
+
npm install
|
|
11
|
+
npm run dev
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
|
15
|
+
|
|
16
|
+
### Tests
|
|
17
|
+
Unit tests are for specific pieces of code and should not make any external calls (http requests). If needed, you can mock out an http response in your unit test.
|
|
18
|
+
|
|
19
|
+
For testing actual api requests, use the `tests/integration` folder.
|
|
20
|
+
|
|
21
|
+
Directory structure should match the `lib` structure. Service tests will go in `tests/unit/service/<servicefilename>.test.ts`
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { JsonValue } from './utils/json-util';
|
|
2
|
+
import { JsonBuilder } from './utils/json-util';
|
|
3
|
+
import TastytradeSession from './models/tastytrade-session';
|
|
4
|
+
export declare enum STREAMER_STATE {
|
|
5
|
+
Open = 0,
|
|
6
|
+
Closed = 1,
|
|
7
|
+
Error = 2
|
|
8
|
+
}
|
|
9
|
+
export declare type Disposer = () => void;
|
|
10
|
+
export declare type StreamerStateObserver = (streamerState: STREAMER_STATE) => void;
|
|
11
|
+
export declare type StreamerMessageObserver = (messageType: string, action: string, status: string) => void;
|
|
12
|
+
export declare class AccountStreamer {
|
|
13
|
+
private readonly url;
|
|
14
|
+
private readonly session;
|
|
15
|
+
private websocket;
|
|
16
|
+
private startResolve;
|
|
17
|
+
private startReject;
|
|
18
|
+
private requestCounter;
|
|
19
|
+
private queued;
|
|
20
|
+
private heartbeatTimerId;
|
|
21
|
+
lastCloseEvent: any;
|
|
22
|
+
lastErrorEvent: any;
|
|
23
|
+
private _streamerState;
|
|
24
|
+
private readonly streamerStateObservers;
|
|
25
|
+
private readonly streamerMessageObservers;
|
|
26
|
+
private startPromise;
|
|
27
|
+
private readonly requestPromises;
|
|
28
|
+
private readonly logger;
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @param url Url of the account streamer service
|
|
32
|
+
*/
|
|
33
|
+
constructor(url: string, session: TastytradeSession);
|
|
34
|
+
get streamerState(): STREAMER_STATE;
|
|
35
|
+
set streamerState(streamerState: STREAMER_STATE);
|
|
36
|
+
private get authToken();
|
|
37
|
+
/**
|
|
38
|
+
* Adds a custom callback that fires when the streamer state changes
|
|
39
|
+
* @param observer
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
addStreamerStateObserver(observer: StreamerStateObserver): Disposer;
|
|
43
|
+
get isOpen(): boolean;
|
|
44
|
+
get isClosed(): boolean;
|
|
45
|
+
get isError(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Entrypoint for beginning a websocket session
|
|
48
|
+
* You must have a valid tastytrade authToken before calling this method
|
|
49
|
+
* @returns Promise that resolves when the "opened" message is received (see handleOpen)
|
|
50
|
+
*/
|
|
51
|
+
start(): Promise<boolean>;
|
|
52
|
+
stop(): void;
|
|
53
|
+
private teardown;
|
|
54
|
+
readonly sendHeartbeat: () => void;
|
|
55
|
+
private scheduleHeartbeatTimer;
|
|
56
|
+
get isHeartbeatScheduled(): boolean;
|
|
57
|
+
private cancelHeartbeatTimer;
|
|
58
|
+
private clearHeartbeatTimerId;
|
|
59
|
+
/**
|
|
60
|
+
* Send a message via websocket
|
|
61
|
+
* @param json JsonBuilder
|
|
62
|
+
* @param includeSessionToken Attaches session token to message if true
|
|
63
|
+
* @returns
|
|
64
|
+
*/
|
|
65
|
+
send(json: JsonBuilder, includeSessionToken?: boolean): number;
|
|
66
|
+
/**
|
|
67
|
+
* Used by other methods to send a specific `action` message
|
|
68
|
+
* @param action
|
|
69
|
+
* @param value
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
subscribeTo(action: string, value?: JsonValue): number;
|
|
73
|
+
/**
|
|
74
|
+
* Subscribes to all user-level messages for given user external id
|
|
75
|
+
* @param userExternalId "external-id" from login response
|
|
76
|
+
* @returns Promise that resolves when ack is received
|
|
77
|
+
*/
|
|
78
|
+
subscribeToUser(userExternalId: string): void;
|
|
79
|
+
/**
|
|
80
|
+
* Subscribes to all account-level messages for given account numbers
|
|
81
|
+
* @param accountNumbers List of account numbers to subscribe to
|
|
82
|
+
* @returns Promise that resolves when an ack is received
|
|
83
|
+
*/
|
|
84
|
+
subscribeToAccounts(accountNumbers: string[]): Promise<string>;
|
|
85
|
+
private sendQueuedMessages;
|
|
86
|
+
private readonly handleOpen;
|
|
87
|
+
private readonly handleClose;
|
|
88
|
+
private readonly handleError;
|
|
89
|
+
private readonly handleMessage;
|
|
90
|
+
addMessageObserver(observer: StreamerMessageObserver): Disposer;
|
|
91
|
+
private readonly handleOneMessage;
|
|
92
|
+
}
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.AccountStreamer = exports.STREAMER_STATE = void 0;
|
|
43
|
+
var lodash_1 = __importDefault(require("lodash"));
|
|
44
|
+
var json_util_1 = require("./utils/json-util");
|
|
45
|
+
var STREAMER_STATE;
|
|
46
|
+
(function (STREAMER_STATE) {
|
|
47
|
+
STREAMER_STATE[STREAMER_STATE["Open"] = 0] = "Open";
|
|
48
|
+
STREAMER_STATE[STREAMER_STATE["Closed"] = 1] = "Closed";
|
|
49
|
+
STREAMER_STATE[STREAMER_STATE["Error"] = 2] = "Error";
|
|
50
|
+
})(STREAMER_STATE = exports.STREAMER_STATE || (exports.STREAMER_STATE = {}));
|
|
51
|
+
var MessageAction;
|
|
52
|
+
(function (MessageAction) {
|
|
53
|
+
MessageAction["ACCOUNT_SUBSCRIBE"] = "account-subscribe";
|
|
54
|
+
MessageAction["CONNECT"] = "connect";
|
|
55
|
+
MessageAction["HEARTBEAT"] = "heartbeat";
|
|
56
|
+
MessageAction["PUBLIC_WATCHLISTS_SUBSCRIBE"] = "public-watchlists-subscribe";
|
|
57
|
+
MessageAction["QUOTE_ALERTS_SUBSCRIBE"] = "quote-alerts-subscribe";
|
|
58
|
+
MessageAction["USER_MESSAGE_SUBSCRIBE"] = "user-message-subscribe";
|
|
59
|
+
})(MessageAction || (MessageAction = {}));
|
|
60
|
+
var HEARTBEAT_INTERVAL = 20000; // 20 seconds
|
|
61
|
+
var SOURCE = 'tastytrade-api-js-sdk';
|
|
62
|
+
var REQUEST_ID = 'request-id';
|
|
63
|
+
function removeElement(array, element) {
|
|
64
|
+
var index = array.indexOf(element);
|
|
65
|
+
if (index < 0) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
array.splice(index, 1);
|
|
69
|
+
}
|
|
70
|
+
var AccountStreamer = /** @class */ (function () {
|
|
71
|
+
/**
|
|
72
|
+
*
|
|
73
|
+
* @param url Url of the account streamer service
|
|
74
|
+
*/
|
|
75
|
+
function AccountStreamer(url, session) {
|
|
76
|
+
var _this = this;
|
|
77
|
+
this.url = url;
|
|
78
|
+
this.session = session;
|
|
79
|
+
this.websocket = null;
|
|
80
|
+
this.startResolve = null;
|
|
81
|
+
this.startReject = null;
|
|
82
|
+
this.requestCounter = 0;
|
|
83
|
+
this.queued = [];
|
|
84
|
+
this.heartbeatTimerId = null;
|
|
85
|
+
this.lastCloseEvent = null;
|
|
86
|
+
this.lastErrorEvent = null;
|
|
87
|
+
this._streamerState = STREAMER_STATE.Closed;
|
|
88
|
+
this.streamerStateObservers = [];
|
|
89
|
+
this.streamerMessageObservers = [];
|
|
90
|
+
this.startPromise = null;
|
|
91
|
+
this.requestPromises = new Map();
|
|
92
|
+
this.logger = console;
|
|
93
|
+
this.sendHeartbeat = function () {
|
|
94
|
+
_this.clearHeartbeatTimerId();
|
|
95
|
+
_this.send(new json_util_1.JsonBuilder({ action: MessageAction.HEARTBEAT }));
|
|
96
|
+
};
|
|
97
|
+
this.handleOpen = function (event) {
|
|
98
|
+
if (_this.startResolve === null) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
_this.logger.info('AccountStreamer opened', event);
|
|
102
|
+
_this.startResolve(true);
|
|
103
|
+
_this.startResolve = _this.startReject = null;
|
|
104
|
+
_this.streamerState = STREAMER_STATE.Open;
|
|
105
|
+
_this.sendQueuedMessages();
|
|
106
|
+
_this.scheduleHeartbeatTimer();
|
|
107
|
+
};
|
|
108
|
+
this.handleClose = function (event) {
|
|
109
|
+
_this.logger.info('AccountStreamer closed', event);
|
|
110
|
+
if (_this.websocket === null) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
_this.lastCloseEvent = event;
|
|
114
|
+
_this.streamerState = STREAMER_STATE.Closed;
|
|
115
|
+
_this.teardown();
|
|
116
|
+
};
|
|
117
|
+
this.handleError = function (event) {
|
|
118
|
+
if (_this.websocket === null) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
_this.logger.warn('AccountStreamer error', event);
|
|
122
|
+
_this.lastErrorEvent = event;
|
|
123
|
+
_this.streamerState = STREAMER_STATE.Error;
|
|
124
|
+
if (_this.startReject !== null) {
|
|
125
|
+
_this.startReject(new Error('Failed to connect'));
|
|
126
|
+
_this.startReject = _this.startResolve = null;
|
|
127
|
+
}
|
|
128
|
+
_this.teardown();
|
|
129
|
+
};
|
|
130
|
+
this.handleMessage = function (event) {
|
|
131
|
+
var json = JSON.parse(event.data);
|
|
132
|
+
if (json.results !== undefined) {
|
|
133
|
+
var results = json.results;
|
|
134
|
+
for (var _i = 0, results_1 = results; _i < results_1.length; _i++) {
|
|
135
|
+
var result = results_1[_i];
|
|
136
|
+
_this.handleOneMessage(result);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
_this.handleOneMessage(json);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
this.handleOneMessage = function (json) {
|
|
144
|
+
_this.logger.info(json);
|
|
145
|
+
var action = json.action;
|
|
146
|
+
_this.streamerMessageObservers.forEach(function (observer) { return observer(json.type, action, json.status); });
|
|
147
|
+
if (action) {
|
|
148
|
+
if (action === MessageAction.HEARTBEAT) {
|
|
149
|
+
// schedule next heartbeat
|
|
150
|
+
_this.scheduleHeartbeatTimer();
|
|
151
|
+
}
|
|
152
|
+
var promiseCallbacks = _this.requestPromises.get(json[REQUEST_ID]);
|
|
153
|
+
if (promiseCallbacks) {
|
|
154
|
+
var resolve = promiseCallbacks[0], reject = promiseCallbacks[1];
|
|
155
|
+
var status_1 = json.status;
|
|
156
|
+
if (status_1 === 'ok') {
|
|
157
|
+
resolve(json.action);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
reject(json.message);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
Object.defineProperty(AccountStreamer.prototype, "streamerState", {
|
|
168
|
+
get: function () {
|
|
169
|
+
return this._streamerState;
|
|
170
|
+
},
|
|
171
|
+
set: function (streamerState) {
|
|
172
|
+
this._streamerState = streamerState;
|
|
173
|
+
this.streamerStateObservers.forEach(function (observer) {
|
|
174
|
+
observer(streamerState);
|
|
175
|
+
});
|
|
176
|
+
},
|
|
177
|
+
enumerable: false,
|
|
178
|
+
configurable: true
|
|
179
|
+
});
|
|
180
|
+
Object.defineProperty(AccountStreamer.prototype, "authToken", {
|
|
181
|
+
get: function () {
|
|
182
|
+
return this.session.authToken;
|
|
183
|
+
},
|
|
184
|
+
enumerable: false,
|
|
185
|
+
configurable: true
|
|
186
|
+
});
|
|
187
|
+
/**
|
|
188
|
+
* Adds a custom callback that fires when the streamer state changes
|
|
189
|
+
* @param observer
|
|
190
|
+
* @returns
|
|
191
|
+
*/
|
|
192
|
+
AccountStreamer.prototype.addStreamerStateObserver = function (observer) {
|
|
193
|
+
var _this = this;
|
|
194
|
+
this.streamerStateObservers.push(observer);
|
|
195
|
+
return function () {
|
|
196
|
+
removeElement(_this.streamerStateObservers, observer);
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
Object.defineProperty(AccountStreamer.prototype, "isOpen", {
|
|
200
|
+
get: function () {
|
|
201
|
+
return this.streamerState === STREAMER_STATE.Open;
|
|
202
|
+
},
|
|
203
|
+
enumerable: false,
|
|
204
|
+
configurable: true
|
|
205
|
+
});
|
|
206
|
+
Object.defineProperty(AccountStreamer.prototype, "isClosed", {
|
|
207
|
+
get: function () {
|
|
208
|
+
return this.streamerState === STREAMER_STATE.Closed;
|
|
209
|
+
},
|
|
210
|
+
enumerable: false,
|
|
211
|
+
configurable: true
|
|
212
|
+
});
|
|
213
|
+
Object.defineProperty(AccountStreamer.prototype, "isError", {
|
|
214
|
+
get: function () {
|
|
215
|
+
return this.streamerState === STREAMER_STATE.Error;
|
|
216
|
+
},
|
|
217
|
+
enumerable: false,
|
|
218
|
+
configurable: true
|
|
219
|
+
});
|
|
220
|
+
/**
|
|
221
|
+
* Entrypoint for beginning a websocket session
|
|
222
|
+
* You must have a valid tastytrade authToken before calling this method
|
|
223
|
+
* @returns Promise that resolves when the "opened" message is received (see handleOpen)
|
|
224
|
+
*/
|
|
225
|
+
AccountStreamer.prototype.start = function () {
|
|
226
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
227
|
+
var websocket;
|
|
228
|
+
var _this = this;
|
|
229
|
+
return __generator(this, function (_a) {
|
|
230
|
+
if (this.startPromise !== null) {
|
|
231
|
+
return [2 /*return*/, this.startPromise];
|
|
232
|
+
}
|
|
233
|
+
websocket = (this.websocket = new WebSocket(this.url));
|
|
234
|
+
this.lastCloseEvent = null;
|
|
235
|
+
this.lastErrorEvent = null;
|
|
236
|
+
websocket.addEventListener('open', this.handleOpen);
|
|
237
|
+
websocket.addEventListener('close', this.handleClose);
|
|
238
|
+
websocket.addEventListener('error', this.handleError);
|
|
239
|
+
websocket.addEventListener('message', this.handleMessage);
|
|
240
|
+
this.logger.info('AccountStreamer - starting');
|
|
241
|
+
this.startPromise = new Promise(function (resolve, reject) {
|
|
242
|
+
_this.startResolve = resolve;
|
|
243
|
+
_this.startReject = reject;
|
|
244
|
+
});
|
|
245
|
+
return [2 /*return*/, this.startPromise];
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
AccountStreamer.prototype.stop = function () {
|
|
250
|
+
this.teardown();
|
|
251
|
+
};
|
|
252
|
+
AccountStreamer.prototype.teardown = function () {
|
|
253
|
+
var websocket = this.websocket;
|
|
254
|
+
if (websocket === null) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
this.startPromise = null;
|
|
258
|
+
this.cancelHeartbeatTimer();
|
|
259
|
+
websocket.close();
|
|
260
|
+
websocket.removeEventListener('open', this.handleOpen);
|
|
261
|
+
websocket.removeEventListener('close', this.handleClose);
|
|
262
|
+
websocket.removeEventListener('message', this.handleMessage);
|
|
263
|
+
websocket.removeEventListener('error', this.handleError);
|
|
264
|
+
this.websocket = null;
|
|
265
|
+
this.logger.info('AccountStreamer - teardown');
|
|
266
|
+
this.streamerState = STREAMER_STATE.Closed; // Manually update status for convenience
|
|
267
|
+
};
|
|
268
|
+
AccountStreamer.prototype.scheduleHeartbeatTimer = function () {
|
|
269
|
+
if (this.isHeartbeatScheduled) {
|
|
270
|
+
// Heartbeat already scheduled
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
this.logger.info('Scheduling heartbeat with interval: ', HEARTBEAT_INTERVAL);
|
|
274
|
+
var scheduler = typeof window === 'undefined' ? setTimeout : window.setTimeout;
|
|
275
|
+
this.heartbeatTimerId = scheduler(this.sendHeartbeat, HEARTBEAT_INTERVAL);
|
|
276
|
+
};
|
|
277
|
+
Object.defineProperty(AccountStreamer.prototype, "isHeartbeatScheduled", {
|
|
278
|
+
get: function () {
|
|
279
|
+
return !lodash_1.default.isNil(this.heartbeatTimerId);
|
|
280
|
+
},
|
|
281
|
+
enumerable: false,
|
|
282
|
+
configurable: true
|
|
283
|
+
});
|
|
284
|
+
AccountStreamer.prototype.cancelHeartbeatTimer = function () {
|
|
285
|
+
if (!this.isHeartbeatScheduled) {
|
|
286
|
+
return; // Nothing to cancel
|
|
287
|
+
}
|
|
288
|
+
if (typeof window === 'undefined') {
|
|
289
|
+
clearTimeout(this.heartbeatTimerId);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
clearTimeout(this.heartbeatTimerId);
|
|
293
|
+
}
|
|
294
|
+
this.clearHeartbeatTimerId();
|
|
295
|
+
};
|
|
296
|
+
AccountStreamer.prototype.clearHeartbeatTimerId = function () {
|
|
297
|
+
this.heartbeatTimerId = null;
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* Send a message via websocket
|
|
301
|
+
* @param json JsonBuilder
|
|
302
|
+
* @param includeSessionToken Attaches session token to message if true
|
|
303
|
+
* @returns
|
|
304
|
+
*/
|
|
305
|
+
AccountStreamer.prototype.send = function (json, includeSessionToken) {
|
|
306
|
+
if (includeSessionToken === void 0) { includeSessionToken = true; }
|
|
307
|
+
this.requestCounter += 1;
|
|
308
|
+
json.add(REQUEST_ID, this.requestCounter);
|
|
309
|
+
json.add('source', SOURCE);
|
|
310
|
+
if (includeSessionToken) {
|
|
311
|
+
var sessionToken = this.authToken;
|
|
312
|
+
if (!sessionToken) {
|
|
313
|
+
throw new Error('sessionToken not set');
|
|
314
|
+
}
|
|
315
|
+
json.add('auth-token', sessionToken);
|
|
316
|
+
}
|
|
317
|
+
var message = JSON.stringify(json.json);
|
|
318
|
+
var websocket = this.websocket;
|
|
319
|
+
if (websocket === null) {
|
|
320
|
+
// Queue up and send on open
|
|
321
|
+
this.queued.push(message);
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
websocket.send(message);
|
|
325
|
+
}
|
|
326
|
+
return this.requestCounter;
|
|
327
|
+
};
|
|
328
|
+
/**
|
|
329
|
+
* Used by other methods to send a specific `action` message
|
|
330
|
+
* @param action
|
|
331
|
+
* @param value
|
|
332
|
+
* @returns
|
|
333
|
+
*/
|
|
334
|
+
AccountStreamer.prototype.subscribeTo = function (action, value) {
|
|
335
|
+
var json = new json_util_1.JsonBuilder();
|
|
336
|
+
json.add('action', action);
|
|
337
|
+
if (!lodash_1.default.isUndefined(value)) {
|
|
338
|
+
json.add('value', value);
|
|
339
|
+
}
|
|
340
|
+
return this.send(json);
|
|
341
|
+
};
|
|
342
|
+
/**
|
|
343
|
+
* Subscribes to all user-level messages for given user external id
|
|
344
|
+
* @param userExternalId "external-id" from login response
|
|
345
|
+
* @returns Promise that resolves when ack is received
|
|
346
|
+
*/
|
|
347
|
+
AccountStreamer.prototype.subscribeToUser = function (userExternalId) {
|
|
348
|
+
if (!userExternalId) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
this.subscribeTo(MessageAction.USER_MESSAGE_SUBSCRIBE, userExternalId);
|
|
352
|
+
};
|
|
353
|
+
/**
|
|
354
|
+
* Subscribes to all account-level messages for given account numbers
|
|
355
|
+
* @param accountNumbers List of account numbers to subscribe to
|
|
356
|
+
* @returns Promise that resolves when an ack is received
|
|
357
|
+
*/
|
|
358
|
+
AccountStreamer.prototype.subscribeToAccounts = function (accountNumbers) {
|
|
359
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
360
|
+
var value, requestId;
|
|
361
|
+
var _this = this;
|
|
362
|
+
return __generator(this, function (_a) {
|
|
363
|
+
if (accountNumbers.length === 0) {
|
|
364
|
+
return [2 /*return*/, Promise.reject('no account numbers')];
|
|
365
|
+
}
|
|
366
|
+
value = accountNumbers.length > 1 ? accountNumbers : accountNumbers[0];
|
|
367
|
+
requestId = this.subscribeTo(MessageAction.CONNECT, value);
|
|
368
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
369
|
+
_this.requestPromises.set(requestId, [resolve, reject]);
|
|
370
|
+
})];
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
};
|
|
374
|
+
AccountStreamer.prototype.sendQueuedMessages = function () {
|
|
375
|
+
var queued = this.queued;
|
|
376
|
+
if (queued.length === 0 || this.websocket === null) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
var websocket = this.websocket;
|
|
380
|
+
queued.forEach(function (msg) {
|
|
381
|
+
websocket.send(msg);
|
|
382
|
+
});
|
|
383
|
+
this.queued = [];
|
|
384
|
+
};
|
|
385
|
+
AccountStreamer.prototype.addMessageObserver = function (observer) {
|
|
386
|
+
var _this = this;
|
|
387
|
+
this.streamerMessageObservers.push(observer);
|
|
388
|
+
return function () {
|
|
389
|
+
removeElement(_this.streamerMessageObservers, observer);
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
return AccountStreamer;
|
|
393
|
+
}());
|
|
394
|
+
exports.AccountStreamer = AccountStreamer;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var lodash_1 = __importDefault(require("lodash"));
|
|
7
|
+
var TastytradeSession = /** @class */ (function () {
|
|
8
|
+
function TastytradeSession() {
|
|
9
|
+
this.authToken = null;
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(TastytradeSession.prototype, "isValid", {
|
|
12
|
+
get: function () {
|
|
13
|
+
return !lodash_1.default.isNil(this.authToken);
|
|
14
|
+
},
|
|
15
|
+
enumerable: false,
|
|
16
|
+
configurable: true
|
|
17
|
+
});
|
|
18
|
+
TastytradeSession.prototype.clear = function () {
|
|
19
|
+
this.authToken = null;
|
|
20
|
+
};
|
|
21
|
+
return TastytradeSession;
|
|
22
|
+
}());
|
|
23
|
+
exports.default = TastytradeSession;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EventType, IEvent } from '@dxfeed/api';
|
|
2
|
+
export declare const SupportedEventTypes: EventType[];
|
|
3
|
+
export default class QuoteStreamer {
|
|
4
|
+
private readonly token;
|
|
5
|
+
private readonly url;
|
|
6
|
+
private feed;
|
|
7
|
+
constructor(token: string, url: string);
|
|
8
|
+
connect(): void;
|
|
9
|
+
disconnect(): void;
|
|
10
|
+
subscribe(dxfeedSymbol: string, eventHandler: (event: IEvent) => void): () => void;
|
|
11
|
+
}
|