1 | "use strict";
|
2 | var __rest = (this && this.__rest) || function (s, e) {
|
3 | var t = {};
|
4 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
5 | t[p] = s[p];
|
6 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
7 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
8 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
9 | t[p[i]] = s[p[i]];
|
10 | }
|
11 | return t;
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 |
|
15 | const node_fetch_1 = require("node-fetch");
|
16 | const abort_controller_1 = require("abort-controller");
|
17 | const common_1 = require("./common");
|
18 | exports.ValidationError = common_1.ValidationError;
|
19 | const interfaces_1 = require("./interfaces");
|
20 | class RequestError extends Error {
|
21 | constructor(message,
|
22 | /**
|
23 | * The original error causing this request to fail
|
24 | * Inherits Error in case of network or parse errors
|
25 | * In case of an invalid HTTP response it will contain an object with the body/trimmed text of the response
|
26 | */
|
27 | cause, method, options) {
|
28 | super(message);
|
29 | this.cause = cause;
|
30 | this.method = method;
|
31 | this.options = options;
|
32 | this.name = 'RequestError';
|
33 | }
|
34 | }
|
35 | exports.RequestError = RequestError;
|
36 | class TimeoutError extends Error {
|
37 | constructor(message, method, options) {
|
38 | super(message);
|
39 | this.method = method;
|
40 | this.options = options;
|
41 | this.name = 'TimeoutError';
|
42 | }
|
43 | }
|
44 | exports.TimeoutError = TimeoutError;
|
45 | class LycanClient {
|
46 | constructor(serverUrl, options = {}) {
|
47 | this.serverUrl = serverUrl;
|
48 | this.options = options;
|
49 | this.props = interfaces_1.schema.definitions.Lycan.properties;
|
50 | this.validators = LycanClient.validators;
|
51 | }
|
52 | async createTicket(options) {
|
53 | const body = {};
|
54 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
55 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
56 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
57 | let timeout;
|
58 | if (timeoutMs) {
|
59 | const controller = new abort_controller_1.default();
|
60 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
61 | fetchOptions.signal = controller.signal;
|
62 | }
|
63 | let response;
|
64 | let responseBody;
|
65 | let responseText;
|
66 | let isJSON;
|
67 | try {
|
68 | response = await fetchImpl(`${serverUrl}/createTicket`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
69 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
70 | if (isJSON) {
|
71 | responseBody = await response.json();
|
72 | }
|
73 | else {
|
74 | responseText = await response.text();
|
75 | }
|
76 | }
|
77 | catch (err) {
|
78 | if (err.message === 'The user aborted a request.') {
|
79 | timeout = undefined;
|
80 | throw new TimeoutError('Request aborted due to timeout', 'createTicket', mergedOptions);
|
81 | }
|
82 | throw new RequestError(err.message, err, 'createTicket', mergedOptions);
|
83 | }
|
84 | finally {
|
85 | if (timeout)
|
86 | clearTimeout(timeout);
|
87 | }
|
88 | if (response.status >= 200 && response.status < 300) {
|
89 | const validator = this.validators.createTicket;
|
90 | const wrapped = { returns: responseBody };
|
91 | if (!validator(wrapped)) {
|
92 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
93 | }
|
94 | return wrapped.returns;
|
95 | }
|
96 | else if (!isJSON) {
|
97 |
|
98 | }
|
99 | else if (response.status === 400) {
|
100 | if (responseBody.name === 'ValidationError') {
|
101 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
102 | }
|
103 | }
|
104 | else if (response.status === 500) {
|
105 | throw new interfaces_1.InternalServerError(responseBody.message);
|
106 | }
|
107 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'createTicket', mergedOptions);
|
108 | }
|
109 | async claimTicket(ticket, options) {
|
110 | const body = {
|
111 | ticket,
|
112 | };
|
113 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
114 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
115 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
116 | let timeout;
|
117 | if (timeoutMs) {
|
118 | const controller = new abort_controller_1.default();
|
119 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
120 | fetchOptions.signal = controller.signal;
|
121 | }
|
122 | let response;
|
123 | let responseBody;
|
124 | let responseText;
|
125 | let isJSON;
|
126 | try {
|
127 | response = await fetchImpl(`${serverUrl}/claimTicket`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
128 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
129 | if (isJSON) {
|
130 | responseBody = await response.json();
|
131 | }
|
132 | else {
|
133 | responseText = await response.text();
|
134 | }
|
135 | }
|
136 | catch (err) {
|
137 | if (err.message === 'The user aborted a request.') {
|
138 | timeout = undefined;
|
139 | throw new TimeoutError('Request aborted due to timeout', 'claimTicket', mergedOptions);
|
140 | }
|
141 | throw new RequestError(err.message, err, 'claimTicket', mergedOptions);
|
142 | }
|
143 | finally {
|
144 | if (timeout)
|
145 | clearTimeout(timeout);
|
146 | }
|
147 | if (response.status >= 200 && response.status < 300) {
|
148 | const validator = this.validators.claimTicket;
|
149 | const wrapped = { returns: responseBody };
|
150 | if (!validator(wrapped)) {
|
151 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
152 | }
|
153 | return wrapped.returns;
|
154 | }
|
155 | else if (!isJSON) {
|
156 |
|
157 | }
|
158 | else if (response.status === 400) {
|
159 | if (responseBody.name === 'ValidationError') {
|
160 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
161 | }
|
162 | }
|
163 | else if (response.status === 500) {
|
164 | if (responseBody.name === 'NotFoundError') {
|
165 | throw new interfaces_1.NotFoundError(responseBody.message);
|
166 | }
|
167 | throw new interfaces_1.InternalServerError(responseBody.message);
|
168 | }
|
169 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'claimTicket', mergedOptions);
|
170 | }
|
171 | async listTemplates(options) {
|
172 | const body = {};
|
173 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
174 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
175 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
176 | let timeout;
|
177 | if (timeoutMs) {
|
178 | const controller = new abort_controller_1.default();
|
179 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
180 | fetchOptions.signal = controller.signal;
|
181 | }
|
182 | let response;
|
183 | let responseBody;
|
184 | let responseText;
|
185 | let isJSON;
|
186 | try {
|
187 | response = await fetchImpl(`${serverUrl}/listTemplates`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
188 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
189 | if (isJSON) {
|
190 | responseBody = await response.json();
|
191 | }
|
192 | else {
|
193 | responseText = await response.text();
|
194 | }
|
195 | }
|
196 | catch (err) {
|
197 | if (err.message === 'The user aborted a request.') {
|
198 | timeout = undefined;
|
199 | throw new TimeoutError('Request aborted due to timeout', 'listTemplates', mergedOptions);
|
200 | }
|
201 | throw new RequestError(err.message, err, 'listTemplates', mergedOptions);
|
202 | }
|
203 | finally {
|
204 | if (timeout)
|
205 | clearTimeout(timeout);
|
206 | }
|
207 | if (response.status >= 200 && response.status < 300) {
|
208 | const validator = this.validators.listTemplates;
|
209 | const wrapped = { returns: responseBody };
|
210 | if (!validator(wrapped)) {
|
211 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
212 | }
|
213 | return wrapped.returns;
|
214 | }
|
215 | else if (!isJSON) {
|
216 |
|
217 | }
|
218 | else if (response.status === 400) {
|
219 | if (responseBody.name === 'ValidationError') {
|
220 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
221 | }
|
222 | }
|
223 | else if (response.status === 500) {
|
224 | throw new interfaces_1.InternalServerError(responseBody.message);
|
225 | }
|
226 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'listTemplates', mergedOptions);
|
227 | }
|
228 | async tryTemplate(id, options) {
|
229 | const body = {
|
230 | id,
|
231 | };
|
232 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
233 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
234 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
235 | let timeout;
|
236 | if (timeoutMs) {
|
237 | const controller = new abort_controller_1.default();
|
238 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
239 | fetchOptions.signal = controller.signal;
|
240 | }
|
241 | let response;
|
242 | let responseBody;
|
243 | let responseText;
|
244 | let isJSON;
|
245 | try {
|
246 | response = await fetchImpl(`${serverUrl}/tryTemplate`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
247 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
248 | if (isJSON) {
|
249 | responseBody = await response.json();
|
250 | }
|
251 | else {
|
252 | responseText = await response.text();
|
253 | }
|
254 | }
|
255 | catch (err) {
|
256 | if (err.message === 'The user aborted a request.') {
|
257 | timeout = undefined;
|
258 | throw new TimeoutError('Request aborted due to timeout', 'tryTemplate', mergedOptions);
|
259 | }
|
260 | throw new RequestError(err.message, err, 'tryTemplate', mergedOptions);
|
261 | }
|
262 | finally {
|
263 | if (timeout)
|
264 | clearTimeout(timeout);
|
265 | }
|
266 | if (response.status >= 200 && response.status < 300) {
|
267 | const validator = this.validators.tryTemplate;
|
268 | const wrapped = { returns: responseBody };
|
269 | if (!validator(wrapped)) {
|
270 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
271 | }
|
272 | return wrapped.returns;
|
273 | }
|
274 | else if (!isJSON) {
|
275 |
|
276 | }
|
277 | else if (response.status === 400) {
|
278 | if (responseBody.name === 'ValidationError') {
|
279 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
280 | }
|
281 | }
|
282 | else if (response.status === 500) {
|
283 | if (responseBody.name === 'NotFoundError') {
|
284 | throw new interfaces_1.NotFoundError(responseBody.message);
|
285 | }
|
286 | throw new interfaces_1.InternalServerError(responseBody.message);
|
287 | }
|
288 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'tryTemplate', mergedOptions);
|
289 | }
|
290 | async whoami(options) {
|
291 | const body = {};
|
292 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
293 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
294 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
295 | let timeout;
|
296 | if (timeoutMs) {
|
297 | const controller = new abort_controller_1.default();
|
298 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
299 | fetchOptions.signal = controller.signal;
|
300 | }
|
301 | let response;
|
302 | let responseBody;
|
303 | let responseText;
|
304 | let isJSON;
|
305 | try {
|
306 | response = await fetchImpl(`${serverUrl}/whoami`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
307 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
308 | if (isJSON) {
|
309 | responseBody = await response.json();
|
310 | }
|
311 | else {
|
312 | responseText = await response.text();
|
313 | }
|
314 | }
|
315 | catch (err) {
|
316 | if (err.message === 'The user aborted a request.') {
|
317 | timeout = undefined;
|
318 | throw new TimeoutError('Request aborted due to timeout', 'whoami', mergedOptions);
|
319 | }
|
320 | throw new RequestError(err.message, err, 'whoami', mergedOptions);
|
321 | }
|
322 | finally {
|
323 | if (timeout)
|
324 | clearTimeout(timeout);
|
325 | }
|
326 | if (response.status >= 200 && response.status < 300) {
|
327 | const validator = this.validators.whoami;
|
328 | const wrapped = { returns: responseBody };
|
329 | if (!validator(wrapped)) {
|
330 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
331 | }
|
332 | return wrapped.returns;
|
333 | }
|
334 | else if (!isJSON) {
|
335 |
|
336 | }
|
337 | else if (response.status === 400) {
|
338 | if (responseBody.name === 'ValidationError') {
|
339 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
340 | }
|
341 | }
|
342 | else if (response.status === 500) {
|
343 | if (responseBody.name === 'UnauthorizedError') {
|
344 | throw new interfaces_1.UnauthorizedError(responseBody.message);
|
345 | }
|
346 | throw new interfaces_1.InternalServerError(responseBody.message);
|
347 | }
|
348 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'whoami', mergedOptions);
|
349 | }
|
350 | async listApps(options) {
|
351 | const body = {};
|
352 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
353 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
354 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
355 | let timeout;
|
356 | if (timeoutMs) {
|
357 | const controller = new abort_controller_1.default();
|
358 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
359 | fetchOptions.signal = controller.signal;
|
360 | }
|
361 | let response;
|
362 | let responseBody;
|
363 | let responseText;
|
364 | let isJSON;
|
365 | try {
|
366 | response = await fetchImpl(`${serverUrl}/listApps`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
367 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
368 | if (isJSON) {
|
369 | responseBody = await response.json();
|
370 | }
|
371 | else {
|
372 | responseText = await response.text();
|
373 | }
|
374 | }
|
375 | catch (err) {
|
376 | if (err.message === 'The user aborted a request.') {
|
377 | timeout = undefined;
|
378 | throw new TimeoutError('Request aborted due to timeout', 'listApps', mergedOptions);
|
379 | }
|
380 | throw new RequestError(err.message, err, 'listApps', mergedOptions);
|
381 | }
|
382 | finally {
|
383 | if (timeout)
|
384 | clearTimeout(timeout);
|
385 | }
|
386 | if (response.status >= 200 && response.status < 300) {
|
387 | const validator = this.validators.listApps;
|
388 | const wrapped = { returns: responseBody };
|
389 | if (!validator(wrapped)) {
|
390 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
391 | }
|
392 | return wrapped.returns;
|
393 | }
|
394 | else if (!isJSON) {
|
395 |
|
396 | }
|
397 | else if (response.status === 400) {
|
398 | if (responseBody.name === 'ValidationError') {
|
399 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
400 | }
|
401 | }
|
402 | else if (response.status === 500) {
|
403 | if (responseBody.name === 'UnauthorizedError') {
|
404 | throw new interfaces_1.UnauthorizedError(responseBody.message);
|
405 | }
|
406 | throw new interfaces_1.InternalServerError(responseBody.message);
|
407 | }
|
408 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'listApps', mergedOptions);
|
409 | }
|
410 | async deployInitial(env, name, digest, envVars, options) {
|
411 | const body = {
|
412 | env,
|
413 | name,
|
414 | digest,
|
415 | envVars,
|
416 | };
|
417 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
418 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
419 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
420 | let timeout;
|
421 | if (timeoutMs) {
|
422 | const controller = new abort_controller_1.default();
|
423 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
424 | fetchOptions.signal = controller.signal;
|
425 | }
|
426 | let response;
|
427 | let responseBody;
|
428 | let responseText;
|
429 | let isJSON;
|
430 | try {
|
431 | response = await fetchImpl(`${serverUrl}/deployInitial`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
432 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
433 | if (isJSON) {
|
434 | responseBody = await response.json();
|
435 | }
|
436 | else {
|
437 | responseText = await response.text();
|
438 | }
|
439 | }
|
440 | catch (err) {
|
441 | if (err.message === 'The user aborted a request.') {
|
442 | timeout = undefined;
|
443 | throw new TimeoutError('Request aborted due to timeout', 'deployInitial', mergedOptions);
|
444 | }
|
445 | throw new RequestError(err.message, err, 'deployInitial', mergedOptions);
|
446 | }
|
447 | finally {
|
448 | if (timeout)
|
449 | clearTimeout(timeout);
|
450 | }
|
451 | if (response.status >= 200 && response.status < 300) {
|
452 | const validator = this.validators.deployInitial;
|
453 | const wrapped = { returns: responseBody };
|
454 | if (!validator(wrapped)) {
|
455 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
456 | }
|
457 | return wrapped.returns;
|
458 | }
|
459 | else if (!isJSON) {
|
460 |
|
461 | }
|
462 | else if (response.status === 400) {
|
463 | if (responseBody.name === 'ValidationError') {
|
464 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
465 | }
|
466 | }
|
467 | else if (response.status === 500) {
|
468 | if (responseBody.name === 'UnauthorizedError') {
|
469 | throw new interfaces_1.UnauthorizedError(responseBody.message);
|
470 | }
|
471 | if (responseBody.name === 'ExistsError') {
|
472 | throw new interfaces_1.ExistsError(responseBody.message);
|
473 | }
|
474 | throw new interfaces_1.InternalServerError(responseBody.message);
|
475 | }
|
476 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'deployInitial', mergedOptions);
|
477 | }
|
478 | async deploy(appId, env, digest, envVars, options) {
|
479 | const body = {
|
480 | appId,
|
481 | env,
|
482 | digest,
|
483 | envVars,
|
484 | };
|
485 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
486 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
487 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
488 | let timeout;
|
489 | if (timeoutMs) {
|
490 | const controller = new abort_controller_1.default();
|
491 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
492 | fetchOptions.signal = controller.signal;
|
493 | }
|
494 | let response;
|
495 | let responseBody;
|
496 | let responseText;
|
497 | let isJSON;
|
498 | try {
|
499 | response = await fetchImpl(`${serverUrl}/deploy`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
500 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
501 | if (isJSON) {
|
502 | responseBody = await response.json();
|
503 | }
|
504 | else {
|
505 | responseText = await response.text();
|
506 | }
|
507 | }
|
508 | catch (err) {
|
509 | if (err.message === 'The user aborted a request.') {
|
510 | timeout = undefined;
|
511 | throw new TimeoutError('Request aborted due to timeout', 'deploy', mergedOptions);
|
512 | }
|
513 | throw new RequestError(err.message, err, 'deploy', mergedOptions);
|
514 | }
|
515 | finally {
|
516 | if (timeout)
|
517 | clearTimeout(timeout);
|
518 | }
|
519 | if (response.status >= 200 && response.status < 300) {
|
520 | const validator = this.validators.deploy;
|
521 | const wrapped = { returns: responseBody };
|
522 | if (!validator(wrapped)) {
|
523 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
524 | }
|
525 | return wrapped.returns;
|
526 | }
|
527 | else if (!isJSON) {
|
528 |
|
529 | }
|
530 | else if (response.status === 400) {
|
531 | if (responseBody.name === 'ValidationError') {
|
532 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
533 | }
|
534 | }
|
535 | else if (response.status === 500) {
|
536 | if (responseBody.name === 'UnauthorizedError') {
|
537 | throw new interfaces_1.UnauthorizedError(responseBody.message);
|
538 | }
|
539 | if (responseBody.name === 'NotFoundError') {
|
540 | throw new interfaces_1.NotFoundError(responseBody.message);
|
541 | }
|
542 | throw new interfaces_1.InternalServerError(responseBody.message);
|
543 | }
|
544 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'deploy', mergedOptions);
|
545 | }
|
546 | async claimApp(accountId, appId, options) {
|
547 | const body = {
|
548 | accountId,
|
549 | appId,
|
550 | };
|
551 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
552 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
553 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
554 | let timeout;
|
555 | if (timeoutMs) {
|
556 | const controller = new abort_controller_1.default();
|
557 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
558 | fetchOptions.signal = controller.signal;
|
559 | }
|
560 | let response;
|
561 | let responseBody;
|
562 | let responseText;
|
563 | let isJSON;
|
564 | try {
|
565 | response = await fetchImpl(`${serverUrl}/claimApp`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
566 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
567 | if (isJSON) {
|
568 | responseBody = await response.json();
|
569 | }
|
570 | else {
|
571 | responseText = await response.text();
|
572 | }
|
573 | }
|
574 | catch (err) {
|
575 | if (err.message === 'The user aborted a request.') {
|
576 | timeout = undefined;
|
577 | throw new TimeoutError('Request aborted due to timeout', 'claimApp', mergedOptions);
|
578 | }
|
579 | throw new RequestError(err.message, err, 'claimApp', mergedOptions);
|
580 | }
|
581 | finally {
|
582 | if (timeout)
|
583 | clearTimeout(timeout);
|
584 | }
|
585 | if (response.status >= 200 && response.status < 300) {
|
586 | const validator = this.validators.claimApp;
|
587 | const wrapped = { returns: responseBody };
|
588 | if (!validator(wrapped)) {
|
589 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
590 | }
|
591 | return wrapped.returns;
|
592 | }
|
593 | else if (!isJSON) {
|
594 |
|
595 | }
|
596 | else if (response.status === 400) {
|
597 | if (responseBody.name === 'ValidationError') {
|
598 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
599 | }
|
600 | }
|
601 | else if (response.status === 500) {
|
602 | if (responseBody.name === 'UnauthorizedError') {
|
603 | throw new interfaces_1.UnauthorizedError(responseBody.message);
|
604 | }
|
605 | if (responseBody.name === 'NotFoundError') {
|
606 | throw new interfaces_1.NotFoundError(responseBody.message);
|
607 | }
|
608 | throw new interfaces_1.InternalServerError(responseBody.message);
|
609 | }
|
610 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'claimApp', mergedOptions);
|
611 | }
|
612 | async getLogs(appId, env, opts, options) {
|
613 | const body = {
|
614 | appId,
|
615 | env,
|
616 | opts,
|
617 | };
|
618 | const mergedOptions = Object.assign({ serverUrl: this.serverUrl }, this.options, options);
|
619 | const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
|
620 | const fetchImpl = fetchImplementation || node_fetch_1.default;
|
621 | let timeout;
|
622 | if (timeoutMs) {
|
623 | const controller = new abort_controller_1.default();
|
624 | timeout = setTimeout(() => controller.abort(), timeoutMs);
|
625 | fetchOptions.signal = controller.signal;
|
626 | }
|
627 | let response;
|
628 | let responseBody;
|
629 | let responseText;
|
630 | let isJSON;
|
631 | try {
|
632 | response = await fetchImpl(`${serverUrl}/getLogs`, Object.assign({}, fetchOptions, { headers: Object.assign({}, headers, { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
|
633 | isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
|
634 | if (isJSON) {
|
635 | responseBody = await response.json();
|
636 | }
|
637 | else {
|
638 | responseText = await response.text();
|
639 | }
|
640 | }
|
641 | catch (err) {
|
642 | if (err.message === 'The user aborted a request.') {
|
643 | timeout = undefined;
|
644 | throw new TimeoutError('Request aborted due to timeout', 'getLogs', mergedOptions);
|
645 | }
|
646 | throw new RequestError(err.message, err, 'getLogs', mergedOptions);
|
647 | }
|
648 | finally {
|
649 | if (timeout)
|
650 | clearTimeout(timeout);
|
651 | }
|
652 | if (response.status >= 200 && response.status < 300) {
|
653 | const validator = this.validators.getLogs;
|
654 | const wrapped = { returns: responseBody };
|
655 | if (!validator(wrapped)) {
|
656 | throw new common_1.ValidationError('Failed to validate response', validator.errors);
|
657 | }
|
658 | return wrapped.returns;
|
659 | }
|
660 | else if (!isJSON) {
|
661 |
|
662 | }
|
663 | else if (response.status === 400) {
|
664 | if (responseBody.name === 'ValidationError') {
|
665 | throw new common_1.ValidationError(responseBody.message, responseBody.errors);
|
666 | }
|
667 | }
|
668 | else if (response.status === 500) {
|
669 | if (responseBody.name === 'UnauthorizedError') {
|
670 | throw new interfaces_1.UnauthorizedError(responseBody.message);
|
671 | }
|
672 | if (responseBody.name === 'NotFoundError') {
|
673 | throw new interfaces_1.NotFoundError(responseBody.message);
|
674 | }
|
675 | throw new interfaces_1.InternalServerError(responseBody.message);
|
676 | }
|
677 | throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'getLogs', mergedOptions);
|
678 | }
|
679 | }
|
680 | LycanClient.methods = [
|
681 | 'createTicket',
|
682 | 'claimTicket',
|
683 | 'listTemplates',
|
684 | 'tryTemplate',
|
685 | 'whoami',
|
686 | 'listApps',
|
687 | 'deployInitial',
|
688 | 'deploy',
|
689 | 'claimApp',
|
690 | 'getLogs',
|
691 | ];
|
692 | LycanClient.validators = common_1.createReturnTypeValidator(interfaces_1.schema, 'Lycan');
|
693 | exports.LycanClient = LycanClient;
|
694 |
|
\ | No newline at end of file |