backend-manager 2.3.16 → 2.3.19

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": "2.3.16",
3
+ "version": "2.3.19",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -0,0 +1,29 @@
1
+ const _ = require('lodash')
2
+
3
+ function Module() {
4
+
5
+ }
6
+
7
+ Module.prototype.main = function () {
8
+ const self = this;
9
+ const Manager = self.Manager;
10
+ const Api = self.Api;
11
+ const assistant = self.assistant;
12
+ const payload = self.payload;
13
+
14
+ return new Promise(async function(resolve, reject) {
15
+ self.Api.resolveUser({adminRequired: true})
16
+ .then(async (user) => {
17
+ // return resolve({data: {success: true}});
18
+ // return reject(assistant.errorManager(`Failed to delete user: ${e}`, {code: 400, sentry: false, send: false, log: false}).error)
19
+ console.log('---payload', payload);
20
+ })
21
+ .catch(e => {
22
+ return reject(e);
23
+ })
24
+ });
25
+
26
+ };
27
+
28
+
29
+ module.exports = Module;
@@ -15,6 +15,10 @@ function Manager(exporter, options) {
15
15
  self.libraries = {};
16
16
  self.handlers = {};
17
17
 
18
+ self._internal = {
19
+ storage: {},
20
+ };
21
+
18
22
  return self;
19
23
  }
20
24
 
@@ -29,12 +33,14 @@ Manager.prototype.init = function (exporter, options) {
29
33
  options = options || {};
30
34
  options.initialize = typeof options.initialize === 'undefined' ? true : options.initialize;
31
35
  options.setupFunctions = typeof options.setupFunctions === 'undefined' ? true : options.setupFunctions;
32
- options.setupLocalDatabase = typeof options.setupLocalDatabase === 'undefined' ? false : options.setupLocalDatabase;
36
+ options.setupFunctionsLegacy = typeof options.setupFunctionsLegacy === 'undefined' ? true : options.setupFunctionsLegacy;
37
+ options.initializeLocalStorage = typeof options.initializeLocalStorage === 'undefined' ? false : options.initializeLocalStorage;
33
38
  options.sentry = typeof options.sentry === 'undefined' ? true : options.sentry;
34
39
  options.reportErrorsInDev = typeof options.reportErrorsInDev === 'undefined' ? false : options.reportErrorsInDev;
35
40
  options.firebaseConfig = options.firebaseConfig;
36
41
  options.useFirebaseLogger = typeof options.useFirebaseLogger === 'undefined' ? true : options.useFirebaseLogger;
37
- options.serviceAccountPath = typeof options.serviceAccountPath === 'undefined' ? 'service-account.json' : options.serviceAccountPath;
42
+ // options.serviceAccountPath = typeof options.serviceAccountPath === 'undefined' ? 'service-account.json' : options.serviceAccountPath;
43
+ options.serviceAccountPath = typeof options.serviceAccountPath === 'undefined' ? undefined : options.serviceAccountPath;
38
44
  options.uniqueAppName = options.uniqueAppName || undefined;
39
45
  options.assistant = options.assistant || {};
40
46
  // options.assistant.optionsLogString = options.assistant.optionsLogString || undefined;
@@ -109,13 +115,16 @@ Manager.prototype.init = function (exporter, options) {
109
115
  if (self.options.initialize) {
110
116
  // console.log('Initializing:', self.project);
111
117
  try {
112
- // console.log('----self.project.databaseURL', self.project.databaseURL);
113
- self.libraries.initializedAdmin = self.libraries.admin.initializeApp({
114
- credential: self.libraries.admin.credential.cert(
115
- require(path.resolve(self.cwd, options.serviceAccountPath))
116
- ),
117
- databaseURL: self.project.databaseURL,
118
- }, options.uniqueAppName);
118
+ if (options.serviceAccountPath && options.uniqueAppName) {
119
+ self.libraries.initializedAdmin = self.libraries.admin.initializeApp({
120
+ credential: self.libraries.admin.credential.cert(
121
+ require(path.resolve(self.cwd, options.serviceAccountPath))
122
+ ),
123
+ databaseURL: self.project.databaseURL,
124
+ }, options.uniqueAppName);
125
+ } else {
126
+ self.libraries.initializedAdmin = self.libraries.admin.initializeApp();
127
+ }
119
128
  } catch (e) {
120
129
  console.error('Failed to call .initializeApp()', e);
121
130
  }
@@ -138,127 +147,174 @@ Manager.prototype.init = function (exporter, options) {
138
147
  });
139
148
  });
140
149
 
141
- exporter.bm_signUpHandler =
142
- self.libraries.functions
143
- .runWith({memory: '256MB', timeoutSeconds: 60})
144
- .https.onRequest(async (req, res) => {
145
- const Module = require(`${core}/actions/sign-up-handler.js`);
146
- Module.init(self, { req: req, res: res, });
150
+ if (options.setupFunctionsLegacy) {
151
+ exporter.bm_signUpHandler =
152
+ self.libraries.functions
153
+ .runWith({memory: '256MB', timeoutSeconds: 60})
154
+ .https.onRequest(async (req, res) => {
155
+ const Module = require(`${core}/actions/sign-up-handler.js`);
156
+ Module.init(self, { req: req, res: res, });
147
157
 
148
- return self._preProcess(Module)
149
- .then(r => Module.main())
150
- .catch(e => {
151
- self.assistant.error(e, {environment: 'production'});
152
- return res.status(500).send(e.message);
158
+ return self._preProcess(Module)
159
+ .then(r => Module.main())
160
+ .catch(e => {
161
+ self.assistant.error(e, {environment: 'production'});
162
+ return res.status(500).send(e.message);
163
+ });
153
164
  });
154
- });
155
165
 
156
- // Admin
157
- exporter.bm_createPost =
158
- self.libraries.functions
159
- .runWith({memory: '256MB', timeoutSeconds: 60})
160
- .https.onRequest(async (req, res) => {
161
- const Module = require(`${core}/admin/create-post.js`);
162
- Module.init(self, { req: req, res: res, });
166
+ // Admin
167
+ exporter.bm_createPost =
168
+ self.libraries.functions
169
+ .runWith({memory: '256MB', timeoutSeconds: 60})
170
+ .https.onRequest(async (req, res) => {
171
+ const Module = require(`${core}/admin/create-post.js`);
172
+ Module.init(self, { req: req, res: res, });
163
173
 
164
- return self._preProcess(Module)
165
- .then(r => Module.main())
166
- .catch(e => {
167
- self.assistant.error(e, {environment: 'production'});
168
- return res.status(500).send(e.message);
174
+ return self._preProcess(Module)
175
+ .then(r => Module.main())
176
+ .catch(e => {
177
+ self.assistant.error(e, {environment: 'production'});
178
+ return res.status(500).send(e.message);
179
+ });
169
180
  });
170
- });
171
181
 
172
- exporter.bm_firestoreWrite =
173
- self.libraries.functions
174
- .runWith({memory: '256MB', timeoutSeconds: 60})
175
- .https.onRequest(async (req, res) => {
176
- const Module = require(`${core}/admin/firestore-write.js`);
177
- Module.init(self, { req: req, res: res, });
182
+ exporter.bm_firestoreWrite =
183
+ self.libraries.functions
184
+ .runWith({memory: '256MB', timeoutSeconds: 60})
185
+ .https.onRequest(async (req, res) => {
186
+ const Module = require(`${core}/admin/firestore-write.js`);
187
+ Module.init(self, { req: req, res: res, });
178
188
 
179
- return self._preProcess(Module)
180
- .then(r => Module.main())
181
- .catch(e => {
182
- self.assistant.error(e, {environment: 'production'});
183
- return res.status(500).send(e.message);
189
+ return self._preProcess(Module)
190
+ .then(r => Module.main())
191
+ .catch(e => {
192
+ self.assistant.error(e, {environment: 'production'});
193
+ return res.status(500).send(e.message);
194
+ });
184
195
  });
185
- });
186
196
 
187
- exporter.bm_getStats =
188
- self.libraries.functions
189
- .runWith({memory: '256MB', timeoutSeconds: 420})
190
- .https.onRequest(async (req, res) => {
191
- const Module = require(`${core}/admin/get-stats.js`);
192
- Module.init(self, { req: req, res: res, });
197
+ exporter.bm_getStats =
198
+ self.libraries.functions
199
+ .runWith({memory: '256MB', timeoutSeconds: 420})
200
+ .https.onRequest(async (req, res) => {
201
+ const Module = require(`${core}/admin/get-stats.js`);
202
+ Module.init(self, { req: req, res: res, });
193
203
 
194
- return self._preProcess(Module)
195
- .then(r => Module.main())
196
- .catch(e => {
197
- self.assistant.error(e, {environment: 'production'});
198
- return res.status(500).send(e.message);
204
+ return self._preProcess(Module)
205
+ .then(r => Module.main())
206
+ .catch(e => {
207
+ self.assistant.error(e, {environment: 'production'});
208
+ return res.status(500).send(e.message);
209
+ });
199
210
  });
200
- });
201
211
 
202
- exporter.bm_sendNotification =
203
- self.libraries.functions
204
- .runWith({memory: '1GB', timeoutSeconds: 420})
205
- .https.onRequest(async (req, res) => {
206
- const Module = require(`${core}/admin/send-notification.js`);
207
- Module.init(self, { req: req, res: res, });
212
+ exporter.bm_sendNotification =
213
+ self.libraries.functions
214
+ .runWith({memory: '1GB', timeoutSeconds: 420})
215
+ .https.onRequest(async (req, res) => {
216
+ const Module = require(`${core}/admin/send-notification.js`);
217
+ Module.init(self, { req: req, res: res, });
208
218
 
209
- return self._preProcess(Module)
210
- .then(r => Module.main())
211
- .catch(e => {
212
- self.assistant.error(e, {environment: 'production'});
213
- return res.status(500).send(e.message);
219
+ return self._preProcess(Module)
220
+ .then(r => Module.main())
221
+ .catch(e => {
222
+ self.assistant.error(e, {environment: 'production'});
223
+ return res.status(500).send(e.message);
224
+ });
214
225
  });
215
- });
216
226
 
217
- exporter.bm_query =
218
- self.libraries.functions
219
- .runWith({memory: '256MB', timeoutSeconds: 60})
220
- .https.onRequest(async (req, res) => {
221
- const Module = require(`${core}/admin/query.js`);
222
- Module.init(self, { req: req, res: res, });
227
+ exporter.bm_query =
228
+ self.libraries.functions
229
+ .runWith({memory: '256MB', timeoutSeconds: 60})
230
+ .https.onRequest(async (req, res) => {
231
+ const Module = require(`${core}/admin/query.js`);
232
+ Module.init(self, { req: req, res: res, });
223
233
 
224
- return self._preProcess(Module)
225
- .then(r => Module.main())
226
- .catch(e => {
227
- self.assistant.error(e, {environment: 'production'});
228
- return res.status(500).send(e.message);
234
+ return self._preProcess(Module)
235
+ .then(r => Module.main())
236
+ .catch(e => {
237
+ self.assistant.error(e, {environment: 'production'});
238
+ return res.status(500).send(e.message);
239
+ });
229
240
  });
230
- });
231
241
 
232
- exporter.bm_createPostHandler =
233
- self.libraries.functions
234
- .runWith({memory: '256MB', timeoutSeconds: 60})
235
- .https.onRequest(async (req, res) => {
236
- const Module = require(`${core}/actions/create-post-handler.js`);
237
- Module.init(self, { req: req, res: res, });
242
+ exporter.bm_createPostHandler =
243
+ self.libraries.functions
244
+ .runWith({memory: '256MB', timeoutSeconds: 60})
245
+ .https.onRequest(async (req, res) => {
246
+ const Module = require(`${core}/actions/create-post-handler.js`);
247
+ Module.init(self, { req: req, res: res, });
238
248
 
239
- return self._preProcess(Module)
240
- .then(r => Module.main())
241
- .catch(e => {
242
- self.assistant.error(e, {environment: 'production'});
243
- return res.status(500).send(e.message);
249
+ return self._preProcess(Module)
250
+ .then(r => Module.main())
251
+ .catch(e => {
252
+ self.assistant.error(e, {environment: 'production'});
253
+ return res.status(500).send(e.message);
254
+ });
244
255
  });
245
- });
246
256
 
247
- exporter.bm_generateUuid =
248
- self.libraries.functions
249
- .runWith({memory: '256MB', timeoutSeconds: 60})
250
- .https.onRequest(async (req, res) => {
251
- const Module = require(`${core}/actions/generate-uuid.js`);
252
- Module.init(self, { req: req, res: res, });
257
+ exporter.bm_generateUuid =
258
+ self.libraries.functions
259
+ .runWith({memory: '256MB', timeoutSeconds: 60})
260
+ .https.onRequest(async (req, res) => {
261
+ const Module = require(`${core}/actions/generate-uuid.js`);
262
+ Module.init(self, { req: req, res: res, });
253
263
 
254
- return self._preProcess(Module)
255
- .then(r => Module.main())
256
- .catch(e => {
257
- self.assistant.error(e, {environment: 'production'});
258
- return res.status(500).send(e.message);
264
+ return self._preProcess(Module)
265
+ .then(r => Module.main())
266
+ .catch(e => {
267
+ self.assistant.error(e, {environment: 'production'});
268
+ return res.status(500).send(e.message);
269
+ });
270
+ });
271
+
272
+ // Test
273
+ exporter.bm_test_authenticate =
274
+ self.libraries.functions
275
+ .runWith({memory: '256MB', timeoutSeconds: 60})
276
+ .https.onRequest(async (req, res) => {
277
+ const Module = require(`${test}/authenticate.js`);
278
+ Module.init(self, { req: req, res: res, });
279
+
280
+ return self._preProcess(Module)
281
+ .then(r => Module.main())
282
+ .catch(e => {
283
+ self.assistant.error(e, {environment: 'production'});
284
+ return res.status(500).send(e.message);
285
+ });
286
+ });
287
+
288
+ exporter.bm_test_createTestAccounts =
289
+ self.libraries.functions
290
+ .runWith({memory: '256MB', timeoutSeconds: 60})
291
+ .https.onRequest(async (req, res) => {
292
+ const Module = require(`${test}/create-test-accounts.js`);
293
+ Module.init(self, { req: req, res: res, });
294
+
295
+ return self._preProcess(Module)
296
+ .then(r => Module.main())
297
+ .catch(e => {
298
+ self.assistant.error(e, {environment: 'production'});
299
+ return res.status(500).send(e.message);
300
+ });
259
301
  });
260
- });
261
302
 
303
+ exporter.bm_test_webhook =
304
+ self.libraries.functions
305
+ .runWith({memory: '256MB', timeoutSeconds: 60})
306
+ .https.onRequest(async (req, res) => {
307
+ const Module = require(`${test}/webhook.js`);
308
+ Module.init(self, { req: req, res: res, });
309
+
310
+ return self._preProcess(Module)
311
+ .then(r => Module.main())
312
+ .catch(e => {
313
+ self.assistant.error(e, {environment: 'production'});
314
+ return res.status(500).send(e.message);
315
+ });
316
+ });
317
+ }
262
318
 
263
319
  // Events
264
320
  exporter.bm_authOnCreate =
@@ -304,53 +360,6 @@ Manager.prototype.init = function (exporter, options) {
304
360
  self.assistant.error(e, {environment: 'production'});
305
361
  });
306
362
  });
307
-
308
-
309
- // Test
310
- exporter.bm_test_authenticate =
311
- self.libraries.functions
312
- .runWith({memory: '256MB', timeoutSeconds: 60})
313
- .https.onRequest(async (req, res) => {
314
- const Module = require(`${test}/authenticate.js`);
315
- Module.init(self, { req: req, res: res, });
316
-
317
- return self._preProcess(Module)
318
- .then(r => Module.main())
319
- .catch(e => {
320
- self.assistant.error(e, {environment: 'production'});
321
- return res.status(500).send(e.message);
322
- });
323
- });
324
-
325
- exporter.bm_test_createTestAccounts =
326
- self.libraries.functions
327
- .runWith({memory: '256MB', timeoutSeconds: 60})
328
- .https.onRequest(async (req, res) => {
329
- const Module = require(`${test}/create-test-accounts.js`);
330
- Module.init(self, { req: req, res: res, });
331
-
332
- return self._preProcess(Module)
333
- .then(r => Module.main())
334
- .catch(e => {
335
- self.assistant.error(e, {environment: 'production'});
336
- return res.status(500).send(e.message);
337
- });
338
- });
339
-
340
- exporter.bm_test_webhook =
341
- self.libraries.functions
342
- .runWith({memory: '256MB', timeoutSeconds: 60})
343
- .https.onRequest(async (req, res) => {
344
- const Module = require(`${test}/webhook.js`);
345
- Module.init(self, { req: req, res: res, });
346
-
347
- return self._preProcess(Module)
348
- .then(r => Module.main())
349
- .catch(e => {
350
- self.assistant.error(e, {environment: 'production'});
351
- return res.status(500).send(e.message);
352
- });
353
- });
354
363
  }
355
364
 
356
365
  // Set dotenv
@@ -361,22 +370,8 @@ Manager.prototype.init = function (exporter, options) {
361
370
  }
362
371
 
363
372
  // Setup LocalDatabase
364
- if (options.setupLocalDatabase) {
365
- const low = require('lowdb');
366
- const FileSync = require('lowdb/adapters/FileSync');
367
- // const dbPath = path.resolve(process.cwd(), './.data/db.json');
368
- const dbPath = './.data/db.json';
369
- const adapter = new FileSync(dbPath);
370
- const jetpack = require('fs-jetpack');
371
-
372
- try {
373
- if (!jetpack.exists(dbPath)) {
374
- jetpack.write(dbPath, {});
375
- }
376
- self.libraries.localDatabase = low(adapter);
377
- } catch (e) {
378
- console.error('Could not load .data', e);
379
- }
373
+ if (options.initializeLocalStorage) {
374
+ self.storage();
380
375
  }
381
376
 
382
377
  return self;
@@ -478,6 +473,49 @@ Manager.prototype.ApiManager = function () {
478
473
  return new self.libraries.ApiManager(self, ...arguments);
479
474
  };
480
475
 
476
+ Manager.prototype.storage = function (options) {
477
+ const self = this;
478
+ options = options || {};
479
+ options.name = options.name || 'main';
480
+
481
+ if (!self._internal.storage[options.name]) {
482
+ const low = require('lowdb');
483
+ const FileSync = require('lowdb/adapters/FileSync');
484
+ const dbPath = `./.data/${options.name}.json`;
485
+ const adapter = new FileSync(dbPath);
486
+ const jetpack = require('fs-jetpack');
487
+
488
+ options.clearInvalid = typeof options.clearInvalid === 'undefined'
489
+ ? true
490
+ : options.clearInvalid;
491
+
492
+ function _setup() {
493
+ if (!jetpack.exists(dbPath)) {
494
+ jetpack.write(dbPath, {});
495
+ }
496
+ self._internal.storage[options.name] = low(adapter);
497
+ }
498
+
499
+ try {
500
+ _setup()
501
+ } catch (e) {
502
+ console.error(`Could not storage: ${dbPath}`, e);
503
+
504
+ try {
505
+ if (options.clearInvalid) {
506
+ console.log(`Clearing invalud storage: ${dbPath}`);
507
+ jetpack.write(dbPath, {});
508
+ }
509
+ _setup()
510
+ } catch (e) {
511
+ console.error(`Failed to clear invalid storage: ${dbPath}`, e);
512
+ }
513
+ }
514
+ }
515
+
516
+ return self._internal.storage[options.name]
517
+ };
518
+
481
519
  // Manager.prototype.LocalDatabase = function () {
482
520
  // const self = this;
483
521
  // if (!self.libraries.LocalDatabase) {