@hautechai/sdk 0.3.6 → 0.3.7

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/dist/sdk/index.js CHANGED
@@ -41,6 +41,7 @@ export const createSDK = (options) => {
41
41
  : null,
42
42
  // TODO: Refactor the API initialization
43
43
  operations: () => getAPI(OperationsApi, optionsWithTokenRefresher),
44
+ allowPollingFallback: options.allowPollingFallback ?? true,
44
45
  });
45
46
  operationsListener.subscribe();
46
47
  return {
@@ -7,12 +7,14 @@ export declare class OperationsListener {
7
7
  } | null;
8
8
  ws: WebSocketClient | null;
9
9
  operations: () => Promise<OperationsApi>;
10
- constructor({ ws, operations, }: {
10
+ allowPollingFallback: boolean;
11
+ constructor({ ws, operations, allowPollingFallback, }: {
11
12
  ws: {
12
13
  endpoint: string;
13
14
  token: () => string | Promise<string>;
14
15
  } | null;
15
16
  operations: () => Promise<OperationsApi>;
17
+ allowPollingFallback: boolean;
16
18
  });
17
19
  operationsStore: Record<string, OperationEntity>;
18
20
  getOperation(id: string): Promise<OperationEntity | null>;
@@ -2,10 +2,12 @@ import { w3cwebsocket as WebSocketClient } from 'websocket';
2
2
  import { HautechError } from '../errors';
3
3
  // This is pretty much a dirty solution until we need to rework this part.
4
4
  export class OperationsListener {
5
- constructor({ ws, operations, }) {
5
+ constructor({ ws, operations, allowPollingFallback = false, }) {
6
6
  this.useWebsocket = null;
7
7
  this.ws = null;
8
8
  this.operationsStore = {};
9
+ if (!ws)
10
+ allowPollingFallback = true;
9
11
  if (ws) {
10
12
  this.useWebsocket = {
11
13
  endpoint: ws?.endpoint,
@@ -13,10 +15,11 @@ export class OperationsListener {
13
15
  };
14
16
  }
15
17
  this.operations = operations;
18
+ this.allowPollingFallback = allowPollingFallback;
16
19
  }
17
20
  async getOperation(id) {
18
- const isWsReady = this.ws?.readyState === WebSocket.OPEN;
19
- if (!this.operationsStore[id] || !isWsReady) {
21
+ const fallbackToPolling = this.allowPollingFallback && !(this.ws?.readyState === WebSocket.OPEN);
22
+ if (!this.operationsStore[id] || fallbackToPolling) {
20
23
  const api = await this.operations();
21
24
  const operation = await api.operationsControllerGetOperationV1(id);
22
25
  if (operation.status == 200)
@@ -34,7 +37,8 @@ export class OperationsListener {
34
37
  if (!this.useWebsocket)
35
38
  return;
36
39
  try {
37
- this.ws = new WebSocketClient(this.useWebsocket.endpoint, ['1', await this.useWebsocket.token()]);
40
+ const token = await this.useWebsocket.token();
41
+ this.ws = new WebSocketClient(this.useWebsocket.endpoint, ['1', token]);
38
42
  this.ws.onopen = () => {
39
43
  this.onOpen();
40
44
  };
package/dist/types.d.ts CHANGED
@@ -2,6 +2,7 @@ export interface SDKOptions {
2
2
  authToken: () => string | Promise<string>;
3
3
  endpoint?: string;
4
4
  useWebsocket?: boolean;
5
+ allowPollingFallback?: boolean;
5
6
  }
6
7
  export type ListProps = {
7
8
  cursor?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hautechai/sdk",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "license": "MIT",
5
5
  "keywords": [],
6
6
  "repository": {