@scout9/app 1.0.0-alpha.0.2.8 → 1.0.0-alpha.0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29483,7 +29483,7 @@ class Body {
29483
29483
  }
29484
29484
  const {
29485
29485
  toFormData
29486
- } = await Promise.resolve().then(function () { return require("./multipart-parser-ca328d7d.cjs"); });
29486
+ } = await Promise.resolve().then(function () { return require("./multipart-parser-46cd8f49.cjs"); });
29487
29487
  return toFormData(this.body, ct);
29488
29488
  }
29489
29489
 
@@ -41892,7 +41892,7 @@ function _loadUserPackageJson() {
41892
41892
  targetPkgUrl = isTest ? packageTestJsonUrl : packageJsonUrl;
41893
41893
  _context.t0 = JSON;
41894
41894
  _context.next = 10;
41895
- return fs__default["default"].readFile(new URL(targetPkgUrl, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-08dc13e8.js', document.baseURI).href))), 'utf-8');
41895
+ return fs__default["default"].readFile(new URL(targetPkgUrl, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-c41a1b55.js', document.baseURI).href))), 'utf-8');
41896
41896
  case 10:
41897
41897
  _context.t1 = _context.sent;
41898
41898
  pkg = _context.t0.parse.call(_context.t0, _context.t1);
@@ -43098,7 +43098,7 @@ var ProjectFiles = /*#__PURE__*/function () {
43098
43098
  return ProjectFiles;
43099
43099
  }();
43100
43100
 
43101
- var __filename$1 = node_url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-08dc13e8.js', document.baseURI).href)));
43101
+ var __filename$1 = node_url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-c41a1b55.js', document.baseURI).href)));
43102
43102
  var __dirname$1 = path__default["default"].dirname(__filename$1);
43103
43103
  function zipDirectory(source, out) {
43104
43104
  var archive = archiver$1('tar', {
@@ -43313,7 +43313,7 @@ function _buildApp() {
43313
43313
  case 11:
43314
43314
  _context4.t0 = JSON;
43315
43315
  _context4.next = 14;
43316
- return fs__default["default"].readFile(new URL(templatePackagePath, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-08dc13e8.js', document.baseURI).href))), 'utf-8');
43316
+ return fs__default["default"].readFile(new URL(templatePackagePath, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-c41a1b55.js', document.baseURI).href))), 'utf-8');
43317
43317
  case 14:
43318
43318
  _context4.t1 = _context4.sent;
43319
43319
  packageTemplate = _context4.t0.parse.call(_context4.t0, _context4.t1);
@@ -44421,6 +44421,15 @@ var Scout9Platform = {
44421
44421
  handleError: function handleError(e) {
44422
44422
  var error = coalesceToError(e);
44423
44423
  switch (error.name) {
44424
+ case 'AxiosError':
44425
+ var request = error.request,
44426
+ response = error.response;
44427
+ var path = request.path,
44428
+ method = request.method;
44429
+ var status = response.status,
44430
+ data = response.data;
44431
+ console.error(dev.$.red(dev.$.bold("".concat(method, " ").concat(path, " Scout9 ApiError (").concat(status, ")")) + ': ' + JSON.stringify(data)));
44432
+ throw new Error(JSON.stringify(data));
44424
44433
  case 'SyntaxError':
44425
44434
  throw error;
44426
44435
  case 'ZodError':
package/dist/index.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require("./index-08dc13e8.cjs");
5
+ var index = require("./index-c41a1b55.cjs");
6
6
  var dev = require("./dev-73437873.cjs");
7
7
  require("./spirits-76855e30.cjs");
8
8
  require("./macros-44f1f872.cjs");
@@ -2,7 +2,7 @@
2
2
 
3
3
  require('node:fs');
4
4
  require('node:path');
5
- var index = require("./index-08dc13e8.cjs");
5
+ var index = require("./index-c41a1b55.cjs");
6
6
  require("./spirits-76855e30.cjs");
7
7
  require("./dev-73437873.cjs");
8
8
  require('util');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scout9/app",
3
- "version": "1.0.0-alpha.0.2.8",
3
+ "version": "1.0.0-alpha.0.3.0",
4
4
  "description": "Build and deploy your Scout9 app for SMS auto replies",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -358,14 +358,17 @@ async function runEntityApi(req, res) {
358
358
  searchParams: req?.query || {}, body: req?.body || undefined,
359
359
  id: params.id
360
360
  });
361
- if (response instanceof EventResponse && !!response.body) {
361
+ if (response instanceof EventResponse || !!response.body) {
362
+ const data = response.body ?? response.data();
362
363
  res.writeHead(response.status || 200, {'Content-Type': 'application/json'});
363
- res.end(JSON.stringify(response.body));
364
+ res.end(JSON.stringify(data));
365
+ console.log(`${req.method} EntityApi.${params.id}:`);
366
+ console.log(colors.grey(JSON.stringify(data)));
364
367
  } else {
365
368
  throw new Error(`Invalid response: not an EventResponse`);
366
369
  }
367
370
  } catch (e) {
368
- console.error(e);
371
+ console.error(`${req.method} EntityApi Runtime Error`, e.message);
369
372
  res.writeHead(500, {'Content-Type': 'application/json'});
370
373
  res.end(JSON.stringify({error: e.message}));
371
374
  }
@@ -399,55 +402,72 @@ async function runCommandApi(req, res) {
399
402
  file = files?.[0];
400
403
  } catch (e) {
401
404
  console.log('No commands found', e.message);
405
+ res.writeHead(404, {'Content-Type': 'application/json'});
406
+ res.end(JSON.stringify({error: `No commands found`}));
407
+ return;
402
408
  }
403
409
 
404
-
405
410
  if (!file) {
406
- throw new Error(`Unable to find command for ${url}`);
411
+ res.writeHead(404, {'Content-Type': 'application/json'});
412
+ res.end(JSON.stringify({error: `Unable to find command for ${url}`}));
413
+ return;
407
414
  }
408
415
 
409
416
  let mod;
410
417
  try {
411
418
  mod = await import(pathToFileURL(path.resolve(commandsDir, file)).href);
412
- console.log(mod);
413
419
  } catch (e) {
414
420
  if ('code' in e) {
415
421
  switch (e.code) {
416
422
  case 'ERR_MODULE_NOT_FOUND':
417
423
  case 'MODULE_NOT_FOUND':
418
424
  console.error(e);
419
- throw new Error(`Invalid command: no API method`);
425
+ res.writeHead(404, {'Content-Type': 'application/json'});
426
+ res.end(JSON.stringify({error: `Invalid command: no API method found`}));
427
+ return;
420
428
  }
421
429
  }
422
430
  console.error(e);
423
- throw new Error(`Invalid command: Internal system error`);
431
+ res.writeHead(500, {'Content-Type': 'application/json'});
432
+ res.end(JSON.stringify({error: `Invalid command: Internal system error: ${e?.message ?? ''}`}));
433
+ return;
424
434
  }
425
435
 
426
436
  if (!mod || !mod.default) {
427
- throw new Error(`Command file "${file}" does not export a default command function`);
437
+ res.writeHead(500, {'Content-Type': 'application/json'});
438
+ res.end(JSON.stringify({error: `Command file "${file}" does not export a default command function`}));
439
+ return;
428
440
  }
429
441
 
430
- console.log(mod);
431
-
432
442
  let result;
433
443
 
434
444
  try {
435
445
  result = await mod.default(body);
436
446
  } catch (e) {
437
447
  console.error('Failed to run command', e);
438
- throw new Error(`Failed to run command: ${e.message}`);
448
+ res.writeHead(500, {'Content-Type': 'application/json'});
449
+ res.end(JSON.stringify({error: `Failed to run command: ${e.message}`}));
450
+ return;
439
451
  }
440
452
 
453
+ let responseBody = {};
454
+ let code = 500;
441
455
  if (result) {
442
456
  if (typeof result === 'string') {
443
- return {message: result};
457
+ responseBody = {message: result};
458
+ code = 200;
444
459
  } else if (typeof result === 'object' && 'message' in result) {
445
- return result;
460
+ responseBody = result;
461
+ code = 200;
446
462
  } else {
447
- throw new Error(`Invalid Command Response, must either return a string or {"message": "<your message>"}`);
463
+ responseBody.error = `Invalid Command Response, must either return a string or {"message": "<your message>"}`;
448
464
  }
465
+ } else {
466
+ responseBody.error = `No command response provided`;
449
467
  }
450
- return {message: `${mod.default.name} Complete`};
468
+
469
+ res.writeHead(code, {'Content-Type': 'application/json'});
470
+ res.end(JSON.stringify(responseBody));
451
471
  }
452
472
 
453
473
  app.post('/commands/:command', runCommandApi);
@@ -517,7 +537,8 @@ if (dev) {
517
537
  for (const [key, value] of Object.entries(payload.context)) {
518
538
  fields += `\n\t\t${colors.bold(colors.white(key))}: ${colors.grey(JSON.stringify(value))}`;
519
539
  }
520
- console.log(`\tParsed in ${payload.ms}ms:${colors.grey(`${fields}`)}`);
540
+ console.log(`\tParsed in ${payload.ms}ms:${colors.grey(`${fields}`)}:`);
541
+ console.log(colors.grey(JSON.stringify(payload)));
521
542
  res.writeHead(200, {'Content-Type': 'application/json'});
522
543
  res.end(JSON.stringify(payload));
523
544
  } catch (e) {
package/src/platform.js CHANGED
@@ -215,6 +215,12 @@ export const Scout9Platform = {
215
215
  const error = coalesceToError(e);
216
216
 
217
217
  switch (error.name) {
218
+ case 'AxiosError':
219
+ const {request, response} = error;
220
+ const { path, method } = request;
221
+ const {status, data} = response;
222
+ console.error(colors.red(colors.bold(`${method} ${path} Scout9 ApiError (${status})`) + ': ' + JSON.stringify(data)));
223
+ throw new Error(JSON.stringify(data));
218
224
  case 'SyntaxError':
219
225
  throw error;
220
226
  case 'ZodError':