@opentap/runner-client 1.0.0-beta.73 → 1.0.0-beta.75

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,6 +1,8 @@
1
1
  import { ConnectionOptions, RequestOptions } from 'nats.ws/cjs/nats.js';
2
2
  import { ComponentSettingsBase, ComponentSettingsIdentifier, ComponentSettingsListItem, DataGridControl, ErrorResponse, FileParameter, FileResponse, ListItemType, ProfileGroup, RepositoryPackageReference, RepositorySettingsPackageDefinition, SettingsTapPackage } from './DTOs';
3
3
  export declare class BaseClient {
4
+ private connection;
5
+ private domainAccess;
4
6
  private _accessToken;
5
7
  /** Get access token */
6
8
  get accessToken(): string;
@@ -17,8 +19,6 @@ export declare class BaseClient {
17
19
  private _connectionOptions;
18
20
  /** Get connection options */
19
21
  get connectionOptions(): ConnectionOptions;
20
- private domainAccess;
21
- private natsConnection;
22
22
  constructor(baseSubject: string, options: ConnectionOptions);
23
23
  /**
24
24
  * Send a request to the nats server.
@@ -28,8 +28,15 @@ export declare class BaseClient {
28
28
  * @returns Promise of an object
29
29
  */
30
30
  protected request<T>(subject: string, payload?: any, options?: RequestOptions): Promise<T>;
31
+ /**
32
+ * Check if the the response is an error from the server.
33
+ * @param {any} response
34
+ * @returns {boolean}
35
+ */
36
+ private isErrorResponse;
31
37
  /**
32
38
  * Create a connection to the nats server.
39
+ * @param {ConnectionOptions} options
33
40
  */
34
41
  private connect;
35
42
  /**
package/lib/BaseClient.js CHANGED
@@ -35,7 +35,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  }
36
36
  };
37
37
  // ! import { connect, ConnectionOptions, Empty, JSONCodec, NatsConnection, RequestOptions, StringCodec } from 'nats.ws' if consuming this library in angular >= 13.
38
- import { connect, Empty, JSONCodec, StringCodec } from 'nats.ws/cjs/nats.js';
38
+ import { connect, Empty, ErrorCode, JSONCodec, StringCodec } from 'nats.ws/cjs/nats.js';
39
+ import { ErrorResponse, } from './DTOs';
39
40
  var BaseClient = /** @class */ (function () {
40
41
  function BaseClient(baseSubject, options) {
41
42
  this.domainAccess = new Map();
@@ -94,35 +95,56 @@ var BaseClient = /** @class */ (function () {
94
95
  * @returns Promise of an object
95
96
  */
96
97
  BaseClient.prototype.request = function (subject, payload, options) {
97
- var _a;
98
98
  return __awaiter(this, void 0, void 0, function () {
99
99
  var stringCodec, data;
100
- return __generator(this, function (_b) {
101
- switch (_b.label) {
100
+ var _this = this;
101
+ return __generator(this, function (_a) {
102
+ switch (_a.label) {
102
103
  case 0:
103
- if (!subject || ((_a = this.natsConnection) === null || _a === void 0 ? void 0 : _a.isClosed()))
104
- return [2 /*return*/, Promise.reject()];
104
+ if (!subject)
105
+ return [2 /*return*/, Promise.reject('Subject is not defined!')];
106
+ if (!this.connection)
107
+ return [2 /*return*/, Promise.reject('Connection is down! Please try again!')];
108
+ if (this.connection.isClosed())
109
+ return [2 /*return*/, Promise.reject('Connection has been closed! Please reconnect!')];
105
110
  subject = "".concat(this.baseSubject, ".Request.").concat(subject);
106
111
  stringCodec = StringCodec();
107
112
  data = payload ? stringCodec.encode(JSON.stringify(payload)) : Empty;
108
- return [4 /*yield*/, this.natsConnection
113
+ return [4 /*yield*/, this.connection
109
114
  .request(subject, data, options)
110
115
  .then(function (message) {
111
116
  var jsonCodec = JSONCodec();
112
- var responseData = jsonCodec.decode(message.data);
113
- // TODO: implement ErrorResponse handling when server returns an error;
114
- return Promise.resolve(responseData);
117
+ var response = jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data);
118
+ return _this.isErrorResponse(response)
119
+ ? Promise.reject("".concat(JSON.stringify(ErrorResponse.fromJS(response))))
120
+ : Promise.resolve(response);
115
121
  })
116
122
  .catch(function (error) {
117
- return Promise.reject(new Error("Request failed: ".concat(error)));
123
+ switch (error === null || error === void 0 ? void 0 : error.code) {
124
+ case ErrorCode.NoResponders:
125
+ return Promise.reject("no responders to ".concat(subject));
126
+ case ErrorCode.Timeout:
127
+ return Promise.reject('request timed out');
128
+ default:
129
+ return Promise.reject(error);
130
+ }
118
131
  })];
119
- case 1: return [2 /*return*/, _b.sent()];
132
+ case 1: return [2 /*return*/, _a.sent()];
120
133
  }
121
134
  });
122
135
  });
123
136
  };
137
+ /**
138
+ * Check if the the response is an error from the server.
139
+ * @param {any} response
140
+ * @returns {boolean}
141
+ */
142
+ BaseClient.prototype.isErrorResponse = function (response) {
143
+ return response && typeof response === 'object' && ['Type', 'Message', 'StackTrace', 'Parameter'].every(function (prop) { return prop in response; });
144
+ };
124
145
  /**
125
146
  * Create a connection to the nats server.
147
+ * @param {ConnectionOptions} options
126
148
  */
127
149
  BaseClient.prototype.connect = function (options) {
128
150
  return __awaiter(this, void 0, void 0, function () {
@@ -133,7 +155,7 @@ var BaseClient = /** @class */ (function () {
133
155
  case 0:
134
156
  _a.trys.push([0, 2, , 3]);
135
157
  return [4 /*yield*/, connect(options).then(function (connection) {
136
- _this.natsConnection = connection;
158
+ _this.connection = connection;
137
159
  })];
138
160
  case 1:
139
161
  _a.sent();
@@ -155,8 +177,8 @@ var BaseClient = /** @class */ (function () {
155
177
  return __generator(this, function (_b) {
156
178
  switch (_b.label) {
157
179
  case 0:
158
- if (!this.natsConnection) return [3 /*break*/, 2];
159
- return [4 /*yield*/, ((_a = this.natsConnection) === null || _a === void 0 ? void 0 : _a.close().catch(function (error) {
180
+ if (!this.connection) return [3 /*break*/, 2];
181
+ return [4 /*yield*/, ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.close().catch(function (error) {
160
182
  throw new Error("failed to close connection: ".concat(error));
161
183
  }))];
162
184
  case 1:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentap/runner-client",
3
- "version": "1.0.0-beta.73",
3
+ "version": "1.0.0-beta.75",
4
4
  "description": "This is the TypeScript Client for the OpenTAP Runner Plugin.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
package/readme.md CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  # OpenTap Runner Client
3
3
 
4
- This is the TypeScript Client for the OpenTAP Runner Plugin.
4
+ This is the web client for the OpenTAP Runner.
5
5
 
6
6
 
7
7
  ## Authors
@@ -14,18 +14,32 @@ This is the TypeScript Client for the OpenTAP Runner Plugin.
14
14
  Install my-project with npm
15
15
 
16
16
  ```bash
17
- npm install my-project
18
- cd my-project
17
+ npm install @opentap/runner-client
19
18
  ```
20
19
 
21
- ## Usage/Examples
22
-
23
- ```javascript
24
- import Component from 'my-project'
25
-
26
- function App() {
27
- return <Component />
20
+ ## Usage example
21
+
22
+ ```typescript
23
+ import { RunnerClient } from "@openTap/runner-client";
24
+
25
+ class client {
26
+ runnerClient: RunnerClient;
27
+
28
+ constructor() {
29
+ this.runnerClient = new RunnerClient('<BASE_SUBJECT>', { servers: '<SERVER_ADDRESS>' });
30
+ }
31
+
32
+ getImages() {
33
+ try {
34
+ this.data = this.runnerClient?.getImages()
35
+ .then(res => console.log(res))
36
+ .catch(error => console.error(error));
37
+ } catch (error) {
38
+ console.error(error);
39
+ }
40
+ }
28
41
  }
42
+
29
43
  ```
30
44
 
31
45
 
@@ -49,19 +63,13 @@ Install dependencies
49
63
  npm install
50
64
  ```
51
65
 
52
- Start the server
66
+ Build the library
53
67
 
54
68
  ```bash
55
69
  npm run build
56
70
  ```
57
71
 
58
72
 
59
- ## Test
60
-
61
- ```bash
62
- npm run test
63
- ```
64
73
  ## License
65
74
 
66
75
  [MIT](https://choosealicense.com/licenses/mit/)
67
-