UNPKG

38.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 getApp(id, options) {
411 const body = {
412 id,
413 };
414 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
415 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
416 const fetchImpl = fetchImplementation || node_fetch_1.default;
417 let timeout;
418 if (timeoutMs) {
419 const controller = new abort_controller_1.default();
420 timeout = setTimeout(() => controller.abort(), timeoutMs);
421 fetchOptions.signal = controller.signal;
422 }
423 let response;
424 let responseBody;
425 let responseText;
426 let isJSON;
427 try {
428 response = await fetchImpl(`${serverUrl}/getApp`, Object.assign(Object.assign({}, fetchOptions), { headers: Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/json' }), body: JSON.stringify(body), method: 'POST' }));
429 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
430 if (isJSON) {
431 responseBody = await response.json();
432 }
433 else {
434 responseText = await response.text();
435 }
436 }
437 catch (err) {
438 if (err.message === 'The user aborted a request.') {
439 timeout = undefined;
440 throw new TimeoutError('Request aborted due to timeout', 'getApp', mergedOptions);
441 }
442 throw new RequestError(err.message, err, 'getApp', mergedOptions);
443 }
444 finally {
445 if (timeout)
446 clearTimeout(timeout);
447 }
448 if (response.status >= 200 && response.status < 300) {
449 const validator = this.validators.getApp;
450 const wrapped = { returns: responseBody }; // wrapped for coersion
451 if (!validator(wrapped)) {
452 throw new common_1.ValidationError('Failed to validate response', validator.errors);
453 }
454 return wrapped.returns;
455 }
456 else if (!isJSON) {
457 // fall through to throw
458 }
459 else if (response.status === 400) {
460 if (responseBody.name === 'ValidationError') {
461 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
462 }
463 }
464 else if (response.status === 500) {
465 if (responseBody.name === 'UnauthorizedError') {
466 throw new interfaces_1.UnauthorizedError(responseBody.message);
467 }
468 throw new interfaces_1.InternalServerError(responseBody.message);
469 }
470 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'getApp', mergedOptions);
471 }
472 async deployInitial(env, name, digest, envVars, options) {
473 const body = {
474 env,
475 name,
476 digest,
477 envVars,
478 };
479 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
480 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
481 const fetchImpl = fetchImplementation || node_fetch_1.default;
482 let timeout;
483 if (timeoutMs) {
484 const controller = new abort_controller_1.default();
485 timeout = setTimeout(() => controller.abort(), timeoutMs);
486 fetchOptions.signal = controller.signal;
487 }
488 let response;
489 let responseBody;
490 let responseText;
491 let isJSON;
492 try {
493 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' }));
494 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
495 if (isJSON) {
496 responseBody = await response.json();
497 }
498 else {
499 responseText = await response.text();
500 }
501 }
502 catch (err) {
503 if (err.message === 'The user aborted a request.') {
504 timeout = undefined;
505 throw new TimeoutError('Request aborted due to timeout', 'deployInitial', mergedOptions);
506 }
507 throw new RequestError(err.message, err, 'deployInitial', mergedOptions);
508 }
509 finally {
510 if (timeout)
511 clearTimeout(timeout);
512 }
513 if (response.status >= 200 && response.status < 300) {
514 const validator = this.validators.deployInitial;
515 const wrapped = { returns: responseBody }; // wrapped for coersion
516 if (!validator(wrapped)) {
517 throw new common_1.ValidationError('Failed to validate response', validator.errors);
518 }
519 return wrapped.returns;
520 }
521 else if (!isJSON) {
522 // fall through to throw
523 }
524 else if (response.status === 400) {
525 if (responseBody.name === 'ValidationError') {
526 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
527 }
528 }
529 else if (response.status === 500) {
530 if (responseBody.name === 'UnauthorizedError') {
531 throw new interfaces_1.UnauthorizedError(responseBody.message);
532 }
533 if (responseBody.name === 'ExistsError') {
534 throw new interfaces_1.ExistsError(responseBody.message);
535 }
536 throw new interfaces_1.InternalServerError(responseBody.message);
537 }
538 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'deployInitial', mergedOptions);
539 }
540 async deploy(appId, env, digest, envVars, options) {
541 const body = {
542 appId,
543 env,
544 digest,
545 envVars,
546 };
547 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
548 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
549 const fetchImpl = fetchImplementation || node_fetch_1.default;
550 let timeout;
551 if (timeoutMs) {
552 const controller = new abort_controller_1.default();
553 timeout = setTimeout(() => controller.abort(), timeoutMs);
554 fetchOptions.signal = controller.signal;
555 }
556 let response;
557 let responseBody;
558 let responseText;
559 let isJSON;
560 try {
561 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' }));
562 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
563 if (isJSON) {
564 responseBody = await response.json();
565 }
566 else {
567 responseText = await response.text();
568 }
569 }
570 catch (err) {
571 if (err.message === 'The user aborted a request.') {
572 timeout = undefined;
573 throw new TimeoutError('Request aborted due to timeout', 'deploy', mergedOptions);
574 }
575 throw new RequestError(err.message, err, 'deploy', mergedOptions);
576 }
577 finally {
578 if (timeout)
579 clearTimeout(timeout);
580 }
581 if (response.status >= 200 && response.status < 300) {
582 const validator = this.validators.deploy;
583 const wrapped = { returns: responseBody }; // wrapped for coersion
584 if (!validator(wrapped)) {
585 throw new common_1.ValidationError('Failed to validate response', validator.errors);
586 }
587 return wrapped.returns;
588 }
589 else if (!isJSON) {
590 // fall through to throw
591 }
592 else if (response.status === 400) {
593 if (responseBody.name === 'ValidationError') {
594 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
595 }
596 }
597 else if (response.status === 500) {
598 if (responseBody.name === 'UnauthorizedError') {
599 throw new interfaces_1.UnauthorizedError(responseBody.message);
600 }
601 if (responseBody.name === 'NotFoundError') {
602 throw new interfaces_1.NotFoundError(responseBody.message);
603 }
604 throw new interfaces_1.InternalServerError(responseBody.message);
605 }
606 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'deploy', mergedOptions);
607 }
608 async claimApp(token, options) {
609 const body = {
610 token,
611 };
612 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
613 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
614 const fetchImpl = fetchImplementation || node_fetch_1.default;
615 let timeout;
616 if (timeoutMs) {
617 const controller = new abort_controller_1.default();
618 timeout = setTimeout(() => controller.abort(), timeoutMs);
619 fetchOptions.signal = controller.signal;
620 }
621 let response;
622 let responseBody;
623 let responseText;
624 let isJSON;
625 try {
626 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' }));
627 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
628 if (isJSON) {
629 responseBody = await response.json();
630 }
631 else {
632 responseText = await response.text();
633 }
634 }
635 catch (err) {
636 if (err.message === 'The user aborted a request.') {
637 timeout = undefined;
638 throw new TimeoutError('Request aborted due to timeout', 'claimApp', mergedOptions);
639 }
640 throw new RequestError(err.message, err, 'claimApp', mergedOptions);
641 }
642 finally {
643 if (timeout)
644 clearTimeout(timeout);
645 }
646 if (response.status >= 200 && response.status < 300) {
647 const validator = this.validators.claimApp;
648 const wrapped = { returns: responseBody }; // wrapped for coersion
649 if (!validator(wrapped)) {
650 throw new common_1.ValidationError('Failed to validate response', validator.errors);
651 }
652 return wrapped.returns;
653 }
654 else if (!isJSON) {
655 // fall through to throw
656 }
657 else if (response.status === 400) {
658 if (responseBody.name === 'ValidationError') {
659 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
660 }
661 }
662 else if (response.status === 500) {
663 if (responseBody.name === 'UnauthorizedError') {
664 throw new interfaces_1.UnauthorizedError(responseBody.message);
665 }
666 if (responseBody.name === 'NotFoundError') {
667 throw new interfaces_1.NotFoundError(responseBody.message);
668 }
669 throw new interfaces_1.InternalServerError(responseBody.message);
670 }
671 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'claimApp', mergedOptions);
672 }
673 async getLogs(appId, env, opts, options) {
674 const body = {
675 appId,
676 env,
677 opts,
678 };
679 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
680 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
681 const fetchImpl = fetchImplementation || node_fetch_1.default;
682 let timeout;
683 if (timeoutMs) {
684 const controller = new abort_controller_1.default();
685 timeout = setTimeout(() => controller.abort(), timeoutMs);
686 fetchOptions.signal = controller.signal;
687 }
688 let response;
689 let responseBody;
690 let responseText;
691 let isJSON;
692 try {
693 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' }));
694 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
695 if (isJSON) {
696 responseBody = await response.json();
697 }
698 else {
699 responseText = await response.text();
700 }
701 }
702 catch (err) {
703 if (err.message === 'The user aborted a request.') {
704 timeout = undefined;
705 throw new TimeoutError('Request aborted due to timeout', 'getLogs', mergedOptions);
706 }
707 throw new RequestError(err.message, err, 'getLogs', mergedOptions);
708 }
709 finally {
710 if (timeout)
711 clearTimeout(timeout);
712 }
713 if (response.status >= 200 && response.status < 300) {
714 const validator = this.validators.getLogs;
715 const wrapped = { returns: responseBody }; // wrapped for coersion
716 if (!validator(wrapped)) {
717 throw new common_1.ValidationError('Failed to validate response', validator.errors);
718 }
719 return wrapped.returns;
720 }
721 else if (!isJSON) {
722 // fall through to throw
723 }
724 else if (response.status === 400) {
725 if (responseBody.name === 'ValidationError') {
726 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
727 }
728 }
729 else if (response.status === 500) {
730 if (responseBody.name === 'UnauthorizedError') {
731 throw new interfaces_1.UnauthorizedError(responseBody.message);
732 }
733 if (responseBody.name === 'NotFoundError') {
734 throw new interfaces_1.NotFoundError(responseBody.message);
735 }
736 throw new interfaces_1.InternalServerError(responseBody.message);
737 }
738 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'getLogs', mergedOptions);
739 }
740 async destroyApp(appId, options) {
741 const body = {
742 appId,
743 };
744 const mergedOptions = Object.assign(Object.assign({ serverUrl: this.serverUrl }, this.options), options);
745 const { fetchImplementation, timeoutMs, headers, serverUrl } = mergedOptions, fetchOptions = __rest(mergedOptions, ["fetchImplementation", "timeoutMs", "headers", "serverUrl"]);
746 const fetchImpl = fetchImplementation || node_fetch_1.default;
747 let timeout;
748 if (timeoutMs) {
749 const controller = new abort_controller_1.default();
750 timeout = setTimeout(() => controller.abort(), timeoutMs);
751 fetchOptions.signal = controller.signal;
752 }
753 let response;
754 let responseBody;
755 let responseText;
756 let isJSON;
757 try {
758 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' }));
759 isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
760 if (isJSON) {
761 responseBody = await response.json();
762 }
763 else {
764 responseText = await response.text();
765 }
766 }
767 catch (err) {
768 if (err.message === 'The user aborted a request.') {
769 timeout = undefined;
770 throw new TimeoutError('Request aborted due to timeout', 'destroyApp', mergedOptions);
771 }
772 throw new RequestError(err.message, err, 'destroyApp', mergedOptions);
773 }
774 finally {
775 if (timeout)
776 clearTimeout(timeout);
777 }
778 if (response.status >= 200 && response.status < 300) {
779 const validator = this.validators.destroyApp;
780 const wrapped = { returns: responseBody }; // wrapped for coersion
781 if (!validator(wrapped)) {
782 throw new common_1.ValidationError('Failed to validate response', validator.errors);
783 }
784 return wrapped.returns;
785 }
786 else if (!isJSON) {
787 // fall through to throw
788 }
789 else if (response.status === 400) {
790 if (responseBody.name === 'ValidationError') {
791 throw new common_1.ValidationError(responseBody.message, responseBody.errors);
792 }
793 }
794 else if (response.status === 500) {
795 if (responseBody.name === 'UnauthorizedError') {
796 throw new interfaces_1.UnauthorizedError(responseBody.message);
797 }
798 if (responseBody.name === 'NotFoundError') {
799 throw new interfaces_1.NotFoundError(responseBody.message);
800 }
801 throw new interfaces_1.InternalServerError(responseBody.message);
802 }
803 throw new RequestError(`${response.status} - ${response.statusText}`, { responseText: responseText && responseText.slice(0, 256), responseBody }, 'destroyApp', mergedOptions);
804 }
805}
806exports.LycanClient = LycanClient;
807LycanClient.methods = [
808 'createTicket',
809 'claimTicket',
810 'listTemplates',
811 'tryTemplate',
812 'whoami',
813 'listApps',
814 'getApp',
815 'deployInitial',
816 'deploy',
817 'claimApp',
818 'getLogs',
819 'destroyApp',
820];
821LycanClient.validators = common_1.createReturnTypeValidator(interfaces_1.schema, 'Lycan');
822//# sourceMappingURL=client.js.map
\No newline at end of file