@brokerize/client 1.2.5 → 1.3.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.
@@ -1,7 +1,7 @@
1
1
  import { Auth, BrokerizeConfig } from "./apiCtx";
2
2
  import * as openApiClient from "./swagger";
3
3
  import { AddSessionParams, ConfirmOAuthParams, CreateTradeChallengeRequest, CreateTradeRequest, DeleteDemoAccountRequest, DemoAccountSettings, GenericTable, GetCostEstimationParams, GetQuoteRequest, OrderChanges, PrepareOAuthRedirectParams, PrepareTradeRequest } from "./swagger";
4
- import { BrokerizeWebSocketClient, BrokerizeWebSocketClientImpl, Callback, Subscription } from "./websocketClient";
4
+ import { BrokerizeWebSocketClient, Callback, Subscription } from "./websocketClient";
5
5
  export declare class AuthorizedApiContext {
6
6
  private _cfg;
7
7
  private _auth;
@@ -26,7 +26,7 @@ export declare class AuthorizedApiContext {
26
26
  private _adminApi;
27
27
  private _userApi;
28
28
  private _securitiesApi;
29
- constructor(cfg: BrokerizeConfig, auth: Auth, wsClient?: BrokerizeWebSocketClientImpl);
29
+ constructor(cfg: BrokerizeConfig, auth: Auth, wsClient?: BrokerizeWebSocketClient);
30
30
  createChildContext(): AuthorizedApiContext;
31
31
  private _initRequestInit;
32
32
  getBrokers(): Promise<openApiClient.GetBrokersResponse>;
@@ -141,3 +141,4 @@ export declare class AuthorizedApiContext {
141
141
  destroy(): void;
142
142
  subscribeLogout(callback: Callback): Subscription;
143
143
  }
144
+ export declare function getWebSocketURLByBasePath(basePath: string): string;
@@ -402,9 +402,7 @@ export class AuthorizedApiContext {
402
402
  }
403
403
  _initInternalWebSocketClient() {
404
404
  const basePath = this._cfg.basePath || "https://api-preview.brokerize.com";
405
- const websocketPath = (basePath.startsWith("https")
406
- ? "wss://" + basePath.substring(8)
407
- : "ws://" + basePath.substring(7)) + "/websocket";
405
+ const websocketPath = getWebSocketURLByBasePath(basePath);
408
406
  if (!this._cfg.createWebSocket) {
409
407
  throw new Error("createWebSocket not provided. This should not happen as there should be a default implementation.");
410
408
  }
@@ -434,3 +432,16 @@ export class AuthorizedApiContext {
434
432
  };
435
433
  }
436
434
  }
435
+ export function getWebSocketURLByBasePath(basePath) {
436
+ const SUFFIX = "/websocket";
437
+ if (basePath.startsWith("https")) {
438
+ return "wss://" + basePath.substring(8) + SUFFIX;
439
+ }
440
+ else if (basePath.startsWith("http")) {
441
+ return "ws://" + basePath.substring(7) + SUFFIX;
442
+ }
443
+ else {
444
+ // might be a relative path
445
+ return basePath + SUFFIX;
446
+ }
447
+ }
package/dist/client.d.ts CHANGED
@@ -1098,7 +1098,7 @@ export declare class AuthorizedApiContext {
1098
1098
  private _adminApi;
1099
1099
  private _userApi;
1100
1100
  private _securitiesApi;
1101
- constructor(cfg: BrokerizeConfig, auth: Auth, wsClient?: BrokerizeWebSocketClientImpl);
1101
+ constructor(cfg: BrokerizeConfig, auth: Auth, wsClient?: BrokerizeWebSocketClient);
1102
1102
  createChildContext(): AuthorizedApiContext;
1103
1103
  private _initRequestInit;
1104
1104
  getBrokers(): Promise<openApiClient.GetBrokersResponse>;
@@ -1447,7 +1447,7 @@ export declare class Brokerize {
1447
1447
  * @param tokenRefreshCallback when a token refresh occurs, this callback is called and can store the stored tokens
1448
1448
  * @returns
1449
1449
  */
1450
- createAuthorizedContext(authCtxCfg: AuthContextConfiguration, tokenRefreshCallback?: TokenRefreshCallback): AuthorizedApiContext;
1450
+ createAuthorizedContext(authCtxCfg: AuthContextConfiguration, tokenRefreshCallback?: TokenRefreshCallback, customWebSocketClient?: BrokerizeWebSocketClient): AuthorizedApiContext;
1451
1451
  getCognitoConfig(): CognitoPoolConfig | undefined;
1452
1452
  /**
1453
1453
  * Create an "Auth" object which can be used to retrive access tokens.
@@ -1459,6 +1459,24 @@ export declare class Brokerize {
1459
1459
  * @returns
1460
1460
  */
1461
1461
  createAuth(authCtxCfg: AuthContextConfiguration, tokenRefreshCallback?: TokenRefreshCallback): Auth;
1462
+ /**
1463
+ * Create a customized WebSocket client. You can override the WebSocket connection URL and the Auth implementation
1464
+ * for a custom token retrieval behavior.
1465
+ *
1466
+ * Note that in most contexts this is not needed.
1467
+ *
1468
+ * If you want to use it, you should use the created client in your call to `createAuthorizedContext`, so that it
1469
+ * is used by clients:
1470
+ *
1471
+ * ```
1472
+ * const customWebSocketClient = Brokerize.createCustomWebSocketClient({ auth: { async getToken() {...} }});
1473
+ * const authorizedApiCtx = Brokerize.createAuthorizedContext(authCtxCfg, tokenRefreshCallback, customWebSocketClient);
1474
+ * ```
1475
+ */
1476
+ createCustomWebSocketClient({ url, auth, }: {
1477
+ url?: string;
1478
+ auth: Auth;
1479
+ }): BrokerizeWebSocketClient;
1462
1480
  }
1463
1481
 
1464
1482
  export declare interface BrokerizeConfig {
@@ -1526,55 +1544,6 @@ export declare interface BrokerizeWebSocketClient {
1526
1544
  subscribeDecoupledOperation: (subscribe: Pick<SubscribeDecoupledOperation, "sessionId" | "decoupledOperationId">, callback: Callback) => Subscription;
1527
1545
  }
1528
1546
 
1529
- declare class BrokerizeWebSocketClientImpl implements BrokerizeWebSocketClient {
1530
- private _url;
1531
- private _map;
1532
- private _id;
1533
- private _socket;
1534
- private _pingIntvl;
1535
- private _reconnectIntvl;
1536
- private _authenticatedCallback;
1537
- private _disconnectTimeout;
1538
- private _isOpen;
1539
- private _auth;
1540
- private _createWebsocket;
1541
- private _fatalError;
1542
- private _lastNonFatalError;
1543
- private _errorCount;
1544
- constructor(websocketUrl: string, auth: Auth, createWebSocket: (url: string) => WebSocket_2);
1545
- private _updateReconnectInterval;
1546
- /**
1547
- * Simple backoff behavior: first retry is fast (100ms), next 9 retries 1s, after that we only reconnect every 10s.
1548
- */
1549
- private _computeReconnectIntervalInMilliseconds;
1550
- private _shouldConnect;
1551
- private subscribe;
1552
- subscribeInvalidate(subscribe: SubscribeInvalidateDetails, callback: Callback): Subscription;
1553
- subscribeDecoupledOperation(subscribe: Pick<SubscribeDecoupledOperation, "sessionId" | "decoupledOperationId">, callback: Callback): Subscription;
1554
- private _startSubscription;
1555
- private _endSubscription;
1556
- _startOrStopDisconnectTimeout(): void;
1557
- private _sendWs;
1558
- private _connect;
1559
- /**
1560
- * Error happend when trying to connect, but it may be solved by trying a reconnect later.
1561
- */
1562
- private _handleNonFatalError;
1563
- /**
1564
- * Fatal error: this means the client is unusable from now on and must be recreated.
1565
- */
1566
- private _handleFatalError;
1567
- /**
1568
- * If a fatal error occurs, all registered callbacks must be called with the error parameter.
1569
- */
1570
- private _notifySubscribersAboutFatalError;
1571
- /**
1572
- * Once we have successfully established a connection, error counts and states must be reset.
1573
- */
1574
- private _resetErrorState;
1575
- private _findSubscriptionEntry;
1576
- }
1577
-
1578
1547
  declare type BrokerizeWebSocketError = {
1579
1548
  message: string;
1580
1549
  };
package/dist/index.d.ts CHANGED
@@ -21,7 +21,7 @@ export declare class Brokerize {
21
21
  * @param tokenRefreshCallback when a token refresh occurs, this callback is called and can store the stored tokens
22
22
  * @returns
23
23
  */
24
- createAuthorizedContext(authCtxCfg: AuthContextConfiguration, tokenRefreshCallback?: TokenRefreshCallback): AuthorizedApiContext;
24
+ createAuthorizedContext(authCtxCfg: AuthContextConfiguration, tokenRefreshCallback?: TokenRefreshCallback, customWebSocketClient?: BrokerizeWebSocketClient): AuthorizedApiContext;
25
25
  getCognitoConfig(): CognitoPoolConfig | undefined;
26
26
  /**
27
27
  * Create an "Auth" object which can be used to retrive access tokens.
@@ -33,6 +33,24 @@ export declare class Brokerize {
33
33
  * @returns
34
34
  */
35
35
  createAuth(authCtxCfg: AuthContextConfiguration, tokenRefreshCallback?: TokenRefreshCallback): Auth;
36
+ /**
37
+ * Create a customized WebSocket client. You can override the WebSocket connection URL and the Auth implementation
38
+ * for a custom token retrieval behavior.
39
+ *
40
+ * Note that in most contexts this is not needed.
41
+ *
42
+ * If you want to use it, you should use the created client in your call to `createAuthorizedContext`, so that it
43
+ * is used by clients:
44
+ *
45
+ * ```
46
+ * const customWebSocketClient = Brokerize.createCustomWebSocketClient({ auth: { async getToken() {...} }});
47
+ * const authorizedApiCtx = Brokerize.createAuthorizedContext(authCtxCfg, tokenRefreshCallback, customWebSocketClient);
48
+ * ```
49
+ */
50
+ createCustomWebSocketClient({ url, auth, }: {
51
+ url?: string;
52
+ auth: Auth;
53
+ }): BrokerizeWebSocketClient;
36
54
  }
37
55
  /**
38
56
  * When a token update occurs (e.g. due to a refreshToken call), this callback is called.
package/dist/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  /* Import/Export the DOM parts we rely on. Those are partial copies from the official TypeScript DOM library definitions (https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts),
2
2
  but reduced to the parts actually used by bg-trading. */
3
3
  import { createAuth, createConfiguration, } from "./apiCtx";
4
- import { AuthorizedApiContext } from "./authorizedApiContext";
4
+ import { AuthorizedApiContext, getWebSocketURLByBasePath, } from "./authorizedApiContext";
5
5
  import { BrokerizeError } from "./errors";
6
6
  import * as openApiClient from "./swagger";
7
7
  import * as Models from "./modelExports";
8
+ import { BrokerizeWebSocketClientImpl, } from "./websocketClient";
8
9
  import * as WebSocketTypes from "./websocketTypes";
9
10
  import * as Utils from "./utils";
10
11
  export { AuthorizedApiContext, Models, WebSocketTypes, Utils, BrokerizeError, };
@@ -61,8 +62,9 @@ export class Brokerize {
61
62
  * @param tokenRefreshCallback when a token refresh occurs, this callback is called and can store the stored tokens
62
63
  * @returns
63
64
  */
64
- createAuthorizedContext(authCtxCfg, tokenRefreshCallback) {
65
- return new AuthorizedApiContext(this._cfg, this.createAuth(authCtxCfg, tokenRefreshCallback));
65
+ createAuthorizedContext(authCtxCfg, tokenRefreshCallback, customWebSocketClient) {
66
+ const auth = this.createAuth(authCtxCfg, tokenRefreshCallback);
67
+ return new AuthorizedApiContext(this._cfg, auth, customWebSocketClient);
66
68
  }
67
69
  getCognitoConfig() {
68
70
  var _a;
@@ -88,6 +90,33 @@ export class Brokerize {
88
90
  },
89
91
  });
90
92
  }
93
+ /**
94
+ * Create a customized WebSocket client. You can override the WebSocket connection URL and the Auth implementation
95
+ * for a custom token retrieval behavior.
96
+ *
97
+ * Note that in most contexts this is not needed.
98
+ *
99
+ * If you want to use it, you should use the created client in your call to `createAuthorizedContext`, so that it
100
+ * is used by clients:
101
+ *
102
+ * ```
103
+ * const customWebSocketClient = Brokerize.createCustomWebSocketClient({ auth: { async getToken() {...} }});
104
+ * const authorizedApiCtx = Brokerize.createAuthorizedContext(authCtxCfg, tokenRefreshCallback, customWebSocketClient);
105
+ * ```
106
+ */
107
+ createCustomWebSocketClient({ url, auth, }) {
108
+ if (!(url === null || url === void 0 ? void 0 : url.length)) {
109
+ const basePath = this._cfg.basePath || "https://api-preview.brokerize.com";
110
+ url = getWebSocketURLByBasePath(basePath);
111
+ }
112
+ if (this._cfg.createWebSocket == null) {
113
+ throw new Error("createWebSocket must be configured");
114
+ }
115
+ if (!(auth === null || auth === void 0 ? void 0 : auth.getToken)) {
116
+ throw new Error("Auth implementation with getToken function must be provided");
117
+ }
118
+ return new BrokerizeWebSocketClientImpl(url, auth, this._cfg.createWebSocket);
119
+ }
91
120
  }
92
121
  function getGlobalObject() {
93
122
  if (typeof globalThis !== "undefined")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brokerize/client",
3
- "version": "1.2.5",
3
+ "version": "1.3.0",
4
4
  "description": "Client for the brokerize.com API",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/brokerize/client-js#readme",