UNPKG

35.2 kBJavaScriptView Raw
1"use strict";
2var __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};
13Object.defineProperty(exports, "__esModule", { value: true });
14// tslint:disable
15const node_fetch_1 = require("node-fetch");
16const abort_controller_1 = require("abort-controller");
17const common_1 = require("./common");
18exports.ValidationError = common_1.ValidationError;
19const interfaces_1 = require("./interfaces");
20class 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}
35exports.RequestError = RequestError;
36class 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}
44exports.TimeoutError = TimeoutError;
45class 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(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(Object.assign({}, fetchOptions), { headers: Object.assign(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 }; // wrapped for coersion
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 // fall through to throw
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(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(Object.assign({}, fetchOptions), { headers: Object.assign(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 }; // wrapped for coersion
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 // fall through to throw
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(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(Object.assign({}, fetchOptions), { headers: Object.assign(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 }; // wrapped for coersion
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 // fall through to throw
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(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(Object.assign({}, fetchOptions), { headers: Object.assign(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 }; // wrapped for coersion
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 // fall through to throw
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(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(Object.assign({}, fetchOptions), { headers: Object.assign(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 }; // wrapped for coersion
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 // fall through to throw
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(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(Object.assign({}, fetchOptions), { headers: Object.assign(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 }; // wrapped for coersion
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 // fall through to throw
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(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(Object.assign({}, fetchOptions), { headers: Object.assign(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 }; // wrapped for coersion
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 // fall through to throw
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(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(Object.assign({}, fetchOptions), { headers: Object.assign(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 }; // wrapped for coersion
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 // fall through to throw
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(token, options) {
547 const body = {
548 token,
549 };
550 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
551 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
552 const fetchImpl = fetchImplementation || node_fetch_1.default;
553 let timeout;
554 if (timeoutMs) {
555 const controller = new abort_controller_1.default();
556 timeout = setTimeout(() => controller.abort(), timeoutMs);
557 fetchOptions.signal = controller.signal;
558 }
559 let response;
560 let responseBody;
561 let responseText;
562 let isJSON;
563 try {
564 response = await fetchImpl(`${serverUrl}/claimApp`, Object.assign(Object.assign({}, fetchOptions), { headers: Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
565 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
566 if (isJSON) {
567 responseBody = await response.json();
568 }
569 else {
570 responseText = await response.text();
571 }
572 }
573 catch (err) {
574 if (err.message === 'The user aborted a request.') {
575 timeout = undefined;
576 throw new TimeoutError('Request aborted due to timeout', 'claimApp', mergedOptions);
577 }
578 throw new RequestError(err.message, err, 'claimApp', mergedOptions);
579 }
580 finally {
581 if (timeout)
582 clearTimeout(timeout);
583 }
584 if (response.status >= 200 && response.status < 300) {
585 const validator = this.validators.claimApp;
586 const wrapped = { returns: responseBody }; // wrapped for coersion
587 if (!validator(wrapped)) {
588 throw new common_1.ValidationError('Failed to validate response', validator.errors);
589 }
590 return wrapped.returns;
591 }
592 else if (!isJSON) {
593 // fall through to throw
594 }
595 else if (response.status === 400) {
596 if (responseBody.name === 'ValidationError') {
597 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
598 }
599 }
600 else if (response.status === 500) {
601 if (responseBody.name === 'UnauthorizedError') {
602 throw new interfaces_1.UnauthorizedError(responseBody.message);
603 }
604 if (responseBody.name === 'NotFoundError') {
605 throw new interfaces_1.NotFoundError(responseBody.message);
606 }
607 throw new interfaces_1.InternalServerError(responseBody.message);
608 }
609 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'claimApp', mergedOptions);
610 }
611 async getLogs(appId, env, opts, options) {
612 const body = {
613 appId,
614 env,
615 opts,
616 };
617 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
618 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
619 const fetchImpl = fetchImplementation || node_fetch_1.default;
620 let timeout;
621 if (timeoutMs) {
622 const controller = new abort_controller_1.default();
623 timeout = setTimeout(() => controller.abort(), timeoutMs);
624 fetchOptions.signal = controller.signal;
625 }
626 let response;
627 let responseBody;
628 let responseText;
629 let isJSON;
630 try {
631 response = await fetchImpl(`${serverUrl}/getLogs`, Object.assign(Object.assign({}, fetchOptions), { headers: Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
632 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
633 if (isJSON) {
634 responseBody = await response.json();
635 }
636 else {
637 responseText = await response.text();
638 }
639 }
640 catch (err) {
641 if (err.message === 'The user aborted a request.') {
642 timeout = undefined;
643 throw new TimeoutError('Request aborted due to timeout', 'getLogs', mergedOptions);
644 }
645 throw new RequestError(err.message, err, 'getLogs', mergedOptions);
646 }
647 finally {
648 if (timeout)
649 clearTimeout(timeout);
650 }
651 if (response.status >= 200 && response.status < 300) {
652 const validator = this.validators.getLogs;
653 const wrapped = { returns: responseBody }; // wrapped for coersion
654 if (!validator(wrapped)) {
655 throw new common_1.ValidationError('Failed to validate response', validator.errors);
656 }
657 return wrapped.returns;
658 }
659 else if (!isJSON) {
660 // fall through to throw
661 }
662 else if (response.status === 400) {
663 if (responseBody.name === 'ValidationError') {
664 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
665 }
666 }
667 else if (response.status === 500) {
668 if (responseBody.name === 'UnauthorizedError') {
669 throw new interfaces_1.UnauthorizedError(responseBody.message);
670 }
671 if (responseBody.name === 'NotFoundError') {
672 throw new interfaces_1.NotFoundError(responseBody.message);
673 }
674 throw new interfaces_1.InternalServerError(responseBody.message);
675 }
676 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'getLogs', mergedOptions);
677 }
678 async destroyApp(appId, options) {
679 const body = {
680 appId,
681 };
682 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
683 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
684 const fetchImpl = fetchImplementation || node_fetch_1.default;
685 let timeout;
686 if (timeoutMs) {
687 const controller = new abort_controller_1.default();
688 timeout = setTimeout(() => controller.abort(), timeoutMs);
689 fetchOptions.signal = controller.signal;
690 }
691 let response;
692 let responseBody;
693 let responseText;
694 let isJSON;
695 try {
696 response = await fetchImpl(`${serverUrl}/destroyApp`, Object.assign(Object.assign({}, fetchOptions), { headers: Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
697 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
698 if (isJSON) {
699 responseBody = await response.json();
700 }
701 else {
702 responseText = await response.text();
703 }
704 }
705 catch (err) {
706 if (err.message === 'The user aborted a request.') {
707 timeout = undefined;
708 throw new TimeoutError('Request aborted due to timeout', 'destroyApp', mergedOptions);
709 }
710 throw new RequestError(err.message, err, 'destroyApp', mergedOptions);
711 }
712 finally {
713 if (timeout)
714 clearTimeout(timeout);
715 }
716 if (response.status >= 200 && response.status < 300) {
717 const validator = this.validators.destroyApp;
718 const wrapped = { returns: responseBody }; // wrapped for coersion
719 if (!validator(wrapped)) {
720 throw new common_1.ValidationError('Failed to validate response', validator.errors);
721 }
722 return wrapped.returns;
723 }
724 else if (!isJSON) {
725 // fall through to throw
726 }
727 else if (response.status === 400) {
728 if (responseBody.name === 'ValidationError') {
729 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
730 }
731 }
732 else if (response.status === 500) {
733 if (responseBody.name === 'UnauthorizedError') {
734 throw new interfaces_1.UnauthorizedError(responseBody.message);
735 }
736 if (responseBody.name === 'NotFoundError') {
737 throw new interfaces_1.NotFoundError(responseBody.message);
738 }
739 throw new interfaces_1.InternalServerError(responseBody.message);
740 }
741 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'destroyApp', mergedOptions);
742 }
743}
744exports.LycanClient = LycanClient;
745LycanClient.methods = [
746 'createTicket',
747 'claimTicket',
748 'listTemplates',
749 'tryTemplate',
750 'whoami',
751 'listApps',
752 'deployInitial',
753 'deploy',
754 'claimApp',
755 'getLogs',
756 'destroyApp',
757];
758LycanClient.validators = common_1.createReturnTypeValidator(interfaces_1.schema, 'Lycan');
759//# sourceMappingURL=client.js.map
\No newline at end of file