@aws-amplify/api-graphql 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 (257) hide show
  1. package/README.md +3 -0
  2. package/dist/cjs/GraphQLAPI.js +66 -0
  3. package/dist/cjs/GraphQLAPI.js.map +1 -0
  4. package/dist/cjs/Providers/AWSAppSyncEventsProvider/index.js +117 -0
  5. package/dist/cjs/Providers/AWSAppSyncEventsProvider/index.js.map +1 -0
  6. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js +96 -0
  7. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -0
  8. package/dist/cjs/Providers/AWSWebSocketProvider/appsyncUrl.js +113 -0
  9. package/dist/cjs/Providers/AWSWebSocketProvider/appsyncUrl.js.map +1 -0
  10. package/dist/cjs/Providers/AWSWebSocketProvider/authHeaders.js +99 -0
  11. package/dist/cjs/Providers/AWSWebSocketProvider/authHeaders.js.map +1 -0
  12. package/dist/cjs/Providers/AWSWebSocketProvider/index.js +693 -0
  13. package/dist/cjs/Providers/AWSWebSocketProvider/index.js.map +1 -0
  14. package/dist/cjs/Providers/constants.js +149 -0
  15. package/dist/cjs/Providers/constants.js.map +1 -0
  16. package/dist/cjs/index.js +18 -0
  17. package/dist/cjs/index.js.map +1 -0
  18. package/dist/cjs/internals/InternalGraphQLAPI.js +269 -0
  19. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -0
  20. package/dist/cjs/internals/events/appsyncRequest.js +96 -0
  21. package/dist/cjs/internals/events/appsyncRequest.js.map +1 -0
  22. package/dist/cjs/internals/events/index.js +142 -0
  23. package/dist/cjs/internals/events/index.js.map +1 -0
  24. package/dist/cjs/internals/events/types.js +6 -0
  25. package/dist/cjs/internals/events/types.js.map +1 -0
  26. package/dist/cjs/internals/events/utils.js +57 -0
  27. package/dist/cjs/internals/events/utils.js.map +1 -0
  28. package/dist/cjs/internals/generateClient.js +86 -0
  29. package/dist/cjs/internals/generateClient.js.map +1 -0
  30. package/dist/cjs/internals/graphqlAuth.js +63 -0
  31. package/dist/cjs/internals/graphqlAuth.js.map +1 -0
  32. package/dist/cjs/internals/graphqlRequest.js +19 -0
  33. package/dist/cjs/internals/graphqlRequest.js.map +1 -0
  34. package/dist/cjs/internals/index.js +16 -0
  35. package/dist/cjs/internals/index.js.map +1 -0
  36. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js +48 -0
  37. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js.map +1 -0
  38. package/dist/cjs/internals/server/index.js +9 -0
  39. package/dist/cjs/internals/server/index.js.map +1 -0
  40. package/dist/cjs/internals/types.js +4 -0
  41. package/dist/cjs/internals/types.js.map +1 -0
  42. package/dist/cjs/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.js +10 -0
  43. package/dist/cjs/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.js.map +1 -0
  44. package/dist/cjs/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.js +10 -0
  45. package/dist/cjs/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.js.map +1 -0
  46. package/dist/cjs/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.js +14 -0
  47. package/dist/cjs/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.js.map +1 -0
  48. package/dist/cjs/internals/v6.js +155 -0
  49. package/dist/cjs/internals/v6.js.map +1 -0
  50. package/dist/cjs/server/generateClient.js +42 -0
  51. package/dist/cjs/server/generateClient.js.map +1 -0
  52. package/dist/cjs/server/index.js +9 -0
  53. package/dist/cjs/server/index.js.map +1 -0
  54. package/dist/cjs/types/PubSub.js +49 -0
  55. package/dist/cjs/types/PubSub.js.map +1 -0
  56. package/dist/cjs/types/index.js +34 -0
  57. package/dist/cjs/types/index.js.map +1 -0
  58. package/dist/cjs/utils/ConnectionStateMonitor.js +134 -0
  59. package/dist/cjs/utils/ConnectionStateMonitor.js.map +1 -0
  60. package/dist/cjs/utils/ReachabilityMonitor/index.js +10 -0
  61. package/dist/cjs/utils/ReachabilityMonitor/index.js.map +1 -0
  62. package/dist/cjs/utils/ReachabilityMonitor/index.native.js +12 -0
  63. package/dist/cjs/utils/ReachabilityMonitor/index.native.js.map +1 -0
  64. package/dist/cjs/utils/ReconnectionMonitor.js +71 -0
  65. package/dist/cjs/utils/ReconnectionMonitor.js.map +1 -0
  66. package/dist/cjs/utils/errors/GraphQLApiError.js +21 -0
  67. package/dist/cjs/utils/errors/GraphQLApiError.js.map +1 -0
  68. package/dist/cjs/utils/errors/assertValidationError.js +18 -0
  69. package/dist/cjs/utils/errors/assertValidationError.js.map +1 -0
  70. package/dist/cjs/utils/errors/constants.js +47 -0
  71. package/dist/cjs/utils/errors/constants.js.map +1 -0
  72. package/dist/cjs/utils/errors/createGraphQLResultWithError.js +15 -0
  73. package/dist/cjs/utils/errors/createGraphQLResultWithError.js.map +1 -0
  74. package/dist/cjs/utils/errors/index.js +16 -0
  75. package/dist/cjs/utils/errors/index.js.map +1 -0
  76. package/dist/cjs/utils/errors/repackageAuthError.js +38 -0
  77. package/dist/cjs/utils/errors/repackageAuthError.js.map +1 -0
  78. package/dist/cjs/utils/errors/validation.js +28 -0
  79. package/dist/cjs/utils/errors/validation.js.map +1 -0
  80. package/dist/cjs/utils/index.js +11 -0
  81. package/dist/cjs/utils/index.js.map +1 -0
  82. package/dist/cjs/utils/resolveConfig.js +32 -0
  83. package/dist/cjs/utils/resolveConfig.js.map +1 -0
  84. package/dist/cjs/utils/resolveLibraryOptions.js +16 -0
  85. package/dist/cjs/utils/resolveLibraryOptions.js.map +1 -0
  86. package/dist/esm/GraphQLAPI.d.ts +37 -0
  87. package/dist/esm/GraphQLAPI.mjs +63 -0
  88. package/dist/esm/GraphQLAPI.mjs.map +1 -0
  89. package/dist/esm/Providers/AWSAppSyncEventsProvider/index.d.ts +49 -0
  90. package/dist/esm/Providers/AWSAppSyncEventsProvider/index.mjs +115 -0
  91. package/dist/esm/Providers/AWSAppSyncEventsProvider/index.mjs.map +1 -0
  92. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +47 -0
  93. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +94 -0
  94. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -0
  95. package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.d.ts +21 -0
  96. package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.mjs +107 -0
  97. package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.mjs.map +1 -0
  98. package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.d.ts +8 -0
  99. package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.mjs +97 -0
  100. package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.mjs.map +1 -0
  101. package/dist/esm/Providers/AWSWebSocketProvider/index.d.ts +99 -0
  102. package/dist/esm/Providers/AWSWebSocketProvider/index.mjs +691 -0
  103. package/dist/esm/Providers/AWSWebSocketProvider/index.mjs.map +1 -0
  104. package/dist/esm/Providers/constants.d.ts +135 -0
  105. package/dist/esm/Providers/constants.mjs +147 -0
  106. package/dist/esm/Providers/constants.mjs.map +1 -0
  107. package/dist/esm/index.d.ts +6 -0
  108. package/dist/esm/index.mjs +7 -0
  109. package/dist/esm/index.mjs.map +1 -0
  110. package/dist/esm/internals/InternalGraphQLAPI.d.ts +45 -0
  111. package/dist/esm/internals/InternalGraphQLAPI.mjs +270 -0
  112. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -0
  113. package/dist/esm/internals/events/appsyncRequest.d.ts +14 -0
  114. package/dist/esm/internals/events/appsyncRequest.mjs +96 -0
  115. package/dist/esm/internals/events/appsyncRequest.mjs.map +1 -0
  116. package/dist/esm/internals/events/index.d.ts +58 -0
  117. package/dist/esm/internals/events/index.mjs +139 -0
  118. package/dist/esm/internals/events/index.mjs.map +1 -0
  119. package/dist/esm/internals/events/types.d.ts +93 -0
  120. package/dist/esm/internals/events/types.mjs +2 -0
  121. package/dist/esm/internals/events/types.mjs.map +1 -0
  122. package/dist/esm/internals/events/utils.d.ts +16 -0
  123. package/dist/esm/internals/events/utils.mjs +53 -0
  124. package/dist/esm/internals/events/utils.mjs.map +1 -0
  125. package/dist/esm/internals/generateClient.d.ts +12 -0
  126. package/dist/esm/internals/generateClient.mjs +85 -0
  127. package/dist/esm/internals/generateClient.mjs.map +1 -0
  128. package/dist/esm/internals/graphqlAuth.d.ts +3 -0
  129. package/dist/esm/internals/graphqlAuth.mjs +64 -0
  130. package/dist/esm/internals/graphqlAuth.mjs.map +1 -0
  131. package/dist/esm/internals/graphqlRequest.d.ts +3 -0
  132. package/dist/esm/internals/graphqlRequest.mjs +18 -0
  133. package/dist/esm/internals/graphqlRequest.mjs.map +1 -0
  134. package/dist/esm/internals/index.d.ts +4 -0
  135. package/dist/esm/internals/index.mjs +4 -0
  136. package/dist/esm/internals/index.mjs.map +1 -0
  137. package/dist/esm/internals/server/generateClientWithAmplifyInstance.d.ts +13 -0
  138. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs +61 -0
  139. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs.map +1 -0
  140. package/dist/esm/internals/server/index.d.ts +1 -0
  141. package/dist/esm/internals/server/index.mjs +2 -0
  142. package/dist/esm/internals/server/index.mjs.map +1 -0
  143. package/dist/esm/internals/types.d.ts +34 -0
  144. package/dist/esm/internals/types.mjs +2 -0
  145. package/dist/esm/internals/types.mjs.map +1 -0
  146. package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.d.ts +2 -0
  147. package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.mjs +8 -0
  148. package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.mjs.map +1 -0
  149. package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.d.ts +8 -0
  150. package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.mjs +8 -0
  151. package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.mjs.map +1 -0
  152. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.d.ts +2 -0
  153. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.mjs +12 -0
  154. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.mjs.map +1 -0
  155. package/dist/esm/internals/v6.d.ts +100 -0
  156. package/dist/esm/internals/v6.mjs +152 -0
  157. package/dist/esm/internals/v6.mjs.map +1 -0
  158. package/dist/esm/server/generateClient.d.ts +21 -0
  159. package/dist/esm/server/generateClient.mjs +41 -0
  160. package/dist/esm/server/generateClient.mjs.map +1 -0
  161. package/dist/esm/server/index.d.ts +1 -0
  162. package/dist/esm/server/index.mjs +2 -0
  163. package/dist/esm/server/index.mjs.map +1 -0
  164. package/dist/esm/types/PubSub.d.ts +27 -0
  165. package/dist/esm/types/PubSub.mjs +47 -0
  166. package/dist/esm/types/PubSub.mjs.map +1 -0
  167. package/dist/esm/types/index.d.ts +369 -0
  168. package/dist/esm/types/index.mjs +30 -0
  169. package/dist/esm/types/index.mjs.map +1 -0
  170. package/dist/esm/utils/ConnectionStateMonitor.d.ts +69 -0
  171. package/dist/esm/utils/ConnectionStateMonitor.mjs +132 -0
  172. package/dist/esm/utils/ConnectionStateMonitor.mjs.map +1 -0
  173. package/dist/esm/utils/ReachabilityMonitor/index.d.ts +1 -0
  174. package/dist/esm/utils/ReachabilityMonitor/index.mjs +8 -0
  175. package/dist/esm/utils/ReachabilityMonitor/index.mjs.map +1 -0
  176. package/dist/esm/utils/ReachabilityMonitor/index.native.d.ts +1 -0
  177. package/dist/esm/utils/ReachabilityMonitor/index.native.mjs +9 -0
  178. package/dist/esm/utils/ReachabilityMonitor/index.native.mjs.map +1 -0
  179. package/dist/esm/utils/ReconnectionMonitor.d.ts +29 -0
  180. package/dist/esm/utils/ReconnectionMonitor.mjs +69 -0
  181. package/dist/esm/utils/ReconnectionMonitor.mjs.map +1 -0
  182. package/dist/esm/utils/errors/GraphQLApiError.d.ts +7 -0
  183. package/dist/esm/utils/errors/GraphQLApiError.mjs +19 -0
  184. package/dist/esm/utils/errors/GraphQLApiError.mjs.map +1 -0
  185. package/dist/esm/utils/errors/assertValidationError.d.ts +5 -0
  186. package/dist/esm/utils/errors/assertValidationError.mjs +17 -0
  187. package/dist/esm/utils/errors/assertValidationError.mjs.map +1 -0
  188. package/dist/esm/utils/errors/constants.d.ts +7 -0
  189. package/dist/esm/utils/errors/constants.mjs +46 -0
  190. package/dist/esm/utils/errors/constants.mjs.map +1 -0
  191. package/dist/esm/utils/errors/createGraphQLResultWithError.d.ts +2 -0
  192. package/dist/esm/utils/errors/createGraphQLResultWithError.mjs +13 -0
  193. package/dist/esm/utils/errors/createGraphQLResultWithError.mjs.map +1 -0
  194. package/dist/esm/utils/errors/index.d.ts +4 -0
  195. package/dist/esm/utils/errors/index.mjs +5 -0
  196. package/dist/esm/utils/errors/index.mjs.map +1 -0
  197. package/dist/esm/utils/errors/repackageAuthError.d.ts +7 -0
  198. package/dist/esm/utils/errors/repackageAuthError.mjs +36 -0
  199. package/dist/esm/utils/errors/repackageAuthError.mjs.map +1 -0
  200. package/dist/esm/utils/errors/validation.d.ts +7 -0
  201. package/dist/esm/utils/errors/validation.mjs +26 -0
  202. package/dist/esm/utils/errors/validation.mjs.map +1 -0
  203. package/dist/esm/utils/index.d.ts +2 -0
  204. package/dist/esm/utils/index.mjs +3 -0
  205. package/dist/esm/utils/index.mjs.map +1 -0
  206. package/dist/esm/utils/resolveConfig.d.ts +12 -0
  207. package/dist/esm/utils/resolveConfig.mjs +33 -0
  208. package/dist/esm/utils/resolveConfig.mjs.map +1 -0
  209. package/dist/esm/utils/resolveLibraryOptions.d.ts +11 -0
  210. package/dist/esm/utils/resolveLibraryOptions.mjs +13 -0
  211. package/dist/esm/utils/resolveLibraryOptions.mjs.map +1 -0
  212. package/internals/package.json +8 -0
  213. package/internals/server/package.json +8 -0
  214. package/package.json +99 -0
  215. package/server/package.json +7 -0
  216. package/src/GraphQLAPI.ts +97 -0
  217. package/src/Providers/AWSAppSyncEventsProvider/index.ts +218 -0
  218. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +185 -0
  219. package/src/Providers/AWSWebSocketProvider/appsyncUrl.ts +152 -0
  220. package/src/Providers/AWSWebSocketProvider/authHeaders.ts +146 -0
  221. package/src/Providers/AWSWebSocketProvider/index.ts +1028 -0
  222. package/src/Providers/constants.ts +154 -0
  223. package/src/index.ts +12 -0
  224. package/src/internals/InternalGraphQLAPI.ts +424 -0
  225. package/src/internals/events/appsyncRequest.ts +169 -0
  226. package/src/internals/events/index.ts +199 -0
  227. package/src/internals/events/types.ts +109 -0
  228. package/src/internals/events/utils.ts +77 -0
  229. package/src/internals/generateClient.ts +120 -0
  230. package/src/internals/graphqlAuth.ts +87 -0
  231. package/src/internals/graphqlRequest.ts +30 -0
  232. package/src/internals/index.ts +10 -0
  233. package/src/internals/server/generateClientWithAmplifyInstance.ts +73 -0
  234. package/src/internals/server/index.ts +4 -0
  235. package/src/internals/types.ts +42 -0
  236. package/src/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.ts +10 -0
  237. package/src/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.ts +13 -0
  238. package/src/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.ts +15 -0
  239. package/src/internals/v6.ts +189 -0
  240. package/src/server/generateClient.ts +68 -0
  241. package/src/server/index.ts +4 -0
  242. package/src/types/PubSub.ts +57 -0
  243. package/src/types/index.ts +524 -0
  244. package/src/utils/ConnectionStateMonitor.ts +195 -0
  245. package/src/utils/ReachabilityMonitor/index.native.ts +7 -0
  246. package/src/utils/ReachabilityMonitor/index.ts +5 -0
  247. package/src/utils/ReconnectionMonitor.ts +77 -0
  248. package/src/utils/errors/GraphQLApiError.ts +21 -0
  249. package/src/utils/errors/assertValidationError.ts +19 -0
  250. package/src/utils/errors/constants.ts +56 -0
  251. package/src/utils/errors/createGraphQLResultWithError.ts +15 -0
  252. package/src/utils/errors/index.ts +7 -0
  253. package/src/utils/errors/repackageAuthError.ts +43 -0
  254. package/src/utils/errors/validation.ts +26 -0
  255. package/src/utils/index.ts +5 -0
  256. package/src/utils/resolveConfig.ts +46 -0
  257. package/src/utils/resolveLibraryOptions.ts +14 -0
@@ -0,0 +1,7 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { Reachability } from '@aws-amplify/core/internals/utils';
4
+ import NetInfo from '@react-native-community/netinfo';
5
+
6
+ export const ReachabilityMonitor = () =>
7
+ new Reachability().networkMonitor(NetInfo);
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { Reachability } from '@aws-amplify/core/internals/utils';
4
+
5
+ export const ReachabilityMonitor = () => new Reachability().networkMonitor();
@@ -0,0 +1,77 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { Observer } from 'rxjs';
4
+
5
+ import { RECONNECT_DELAY, RECONNECT_INTERVAL } from '../Providers/constants';
6
+
7
+ export enum ReconnectEvent {
8
+ START_RECONNECT = 'START_RECONNECT',
9
+ HALT_RECONNECT = 'HALT_RECONNECT',
10
+ }
11
+
12
+ /**
13
+ * Captures the reconnect event logic used to determine when to reconnect to PubSub providers.
14
+ * Reconnect attempts are delayed by 5 seconds to let the interface settle.
15
+ * Attempting to reconnect only once creates unrecoverable states when the network state isn't
16
+ * supported by the browser, so this keeps retrying every minute until halted.
17
+ */
18
+ export class ReconnectionMonitor {
19
+ private reconnectObservers: Observer<void>[] = [];
20
+ private reconnectIntervalId?: ReturnType<typeof setInterval>;
21
+ private reconnectSetTimeoutId?: ReturnType<typeof setTimeout>;
22
+
23
+ /**
24
+ * Add reconnect observer to the list of observers to alert on reconnect
25
+ */
26
+ addObserver(reconnectObserver: Observer<void>) {
27
+ this.reconnectObservers.push(reconnectObserver);
28
+ }
29
+
30
+ /**
31
+ * Given a reconnect event, start the appropriate behavior
32
+ */
33
+ record(event: ReconnectEvent) {
34
+ if (event === ReconnectEvent.START_RECONNECT) {
35
+ // If the reconnection hasn't been started
36
+ if (
37
+ this.reconnectSetTimeoutId === undefined &&
38
+ this.reconnectIntervalId === undefined
39
+ ) {
40
+ this.reconnectSetTimeoutId = setTimeout(() => {
41
+ // Reconnect now
42
+ this._triggerReconnect();
43
+ // Retry reconnect every periodically until it works
44
+ this.reconnectIntervalId = setInterval(() => {
45
+ this._triggerReconnect();
46
+ }, RECONNECT_INTERVAL);
47
+ }, RECONNECT_DELAY);
48
+ }
49
+ }
50
+
51
+ if (event === ReconnectEvent.HALT_RECONNECT) {
52
+ if (this.reconnectIntervalId) {
53
+ clearInterval(this.reconnectIntervalId);
54
+ this.reconnectIntervalId = undefined;
55
+ }
56
+ if (this.reconnectSetTimeoutId) {
57
+ clearTimeout(this.reconnectSetTimeoutId);
58
+ this.reconnectSetTimeoutId = undefined;
59
+ }
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Complete all reconnect observers
65
+ */
66
+ close() {
67
+ this.reconnectObservers.forEach(reconnectObserver => {
68
+ reconnectObserver.complete?.();
69
+ });
70
+ }
71
+
72
+ private _triggerReconnect() {
73
+ this.reconnectObservers.forEach(reconnectObserver => {
74
+ reconnectObserver.next?.();
75
+ });
76
+ }
77
+ }
@@ -0,0 +1,21 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ AmplifyError,
6
+ AmplifyErrorParams,
7
+ } from '@aws-amplify/core/internals/utils';
8
+
9
+ /**
10
+ * @internal
11
+ */
12
+ export class GraphQLApiError extends AmplifyError {
13
+ constructor(params: AmplifyErrorParams) {
14
+ super(params);
15
+
16
+ // Hack for making the custom error class work when transpiled to es5
17
+ // TODO: Delete the following 2 lines after we change the build target to >= es2015
18
+ this.constructor = GraphQLApiError;
19
+ Object.setPrototypeOf(this, GraphQLApiError.prototype);
20
+ }
21
+ }
@@ -0,0 +1,19 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { GraphQLApiError } from './GraphQLApiError';
5
+ import { APIValidationErrorCode, validationErrorMap } from './validation';
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export function assertValidationError(
11
+ assertion: boolean,
12
+ name: APIValidationErrorCode,
13
+ ): asserts assertion {
14
+ const { message, recoverySuggestion } = validationErrorMap[name];
15
+
16
+ if (!assertion) {
17
+ throw new GraphQLApiError({ name, message, recoverySuggestion });
18
+ }
19
+ }
@@ -0,0 +1,56 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AmplifyErrorParams } from '@aws-amplify/core/internals/utils';
5
+
6
+ import { GraphQLAuthError } from '../../types';
7
+
8
+ export const NO_API_KEY: AmplifyErrorParams = {
9
+ name: 'NoApiKey',
10
+ // ideal: No API key configured.
11
+ message: GraphQLAuthError.NO_API_KEY,
12
+ recoverySuggestion:
13
+ 'The API request was made with `authMode: "apiKey"` but no API Key was passed into `Amplify.configure()`. Review if your API key is passed into the `Amplify.configure()` function.',
14
+ };
15
+
16
+ export const NO_VALID_CREDENTIALS: AmplifyErrorParams = {
17
+ name: 'NoCredentials',
18
+ // ideal: No auth credentials available.
19
+ message: GraphQLAuthError.NO_CREDENTIALS,
20
+ recoverySuggestion: `The API request was made with \`authMode: "iam"\` but no authentication credentials are available.
21
+
22
+ If you intended to make a request using an authenticated role, review if your user is signed in before making the request.
23
+
24
+ If you intend to make a request using an unauthenticated role or also known as "guest access", verify if "Auth.Cognito.allowGuestAccess" is set to "true" in the \`Amplify.configure()\` function.`,
25
+ };
26
+
27
+ export const NO_VALID_AUTH_TOKEN: AmplifyErrorParams = {
28
+ name: 'NoValidAuthTokens',
29
+ // ideal: No valid JWT auth token provided to make the API request..
30
+ message: GraphQLAuthError.NO_FEDERATED_JWT,
31
+ recoverySuggestion:
32
+ 'If you intended to make an authenticated API request, review if the current user is signed in.',
33
+ };
34
+
35
+ export const NO_SIGNED_IN_USER: AmplifyErrorParams = {
36
+ name: 'NoSignedUser',
37
+ // ideal: Couldn't retrieve authentication credentials to make the API request.
38
+ message: GraphQLAuthError.NO_CURRENT_USER,
39
+ recoverySuggestion:
40
+ 'Review the underlying exception field for more details. If you intended to make an authenticated API request, review if the current user is signed in.',
41
+ };
42
+
43
+ export const NO_AUTH_TOKEN_HEADER: AmplifyErrorParams = {
44
+ name: 'NoAuthorizationHeader',
45
+ // ideal: Authorization header not specified.
46
+ message: GraphQLAuthError.NO_AUTH_TOKEN,
47
+ recoverySuggestion:
48
+ 'The API request was made with `authMode: "lambda"` but no `authToken` is set. Review if a valid authToken is passed into the request options or in the `Amplify.configure()` function.',
49
+ };
50
+
51
+ export const NO_ENDPOINT: AmplifyErrorParams = {
52
+ name: 'NoEndpoint',
53
+ message: 'No GraphQL endpoint configured in `Amplify.configure()`.',
54
+ recoverySuggestion:
55
+ 'Review if the GraphQL API endpoint is set in the `Amplify.configure()` function.',
56
+ };
@@ -0,0 +1,15 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { GraphQLError } from 'graphql';
5
+
6
+ import { GraphQLResult } from '../../types';
7
+
8
+ export const createGraphQLResultWithError = <T>(
9
+ error: Error,
10
+ ): GraphQLResult<T> => {
11
+ return {
12
+ data: {} as T,
13
+ errors: [new GraphQLError(error.message, null, null, null, null, error)],
14
+ };
15
+ };
@@ -0,0 +1,7 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { GraphQLApiError } from './GraphQLApiError';
5
+ export { assertValidationError } from './assertValidationError';
6
+ export { APIValidationErrorCode, validationErrorMap } from './validation';
7
+ export { createGraphQLResultWithError } from './createGraphQLResultWithError';
@@ -0,0 +1,43 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { GraphQLResult } from '../../types';
5
+
6
+ /**
7
+ * Checks to see if the given response or subscription message contains an
8
+ * Unauthorized error. If it does, it changes the error message to include instructions
9
+ * for the app developer.
10
+ */
11
+ export function repackageUnauthorizedError<T extends GraphQLResult<any>>(
12
+ content: T,
13
+ ): T {
14
+ if (content.errors && Array.isArray(content.errors)) {
15
+ content.errors.forEach(e => {
16
+ if (isUnauthorizedError(e)) {
17
+ e.message = 'Unauthorized';
18
+ (e as any).recoverySuggestion =
19
+ `If you're calling an Amplify-generated API, make sure ` +
20
+ `to set the "authMode" in generateClient({ authMode: '...' }) to the backend authorization ` +
21
+ `rule's auth provider ('apiKey', 'userPool', 'iam', 'oidc', 'lambda')`;
22
+ }
23
+ });
24
+ }
25
+
26
+ return content;
27
+ }
28
+
29
+ function isUnauthorizedError(error: any): boolean {
30
+ // Error pattern corresponding to appsync calls
31
+ if (error?.originalError?.name?.startsWith('UnauthorizedException')) {
32
+ return true;
33
+ }
34
+ // Error pattern corresponding to appsync subscriptions
35
+ if (
36
+ error.message?.startsWith('Connection failed:') &&
37
+ error.message?.includes('Permission denied')
38
+ ) {
39
+ return true;
40
+ }
41
+
42
+ return false;
43
+ }
@@ -0,0 +1,26 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AmplifyErrorMap } from '@aws-amplify/core/internals/utils';
5
+
6
+ export enum APIValidationErrorCode {
7
+ NoAuthSession = 'NoAuthSession',
8
+ NoRegion = 'NoRegion',
9
+ NoCustomEndpoint = 'NoCustomEndpoint',
10
+ }
11
+
12
+ export const validationErrorMap: AmplifyErrorMap<APIValidationErrorCode> = {
13
+ [APIValidationErrorCode.NoAuthSession]: {
14
+ message: 'Auth session should not be empty.',
15
+ },
16
+ // TODO: re-enable when working in all test environments:
17
+ // [APIValidationErrorCode.NoEndpoint]: {
18
+ // message: 'Missing endpoint',
19
+ // },
20
+ [APIValidationErrorCode.NoRegion]: {
21
+ message: 'Missing region.',
22
+ },
23
+ [APIValidationErrorCode.NoCustomEndpoint]: {
24
+ message: 'Custom endpoint region is present without custom endpoint.',
25
+ },
26
+ };
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { resolveConfig } from './resolveConfig';
5
+ export { resolveLibraryOptions } from './resolveLibraryOptions';
@@ -0,0 +1,46 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AmplifyClassV6, ConsoleLogger } from '@aws-amplify/core';
5
+
6
+ import { APIValidationErrorCode, assertValidationError } from './errors';
7
+
8
+ const logger = new ConsoleLogger('GraphQLAPI resolveConfig');
9
+
10
+ /**
11
+ * @internal
12
+ */
13
+ export const resolveConfig = (amplify: AmplifyClassV6) => {
14
+ const config = amplify.getConfig();
15
+
16
+ if (!config.API?.GraphQL) {
17
+ logger.warn(
18
+ 'The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient().',
19
+ );
20
+ }
21
+
22
+ const {
23
+ apiKey,
24
+ customEndpoint,
25
+ customEndpointRegion,
26
+ defaultAuthMode,
27
+ endpoint,
28
+ region,
29
+ } = config.API?.GraphQL ?? {};
30
+
31
+ // TODO: re-enable when working in all test environments:
32
+ // assertValidationError(!!endpoint, APIValidationErrorCode.NoEndpoint);
33
+ assertValidationError(
34
+ !(!customEndpoint && customEndpointRegion),
35
+ APIValidationErrorCode.NoCustomEndpoint,
36
+ );
37
+
38
+ return {
39
+ apiKey,
40
+ customEndpoint,
41
+ customEndpointRegion,
42
+ defaultAuthMode,
43
+ endpoint,
44
+ region,
45
+ };
46
+ };
@@ -0,0 +1,14 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AmplifyClassV6 } from '@aws-amplify/core';
5
+
6
+ /**
7
+ * @internal
8
+ */
9
+ export const resolveLibraryOptions = (amplify: AmplifyClassV6) => {
10
+ const headers = amplify.libraryOptions?.API?.GraphQL?.headers;
11
+ const withCredentials = amplify.libraryOptions?.API?.GraphQL?.withCredentials;
12
+
13
+ return { headers, withCredentials };
14
+ };