@brokerize/client 1.10.0 → 1.12.0

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.
Files changed (94) hide show
  1. package/README.md +45 -0
  2. package/dist/apiCtx.d.ts +10 -2
  3. package/dist/apiCtx.js +4 -4
  4. package/dist/authorizedApiContext.d.ts +5 -0
  5. package/dist/authorizedApiContext.js +12 -4
  6. package/dist/cjs/apiCtx.d.ts +10 -2
  7. package/dist/cjs/apiCtx.js +4 -4
  8. package/dist/cjs/authorizedApiContext.d.ts +5 -0
  9. package/dist/cjs/authorizedApiContext.js +11 -3
  10. package/dist/cjs/errors.d.ts +74 -0
  11. package/dist/cjs/errors.js +95 -1
  12. package/dist/cjs/index.d.ts +4 -4
  13. package/dist/cjs/index.js +6 -4
  14. package/dist/cjs/modelExports.d.ts +3 -0
  15. package/dist/cjs/modelExports.js +5 -1
  16. package/dist/cjs/swagger/models/BrokerClientCfg.d.ts +7 -0
  17. package/dist/cjs/swagger/models/BrokerClientCfg.js +5 -0
  18. package/dist/cjs/swagger/models/BrokerMeta.d.ts +15 -0
  19. package/dist/cjs/swagger/models/BrokerMeta.js +2 -0
  20. package/dist/cjs/swagger/models/ClientConfigUpdateOAuthLoginForm.d.ts +12 -1
  21. package/dist/cjs/swagger/models/ClientConfigUpdateOAuthLoginForm.js +2 -0
  22. package/dist/cjs/swagger/models/OAuthLoginFormConfig.d.ts +12 -1
  23. package/dist/cjs/swagger/models/OAuthLoginFormConfig.js +2 -0
  24. package/dist/cjs/swagger/models/Order.d.ts +9 -1
  25. package/dist/cjs/swagger/models/OrderCreate.d.ts +2 -1
  26. package/dist/cjs/swagger/models/PortfolioOrderDetailsSyncInfo.d.ts +60 -0
  27. package/dist/cjs/swagger/models/PortfolioOrderDetailsSyncInfo.js +51 -0
  28. package/dist/cjs/swagger/models/PortfolioOrderDetailsSyncInfoStatus.d.ts +22 -0
  29. package/dist/cjs/swagger/models/PortfolioOrderDetailsSyncInfoStatus.js +35 -0
  30. package/dist/cjs/swagger/models/PortfolioSyncInfoComplete.d.ts +7 -0
  31. package/dist/cjs/swagger/models/PortfolioSyncInfoComplete.js +6 -0
  32. package/dist/cjs/swagger/models/PortfolioSyncInfoCompleteSpecifics.d.ts +7 -0
  33. package/dist/cjs/swagger/models/PortfolioSyncInfoCompleteSpecifics.js +5 -0
  34. package/dist/cjs/swagger/models/PortfolioSyncInfoIncomplete.d.ts +7 -0
  35. package/dist/cjs/swagger/models/PortfolioSyncInfoIncomplete.js +6 -0
  36. package/dist/cjs/swagger/models/PortfolioSyncInfoIncompleteSpecifics.d.ts +7 -0
  37. package/dist/cjs/swagger/models/PortfolioSyncInfoIncompleteSpecifics.js +5 -0
  38. package/dist/cjs/swagger/models/SmartbrokerClientCfg.d.ts +27 -0
  39. package/dist/cjs/swagger/models/SmartbrokerClientCfg.js +46 -0
  40. package/dist/cjs/swagger/models/SummarizedTrade.d.ts +14 -0
  41. package/dist/cjs/swagger/models/SummarizedTrade.js +4 -0
  42. package/dist/cjs/swagger/models/TradeWarning.d.ts +31 -1
  43. package/dist/cjs/swagger/models/TradeWarning.js +13 -0
  44. package/dist/cjs/swagger/models/TradeWarningReason.d.ts +46 -0
  45. package/dist/cjs/swagger/models/TradeWarningReason.js +59 -0
  46. package/dist/cjs/swagger/models/index.d.ts +4 -0
  47. package/dist/cjs/swagger/models/index.js +4 -0
  48. package/dist/cjs/websocketClient.d.ts +109 -2
  49. package/dist/cjs/websocketClient.js +416 -74
  50. package/dist/cjs/websocketTypes.d.ts +10 -0
  51. package/dist/client.d.ts +437 -61
  52. package/dist/errors.d.ts +74 -0
  53. package/dist/errors.js +89 -0
  54. package/dist/index.d.ts +4 -4
  55. package/dist/index.js +3 -5
  56. package/dist/modelExports.d.ts +3 -0
  57. package/dist/modelExports.js +2 -0
  58. package/dist/swagger/models/BrokerClientCfg.d.ts +7 -0
  59. package/dist/swagger/models/BrokerClientCfg.js +5 -0
  60. package/dist/swagger/models/BrokerMeta.d.ts +15 -0
  61. package/dist/swagger/models/BrokerMeta.js +2 -0
  62. package/dist/swagger/models/ClientConfigUpdateOAuthLoginForm.d.ts +12 -1
  63. package/dist/swagger/models/ClientConfigUpdateOAuthLoginForm.js +2 -0
  64. package/dist/swagger/models/OAuthLoginFormConfig.d.ts +12 -1
  65. package/dist/swagger/models/OAuthLoginFormConfig.js +2 -0
  66. package/dist/swagger/models/Order.d.ts +9 -1
  67. package/dist/swagger/models/OrderCreate.d.ts +2 -1
  68. package/dist/swagger/models/PortfolioOrderDetailsSyncInfo.d.ts +60 -0
  69. package/dist/swagger/models/PortfolioOrderDetailsSyncInfo.js +45 -0
  70. package/dist/swagger/models/PortfolioOrderDetailsSyncInfoStatus.d.ts +22 -0
  71. package/dist/swagger/models/PortfolioOrderDetailsSyncInfoStatus.js +29 -0
  72. package/dist/swagger/models/PortfolioSyncInfoComplete.d.ts +7 -0
  73. package/dist/swagger/models/PortfolioSyncInfoComplete.js +6 -0
  74. package/dist/swagger/models/PortfolioSyncInfoCompleteSpecifics.d.ts +7 -0
  75. package/dist/swagger/models/PortfolioSyncInfoCompleteSpecifics.js +5 -0
  76. package/dist/swagger/models/PortfolioSyncInfoIncomplete.d.ts +7 -0
  77. package/dist/swagger/models/PortfolioSyncInfoIncomplete.js +6 -0
  78. package/dist/swagger/models/PortfolioSyncInfoIncompleteSpecifics.d.ts +7 -0
  79. package/dist/swagger/models/PortfolioSyncInfoIncompleteSpecifics.js +5 -0
  80. package/dist/swagger/models/SmartbrokerClientCfg.d.ts +27 -0
  81. package/dist/swagger/models/SmartbrokerClientCfg.js +40 -0
  82. package/dist/swagger/models/SummarizedTrade.d.ts +14 -0
  83. package/dist/swagger/models/SummarizedTrade.js +4 -0
  84. package/dist/swagger/models/TradeWarning.d.ts +31 -1
  85. package/dist/swagger/models/TradeWarning.js +13 -0
  86. package/dist/swagger/models/TradeWarningReason.d.ts +46 -0
  87. package/dist/swagger/models/TradeWarningReason.js +53 -0
  88. package/dist/swagger/models/index.d.ts +4 -0
  89. package/dist/swagger/models/index.js +4 -0
  90. package/dist/tsdoc-metadata.json +1 -1
  91. package/dist/websocketClient.d.ts +109 -2
  92. package/dist/websocketClient.js +416 -74
  93. package/dist/websocketTypes.d.ts +10 -0
  94. package/package.json +11 -10
@@ -1,6 +1,7 @@
1
1
  import { Auth } from "./apiCtx";
2
2
  import { AuthorizedApiContext } from "./authorizedApiContext";
3
3
  import { WebSocket } from "./dependencyDefinitions/webSocket";
4
+ import { BrokerizeWebSocketError } from "./errors";
4
5
  import type { SubscribeDecoupledOperation, SubscribeInvalidateDetails } from "./websocketTypes";
5
6
  export declare class BrokerizeWebSocketClientImpl implements BrokerizeWebSocketClient {
6
7
  private _url;
@@ -18,18 +19,70 @@ export declare class BrokerizeWebSocketClientImpl implements BrokerizeWebSocketC
18
19
  private _lastNonFatalError;
19
20
  private _errorCount;
20
21
  private _apiCtx;
21
- constructor(websocketUrl: string, auth: Auth, createWebSocket: (url: string) => WebSocket);
22
+ /**
23
+ * How often the server rejected our token in a row. Every retry forces a token refresh, and
24
+ * only `_maxConsecutiveAuthFailures` of them in a row are fatal. Reset on every successful
25
+ * authentication.
26
+ */
27
+ private _authFailureCount;
28
+ private _maxConsecutiveAuthFailures;
29
+ private _forceTokenRefresh;
30
+ private _destroyed;
31
+ private _connectionState;
32
+ private _connectionStateCallbacks;
33
+ constructor(websocketUrl: string, auth: Auth, createWebSocket: (url: string) => WebSocket, options?: BrokerizeWebSocketClientOptions);
22
34
  private _updateReconnectInterval;
23
35
  /**
24
36
  * Simple backoff behavior: first retry is fast (100ms), next 9 retries 1s, after that we only reconnect every 10s.
25
37
  * Jitter is added to avoid a thundering herd when many clients reconnect simultaneously (e.g. after a server-side disconnect).
26
38
  * The first retry uses a wider jitter window (~1s) because that's the scenario most prone to bursts of TLS handshakes.
39
+ *
40
+ * A token that keeps being rejected is retried far more slowly (`AUTH_RETRY_INTERVAL`), because
41
+ * every one of those attempts refreshes the token and a rejection is not something that tends to
42
+ * resolve within a second.
27
43
  */
28
44
  private _computeReconnectIntervalInMilliseconds;
29
45
  private _shouldConnect;
46
+ /**
47
+ * Invoke a subscriber callback. A callback that throws must not prevent the remaining
48
+ * subscribers from being notified, so everything is contained here.
49
+ */
50
+ private _invokeCallback;
30
51
  private subscribe;
31
52
  subscribeInvalidate(subscribe: SubscribeInvalidateDetails, callback: Callback): Subscription;
32
53
  subscribeDecoupledOperation(subscribe: Pick<SubscribeDecoupledOperation, "sessionId" | "decoupledOperationId">, callback: Callback): Subscription;
54
+ /**
55
+ * The current connection state. See `subscribeConnectionState`.
56
+ */
57
+ getConnectionState(): WebSocketConnectionState;
58
+ /**
59
+ * Observe the state of the underlying websocket connection. The callback is invoked immediately
60
+ * with the current state and afterwards on every change.
61
+ *
62
+ * This is the only place where non-terminal errors are reported: the client recovers from them
63
+ * on its own, so they are not passed to the subscription callbacks, which would force
64
+ * applications to tear down subscriptions on every connection hiccup. Use this to show that
65
+ * live updates are currently unavailable.
66
+ */
67
+ subscribeConnectionState(callback: ConnectionStateCallback): Subscription;
68
+ /**
69
+ * Recover from a fatal error (see `WebSocketConnectionState`). The client drops the fatal error
70
+ * state and connects again, retrieving a fresh token. Existing subscriptions were already
71
+ * terminated when the fatal error occurred, so applications have to subscribe again.
72
+ *
73
+ * Calling this without a fatal error forces a reconnect of the current connection.
74
+ */
75
+ reconnect(): void;
76
+ /**
77
+ * Release all resources: close the connection, drop all subscriptions and stop the reconnect and
78
+ * ping timers. A destroyed client cannot be revived with `reconnect()`.
79
+ *
80
+ * This is called by `AuthorizedApiContext.destroy()` for the client it created itself. A client
81
+ * that was passed in from outside is owned by the application and is not destroyed with the
82
+ * context.
83
+ */
84
+ destroy(): void;
85
+ private _setConnectionState;
33
86
  private _startSubscription;
34
87
  private _fillInMessagesAfterConnectionInterruption;
35
88
  private _endSubscription;
@@ -40,13 +93,26 @@ export declare class BrokerizeWebSocketClientImpl implements BrokerizeWebSocketC
40
93
  _setAuthorizedApiContext(ctx: AuthorizedApiContext): void;
41
94
  _startOrStopDisconnectTimeout(): void;
42
95
  private _sendWs;
96
+ /**
97
+ * Close the current socket, if any, and detach its handlers so that a late `close` event of an
98
+ * outdated socket cannot interfere with a connection established in the meantime.
99
+ */
100
+ private _closeSocket;
43
101
  private _connect;
102
+ private _handleMessage;
103
+ /**
104
+ * The server rejected our token. The usual cause is an id token that expired between retrieving
105
+ * it and the server validating it, which a fresh token fixes, so retry once before giving up on
106
+ * this client.
107
+ */
108
+ private _handleAuthenticationFailure;
44
109
  /**
45
110
  * Error happend when trying to connect, but it may be solved by trying a reconnect later.
46
111
  */
47
112
  private _handleNonFatalError;
48
113
  /**
49
- * Fatal error: this means the client is unusable from now on and must be recreated.
114
+ * Fatal error: this means the client is unusable from now on and must be recreated or revived
115
+ * with `reconnect()`.
50
116
  */
51
117
  private _handleFatalError;
52
118
  /**
@@ -59,10 +125,51 @@ export declare class BrokerizeWebSocketClientImpl implements BrokerizeWebSocketC
59
125
  private _resetErrorState;
60
126
  private _findSubscriptionEntry;
61
127
  }
128
+ /**
129
+ * Optional behavior of `BrokerizeWebSocketClientImpl`. The defaults are what an application
130
+ * wants; this exists for the rare case that they have to be tightened.
131
+ */
132
+ export type BrokerizeWebSocketClientOptions = {
133
+ /**
134
+ * How often in a row the server may reject our token before the client gives up and goes
135
+ * `fatal`. Defaults to 10, which together with the backoff spans several minutes. Mainly here so
136
+ * that tests do not have to wait that long.
137
+ */
138
+ maxConsecutiveAuthFailures?: number;
139
+ };
62
140
  export interface BrokerizeWebSocketClient {
63
141
  subscribeInvalidate: (subscribe: SubscribeInvalidateDetails, callback: Callback) => Subscription;
64
142
  subscribeDecoupledOperation: (subscribe: Pick<SubscribeDecoupledOperation, "sessionId" | "decoupledOperationId">, callback: Callback) => Subscription;
143
+ subscribeConnectionState: (callback: ConnectionStateCallback) => Subscription;
144
+ getConnectionState: () => WebSocketConnectionState;
145
+ reconnect: () => void;
65
146
  }
147
+ /**
148
+ * - `closed`: not connected, because nothing is subscribed.
149
+ * - `connecting`: the first connection attempt is in progress.
150
+ * - `open`: connected and authenticated, live updates are being delivered.
151
+ * - `reconnecting`: the connection failed or was lost and the client is retrying. `error` holds
152
+ * the error that caused it. Subscriptions stay alive and receive a synthetic invalidate once
153
+ * the connection is back.
154
+ *
155
+ * A rejected token lands here too, so an application can show that live updates are currently
156
+ * unavailable - and why - while the client works on getting back in, instead of having to
157
+ * rebuild everything over a token that expired in flight.
158
+ * - `fatal`: the client gave up and is unusable. All subscriptions were terminated with `error`.
159
+ * This only happens after the token has been rejected over and over for several minutes, i.e.
160
+ * when the session is gone rather than stale. Call `reconnect()` or create a new client.
161
+ */
162
+ export type WebSocketConnectionStatus = "closed" | "connecting" | "open" | "reconnecting" | "fatal";
163
+ export type WebSocketConnectionState = {
164
+ status: WebSocketConnectionStatus;
165
+ error?: BrokerizeWebSocketError;
166
+ };
167
+ export type ConnectionStateCallback = (state: WebSocketConnectionState) => void;
168
+ /**
169
+ * Callback of a subscription. `err` is always a `BrokerizeWebSocketError` with
170
+ * `terminal === true`: the subscription is over and will not receive any further data. Errors the
171
+ * client recovers from on its own are reported via `subscribeConnectionState` instead.
172
+ */
66
173
  export type Callback<T = any> = (err: any, data: T) => void;
67
174
  export type Subscription = {
68
175
  unsubscribe: () => void;