backend-manager 3.1.3 → 3.1.6

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": "backend-manager",
3
- "version": "3.1.3",
3
+ "version": "3.1.6",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -371,7 +371,9 @@ BackendAssistant.prototype.errorify = function (e, options) {
371
371
  ? e
372
372
  : new Error(stringify(e));
373
373
 
374
+ // Fix code
374
375
  options.code = newError.code || options.code;
376
+ options.code = isBetween(options.code, 400, 599) ? options.code : 500;
375
377
 
376
378
  // Attach properties
377
379
  _attachHeaderProperties(self, options, newError);
@@ -428,10 +430,12 @@ BackendAssistant.prototype.respond = function(response, options) {
428
430
  : options.log;
429
431
 
430
432
  // Handle error
433
+ const isErrorCode = isBetween(options.code, 400, 599);
431
434
  if (
432
435
  response instanceof Error
433
- || (options.code >= 400 && options.code <= 599)
436
+ || isErrorCode
434
437
  ) {
438
+ options.code = !isErrorCode ? undefined : options.code;
435
439
  return self.errorify(response, options);
436
440
  }
437
441
 
@@ -454,6 +458,10 @@ BackendAssistant.prototype.respond = function(response, options) {
454
458
  }
455
459
  }
456
460
 
461
+ function isBetween(value, min, max) {
462
+ return value >= min && value <= max;
463
+ }
464
+
457
465
  function stringify(e) {
458
466
  if (typeof e === 'string') {
459
467
  return e;
@@ -4,6 +4,7 @@
4
4
  */
5
5
 
6
6
  const path = require('path');
7
+ const powertools = require('node-powertools');
7
8
 
8
9
  function Middleware(m) {
9
10
  const self = this;
@@ -27,16 +28,20 @@ Middleware.prototype.run = function (library, req, res, options) {
27
28
  options = options || {};
28
29
  options.setupAnalytics = typeof options.setupAnalytics === 'boolean' ? options.setupAnalytics : true;
29
30
  options.setupUsage = typeof options.setupUsage === 'boolean' ? options.setupUsage : true;
31
+ options.authenticate = typeof options.authenticate === 'boolean' ? options.authenticate : true;
32
+ options.setupSettings = typeof options.setupSettings === 'undefined' ? true : options.setupSettings;
30
33
 
31
34
  // Log
32
35
  assistant.log(`Middleware.process(): Request (${geolocation.ip} @ ${geolocation.country}, ${geolocation.region}, ${geolocation.city})`, JSON.stringify(data));
33
36
 
37
+ const basePath = path.resolve(process.cwd(), library.replace('.js', ''));
38
+
34
39
  // Load library
35
40
  try {
36
- library = path.resolve(process.cwd(), `${library}.js`);
41
+ library = path.resolve(basePath, `methods/${basePath}/index.js`);
37
42
  library = new (require(library))();
38
43
  } catch (e) {
39
- assistant.errorify(`Unable to load library @ (${library}): ${e.message}`, {sentry: true, send: true, log: true});
44
+ return assistant.errorify(`Unable to load library @ (${library}): ${e.message}`, {sentry: true, send: true, log: true});
40
45
  }
41
46
 
42
47
  // Setup usage
@@ -44,6 +49,11 @@ Middleware.prototype.run = function (library, req, res, options) {
44
49
  assistant.usage = await Manager.Usage().init(assistant);
45
50
  }
46
51
 
52
+ // Setup user
53
+ if (!options.setupUsage && options.authenticate) {
54
+ await assistant.authenticate();
55
+ }
56
+
47
57
  // Setup analytics
48
58
  if (options.setupAnalytics) {
49
59
  const uuid = assistant?.usage?.user?.auth?.uid
@@ -56,6 +66,18 @@ Middleware.prototype.run = function (library, req, res, options) {
56
66
  })
57
67
  }
58
68
 
69
+ // Resolve settings
70
+ if (options.setupSettings) {
71
+ // try {
72
+ // const planId = assistant.request.user.plan.id;
73
+ // let settings = path.resolve(basePath, `methods/${basePath}/settings.js`);
74
+ // settings = require(settings)(assistant)[planId];
75
+ // assistant.request.data = powertools.defaults(data, settings);
76
+ // } catch (e) {
77
+ // return assistant.errorify(`Unable to resolve settings @ (${library}): ${e.message}`, {sentry: true, send: true, log: true});
78
+ // }
79
+ }
80
+
59
81
  // Process
60
82
  try {
61
83
  // Set properties
@@ -68,10 +90,10 @@ Middleware.prototype.run = function (library, req, res, options) {
68
90
  // return res.status(200).json(result);
69
91
  // })
70
92
  .catch(e => {
71
- assistant.errorify(e, {sentry: true, send: true, log: true});
93
+ return assistant.errorify(e, {sentry: true, send: true, log: true});
72
94
  });
73
95
  } catch (e) {
74
- assistant.errorify(e, {sentry: true, send: true, log: true});
96
+ return assistant.errorify(e, {sentry: true, send: true, log: true});
75
97
  }
76
98
  });
77
99
  };
@@ -12,7 +12,7 @@ function Settings(m) {
12
12
  self.initialized = false;
13
13
  }
14
14
 
15
- Settings.prototype.init = function (assistant, options) {
15
+ Settings.prototype.resolve = function (assistant, options) {
16
16
  const self = this;
17
17
 
18
18
  return new Promise(async function(resolve, reject) {