@aws-amplify/datastore 0.0.0-unstable-1ca644b-20260128091132

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 (200) hide show
  1. package/README.md +158 -0
  2. package/dist/cjs/authModeStrategies/defaultAuthStrategy.js +10 -0
  3. package/dist/cjs/authModeStrategies/defaultAuthStrategy.js.map +1 -0
  4. package/dist/cjs/authModeStrategies/index.js +11 -0
  5. package/dist/cjs/authModeStrategies/index.js.map +1 -0
  6. package/dist/cjs/authModeStrategies/multiAuthStrategy.js +131 -0
  7. package/dist/cjs/authModeStrategies/multiAuthStrategy.js.map +1 -0
  8. package/dist/cjs/datastore/datastore.js +1914 -0
  9. package/dist/cjs/datastore/datastore.js.map +1 -0
  10. package/dist/cjs/datastore/utils.js +11 -0
  11. package/dist/cjs/datastore/utils.js.map +1 -0
  12. package/dist/cjs/index.js +31 -0
  13. package/dist/cjs/index.js.map +1 -0
  14. package/dist/cjs/predicates/index.js +248 -0
  15. package/dist/cjs/predicates/index.js.map +1 -0
  16. package/dist/cjs/predicates/next.js +796 -0
  17. package/dist/cjs/predicates/next.js.map +1 -0
  18. package/dist/cjs/predicates/sort.js +53 -0
  19. package/dist/cjs/predicates/sort.js.map +1 -0
  20. package/dist/cjs/storage/adapter/AsyncStorageAdapter.js +148 -0
  21. package/dist/cjs/storage/adapter/AsyncStorageAdapter.js.map +1 -0
  22. package/dist/cjs/storage/adapter/AsyncStorageDatabase.js +264 -0
  23. package/dist/cjs/storage/adapter/AsyncStorageDatabase.js.map +1 -0
  24. package/dist/cjs/storage/adapter/InMemoryStore.js +45 -0
  25. package/dist/cjs/storage/adapter/InMemoryStore.js.map +1 -0
  26. package/dist/cjs/storage/adapter/InMemoryStore.native.js +13 -0
  27. package/dist/cjs/storage/adapter/InMemoryStore.native.js.map +1 -0
  28. package/dist/cjs/storage/adapter/IndexedDBAdapter.js +547 -0
  29. package/dist/cjs/storage/adapter/IndexedDBAdapter.js.map +1 -0
  30. package/dist/cjs/storage/adapter/StorageAdapterBase.js +293 -0
  31. package/dist/cjs/storage/adapter/StorageAdapterBase.js.map +1 -0
  32. package/dist/cjs/storage/adapter/getDefaultAdapter/index.js +18 -0
  33. package/dist/cjs/storage/adapter/getDefaultAdapter/index.js.map +1 -0
  34. package/dist/cjs/storage/adapter/getDefaultAdapter/index.native.js +11 -0
  35. package/dist/cjs/storage/adapter/getDefaultAdapter/index.native.js.map +1 -0
  36. package/dist/cjs/storage/adapter/index.js +4 -0
  37. package/dist/cjs/storage/adapter/index.js.map +1 -0
  38. package/dist/cjs/storage/relationship.js +273 -0
  39. package/dist/cjs/storage/relationship.js.map +1 -0
  40. package/dist/cjs/storage/storage.js +337 -0
  41. package/dist/cjs/storage/storage.js.map +1 -0
  42. package/dist/cjs/sync/datastoreConnectivity.js +54 -0
  43. package/dist/cjs/sync/datastoreConnectivity.js.map +1 -0
  44. package/dist/cjs/sync/datastoreReachability/index.js +9 -0
  45. package/dist/cjs/sync/datastoreReachability/index.js.map +1 -0
  46. package/dist/cjs/sync/datastoreReachability/index.native.js +10 -0
  47. package/dist/cjs/sync/datastoreReachability/index.native.js.map +1 -0
  48. package/dist/cjs/sync/index.js +726 -0
  49. package/dist/cjs/sync/index.js.map +1 -0
  50. package/dist/cjs/sync/merger.js +46 -0
  51. package/dist/cjs/sync/merger.js.map +1 -0
  52. package/dist/cjs/sync/outbox.js +203 -0
  53. package/dist/cjs/sync/outbox.js.map +1 -0
  54. package/dist/cjs/sync/processors/errorMaps.js +79 -0
  55. package/dist/cjs/sync/processors/errorMaps.js.map +1 -0
  56. package/dist/cjs/sync/processors/mutation.js +440 -0
  57. package/dist/cjs/sync/processors/mutation.js.map +1 -0
  58. package/dist/cjs/sync/processors/subscription.js +381 -0
  59. package/dist/cjs/sync/processors/subscription.js.map +1 -0
  60. package/dist/cjs/sync/processors/sync.js +325 -0
  61. package/dist/cjs/sync/processors/sync.js.map +1 -0
  62. package/dist/cjs/sync/utils.js +700 -0
  63. package/dist/cjs/sync/utils.js.map +1 -0
  64. package/dist/cjs/types.js +343 -0
  65. package/dist/cjs/types.js.map +1 -0
  66. package/dist/cjs/util.js +949 -0
  67. package/dist/cjs/util.js.map +1 -0
  68. package/dist/esm/authModeStrategies/defaultAuthStrategy.d.ts +2 -0
  69. package/dist/esm/authModeStrategies/defaultAuthStrategy.mjs +7 -0
  70. package/dist/esm/authModeStrategies/defaultAuthStrategy.mjs.map +1 -0
  71. package/dist/esm/authModeStrategies/index.d.ts +2 -0
  72. package/dist/esm/authModeStrategies/index.mjs +3 -0
  73. package/dist/esm/authModeStrategies/index.mjs.map +1 -0
  74. package/dist/esm/authModeStrategies/multiAuthStrategy.d.ts +13 -0
  75. package/dist/esm/authModeStrategies/multiAuthStrategy.mjs +129 -0
  76. package/dist/esm/authModeStrategies/multiAuthStrategy.mjs.map +1 -0
  77. package/dist/esm/datastore/datastore.d.ts +229 -0
  78. package/dist/esm/datastore/datastore.mjs +1912 -0
  79. package/dist/esm/datastore/datastore.mjs.map +1 -0
  80. package/dist/esm/datastore/utils.d.ts +1 -0
  81. package/dist/esm/datastore/utils.mjs +8 -0
  82. package/dist/esm/datastore/utils.mjs.map +1 -0
  83. package/dist/esm/index.d.ts +16 -0
  84. package/dist/esm/index.mjs +19 -0
  85. package/dist/esm/index.mjs.map +1 -0
  86. package/dist/esm/predicates/index.d.ts +100 -0
  87. package/dist/esm/predicates/index.mjs +243 -0
  88. package/dist/esm/predicates/index.mjs.map +1 -0
  89. package/dist/esm/predicates/next.d.ts +317 -0
  90. package/dist/esm/predicates/next.mjs +790 -0
  91. package/dist/esm/predicates/next.mjs.map +1 -0
  92. package/dist/esm/predicates/sort.d.ts +8 -0
  93. package/dist/esm/predicates/sort.mjs +50 -0
  94. package/dist/esm/predicates/sort.mjs.map +1 -0
  95. package/dist/esm/storage/adapter/AsyncStorageAdapter.d.ts +40 -0
  96. package/dist/esm/storage/adapter/AsyncStorageAdapter.mjs +145 -0
  97. package/dist/esm/storage/adapter/AsyncStorageAdapter.mjs.map +1 -0
  98. package/dist/esm/storage/adapter/AsyncStorageDatabase.d.ts +39 -0
  99. package/dist/esm/storage/adapter/AsyncStorageDatabase.mjs +263 -0
  100. package/dist/esm/storage/adapter/AsyncStorageDatabase.mjs.map +1 -0
  101. package/dist/esm/storage/adapter/InMemoryStore.d.ts +11 -0
  102. package/dist/esm/storage/adapter/InMemoryStore.mjs +41 -0
  103. package/dist/esm/storage/adapter/InMemoryStore.mjs.map +1 -0
  104. package/dist/esm/storage/adapter/InMemoryStore.native.d.ts +1 -0
  105. package/dist/esm/storage/adapter/InMemoryStore.native.mjs +11 -0
  106. package/dist/esm/storage/adapter/InMemoryStore.native.mjs.map +1 -0
  107. package/dist/esm/storage/adapter/IndexedDBAdapter.d.ts +70 -0
  108. package/dist/esm/storage/adapter/IndexedDBAdapter.mjs +546 -0
  109. package/dist/esm/storage/adapter/IndexedDBAdapter.mjs.map +1 -0
  110. package/dist/esm/storage/adapter/StorageAdapterBase.d.ts +134 -0
  111. package/dist/esm/storage/adapter/StorageAdapterBase.mjs +291 -0
  112. package/dist/esm/storage/adapter/StorageAdapterBase.mjs.map +1 -0
  113. package/dist/esm/storage/adapter/getDefaultAdapter/index.d.ts +3 -0
  114. package/dist/esm/storage/adapter/getDefaultAdapter/index.mjs +15 -0
  115. package/dist/esm/storage/adapter/getDefaultAdapter/index.mjs.map +1 -0
  116. package/dist/esm/storage/adapter/getDefaultAdapter/index.native.d.ts +3 -0
  117. package/dist/esm/storage/adapter/getDefaultAdapter/index.native.mjs +9 -0
  118. package/dist/esm/storage/adapter/getDefaultAdapter/index.native.mjs.map +1 -0
  119. package/dist/esm/storage/adapter/index.d.ts +9 -0
  120. package/dist/esm/storage/adapter/index.mjs +2 -0
  121. package/dist/esm/storage/adapter/index.mjs.map +1 -0
  122. package/dist/esm/storage/relationship.d.ts +149 -0
  123. package/dist/esm/storage/relationship.mjs +271 -0
  124. package/dist/esm/storage/relationship.mjs.map +1 -0
  125. package/dist/esm/storage/storage.d.ts +50 -0
  126. package/dist/esm/storage/storage.mjs +334 -0
  127. package/dist/esm/storage/storage.mjs.map +1 -0
  128. package/dist/esm/sync/datastoreConnectivity.d.ts +16 -0
  129. package/dist/esm/sync/datastoreConnectivity.mjs +53 -0
  130. package/dist/esm/sync/datastoreConnectivity.mjs.map +1 -0
  131. package/dist/esm/sync/datastoreReachability/index.d.ts +1 -0
  132. package/dist/esm/sync/datastoreReachability/index.mjs +8 -0
  133. package/dist/esm/sync/datastoreReachability/index.mjs.map +1 -0
  134. package/dist/esm/sync/datastoreReachability/index.native.d.ts +1 -0
  135. package/dist/esm/sync/datastoreReachability/index.native.mjs +9 -0
  136. package/dist/esm/sync/datastoreReachability/index.native.mjs.map +1 -0
  137. package/dist/esm/sync/index.d.ts +103 -0
  138. package/dist/esm/sync/index.mjs +723 -0
  139. package/dist/esm/sync/index.mjs.map +1 -0
  140. package/dist/esm/sync/merger.d.ts +17 -0
  141. package/dist/esm/sync/merger.mjs +44 -0
  142. package/dist/esm/sync/merger.mjs.map +1 -0
  143. package/dist/esm/sync/outbox.d.ts +27 -0
  144. package/dist/esm/sync/outbox.mjs +201 -0
  145. package/dist/esm/sync/outbox.mjs.map +1 -0
  146. package/dist/esm/sync/processors/errorMaps.d.ts +15 -0
  147. package/dist/esm/sync/processors/errorMaps.mjs +74 -0
  148. package/dist/esm/sync/processors/errorMaps.mjs.map +1 -0
  149. package/dist/esm/sync/processors/mutation.d.ts +67 -0
  150. package/dist/esm/sync/processors/mutation.mjs +437 -0
  151. package/dist/esm/sync/processors/mutation.mjs.map +1 -0
  152. package/dist/esm/sync/processors/subscription.d.ts +42 -0
  153. package/dist/esm/sync/processors/subscription.mjs +379 -0
  154. package/dist/esm/sync/processors/subscription.mjs.map +1 -0
  155. package/dist/esm/sync/processors/sync.d.ts +28 -0
  156. package/dist/esm/sync/processors/sync.mjs +323 -0
  157. package/dist/esm/sync/processors/sync.mjs.map +1 -0
  158. package/dist/esm/sync/utils.d.ts +103 -0
  159. package/dist/esm/sync/utils.mjs +679 -0
  160. package/dist/esm/sync/utils.mjs.map +1 -0
  161. package/dist/esm/types.d.ts +754 -0
  162. package/dist/esm/types.mjs +324 -0
  163. package/dist/esm/types.mjs.map +1 -0
  164. package/dist/esm/util.d.ts +197 -0
  165. package/dist/esm/util.mjs +898 -0
  166. package/dist/esm/util.mjs.map +1 -0
  167. package/package.json +75 -0
  168. package/src/authModeStrategies/defaultAuthStrategy.ts +8 -0
  169. package/src/authModeStrategies/index.ts +4 -0
  170. package/src/authModeStrategies/multiAuthStrategy.ts +172 -0
  171. package/src/datastore/datastore.ts +2787 -0
  172. package/src/datastore/utils.ts +7 -0
  173. package/src/index.ts +39 -0
  174. package/src/predicates/index.ts +302 -0
  175. package/src/predicates/next.ts +1013 -0
  176. package/src/predicates/sort.ts +92 -0
  177. package/src/storage/adapter/AsyncStorageAdapter.ts +270 -0
  178. package/src/storage/adapter/AsyncStorageDatabase.ts +364 -0
  179. package/src/storage/adapter/InMemoryStore.native.ts +8 -0
  180. package/src/storage/adapter/InMemoryStore.ts +50 -0
  181. package/src/storage/adapter/IndexedDBAdapter.ts +792 -0
  182. package/src/storage/adapter/StorageAdapterBase.ts +553 -0
  183. package/src/storage/adapter/getDefaultAdapter/index.native.ts +11 -0
  184. package/src/storage/adapter/getDefaultAdapter/index.ts +18 -0
  185. package/src/storage/adapter/index.ts +37 -0
  186. package/src/storage/relationship.ts +287 -0
  187. package/src/storage/storage.ts +587 -0
  188. package/src/sync/datastoreConnectivity.ts +71 -0
  189. package/src/sync/datastoreReachability/index.native.ts +8 -0
  190. package/src/sync/datastoreReachability/index.ts +5 -0
  191. package/src/sync/index.ts +1127 -0
  192. package/src/sync/merger.ts +74 -0
  193. package/src/sync/outbox.ts +328 -0
  194. package/src/sync/processors/errorMaps.ts +99 -0
  195. package/src/sync/processors/mutation.ts +703 -0
  196. package/src/sync/processors/subscription.ts +737 -0
  197. package/src/sync/processors/sync.ts +524 -0
  198. package/src/sync/utils.ts +963 -0
  199. package/src/types.ts +1374 -0
  200. package/src/util.ts +1254 -0
package/README.md ADDED
@@ -0,0 +1,158 @@
1
+ > INTERNAL USE ONLY
2
+
3
+ This package contains the AWS Amplify DataStore category and is intended for internal use only. To integrate Amplify into your app, please use [aws-amplify](https://www.npmjs.com/package/aws-amplify).
4
+
5
+ # AWS Amplify DataStore Docs
6
+
7
+ [Amplify DataStore](https://docs.amplify.aws/lib/datastore/getting-started/q/platform/js/) provides a programming model for leveraging shared and distributed data without writing additional code for offline and online scenarios, which makes working with distributed, cross-user data just as simple as working with local-only data.
8
+
9
+ ---
10
+
11
+ | package | version | open issues | closed issues |
12
+ | ---------------------- | --------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
13
+ | @aws-amplify/datastore | ![npm](https://img.shields.io/npm/v/@aws-amplify/datastore.svg) | [![Open Issues](https://img.shields.io/github/issues/aws-amplify/amplify-js/DataStore?color=red)](https://github.com/aws-amplify/amplify-js/issues?q=is%3Aissue+label%3ADataStore+is%3Aopen) | [![Closed Issues](https://img.shields.io/github/issues-closed/aws-amplify/amplify-js/DataStore)](https://github.com/aws-amplify/amplify-js/issues?q=is%3Aissue+label%3ADataStore+is%3Aclosed) |
14
+
15
+ ---
16
+
17
+ ## **👋 Note For Contributers: 👋**
18
+
19
+ _**Please update these docs any time you find something that is incorrect or lacking. In particular, if a line in the docs prompts a question, take a moment to figure out the answer, then update the docs with the necessary detail.**_
20
+
21
+ ---
22
+
23
+ ## Getting Started
24
+
25
+ Before you start reading through these docs, take a moment to understand [how DataStore works at a high level](https://docs.amplify.aws/lib/datastore/how-it-works/q/platform/js/). Additionally, we recommend first reading through [docs.amplify.aws](https://docs.amplify.aws/lib/datastore/getting-started/q/platform/js/). The purpose of these docs is to dive deep into the codebase itself and understand the inner workings of DataStore for the purpose of contributing. Understanding these docs is **not** necessary for using DataStore. Lastly, before reading, take a look at [the diagrams below](#diagrams).
26
+
27
+ ---
28
+
29
+ ## Docs
30
+
31
+ - [Conflict Resolution](docs/conflict-resolution.md)
32
+ - [Contributing](docs/contributing.md)
33
+ - [DataStore Lifecycle Events ("Start", "Stop", "Clear")](docs/datastore-lifecycle-events.md)
34
+ - This explains how DataStore fundementally works, and is a great place to start.
35
+ - [Getting Started](docs/getting-started.md) (Running against a sample app, etc.)
36
+ - [Namespaces](docs/namespaces.md)
37
+ - [How DataStore uses Observables](docs/observables.md)
38
+ - [Schema Changes](docs/schema-changes.md)
39
+ - [Storage](docs/storage.md)
40
+ - [Sync Engine](docs/sync-engine.md)
41
+ - ["Unsupported hacks" / workarounds](docs/workarounds.md)
42
+
43
+ ---
44
+
45
+ # Diagrams
46
+
47
+ _Note: relationships with dotted lines are explained more in a separate diagram._
48
+
49
+ ## How the DataStore API and Storage Engine Interact
50
+
51
+ ```mermaid
52
+ flowchart TD
53
+ %% API and Storage
54
+ api[[DS API]]-- observe -->storage{Storage Engine}
55
+ storage-- next -->adapter[[Adapter]]
56
+ adapter-->db[[Local DB]]
57
+ db-->api
58
+ sync[[Sync Engine*]]-.-storage
59
+ sync-.-appSync[(AppSync)]
60
+ ```
61
+
62
+ # How the Sync Engine Observes Changes in Storage and AppSync
63
+
64
+ _Note: All green nodes belong to the Sync Engine._
65
+
66
+ \* Merger first checks outbox
67
+
68
+ \*\* Outbox sends outgoing messages to AppSync
69
+
70
+ ```mermaid
71
+ flowchart TD
72
+
73
+ subgraph SyncEngine
74
+ index{index.ts}-- observe -->reach[Core reachability]
75
+
76
+ subgraph processors
77
+ mp[Mutation Processor]
78
+ sp[Subscription Processor]
79
+ syp[Sync Processor]
80
+ end
81
+
82
+ reach--next-->mp[Mutation Processor]
83
+ reach--next-->sp[Subscription Processor]
84
+ reach--next-->syp[Sync Processor]
85
+
86
+ subgraph outbox / merger
87
+ outbox[Outbox]
88
+ merger[Merger]
89
+ outbox---merger
90
+ end
91
+
92
+ end
93
+
94
+ api[DS API]-.->storage
95
+ mp-- 1. observe -->storage{Storage Engine}
96
+ storage-- 2. next -->merger[merger*]-- next -->storage
97
+
98
+
99
+ sp-- observe -->appsync[(AppSync)]
100
+ appsync-- next -->sp
101
+
102
+ syp---appsync
103
+
104
+ mp-->outbox[outbox**]
105
+
106
+ appsync<--->outbox
107
+ %% styling
108
+ classDef syncEngineClass fill:#8FB,stroke:#333,stroke-width:4px,color:#333;
109
+ class index,mp,sp,syp,merger,outbox syncEngineClass;
110
+ ```
111
+
112
+ ---
113
+
114
+ # Project Structure
115
+
116
+ <pre>
117
+ amplify-js/packages/datastore/src
118
+ ├── authModeStrategies
119
+ │ └── defaultAuthStraegy.ts
120
+ │ └── index.ts
121
+ │ └── multiAuthStrategy.ts
122
+ ├── datastore
123
+ │ └── datastore.ts # Entry point for DataStore
124
+ ├── predicates
125
+ │ └── index.ts
126
+ │ └── sort.ts
127
+ ├── ssr
128
+ ├── storage # Storage Engine
129
+ │ └── adapter # Platform-specific Storage Adapters
130
+ │ └── getDefaultAdapter
131
+ │ └── AsyncStorageAdapter.ts
132
+ │ └── AsyncStorageDatabase.ts
133
+ │ └── index.ts
134
+ │ └── IndexedDBAdapter.ts
135
+ │ └── InMemoryStore.native.ts
136
+ │ └── InMemoryStore.ts
137
+ │ └── storage.ts # Entry point for Storage
138
+ ├── sync # Sync Engine
139
+ │ └── dataStoreReachability
140
+ │ └── index.native.ts
141
+ │ └── index.ts
142
+ │ └── processors # Sync Engine Processors
143
+ │ └── mutation.ts
144
+ │ └── subscription.ts
145
+ │ └── sync.ts
146
+ │ └── datastoreConnectivity.ts # Subscribe to reachability monitor
147
+ │ └── index.ts # Entry point for Sync Engine
148
+ │ └── merger.ts # <a href="https://github.com/aws-amplify/amplify-js/blob/datastore-docs/packages/datastore/docs/sync-engine.md#merger" title="merger doc">doc</a>
149
+ │ └── outbox.ts # <a href="https://github.com/aws-amplify/amplify-js/blob/datastore-docs/packages/datastore/docs/sync-engine.md#outbox" title="outbox doc">doc</a>
150
+ </pre>
151
+
152
+ ---
153
+
154
+ ## Other Resources:
155
+
156
+ - [High-level overview of how DataStore works](https://docs.amplify.aws/lib/datastore/how-it-works/q/platform/js/)
157
+ - [DataStore Docs](https://docs.amplify.aws/lib/datastore/getting-started/q/platform/js/)
158
+ - [re:Invent talk](https://www.youtube.com/watch?v=KcYl6_We0EU)
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.defaultAuthStrategy = void 0;
5
+ // Default behavior is to use the primary auth mode for an API,
6
+ // so we are returning an empty array so that DataStore will default
7
+ // to using the primary auth mode.
8
+ const defaultAuthStrategy = () => [];
9
+ exports.defaultAuthStrategy = defaultAuthStrategy;
10
+ //# sourceMappingURL=defaultAuthStrategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaultAuthStrategy.js","sources":["../../../src/authModeStrategies/defaultAuthStrategy.ts"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.defaultAuthStrategy = void 0;\n// Default behavior is to use the primary auth mode for an API,\n// so we are returning an empty array so that DataStore will default\n// to using the primary auth mode.\nconst defaultAuthStrategy = () => [];\nexports.defaultAuthStrategy = defaultAuthStrategy;\n"],"names":[],"mappings":";;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,mBAAmB,GAAG,MAAM;AACpC;AACA;AACA;AACA,MAAM,mBAAmB,GAAG,MAAM,EAAE;AACpC,OAAO,CAAC,mBAAmB,GAAG,mBAAmB;;"}
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.multiAuthStrategy = exports.defaultAuthStrategy = void 0;
5
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6
+ // SPDX-License-Identifier: Apache-2.0
7
+ var defaultAuthStrategy_1 = require("./defaultAuthStrategy");
8
+ Object.defineProperty(exports, "defaultAuthStrategy", { enumerable: true, get: function () { return defaultAuthStrategy_1.defaultAuthStrategy; } });
9
+ var multiAuthStrategy_1 = require("./multiAuthStrategy");
10
+ Object.defineProperty(exports, "multiAuthStrategy", { enumerable: true, get: function () { return multiAuthStrategy_1.multiAuthStrategy; } });
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../../src/authModeStrategies/index.ts"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.multiAuthStrategy = exports.defaultAuthStrategy = void 0;\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nvar defaultAuthStrategy_1 = require(\"./defaultAuthStrategy\");\nObject.defineProperty(exports, \"defaultAuthStrategy\", { enumerable: true, get: function () { return defaultAuthStrategy_1.defaultAuthStrategy; } });\nvar multiAuthStrategy_1 = require(\"./multiAuthStrategy\");\nObject.defineProperty(exports, \"multiAuthStrategy\", { enumerable: true, get: function () { return multiAuthStrategy_1.multiAuthStrategy; } });\n"],"names":[],"mappings":";;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,iBAAiB,GAAG,OAAO,CAAC,mBAAmB,GAAG,MAAM;AAChE;AACA;AACA,IAAI,qBAAqB,GAAG,OAAO,CAAC,uBAAuB,CAAC;AAC5D,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,qBAAqB,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,OAAO,qBAAqB,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,CAAC;AACnJ,IAAI,mBAAmB,GAAG,OAAO,CAAC,qBAAqB,CAAC;AACxD,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,mBAAmB,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,OAAO,mBAAmB,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC;;"}
@@ -0,0 +1,131 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.multiAuthStrategy = void 0;
5
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6
+ // SPDX-License-Identifier: Apache-2.0
7
+ const core_1 = require("@aws-amplify/core");
8
+ const types_1 = require("../types");
9
+ function getProviderFromRule(rule) {
10
+ // private with no provider means userPools
11
+ if (rule.allow === 'private' && !rule.provider) {
12
+ return types_1.ModelAttributeAuthProvider.USER_POOLS;
13
+ }
14
+ // public with no provider means apiKey
15
+ if (rule.allow === 'public' && !rule.provider) {
16
+ return types_1.ModelAttributeAuthProvider.API_KEY;
17
+ }
18
+ return rule.provider;
19
+ }
20
+ function sortAuthRulesWithPriority(rules) {
21
+ const allowSortPriority = [
22
+ types_1.ModelAttributeAuthAllow.CUSTOM,
23
+ types_1.ModelAttributeAuthAllow.OWNER,
24
+ types_1.ModelAttributeAuthAllow.GROUPS,
25
+ types_1.ModelAttributeAuthAllow.PRIVATE,
26
+ types_1.ModelAttributeAuthAllow.PUBLIC,
27
+ ];
28
+ const providerSortPriority = [
29
+ types_1.ModelAttributeAuthProvider.FUNCTION,
30
+ types_1.ModelAttributeAuthProvider.USER_POOLS,
31
+ types_1.ModelAttributeAuthProvider.OIDC,
32
+ types_1.ModelAttributeAuthProvider.IAM,
33
+ types_1.ModelAttributeAuthProvider.API_KEY,
34
+ ];
35
+ return [...rules].sort((a, b) => {
36
+ if (a.allow === b.allow) {
37
+ return (providerSortPriority.indexOf(getProviderFromRule(a)) -
38
+ providerSortPriority.indexOf(getProviderFromRule(b)));
39
+ }
40
+ return (allowSortPriority.indexOf(a.allow) - allowSortPriority.indexOf(b.allow));
41
+ });
42
+ }
43
+ function getAuthRules({ rules, currentUser, }) {
44
+ // Using Set to ensure uniqueness
45
+ const authModes = new Set();
46
+ rules.forEach(rule => {
47
+ switch (rule.allow) {
48
+ case types_1.ModelAttributeAuthAllow.CUSTOM:
49
+ // custom with no provider -> function
50
+ if (!rule.provider ||
51
+ rule.provider === types_1.ModelAttributeAuthProvider.FUNCTION) {
52
+ authModes.add('lambda');
53
+ }
54
+ break;
55
+ case types_1.ModelAttributeAuthAllow.GROUPS:
56
+ case types_1.ModelAttributeAuthAllow.OWNER: {
57
+ // We shouldn't attempt User Pool or OIDC if there isn't an authenticated user
58
+ if (currentUser) {
59
+ if (rule.provider === types_1.ModelAttributeAuthProvider.USER_POOLS) {
60
+ authModes.add('userPool');
61
+ }
62
+ else if (rule.provider === types_1.ModelAttributeAuthProvider.OIDC) {
63
+ authModes.add('oidc');
64
+ }
65
+ }
66
+ break;
67
+ }
68
+ case types_1.ModelAttributeAuthAllow.PRIVATE: {
69
+ // We shouldn't attempt private if there isn't an authenticated user
70
+ if (currentUser) {
71
+ // private with no provider means userPools
72
+ if (!rule.provider ||
73
+ rule.provider === types_1.ModelAttributeAuthProvider.USER_POOLS) {
74
+ authModes.add('userPool');
75
+ }
76
+ else if (rule.provider === types_1.ModelAttributeAuthProvider.IAM) {
77
+ authModes.add('iam');
78
+ }
79
+ }
80
+ break;
81
+ }
82
+ case types_1.ModelAttributeAuthAllow.PUBLIC: {
83
+ if (rule.provider === types_1.ModelAttributeAuthProvider.IAM) {
84
+ authModes.add('iam');
85
+ }
86
+ else if (!rule.provider ||
87
+ rule.provider === types_1.ModelAttributeAuthProvider.API_KEY) {
88
+ // public with no provider means apiKey
89
+ authModes.add('apiKey');
90
+ }
91
+ break;
92
+ }
93
+ }
94
+ });
95
+ return Array.from(authModes);
96
+ }
97
+ /**
98
+ * Returns an array of auth modes to try based on the schema, model, and
99
+ * authenticated user (or lack thereof). Rules are sourced from `getAuthRules`
100
+ * and returned in the order they ought to be attempted.
101
+ *
102
+ * @see sortAuthRulesWithPriority
103
+ * @see getAuthRules
104
+ *
105
+ * @param param0 The `{schema, modelName}` to inspect.
106
+ * @returns A sorted array of auth modes to attempt.
107
+ */
108
+ const multiAuthStrategy = () => async ({ schema, modelName }) => {
109
+ let currentUser;
110
+ try {
111
+ const authSession = await (0, core_1.fetchAuthSession)();
112
+ if (authSession.tokens.accessToken) {
113
+ // the user is authenticated
114
+ currentUser = authSession;
115
+ }
116
+ }
117
+ catch (e) {
118
+ // No current user
119
+ }
120
+ const { attributes } = schema.namespaces.user.models[modelName];
121
+ if (attributes) {
122
+ const authAttribute = attributes.find(attr => attr.type === 'auth');
123
+ if (authAttribute?.properties?.rules) {
124
+ const sortedRules = sortAuthRulesWithPriority(authAttribute.properties.rules);
125
+ return getAuthRules({ currentUser, rules: sortedRules });
126
+ }
127
+ }
128
+ return [];
129
+ };
130
+ exports.multiAuthStrategy = multiAuthStrategy;
131
+ //# sourceMappingURL=multiAuthStrategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"multiAuthStrategy.js","sources":["../../../src/authModeStrategies/multiAuthStrategy.ts"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.multiAuthStrategy = void 0;\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nconst core_1 = require(\"@aws-amplify/core\");\nconst types_1 = require(\"../types\");\nfunction getProviderFromRule(rule) {\n // private with no provider means userPools\n if (rule.allow === 'private' && !rule.provider) {\n return types_1.ModelAttributeAuthProvider.USER_POOLS;\n }\n // public with no provider means apiKey\n if (rule.allow === 'public' && !rule.provider) {\n return types_1.ModelAttributeAuthProvider.API_KEY;\n }\n return rule.provider;\n}\nfunction sortAuthRulesWithPriority(rules) {\n const allowSortPriority = [\n types_1.ModelAttributeAuthAllow.CUSTOM,\n types_1.ModelAttributeAuthAllow.OWNER,\n types_1.ModelAttributeAuthAllow.GROUPS,\n types_1.ModelAttributeAuthAllow.PRIVATE,\n types_1.ModelAttributeAuthAllow.PUBLIC,\n ];\n const providerSortPriority = [\n types_1.ModelAttributeAuthProvider.FUNCTION,\n types_1.ModelAttributeAuthProvider.USER_POOLS,\n types_1.ModelAttributeAuthProvider.OIDC,\n types_1.ModelAttributeAuthProvider.IAM,\n types_1.ModelAttributeAuthProvider.API_KEY,\n ];\n return [...rules].sort((a, b) => {\n if (a.allow === b.allow) {\n return (providerSortPriority.indexOf(getProviderFromRule(a)) -\n providerSortPriority.indexOf(getProviderFromRule(b)));\n }\n return (allowSortPriority.indexOf(a.allow) - allowSortPriority.indexOf(b.allow));\n });\n}\nfunction getAuthRules({ rules, currentUser, }) {\n // Using Set to ensure uniqueness\n const authModes = new Set();\n rules.forEach(rule => {\n switch (rule.allow) {\n case types_1.ModelAttributeAuthAllow.CUSTOM:\n // custom with no provider -> function\n if (!rule.provider ||\n rule.provider === types_1.ModelAttributeAuthProvider.FUNCTION) {\n authModes.add('lambda');\n }\n break;\n case types_1.ModelAttributeAuthAllow.GROUPS:\n case types_1.ModelAttributeAuthAllow.OWNER: {\n // We shouldn't attempt User Pool or OIDC if there isn't an authenticated user\n if (currentUser) {\n if (rule.provider === types_1.ModelAttributeAuthProvider.USER_POOLS) {\n authModes.add('userPool');\n }\n else if (rule.provider === types_1.ModelAttributeAuthProvider.OIDC) {\n authModes.add('oidc');\n }\n }\n break;\n }\n case types_1.ModelAttributeAuthAllow.PRIVATE: {\n // We shouldn't attempt private if there isn't an authenticated user\n if (currentUser) {\n // private with no provider means userPools\n if (!rule.provider ||\n rule.provider === types_1.ModelAttributeAuthProvider.USER_POOLS) {\n authModes.add('userPool');\n }\n else if (rule.provider === types_1.ModelAttributeAuthProvider.IAM) {\n authModes.add('iam');\n }\n }\n break;\n }\n case types_1.ModelAttributeAuthAllow.PUBLIC: {\n if (rule.provider === types_1.ModelAttributeAuthProvider.IAM) {\n authModes.add('iam');\n }\n else if (!rule.provider ||\n rule.provider === types_1.ModelAttributeAuthProvider.API_KEY) {\n // public with no provider means apiKey\n authModes.add('apiKey');\n }\n break;\n }\n default:\n break;\n }\n });\n return Array.from(authModes);\n}\n/**\n * Returns an array of auth modes to try based on the schema, model, and\n * authenticated user (or lack thereof). Rules are sourced from `getAuthRules`\n * and returned in the order they ought to be attempted.\n *\n * @see sortAuthRulesWithPriority\n * @see getAuthRules\n *\n * @param param0 The `{schema, modelName}` to inspect.\n * @returns A sorted array of auth modes to attempt.\n */\nconst multiAuthStrategy = () => async ({ schema, modelName }) => {\n let currentUser;\n try {\n const authSession = await (0, core_1.fetchAuthSession)();\n if (authSession.tokens.accessToken) {\n // the user is authenticated\n currentUser = authSession;\n }\n }\n catch (e) {\n // No current user\n }\n const { attributes } = schema.namespaces.user.models[modelName];\n if (attributes) {\n const authAttribute = attributes.find(attr => attr.type === 'auth');\n if (authAttribute?.properties?.rules) {\n const sortedRules = sortAuthRulesWithPriority(authAttribute.properties.rules);\n return getAuthRules({ currentUser, rules: sortedRules });\n }\n }\n return [];\n};\nexports.multiAuthStrategy = multiAuthStrategy;\n"],"names":[],"mappings":";;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,iBAAiB,GAAG,MAAM;AAClC;AACA;AACA,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC;AAC3C,MAAM,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC;AACnC,SAAS,mBAAmB,CAAC,IAAI,EAAE;AACnC;AACA,IAAI,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;AACpD,QAAQ,OAAO,OAAO,CAAC,0BAA0B,CAAC,UAAU;AAC5D,IAAI;AACJ;AACA,IAAI,IAAI,IAAI,CAAC,KAAK,KAAK,QAAQ,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;AACnD,QAAQ,OAAO,OAAO,CAAC,0BAA0B,CAAC,OAAO;AACz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