@dereekb/firebase-server 13.2.2 → 13.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.
Files changed (166) hide show
  1. package/index.cjs.js +7506 -1884
  2. package/index.esm.js +7505 -1890
  3. package/mailgun/index.cjs.js +333 -31
  4. package/mailgun/index.esm.js +333 -31
  5. package/mailgun/package.json +8 -8
  6. package/mailgun/src/lib/auth.mailgun.d.ts +22 -3
  7. package/model/index.cjs.js +11334 -3830
  8. package/model/index.esm.js +11338 -3834
  9. package/model/package.json +8 -8
  10. package/model/src/lib/mailgun/notification.send.service.mailgun.d.ts +27 -1
  11. package/model/src/lib/notification/notification.action.init.service.d.ts +93 -1
  12. package/model/src/lib/notification/notification.action.service.d.ts +151 -2
  13. package/model/src/lib/notification/notification.config.d.ts +19 -7
  14. package/model/src/lib/notification/notification.config.service.d.ts +44 -6
  15. package/model/src/lib/notification/notification.create.run.d.ts +33 -5
  16. package/model/src/lib/notification/notification.error.d.ts +63 -0
  17. package/model/src/lib/notification/notification.module.d.ts +11 -0
  18. package/model/src/lib/notification/notification.send.d.ts +8 -1
  19. package/model/src/lib/notification/notification.send.service.d.ts +7 -1
  20. package/model/src/lib/notification/notification.send.service.notificationsummary.d.ts +32 -2
  21. package/model/src/lib/notification/notification.send.service.text.d.ts +11 -3
  22. package/model/src/lib/notification/notification.task.service.d.ts +13 -1
  23. package/model/src/lib/notification/notification.task.service.handler.d.ts +27 -3
  24. package/model/src/lib/notification/notification.task.service.util.d.ts +16 -4
  25. package/model/src/lib/notification/notification.task.subtask.handler.d.ts +31 -1
  26. package/model/src/lib/notification/notification.util.d.ts +70 -0
  27. package/model/src/lib/storagefile/storagefile.action.init.service.d.ts +62 -1
  28. package/model/src/lib/storagefile/storagefile.action.server.d.ts +124 -2
  29. package/model/src/lib/storagefile/storagefile.error.d.ts +44 -0
  30. package/model/src/lib/storagefile/storagefile.module.d.ts +11 -0
  31. package/model/src/lib/storagefile/storagefile.task.service.handler.d.ts +4 -1
  32. package/model/src/lib/storagefile/storagefile.upload.service.d.ts +13 -1
  33. package/model/src/lib/storagefile/storagefile.upload.service.initializer.d.ts +43 -1
  34. package/model/src/lib/storagefile/storagefile.util.d.ts +8 -0
  35. package/oidc/index.cjs.default.js +1 -0
  36. package/oidc/index.cjs.js +5607 -0
  37. package/oidc/index.cjs.mjs +2 -0
  38. package/oidc/index.d.ts +1 -0
  39. package/oidc/index.esm.js +5560 -0
  40. package/oidc/package.json +26 -0
  41. package/oidc/src/index.d.ts +1 -0
  42. package/oidc/src/lib/controller/index.d.ts +3 -0
  43. package/oidc/src/lib/controller/oidc.interaction.controller.d.ts +54 -0
  44. package/oidc/src/lib/controller/oidc.provider.controller.d.ts +18 -0
  45. package/oidc/src/lib/controller/oidc.wellknown.controller.d.ts +36 -0
  46. package/oidc/src/lib/index.d.ts +6 -0
  47. package/oidc/src/lib/middleware/index.d.ts +3 -0
  48. package/oidc/src/lib/middleware/oauth-auth.decorator.d.ts +14 -0
  49. package/oidc/src/lib/middleware/oauth-auth.middleware.d.ts +21 -0
  50. package/oidc/src/lib/middleware/oauth-auth.module.d.ts +50 -0
  51. package/oidc/src/lib/model/index.d.ts +3 -0
  52. package/oidc/src/lib/model/jwks/index.d.ts +3 -0
  53. package/oidc/src/lib/model/jwks/jwks.d.ts +107 -0
  54. package/oidc/src/lib/model/jwks/jwks.id.d.ts +9 -0
  55. package/oidc/src/lib/model/jwks/jwks.query.d.ts +18 -0
  56. package/oidc/src/lib/model/model.d.ts +12 -0
  57. package/oidc/src/lib/model/oidc/index.d.ts +2 -0
  58. package/oidc/src/lib/model/oidc/oidcmodel.action.server.d.ts +62 -0
  59. package/oidc/src/lib/model/oidc/oidcmodel.module.d.ts +23 -0
  60. package/oidc/src/lib/oidc.config.d.ts +175 -0
  61. package/oidc/src/lib/oidc.module.d.ts +100 -0
  62. package/oidc/src/lib/service/index.d.ts +10 -0
  63. package/oidc/src/lib/service/oidc.account.d.ts +36 -0
  64. package/oidc/src/lib/service/oidc.account.service.d.ts +104 -0
  65. package/oidc/src/lib/service/oidc.adapter.service.d.ts +20 -0
  66. package/oidc/src/lib/service/oidc.auth.d.ts +26 -0
  67. package/oidc/src/lib/service/oidc.client.service.d.ts +57 -0
  68. package/oidc/src/lib/service/oidc.config.service.d.ts +100 -0
  69. package/oidc/src/lib/service/oidc.encryption.service.d.ts +53 -0
  70. package/oidc/src/lib/service/oidc.interaction.service.d.ts +44 -0
  71. package/oidc/src/lib/service/oidc.jwks.service.d.ts +105 -0
  72. package/oidc/src/lib/service/oidc.service.d.ts +55 -0
  73. package/package.json +15 -9
  74. package/src/lib/auth/auth.context.d.ts +22 -2
  75. package/src/lib/auth/auth.service.d.ts +5 -0
  76. package/src/lib/auth/auth.service.error.d.ts +2 -1
  77. package/src/lib/auth/auth.util.d.ts +15 -3
  78. package/src/lib/env/env.config.d.ts +42 -0
  79. package/src/lib/env/env.service.d.ts +17 -0
  80. package/src/lib/env/index.d.ts +1 -0
  81. package/src/lib/firestore/array.d.ts +11 -3
  82. package/src/lib/firestore/driver.accessor.batch.d.ts +27 -4
  83. package/src/lib/firestore/driver.accessor.d.ts +43 -0
  84. package/src/lib/firestore/driver.accessor.default.d.ts +20 -0
  85. package/src/lib/firestore/driver.accessor.transaction.d.ts +28 -4
  86. package/src/lib/firestore/driver.d.ts +14 -0
  87. package/src/lib/firestore/driver.query.d.ts +25 -0
  88. package/src/lib/firestore/increment.d.ts +13 -3
  89. package/src/lib/firestore/snapshot/index.d.ts +1 -1
  90. package/src/lib/firestore/snapshot/{snapshot.field.d.ts → snapshot.field.encrypt.d.ts} +13 -27
  91. package/src/lib/function/assert.d.ts +32 -16
  92. package/src/lib/function/context.d.ts +26 -0
  93. package/src/lib/function/error.auth.d.ts +15 -0
  94. package/src/lib/function/error.d.ts +68 -4
  95. package/src/lib/nest/app.d.ts +53 -9
  96. package/src/lib/nest/app.module.d.ts +90 -0
  97. package/src/lib/nest/auth/auth.module.d.ts +30 -4
  98. package/src/lib/nest/auth/auth.util.d.ts +38 -0
  99. package/src/lib/nest/controller/auth.context.server.d.ts +27 -0
  100. package/src/lib/nest/controller/index.d.ts +1 -0
  101. package/src/lib/nest/development/development.app.function.d.ts +45 -0
  102. package/src/lib/nest/development/development.assert.function.d.ts +19 -1
  103. package/src/lib/nest/development/development.function.d.ts +42 -3
  104. package/src/lib/nest/development/development.schedule.function.d.ts +29 -0
  105. package/src/lib/nest/development/development.schedule.function.error.d.ts +21 -0
  106. package/src/lib/nest/env/env.service.d.ts +9 -0
  107. package/src/lib/nest/env/env.util.d.ts +16 -0
  108. package/src/lib/nest/firebase/firebase.module.d.ts +13 -1
  109. package/src/lib/nest/firestore/firestore.module.d.ts +24 -8
  110. package/src/lib/nest/function/call.d.ts +75 -6
  111. package/src/lib/nest/function/context.d.ts +85 -2
  112. package/src/lib/nest/function/nest.d.ts +46 -0
  113. package/src/lib/nest/function/schedule.d.ts +49 -0
  114. package/src/lib/nest/function/v2/blocking.d.ts +92 -8
  115. package/src/lib/nest/function/v2/call.d.ts +38 -8
  116. package/src/lib/nest/function/v2/event.d.ts +67 -4
  117. package/src/lib/nest/function/v2/schedule.d.ts +36 -5
  118. package/src/lib/nest/index.d.ts +3 -0
  119. package/src/lib/nest/middleware/appcheck.d.ts +5 -0
  120. package/src/lib/nest/middleware/appcheck.middleware.d.ts +44 -14
  121. package/src/lib/nest/middleware/appcheck.module.d.ts +10 -1
  122. package/src/lib/nest/middleware/globalprefix.d.ts +31 -3
  123. package/src/lib/nest/middleware/index.d.ts +2 -1
  124. package/src/lib/nest/middleware/rawbody.middleware.d.ts +6 -0
  125. package/src/lib/nest/model/call.model.function.d.ts +69 -3
  126. package/src/lib/nest/model/create.model.function.d.ts +66 -0
  127. package/src/lib/nest/model/crud.assert.function.d.ts +27 -1
  128. package/src/lib/nest/model/delete.model.function.d.ts +55 -0
  129. package/src/lib/nest/model/permission.error.d.ts +15 -0
  130. package/src/lib/nest/model/read.model.function.d.ts +57 -0
  131. package/src/lib/nest/model/specifier.function.d.ts +58 -1
  132. package/src/lib/nest/model/update.model.function.d.ts +58 -0
  133. package/src/lib/nest/nest.provider.d.ts +24 -11
  134. package/src/lib/nest/nest.provider.server.d.ts +12 -0
  135. package/src/lib/nest/storage/storage.module.d.ts +30 -4
  136. package/src/lib/storage/driver.accessor.d.ts +33 -0
  137. package/src/lib/storage/driver.d.ts +14 -0
  138. package/src/lib/storage/storage.d.ts +11 -4
  139. package/src/lib/storage/storage.service.d.ts +10 -1
  140. package/src/lib/type.d.ts +7 -2
  141. package/test/index.cjs.js +4072 -757
  142. package/test/index.esm.js +4073 -761
  143. package/test/package.json +8 -8
  144. package/test/src/lib/firebase/firebase.admin.auth.d.ts +82 -1
  145. package/test/src/lib/firebase/firebase.admin.collection.d.ts +55 -3
  146. package/test/src/lib/firebase/firebase.admin.d.ts +64 -0
  147. package/test/src/lib/firebase/firebase.admin.function.d.ts +84 -0
  148. package/test/src/lib/firebase/firebase.admin.nest.d.ts +125 -17
  149. package/test/src/lib/firebase/firebase.admin.nest.function.callable.context.d.ts +44 -0
  150. package/test/src/lib/firebase/firebase.admin.nest.function.cloud.context.d.ts +41 -0
  151. package/test/src/lib/firebase/firebase.admin.nest.function.d.ts +98 -0
  152. package/test/src/lib/firebase/firebase.admin.test.server.d.ts +26 -0
  153. package/test/src/lib/firebase/firebase.d.ts +57 -2
  154. package/test/src/lib/firebase/firebase.function.d.ts +138 -0
  155. package/test/src/lib/firestore/firestore.admin.d.ts +6 -0
  156. package/test/src/lib/firestore/firestore.d.ts +30 -0
  157. package/test/src/lib/storage/storage.admin.d.ts +9 -3
  158. package/test/src/lib/storage/storage.d.ts +30 -0
  159. package/zoho/index.cjs.js +406 -73
  160. package/zoho/index.esm.js +406 -73
  161. package/zoho/package.json +8 -8
  162. package/zoho/src/lib/zoho.accounts.firebase.d.ts +13 -2
  163. package/zoho/src/lib/zoho.accounts.firebase.system.d.ts +29 -6
  164. package/index.cjs.js.map +0 -1
  165. package/index.esm.js.map +0 -1
  166. /package/src/lib/nest/middleware/{webhook.d.ts → webhook.module.d.ts} +0 -0
package/zoho/index.cjs.js CHANGED
@@ -3,109 +3,442 @@
3
3
  var firebase = require('@dereekb/firebase');
4
4
  var util = require('@dereekb/util');
5
5
 
6
- const ZOHO_ACCESS_TOKEN_SYSTEM_STATE_TYPE = 'zoho_access_token';
7
- const zohoAccessTokenSystemStateEmbeddedTokenConverter = firebase.firestoreSubObject({
6
+ /**
7
+ * {@link SystemState} type identifier for storing Zoho access tokens in Firestore.
8
+ */ var ZOHO_ACCESS_TOKEN_SYSTEM_STATE_TYPE = 'zoho_access_token';
9
+ var zohoAccessTokenSystemStateEmbeddedTokenConverter = firebase.firestoreSubObject({
8
10
  objectField: {
9
11
  fields: {
10
12
  key: firebase.firestoreString(),
11
13
  accessToken: firebase.firestoreString(),
12
14
  scope: firebase.firestoreString(),
13
15
  apiDomain: firebase.firestoreString(),
14
- expiresIn: firebase.firestoreNumber({ default: 3600 }),
16
+ expiresIn: firebase.firestoreNumber({
17
+ default: 3600
18
+ }),
15
19
  expiresAt: firebase.firestoreDate()
16
20
  }
17
21
  }
18
22
  });
19
23
  /**
20
- * NOTE: Be sure to register this data converter with the SystemStateStoredDataConverterMap for your app.
21
- */
22
- const zohoAccessTokenSystemStateDataConverter = firebase.firestoreSubObject({
24
+ * Firestore field converter for {@link ZohoAccessTokenSystemStateData}.
25
+ *
26
+ * Automatically filters out expired tokens on read and enforces uniqueness by service key.
27
+ * Must be registered in the app's {@link SystemStateStoredDataConverterMap} under
28
+ * the {@link ZOHO_ACCESS_TOKEN_SYSTEM_STATE_TYPE} key.
29
+ */ var zohoAccessTokenSystemStateDataConverter = firebase.firestoreSubObject({
23
30
  objectField: {
24
31
  fields: {
25
32
  tokens: firebase.firestoreObjectArray({
26
33
  firestoreField: zohoAccessTokenSystemStateEmbeddedTokenConverter,
27
- filterUnique: util.filterUniqueFunction((x) => x.key), // only one token per key is allowed
28
- filter: (x) => (x?.expiresAt ? !util.isPast(x.expiresAt) : true) // filter out expired values or values that have no expiration
34
+ filterUnique: util.filterUniqueFunction(function(x) {
35
+ return x.key;
36
+ }),
37
+ filter: function filter(x) {
38
+ return (x === null || x === void 0 ? void 0 : x.expiresAt) ? !util.isPast(x.expiresAt) : true // filter out expired values or values that have no expiration
39
+ ;
40
+ }
29
41
  }),
30
- lat: firebase.firestoreDate({ saveDefaultAsNow: true })
42
+ lat: firebase.firestoreDate({
43
+ saveDefaultAsNow: true
44
+ })
31
45
  }
32
46
  }
33
47
  });
34
48
  /**
35
- * Convenience function for loading the document for ZohoAccessTokenSystemStateData.
49
+ * Loads the {@link SystemStateDocument} that stores {@link ZohoAccessTokenSystemStateData},
50
+ * using {@link ZOHO_ACCESS_TOKEN_SYSTEM_STATE_TYPE} as the document ID.
51
+ *
52
+ * @param accessor - the document accessor for the SystemState collection
36
53
  *
37
- * @param accessor
38
- * @returns
39
- */
40
- function loadZohoAccessTokenSystemState(accessor) {
54
+ * @example
55
+ * ```ts
56
+ * const doc = loadZohoAccessTokenSystemState(systemStateCollection.documentAccessor());
57
+ * const data = await doc.snapshotData();
58
+ * ```
59
+ */ function loadZohoAccessTokenSystemState(accessor) {
41
60
  return accessor.loadDocumentForId(ZOHO_ACCESS_TOKEN_SYSTEM_STATE_TYPE);
42
61
  }
43
62
 
63
+ function _array_like_to_array(arr, len) {
64
+ if (len == null || len > arr.length) len = arr.length;
65
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
66
+ return arr2;
67
+ }
68
+ function _array_without_holes(arr) {
69
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
70
+ }
71
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
72
+ try {
73
+ var info = gen[key](arg);
74
+ var value = info.value;
75
+ } catch (error) {
76
+ reject(error);
77
+ return;
78
+ }
79
+ if (info.done) {
80
+ resolve(value);
81
+ } else {
82
+ Promise.resolve(value).then(_next, _throw);
83
+ }
84
+ }
85
+ function _async_to_generator(fn) {
86
+ return function() {
87
+ var self = this, args = arguments;
88
+ return new Promise(function(resolve, reject) {
89
+ var gen = fn.apply(self, args);
90
+ function _next(value) {
91
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
92
+ }
93
+ function _throw(err) {
94
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
95
+ }
96
+ _next(undefined);
97
+ });
98
+ };
99
+ }
100
+ function _define_property(obj, key, value) {
101
+ if (key in obj) {
102
+ Object.defineProperty(obj, key, {
103
+ value: value,
104
+ enumerable: true,
105
+ configurable: true,
106
+ writable: true
107
+ });
108
+ } else {
109
+ obj[key] = value;
110
+ }
111
+ return obj;
112
+ }
113
+ function _iterable_to_array(iter) {
114
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
115
+ }
116
+ function _non_iterable_spread() {
117
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
118
+ }
119
+ function _object_spread(target) {
120
+ for(var i = 1; i < arguments.length; i++){
121
+ var source = arguments[i] != null ? arguments[i] : {};
122
+ var ownKeys = Object.keys(source);
123
+ if (typeof Object.getOwnPropertySymbols === "function") {
124
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
125
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
126
+ }));
127
+ }
128
+ ownKeys.forEach(function(key) {
129
+ _define_property(target, key, source[key]);
130
+ });
131
+ }
132
+ return target;
133
+ }
134
+ function ownKeys(object, enumerableOnly) {
135
+ var keys = Object.keys(object);
136
+ if (Object.getOwnPropertySymbols) {
137
+ var symbols = Object.getOwnPropertySymbols(object);
138
+ keys.push.apply(keys, symbols);
139
+ }
140
+ return keys;
141
+ }
142
+ function _object_spread_props(target, source) {
143
+ source = source != null ? source : {};
144
+ if (Object.getOwnPropertyDescriptors) {
145
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
146
+ } else {
147
+ ownKeys(Object(source)).forEach(function(key) {
148
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
149
+ });
150
+ }
151
+ return target;
152
+ }
153
+ function _to_consumable_array(arr) {
154
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
155
+ }
156
+ function _unsupported_iterable_to_array(o, minLen) {
157
+ if (!o) return;
158
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
159
+ var n = Object.prototype.toString.call(o).slice(8, -1);
160
+ if (n === "Object" && o.constructor) n = o.constructor.name;
161
+ if (n === "Map" || n === "Set") return Array.from(n);
162
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
163
+ }
164
+ function _ts_generator(thisArg, body) {
165
+ var f, y, t, _ = {
166
+ label: 0,
167
+ sent: function() {
168
+ if (t[0] & 1) throw t[1];
169
+ return t[1];
170
+ },
171
+ trys: [],
172
+ ops: []
173
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
174
+ return d(g, "next", {
175
+ value: verb(0)
176
+ }), d(g, "throw", {
177
+ value: verb(1)
178
+ }), d(g, "return", {
179
+ value: verb(2)
180
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
181
+ value: function() {
182
+ return this;
183
+ }
184
+ }), g;
185
+ function verb(n) {
186
+ return function(v) {
187
+ return step([
188
+ n,
189
+ v
190
+ ]);
191
+ };
192
+ }
193
+ function step(op) {
194
+ if (f) throw new TypeError("Generator is already executing.");
195
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
196
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
197
+ if (y = 0, t) op = [
198
+ op[0] & 2,
199
+ t.value
200
+ ];
201
+ switch(op[0]){
202
+ case 0:
203
+ case 1:
204
+ t = op;
205
+ break;
206
+ case 4:
207
+ _.label++;
208
+ return {
209
+ value: op[1],
210
+ done: false
211
+ };
212
+ case 5:
213
+ _.label++;
214
+ y = op[1];
215
+ op = [
216
+ 0
217
+ ];
218
+ continue;
219
+ case 7:
220
+ op = _.ops.pop();
221
+ _.trys.pop();
222
+ continue;
223
+ default:
224
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
225
+ _ = 0;
226
+ continue;
227
+ }
228
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
229
+ _.label = op[1];
230
+ break;
231
+ }
232
+ if (op[0] === 6 && _.label < t[1]) {
233
+ _.label = t[1];
234
+ t = op;
235
+ break;
236
+ }
237
+ if (t && _.label < t[2]) {
238
+ _.label = t[2];
239
+ _.ops.push(op);
240
+ break;
241
+ }
242
+ if (t[2]) _.ops.pop();
243
+ _.trys.pop();
244
+ continue;
245
+ }
246
+ op = body.call(thisArg, _);
247
+ } catch (e) {
248
+ op = [
249
+ 6,
250
+ e
251
+ ];
252
+ y = 0;
253
+ } finally{
254
+ f = t = 0;
255
+ }
256
+ if (op[0] & 5) throw op[1];
257
+ return {
258
+ value: op[0] ? op[1] : void 0,
259
+ done: true
260
+ };
261
+ }
262
+ }
44
263
  /**
45
- * Creates a ZohoAccountsAccessTokenCacheService from the input SystemStateFirestoreCollection.
264
+ * Creates a {@link ZohoAccountsAccessTokenCacheService} backed by Firestore {@link SystemState} documents.
46
265
  *
47
- * @param collection
48
- */
49
- function firebaseZohoAccountsAccessTokenCacheService(systemStateCollection) {
50
- const systemStateDocumentAccessor = systemStateCollection.documentAccessor();
51
- const service = {
52
- loadZohoAccessTokenCache: function (serviceKey) {
53
- const cache = {
54
- loadCachedToken: async function () {
55
- const document = loadZohoAccessTokenSystemState(systemStateDocumentAccessor);
56
- const existingData = await document.snapshotData();
57
- let result = null;
58
- if (existingData != null) {
59
- const tokensArray = existingData?.data?.tokens ?? [];
60
- result = tokensArray.find((x) => x.key === serviceKey);
61
- }
62
- return result;
63
- },
64
- updateCachedToken: async function (accessToken) {
65
- // run in a transaction
66
- await systemStateCollection.firestoreContext.runTransaction(async (transaction) => {
67
- const documentInTransaction = loadZohoAccessTokenSystemState(systemStateCollection.documentAccessorForTransaction(transaction));
68
- const existingData = await documentInTransaction.snapshotData();
69
- const existingTokens = existingData?.data?.tokens ?? [];
70
- const tokens = [
71
- // filter any potential old token for this service key
72
- ...existingTokens.filter((x) => x.key !== serviceKey),
73
- // add the new token
74
- {
75
- ...accessToken,
76
- key: serviceKey
266
+ * Each Zoho service integration gets its own cached access token entry keyed by the service key.
267
+ * Tokens are stored in a single {@link SystemState} document (type {@link ZOHO_ACCESS_TOKEN_SYSTEM_STATE_TYPE})
268
+ * and token updates/clears use Firestore transactions for concurrency safety.
269
+ *
270
+ * @param systemStateCollection - the Firestore collection for system state documents
271
+ *
272
+ * @example
273
+ * ```ts
274
+ * const cacheService = firebaseZohoAccountsAccessTokenCacheService(systemStateCollection);
275
+ * const cache = cacheService.loadZohoAccessTokenCache('my-zoho-service');
276
+ * const token = await cache.loadCachedToken();
277
+ * ```
278
+ */ function firebaseZohoAccountsAccessTokenCacheService(systemStateCollection) {
279
+ var systemStateDocumentAccessor = systemStateCollection.documentAccessor();
280
+ var service = {
281
+ loadZohoAccessTokenCache: function loadZohoAccessTokenCache(serviceKey) {
282
+ var cache = {
283
+ loadCachedToken: function loadCachedToken() {
284
+ return _async_to_generator(function() {
285
+ var document, existingData, result, _ref, _existingData_data, tokensArray;
286
+ return _ts_generator(this, function(_state) {
287
+ switch(_state.label){
288
+ case 0:
289
+ document = loadZohoAccessTokenSystemState(systemStateDocumentAccessor);
290
+ return [
291
+ 4,
292
+ document.snapshotData()
293
+ ];
294
+ case 1:
295
+ existingData = _state.sent();
296
+ result = null;
297
+ if (existingData != null) {
298
+ tokensArray = (_ref = existingData === null || existingData === void 0 ? void 0 : (_existingData_data = existingData.data) === null || _existingData_data === void 0 ? void 0 : _existingData_data.tokens) !== null && _ref !== void 0 ? _ref : [];
299
+ result = tokensArray.find(function(x) {
300
+ return x.key === serviceKey;
301
+ });
302
+ }
303
+ return [
304
+ 2,
305
+ result
306
+ ];
77
307
  }
78
- ];
79
- const templateOrUpdate = {
80
- data: {
81
- tokens,
82
- lat: new Date()
308
+ });
309
+ })();
310
+ },
311
+ updateCachedToken: function updateCachedToken(accessToken) {
312
+ return _async_to_generator(function() {
313
+ return _ts_generator(this, function(_state) {
314
+ switch(_state.label){
315
+ case 0:
316
+ // run in a transaction
317
+ return [
318
+ 4,
319
+ systemStateCollection.firestoreContext.runTransaction(function(transaction) {
320
+ return _async_to_generator(function() {
321
+ var _ref, _existingData_data, documentInTransaction, existingData, existingTokens, tokens, templateOrUpdate;
322
+ return _ts_generator(this, function(_state) {
323
+ switch(_state.label){
324
+ case 0:
325
+ documentInTransaction = loadZohoAccessTokenSystemState(systemStateCollection.documentAccessorForTransaction(transaction));
326
+ return [
327
+ 4,
328
+ documentInTransaction.snapshotData()
329
+ ];
330
+ case 1:
331
+ existingData = _state.sent();
332
+ existingTokens = (_ref = existingData === null || existingData === void 0 ? void 0 : (_existingData_data = existingData.data) === null || _existingData_data === void 0 ? void 0 : _existingData_data.tokens) !== null && _ref !== void 0 ? _ref : [];
333
+ tokens = // filter any potential old token for this service key
334
+ _to_consumable_array(existingTokens.filter(function(x) {
335
+ return x.key !== serviceKey;
336
+ })).concat([
337
+ // add the new token
338
+ _object_spread_props(_object_spread({}, accessToken), {
339
+ key: serviceKey
340
+ })
341
+ ]);
342
+ templateOrUpdate = {
343
+ data: {
344
+ tokens: tokens,
345
+ lat: new Date()
346
+ }
347
+ };
348
+ if (!!existingData) return [
349
+ 3,
350
+ 3
351
+ ];
352
+ return [
353
+ 4,
354
+ documentInTransaction.create(templateOrUpdate)
355
+ ];
356
+ case 2:
357
+ _state.sent();
358
+ return [
359
+ 3,
360
+ 5
361
+ ];
362
+ case 3:
363
+ return [
364
+ 4,
365
+ documentInTransaction.update(templateOrUpdate)
366
+ ];
367
+ case 4:
368
+ _state.sent();
369
+ _state.label = 5;
370
+ case 5:
371
+ return [
372
+ 2
373
+ ];
374
+ }
375
+ });
376
+ })();
377
+ })
378
+ ];
379
+ case 1:
380
+ _state.sent();
381
+ return [
382
+ 2
383
+ ];
83
384
  }
84
- };
85
- // create/update depending on the current document's existence
86
- if (!existingData) {
87
- await documentInTransaction.create(templateOrUpdate);
88
- }
89
- else {
90
- await documentInTransaction.update(templateOrUpdate);
91
- }
92
- });
385
+ });
386
+ })();
93
387
  },
94
- clearCachedToken: async function () {
95
- await systemStateCollection.firestoreContext.runTransaction(async (transaction) => {
96
- const documentInTransaction = loadZohoAccessTokenSystemState(systemStateCollection.documentAccessorForTransaction(transaction));
97
- const existingData = await documentInTransaction.snapshotData();
98
- const templateOrUpdate = {
99
- data: {
100
- tokens: [],
101
- lat: new Date()
388
+ clearCachedToken: function clearCachedToken() {
389
+ return _async_to_generator(function() {
390
+ return _ts_generator(this, function(_state) {
391
+ switch(_state.label){
392
+ case 0:
393
+ return [
394
+ 4,
395
+ systemStateCollection.firestoreContext.runTransaction(function(transaction) {
396
+ return _async_to_generator(function() {
397
+ var documentInTransaction, existingData, templateOrUpdate;
398
+ return _ts_generator(this, function(_state) {
399
+ switch(_state.label){
400
+ case 0:
401
+ documentInTransaction = loadZohoAccessTokenSystemState(systemStateCollection.documentAccessorForTransaction(transaction));
402
+ return [
403
+ 4,
404
+ documentInTransaction.snapshotData()
405
+ ];
406
+ case 1:
407
+ existingData = _state.sent();
408
+ templateOrUpdate = {
409
+ data: {
410
+ tokens: [],
411
+ lat: new Date()
412
+ }
413
+ };
414
+ if (!existingData) return [
415
+ 3,
416
+ 3
417
+ ];
418
+ return [
419
+ 4,
420
+ documentInTransaction.update(templateOrUpdate)
421
+ ];
422
+ case 2:
423
+ _state.sent();
424
+ _state.label = 3;
425
+ case 3:
426
+ return [
427
+ 2
428
+ ];
429
+ }
430
+ });
431
+ })();
432
+ })
433
+ ];
434
+ case 1:
435
+ _state.sent();
436
+ return [
437
+ 2
438
+ ];
102
439
  }
103
- };
104
- // clear the tokens
105
- if (existingData) {
106
- await documentInTransaction.update(templateOrUpdate);
107
- }
108
- });
440
+ });
441
+ })();
109
442
  }
110
443
  };
111
444
  return cache;