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

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,4 +1,4 @@
1
- import { ConnectionOptions, RequestOptions } from 'nats.ws/cjs/nats.js';
1
+ import { ConnectionOptions, NatsConnection, 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
4
  private _accessToken;
@@ -17,8 +17,10 @@ export declare class BaseClient {
17
17
  private _connectionOptions;
18
18
  /** Get connection options */
19
19
  get connectionOptions(): ConnectionOptions;
20
+ private _connection;
21
+ /** Get connection instance */
22
+ get connection(): NatsConnection;
20
23
  private domainAccess;
21
- private natsConnection;
22
24
  constructor(baseSubject: string, options: ConnectionOptions);
23
25
  /**
24
26
  * Send a request to the nats server.
@@ -28,8 +30,15 @@ export declare class BaseClient {
28
30
  * @returns Promise of an object
29
31
  */
30
32
  protected request<T>(subject: string, payload?: any, options?: RequestOptions): Promise<T>;
33
+ /**
34
+ * Check if the the response is an error from the server.
35
+ * @param {any} response
36
+ * @returns {boolean}
37
+ */
38
+ private isErrorResponse;
31
39
  /**
32
40
  * Create a connection to the nats server.
41
+ * @param {ConnectionOptions} options
33
42
  */
34
43
  private connect;
35
44
  /**
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();
@@ -86,6 +87,14 @@ var BaseClient = /** @class */ (function () {
86
87
  enumerable: false,
87
88
  configurable: true
88
89
  });
90
+ Object.defineProperty(BaseClient.prototype, "connection", {
91
+ /** Get connection instance */
92
+ get: function () {
93
+ return this._connection;
94
+ },
95
+ enumerable: false,
96
+ configurable: true
97
+ });
89
98
  /**
90
99
  * Send a request to the nats server.
91
100
  * @param subject The subject to request
@@ -97,32 +106,50 @@ var BaseClient = /** @class */ (function () {
97
106
  var _a;
98
107
  return __awaiter(this, void 0, void 0, function () {
99
108
  var stringCodec, data;
109
+ var _this = this;
100
110
  return __generator(this, function (_b) {
101
111
  switch (_b.label) {
102
112
  case 0:
103
- if (!subject || ((_a = this.natsConnection) === null || _a === void 0 ? void 0 : _a.isClosed()))
113
+ if (!subject || ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.isClosed()))
104
114
  return [2 /*return*/, Promise.reject()];
105
115
  subject = "".concat(this.baseSubject, ".Request.").concat(subject);
106
116
  stringCodec = StringCodec();
107
117
  data = payload ? stringCodec.encode(JSON.stringify(payload)) : Empty;
108
- return [4 /*yield*/, this.natsConnection
118
+ return [4 /*yield*/, this.connection
109
119
  .request(subject, data, options)
110
120
  .then(function (message) {
111
121
  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);
122
+ var response = jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data);
123
+ return _this.isErrorResponse(response)
124
+ ? Promise.reject("".concat(JSON.stringify(ErrorResponse.fromJS(response))))
125
+ : Promise.resolve(response);
115
126
  })
116
127
  .catch(function (error) {
117
- return Promise.reject(new Error("Request failed: ".concat(error)));
128
+ switch (error === null || error === void 0 ? void 0 : error.code) {
129
+ case ErrorCode.NoResponders:
130
+ return Promise.reject("no responders to ".concat(subject));
131
+ case ErrorCode.Timeout:
132
+ return Promise.reject('request timed out');
133
+ default:
134
+ return Promise.reject(error);
135
+ }
118
136
  })];
119
137
  case 1: return [2 /*return*/, _b.sent()];
120
138
  }
121
139
  });
122
140
  });
123
141
  };
142
+ /**
143
+ * Check if the the response is an error from the server.
144
+ * @param {any} response
145
+ * @returns {boolean}
146
+ */
147
+ BaseClient.prototype.isErrorResponse = function (response) {
148
+ return response && typeof response === 'object' && ['Type', 'Message', 'StackTrace', 'Parameter'].every(function (prop) { return prop in response; });
149
+ };
124
150
  /**
125
151
  * Create a connection to the nats server.
152
+ * @param {ConnectionOptions} options
126
153
  */
127
154
  BaseClient.prototype.connect = function (options) {
128
155
  return __awaiter(this, void 0, void 0, function () {
@@ -133,7 +160,7 @@ var BaseClient = /** @class */ (function () {
133
160
  case 0:
134
161
  _a.trys.push([0, 2, , 3]);
135
162
  return [4 /*yield*/, connect(options).then(function (connection) {
136
- _this.natsConnection = connection;
163
+ _this._connection = connection;
137
164
  })];
138
165
  case 1:
139
166
  _a.sent();
@@ -155,8 +182,8 @@ var BaseClient = /** @class */ (function () {
155
182
  return __generator(this, function (_b) {
156
183
  switch (_b.label) {
157
184
  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) {
185
+ if (!this._connection) return [3 /*break*/, 2];
186
+ return [4 /*yield*/, ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.close().catch(function (error) {
160
187
  throw new Error("failed to close connection: ".concat(error));
161
188
  }))];
162
189
  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.74",
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",