@opentap/runner-client 1.0.0-alpha.37.12

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.
@@ -0,0 +1,44 @@
1
+ import { RequestOptions } from 'nats.ws';
2
+ export declare class BaseClient {
3
+ private _accessToken;
4
+ /** Get access token */
5
+ get accessToken(): string;
6
+ /** Set access token */
7
+ set accessToken(value: string);
8
+ private _timeout;
9
+ /** Get timeout */
10
+ get timeout(): number;
11
+ /** Set timeout */
12
+ set timeout(value: number);
13
+ private _natsServer;
14
+ /** Get nats server */
15
+ get natsServer(): string;
16
+ private _baseSubject;
17
+ /** Get subject */
18
+ get baseSubject(): string;
19
+ private domainAccess;
20
+ private natsConnection;
21
+ constructor(baseSubject: string, natsServer?: string);
22
+ /**
23
+ * Send a request to the nats server.
24
+ * @param subject The subject to request
25
+ * @param payload (optional)
26
+ * @param options (optional)
27
+ * @returns Promise of an object
28
+ */
29
+ protected request<T>(subject: string, payload?: any, options?: RequestOptions): Promise<T>;
30
+ /**
31
+ * Create a connection to the nats server.
32
+ */
33
+ private connect;
34
+ /**
35
+ * Close the connection.
36
+ */
37
+ private close;
38
+ /**
39
+ * Add a domain specific access token to the dictionary.
40
+ * @param domain
41
+ * @param accessToken
42
+ */
43
+ protected setDomainAccessToken(domain: string, accessToken: string): void;
44
+ }
@@ -0,0 +1,181 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { connect, Empty, JSONCodec, StringCodec } from 'nats.ws';
38
+ var BaseClient = /** @class */ (function () {
39
+ function BaseClient(baseSubject, natsServer) {
40
+ if (natsServer === void 0) { natsServer = 'ws://localhost:443'; }
41
+ this.domainAccess = new Map();
42
+ this._baseSubject = baseSubject;
43
+ this._natsServer = natsServer;
44
+ if (baseSubject && natsServer)
45
+ this.connect();
46
+ }
47
+ Object.defineProperty(BaseClient.prototype, "accessToken", {
48
+ /** Get access token */
49
+ get: function () {
50
+ return this._accessToken;
51
+ },
52
+ /** Set access token */
53
+ set: function (value) {
54
+ this._accessToken = value;
55
+ },
56
+ enumerable: false,
57
+ configurable: true
58
+ });
59
+ Object.defineProperty(BaseClient.prototype, "timeout", {
60
+ /** Get timeout */
61
+ get: function () {
62
+ return this._timeout;
63
+ },
64
+ /** Set timeout */
65
+ set: function (value) {
66
+ this._timeout = value;
67
+ },
68
+ enumerable: false,
69
+ configurable: true
70
+ });
71
+ Object.defineProperty(BaseClient.prototype, "natsServer", {
72
+ /** Get nats server */
73
+ get: function () {
74
+ return this._natsServer;
75
+ },
76
+ enumerable: false,
77
+ configurable: true
78
+ });
79
+ Object.defineProperty(BaseClient.prototype, "baseSubject", {
80
+ /** Get subject */
81
+ get: function () {
82
+ return this._baseSubject;
83
+ },
84
+ enumerable: false,
85
+ configurable: true
86
+ });
87
+ /**
88
+ * Send a request to the nats server.
89
+ * @param subject The subject to request
90
+ * @param payload (optional)
91
+ * @param options (optional)
92
+ * @returns Promise of an object
93
+ */
94
+ BaseClient.prototype.request = function (subject, payload, options) {
95
+ var _a;
96
+ return __awaiter(this, void 0, void 0, function () {
97
+ var stringCodec, data;
98
+ return __generator(this, function (_b) {
99
+ switch (_b.label) {
100
+ case 0:
101
+ if (!subject || ((_a = this.natsConnection) === null || _a === void 0 ? void 0 : _a.isClosed()))
102
+ return [2 /*return*/, Promise.reject()];
103
+ subject = "".concat(this.baseSubject, ".Request.").concat(subject);
104
+ stringCodec = StringCodec();
105
+ data = payload ? stringCodec.encode(JSON.stringify(payload)) : Empty;
106
+ return [4 /*yield*/, this.natsConnection
107
+ .request(subject, data, options)
108
+ .then(function (message) {
109
+ var jsonCodec = JSONCodec();
110
+ var responseData = jsonCodec.decode(message.data);
111
+ // TODO: implement ErrorResponse handling when server returns an error;
112
+ return Promise.resolve(responseData);
113
+ })
114
+ .catch(function (error) {
115
+ return Promise.reject(new Error("Request failed: ".concat(error)));
116
+ })];
117
+ case 1: return [2 /*return*/, _b.sent()];
118
+ }
119
+ });
120
+ });
121
+ };
122
+ /**
123
+ * Create a connection to the nats server.
124
+ */
125
+ BaseClient.prototype.connect = function () {
126
+ return __awaiter(this, void 0, void 0, function () {
127
+ var options, error_1;
128
+ var _this = this;
129
+ return __generator(this, function (_a) {
130
+ switch (_a.label) {
131
+ case 0:
132
+ _a.trys.push([0, 2, , 3]);
133
+ options = { servers: this._natsServer };
134
+ return [4 /*yield*/, connect(options).then(function (connection) {
135
+ _this.natsConnection = connection;
136
+ })];
137
+ case 1:
138
+ _a.sent();
139
+ return [3 /*break*/, 3];
140
+ case 2:
141
+ error_1 = _a.sent();
142
+ throw new Error("error connecting to ".concat(this.natsServer));
143
+ case 3: return [2 /*return*/];
144
+ }
145
+ });
146
+ });
147
+ };
148
+ /**
149
+ * Close the connection.
150
+ */
151
+ BaseClient.prototype.close = function () {
152
+ var _a;
153
+ return __awaiter(this, void 0, void 0, function () {
154
+ return __generator(this, function (_b) {
155
+ switch (_b.label) {
156
+ case 0:
157
+ if (!this.natsConnection) return [3 /*break*/, 2];
158
+ return [4 /*yield*/, ((_a = this.natsConnection) === null || _a === void 0 ? void 0 : _a.close().catch(function (error) {
159
+ throw new Error("failed to close connection: ".concat(error));
160
+ }))];
161
+ case 1:
162
+ _b.sent();
163
+ _b.label = 2;
164
+ case 2: return [2 /*return*/];
165
+ }
166
+ });
167
+ });
168
+ };
169
+ /**
170
+ * Add a domain specific access token to the dictionary.
171
+ * @param domain
172
+ * @param accessToken
173
+ */
174
+ BaseClient.prototype.setDomainAccessToken = function (domain, accessToken) {
175
+ if (!domain)
176
+ return;
177
+ this.domainAccess.set(domain, accessToken);
178
+ };
179
+ return BaseClient;
180
+ }());
181
+ export { BaseClient };