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

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.
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.2.9",
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) {