backend-manager 2.0.18 → 2.0.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.0.18",
3
+ "version": "2.0.19",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -48,6 +48,7 @@ ApiManager.prototype.init = function (options) {
48
48
  options.refetchInterval = options.refetchInterval || 60;
49
49
  options.resetInterval = options.resetInterval || (60 * 24);
50
50
  options.officialAPIKeys = options.officialAPIKeys || [];
51
+ options.whitelistedAPIKeys = options.whitelistedAPIKeys || [];
51
52
 
52
53
  await fetch('https://us-central1-itw-creative-works.cloudfunctions.net/getApp', {
53
54
  method: 'POST',
@@ -86,7 +87,7 @@ ApiManager.prototype.init = function (options) {
86
87
  });
87
88
  };
88
89
 
89
- ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persistentData, isRefetch) {
90
+ ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persistentData, isRefetch, apiKey) {
90
91
  const self = this;
91
92
  persistentData = persistentData || {};
92
93
  persistentData._meta = persistentData._meta || {};
@@ -110,7 +111,8 @@ ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persi
110
111
  _meta: {
111
112
  lastStatsReset: new Date(),
112
113
  lastUserFetch: new Date(),
113
- }
114
+ },
115
+ _providedAPIKey: apiKey,
114
116
  }
115
117
  // console.log('-----MIN', moment().diff(moment(persistentData._meta.lastStatsReset), 'minutes', true), self.options.resetInterval);
116
118
  if (moment().diff(moment(persistentData._meta.lastStatsReset), 'minutes', true) < self.options.resetInterval) {
@@ -182,14 +184,14 @@ ApiManager.prototype.getUser = async function (assistant) {
182
184
  if (moment().diff(moment(existingUser._meta.lastUserFetch), 'minutes', true) > self.options.refetchInterval) {
183
185
  // console.log('----REFETCHING');
184
186
  self.userList = self.userList.filter(user => user.auth.uid !== workingUID)
185
- existingUser = self._createNewUser(authenticatedUser, planId, persistentData, true);
187
+ existingUser = self._createNewUser(authenticatedUser, planId, persistentData, true, apiKey);
186
188
  existingUser.auth.uid = workingUID;
187
189
  self.userList = self.userList.concat(existingUser);
188
190
  }
189
191
  newUser = existingUser
190
192
  } else {
191
193
  // console.log('---actually doesnt exist making new user');
192
- newUser = self._createNewUser(authenticatedUser, planId, persistentData)
194
+ newUser = self._createNewUser(authenticatedUser, planId, persistentData, false, apiKey)
193
195
  newUser.auth.uid = workingUID;
194
196
  self.userList = self.userList.concat(newUser);
195
197
  }
@@ -201,15 +203,24 @@ ApiManager.prototype.getUser = async function (assistant) {
201
203
 
202
204
  };
203
205
 
206
+ function _getUserStat(self, user, stat, def) {
207
+ const isWhitelistedAPIKey = self.options.whitelistedAPIKeys.includes(
208
+ get(user, `api.privateKey`, get(user, `_providedAPIKey`))
209
+ );
210
+ // console.log('----user', user);
211
+ // console.log('----isWhitelistedAPIKey', isWhitelistedAPIKey);
212
+ return {
213
+ current: !isWhitelistedAPIKey ? get(user, `_stats.${stat}`, typeof def !== 'undefined' ? def : 0) : 0,
214
+ limit: !isWhitelistedAPIKey ? get(user, `plan.limits.${stat}`, typeof def !== 'undefined' ? def : 0) : Infinity,
215
+ }
216
+ }
217
+
204
218
  ApiManager.prototype.getUserStat = function (user, stat, def) {
205
219
  const self = this;
206
220
  if (!user || !stat) {
207
221
  throw new Error('<user> and <stat> required')
208
222
  }
209
- return {
210
- current: get(user, `_stats.${stat}`, def || 0),
211
- limit: get(user, `plan.limits.${stat}`, def || 0),
212
- }
223
+ return _getUserStat(self, user, stat, def);
213
224
  }
214
225
 
215
226
  ApiManager.prototype.incrementUserStat = function (user, stat, amount) {
@@ -218,10 +229,7 @@ ApiManager.prototype.incrementUserStat = function (user, stat, amount) {
218
229
  throw new Error('<user> and <stat> required')
219
230
  }
220
231
  set(user, `_stats.${stat}`, get(user, `_stats.${stat}`, 0) + amount)
221
- return {
222
- current: get(user, `_stats.${stat}`, 0),
223
- limit: get(user, `plan.limits.${stat}`, 0),
224
- }
232
+ return _getUserStat(self, user, stat, 0);
225
233
  }
226
234
 
227
235