@aws-amplify/datastore 5.0.1-api-v6-models.9351bcf.0 → 5.0.1-api-v6-models.891fe0d.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 (70) hide show
  1. package/lib/authModeStrategies/defaultAuthStrategy.js +1 -1
  2. package/lib/authModeStrategies/multiAuthStrategy.js +29 -105
  3. package/lib/datastore/datastore.js +887 -1370
  4. package/lib/index.js +1 -1
  5. package/lib/predicates/index.js +53 -102
  6. package/lib/predicates/next.js +310 -557
  7. package/lib/predicates/sort.js +24 -27
  8. package/lib/ssr/index.js +1 -1
  9. package/lib/storage/adapter/AsyncStorageAdapter.js +115 -449
  10. package/lib/storage/adapter/AsyncStorageDatabase.js +215 -480
  11. package/lib/storage/adapter/InMemoryStore.js +27 -101
  12. package/lib/storage/adapter/InMemoryStore.native.js +1 -1
  13. package/lib/storage/adapter/IndexedDBAdapter.js +441 -1002
  14. package/lib/storage/adapter/StorageAdapterBase.js +177 -396
  15. package/lib/storage/adapter/getDefaultAdapter/index.js +5 -6
  16. package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -2
  17. package/lib/storage/relationship.js +174 -260
  18. package/lib/storage/storage.js +244 -507
  19. package/lib/sync/datastoreConnectivity.js +29 -84
  20. package/lib/sync/datastoreReachability/index.js +1 -1
  21. package/lib/sync/datastoreReachability/index.native.js +2 -2
  22. package/lib/sync/index.js +512 -885
  23. package/lib/sync/merger.js +30 -133
  24. package/lib/sync/outbox.js +147 -302
  25. package/lib/sync/processors/errorMaps.js +30 -80
  26. package/lib/sync/processors/mutation.js +331 -579
  27. package/lib/sync/processors/subscription.js +268 -428
  28. package/lib/sync/processors/sync.js +276 -464
  29. package/lib/sync/utils.js +248 -393
  30. package/lib/tsconfig.tsbuildinfo +1 -1
  31. package/lib/types.js +16 -58
  32. package/lib/util.js +335 -575
  33. package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -1
  34. package/lib-esm/authModeStrategies/multiAuthStrategy.js +27 -103
  35. package/lib-esm/datastore/datastore.js +874 -1359
  36. package/lib-esm/index.js +6 -6
  37. package/lib-esm/predicates/index.js +54 -104
  38. package/lib-esm/predicates/next.js +306 -555
  39. package/lib-esm/predicates/sort.js +24 -27
  40. package/lib-esm/ssr/index.js +1 -1
  41. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -446
  42. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -477
  43. package/lib-esm/storage/adapter/InMemoryStore.js +27 -102
  44. package/lib-esm/storage/adapter/IndexedDBAdapter.js +436 -997
  45. package/lib-esm/storage/adapter/StorageAdapterBase.js +172 -392
  46. package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -3
  47. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -1
  48. package/lib-esm/storage/relationship.js +173 -260
  49. package/lib-esm/storage/storage.js +236 -499
  50. package/lib-esm/sync/datastoreConnectivity.js +26 -82
  51. package/lib-esm/sync/datastoreReachability/index.js +1 -1
  52. package/lib-esm/sync/datastoreReachability/index.native.js +1 -1
  53. package/lib-esm/sync/index.js +498 -872
  54. package/lib-esm/sync/merger.js +28 -131
  55. package/lib-esm/sync/outbox.js +143 -298
  56. package/lib-esm/sync/processors/errorMaps.js +32 -82
  57. package/lib-esm/sync/processors/mutation.js +324 -572
  58. package/lib-esm/sync/processors/subscription.js +258 -418
  59. package/lib-esm/sync/processors/sync.js +269 -457
  60. package/lib-esm/sync/utils.js +245 -390
  61. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  62. package/lib-esm/types.js +16 -59
  63. package/lib-esm/util.js +335 -577
  64. package/package.json +12 -12
  65. package/src/datastore/datastore.ts +4 -3
  66. package/src/storage/adapter/getDefaultAdapter/index.ts +1 -3
  67. package/src/sync/processors/mutation.ts +1 -1
  68. package/src/sync/processors/sync.ts +1 -1
  69. package/src/sync/utils.ts +1 -1
  70. package/src/util.ts +44 -6
@@ -4,5 +4,5 @@ exports.defaultAuthStrategy = void 0;
4
4
  // Default behavior is to use the primary auth mode for an API,
5
5
  // so we are returning an empty array so that DataStore will default
6
6
  // to using the primary auth mode.
7
- var defaultAuthStrategy = function () { return []; };
7
+ const defaultAuthStrategy = () => [];
8
8
  exports.defaultAuthStrategy = defaultAuthStrategy;
@@ -1,71 +1,10 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- 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;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __read = (this && this.__read) || function (o, n) {
39
- var m = typeof Symbol === "function" && o[Symbol.iterator];
40
- if (!m) return o;
41
- var i = m.call(o), r, ar = [], e;
42
- try {
43
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
- }
45
- catch (error) { e = { error: error }; }
46
- finally {
47
- try {
48
- if (r && !r.done && (m = i["return"])) m.call(i);
49
- }
50
- finally { if (e) throw e.error; }
51
- }
52
- return ar;
53
- };
54
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
55
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
56
- if (ar || !(i in from)) {
57
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
58
- ar[i] = from[i];
59
- }
60
- }
61
- return to.concat(ar || Array.prototype.slice.call(from));
62
- };
63
2
  Object.defineProperty(exports, "__esModule", { value: true });
64
3
  exports.multiAuthStrategy = void 0;
65
4
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
66
5
  // SPDX-License-Identifier: Apache-2.0
67
- var core_1 = require("@aws-amplify/core");
68
- var types_1 = require("../types");
6
+ const core_1 = require("@aws-amplify/core");
7
+ const types_1 = require("../types");
69
8
  function getProviderFromRule(rule) {
70
9
  // private with no provider means userPools
71
10
  if (rule.allow === 'private' && !rule.provider) {
@@ -78,21 +17,21 @@ function getProviderFromRule(rule) {
78
17
  return rule.provider;
79
18
  }
80
19
  function sortAuthRulesWithPriority(rules) {
81
- var allowSortPriority = [
20
+ const allowSortPriority = [
82
21
  types_1.ModelAttributeAuthAllow.CUSTOM,
83
22
  types_1.ModelAttributeAuthAllow.OWNER,
84
23
  types_1.ModelAttributeAuthAllow.GROUPS,
85
24
  types_1.ModelAttributeAuthAllow.PRIVATE,
86
25
  types_1.ModelAttributeAuthAllow.PUBLIC,
87
26
  ];
88
- var providerSortPriority = [
27
+ const providerSortPriority = [
89
28
  types_1.ModelAttributeAuthProvider.FUNCTION,
90
29
  types_1.ModelAttributeAuthProvider.USER_POOLS,
91
30
  types_1.ModelAttributeAuthProvider.OIDC,
92
31
  types_1.ModelAttributeAuthProvider.IAM,
93
32
  types_1.ModelAttributeAuthProvider.API_KEY,
94
33
  ];
95
- return __spreadArray([], __read(rules), false).sort(function (a, b) {
34
+ return [...rules].sort((a, b) => {
96
35
  if (a.allow === b.allow) {
97
36
  return (providerSortPriority.indexOf(getProviderFromRule(a)) -
98
37
  providerSortPriority.indexOf(getProviderFromRule(b)));
@@ -100,11 +39,10 @@ function sortAuthRulesWithPriority(rules) {
100
39
  return (allowSortPriority.indexOf(a.allow) - allowSortPriority.indexOf(b.allow));
101
40
  });
102
41
  }
103
- function getAuthRules(_a) {
104
- var rules = _a.rules, currentUser = _a.currentUser;
42
+ function getAuthRules({ rules, currentUser, }) {
105
43
  // Using Set to ensure uniqueness
106
- var authModes = new Set();
107
- rules.forEach(function (rule) {
44
+ const authModes = new Set();
45
+ rules.forEach(rule => {
108
46
  switch (rule.allow) {
109
47
  case types_1.ModelAttributeAuthAllow.CUSTOM:
110
48
  // custom with no provider -> function
@@ -168,40 +106,26 @@ function getAuthRules(_a) {
168
106
  * @param param0 The `{schema, modelName}` to inspect.
169
107
  * @returns A sorted array of auth modes to attempt.
170
108
  */
171
- var multiAuthStrategy = function (amplifyContext) {
172
- return function (_a) {
173
- var schema = _a.schema, modelName = _a.modelName;
174
- return __awaiter(void 0, void 0, void 0, function () {
175
- var currentUser, authSession, e_1, attributes, authAttribute, sortedRules;
176
- var _b;
177
- return __generator(this, function (_c) {
178
- switch (_c.label) {
179
- case 0:
180
- _c.trys.push([0, 2, , 3]);
181
- return [4 /*yield*/, (0, core_1.fetchAuthSession)()];
182
- case 1:
183
- authSession = _c.sent();
184
- if (authSession.tokens.accessToken) {
185
- // the user is authenticated
186
- currentUser = authSession;
187
- }
188
- return [3 /*break*/, 3];
189
- case 2:
190
- e_1 = _c.sent();
191
- return [3 /*break*/, 3];
192
- case 3:
193
- attributes = schema.namespaces.user.models[modelName].attributes;
194
- if (attributes) {
195
- authAttribute = attributes.find(function (attr) { return attr.type === 'auth'; });
196
- if ((_b = authAttribute === null || authAttribute === void 0 ? void 0 : authAttribute.properties) === null || _b === void 0 ? void 0 : _b.rules) {
197
- sortedRules = sortAuthRulesWithPriority(authAttribute.properties.rules);
198
- return [2 /*return*/, getAuthRules({ currentUser: currentUser, rules: sortedRules })];
199
- }
200
- }
201
- return [2 /*return*/, []];
202
- }
203
- });
204
- });
205
- };
109
+ const multiAuthStrategy = (amplifyContext) => async ({ schema, modelName }) => {
110
+ let currentUser;
111
+ try {
112
+ const authSession = await (0, core_1.fetchAuthSession)();
113
+ if (authSession.tokens.accessToken) {
114
+ // the user is authenticated
115
+ currentUser = authSession;
116
+ }
117
+ }
118
+ catch (e) {
119
+ // No current user
120
+ }
121
+ const { attributes } = schema.namespaces.user.models[modelName];
122
+ if (attributes) {
123
+ const authAttribute = attributes.find(attr => attr.type === 'auth');
124
+ if (authAttribute?.properties?.rules) {
125
+ const sortedRules = sortAuthRulesWithPriority(authAttribute.properties.rules);
126
+ return getAuthRules({ currentUser, rules: sortedRules });
127
+ }
128
+ }
129
+ return [];
206
130
  };
207
131
  exports.multiAuthStrategy = multiAuthStrategy;