@aws-amplify/api-graphql 3.4.12-api-v6-models.b3abc9b.0 → 4.0.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 (118) hide show
  1. package/README.md +3 -0
  2. package/lib/GraphQLAPI.d.ts +17 -4
  3. package/lib/GraphQLAPI.js +33 -27
  4. package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +64 -0
  5. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +690 -0
  6. package/lib/Providers/constants.d.ts +95 -0
  7. package/lib/Providers/constants.js +103 -0
  8. package/lib/index.d.ts +1 -3
  9. package/lib/index.js +6 -10
  10. package/lib/internals/InternalGraphQLAPI.d.ts +11 -34
  11. package/lib/internals/InternalGraphQLAPI.js +219 -345
  12. package/lib/internals/index.d.ts +1 -1
  13. package/lib/internals/index.js +2 -1
  14. package/lib/internals/v6.d.ts +14 -2
  15. package/lib/internals/v6.js +21 -3
  16. package/lib/tsconfig.tsbuildinfo +6813 -0
  17. package/lib/types/PubSub.d.ts +27 -0
  18. package/lib/types/PubSub.js +46 -0
  19. package/lib/types/index.d.ts +119 -12
  20. package/lib/types/index.js +4 -3
  21. package/lib/utils/ConnectionStateMonitor.d.ts +41 -0
  22. package/lib/utils/ConnectionStateMonitor.js +131 -0
  23. package/lib/utils/ReachabilityMonitor/index.d.ts +1 -0
  24. package/lib/utils/ReachabilityMonitor/index.js +6 -0
  25. package/lib/utils/ReachabilityMonitor/index.native.d.ts +1 -0
  26. package/lib/utils/ReachabilityMonitor/index.native.js +8 -0
  27. package/lib/utils/ReconnectionMonitor.d.ts +29 -0
  28. package/lib/utils/ReconnectionMonitor.js +68 -0
  29. package/lib/utils/errors/APIError.d.ts +7 -0
  30. package/lib/utils/errors/APIError.js +18 -0
  31. package/lib/utils/errors/assertValidationError.d.ts +5 -0
  32. package/lib/utils/errors/assertValidationError.js +16 -0
  33. package/lib/utils/errors/index.d.ts +3 -0
  34. package/lib/utils/errors/index.js +11 -0
  35. package/lib/utils/errors/validation.d.ts +7 -0
  36. package/lib/utils/errors/validation.js +25 -0
  37. package/lib/utils/index.d.ts +2 -0
  38. package/lib/utils/index.js +8 -0
  39. package/lib/utils/resolveConfig.d.ts +12 -0
  40. package/lib/utils/resolveConfig.js +22 -0
  41. package/lib/utils/resolveLibraryOptions.d.ts +11 -0
  42. package/lib/utils/resolveLibraryOptions.js +12 -0
  43. package/lib-esm/GraphQLAPI.d.ts +17 -4
  44. package/lib-esm/GraphQLAPI.js +33 -28
  45. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +64 -0
  46. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +687 -0
  47. package/lib-esm/Providers/constants.d.ts +95 -0
  48. package/lib-esm/Providers/constants.js +100 -0
  49. package/lib-esm/index.d.ts +1 -3
  50. package/lib-esm/index.js +1 -4
  51. package/lib-esm/internals/InternalGraphQLAPI.d.ts +11 -34
  52. package/lib-esm/internals/InternalGraphQLAPI.js +218 -345
  53. package/lib-esm/internals/index.d.ts +1 -1
  54. package/lib-esm/internals/index.js +1 -2
  55. package/lib-esm/internals/v6.d.ts +14 -2
  56. package/lib-esm/internals/v6.js +18 -2
  57. package/lib-esm/tsconfig.tsbuildinfo +6813 -0
  58. package/lib-esm/types/PubSub.d.ts +27 -0
  59. package/lib-esm/types/PubSub.js +44 -0
  60. package/lib-esm/types/index.d.ts +119 -12
  61. package/lib-esm/types/index.js +2 -3
  62. package/lib-esm/utils/ConnectionStateMonitor.d.ts +41 -0
  63. package/lib-esm/utils/ConnectionStateMonitor.js +128 -0
  64. package/lib-esm/utils/ReachabilityMonitor/index.d.ts +1 -0
  65. package/lib-esm/utils/ReachabilityMonitor/index.js +4 -0
  66. package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +1 -0
  67. package/lib-esm/utils/ReachabilityMonitor/index.native.js +5 -0
  68. package/lib-esm/utils/ReconnectionMonitor.d.ts +29 -0
  69. package/lib-esm/utils/ReconnectionMonitor.js +65 -0
  70. package/lib-esm/utils/errors/APIError.d.ts +7 -0
  71. package/lib-esm/utils/errors/APIError.js +15 -0
  72. package/lib-esm/utils/errors/assertValidationError.d.ts +5 -0
  73. package/lib-esm/utils/errors/assertValidationError.js +13 -0
  74. package/lib-esm/utils/errors/index.d.ts +3 -0
  75. package/lib-esm/utils/errors/index.js +5 -0
  76. package/lib-esm/utils/errors/validation.d.ts +7 -0
  77. package/lib-esm/utils/errors/validation.js +23 -0
  78. package/lib-esm/utils/index.d.ts +2 -0
  79. package/lib-esm/utils/index.js +4 -0
  80. package/lib-esm/utils/resolveConfig.d.ts +12 -0
  81. package/lib-esm/utils/resolveConfig.js +20 -0
  82. package/lib-esm/utils/resolveLibraryOptions.d.ts +11 -0
  83. package/lib-esm/utils/resolveLibraryOptions.js +10 -0
  84. package/package.json +122 -118
  85. package/src/GraphQLAPI.ts +26 -4
  86. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +998 -0
  87. package/src/Providers/constants.ts +110 -0
  88. package/src/index.ts +2 -3
  89. package/src/internals/InternalGraphQLAPI.ts +181 -218
  90. package/src/internals/index.ts +1 -1
  91. package/src/internals/v6.ts +34 -2
  92. package/src/types/PubSub.ts +56 -0
  93. package/src/types/index.ts +175 -15
  94. package/src/utils/ConnectionStateMonitor.ts +199 -0
  95. package/src/utils/ReachabilityMonitor/index.native.ts +7 -0
  96. package/src/utils/ReachabilityMonitor/index.ts +4 -0
  97. package/src/utils/ReconnectionMonitor.ts +76 -0
  98. package/src/utils/errors/APIError.ts +21 -0
  99. package/src/utils/errors/assertValidationError.ts +19 -0
  100. package/src/utils/errors/index.ts +6 -0
  101. package/src/utils/errors/validation.ts +26 -0
  102. package/src/utils/index.ts +5 -0
  103. package/src/utils/resolveConfig.ts +35 -0
  104. package/src/utils/resolveLibraryOptions.ts +13 -0
  105. package/lib/.tsbuildinfo +0 -3
  106. package/lib/GraphQLAPI.js.map +0 -1
  107. package/lib/index.js.map +0 -1
  108. package/lib/internals/InternalGraphQLAPI.js.map +0 -1
  109. package/lib/internals/index.js.map +0 -1
  110. package/lib/internals/v6.js.map +0 -1
  111. package/lib/types/index.js.map +0 -1
  112. package/lib-esm/.tsbuildinfo +0 -3
  113. package/lib-esm/GraphQLAPI.js.map +0 -1
  114. package/lib-esm/index.js.map +0 -1
  115. package/lib-esm/internals/InternalGraphQLAPI.js.map +0 -1
  116. package/lib-esm/internals/index.js.map +0 -1
  117. package/lib-esm/internals/v6.js.map +0 -1
  118. package/lib-esm/types/index.js.map +0 -1
@@ -0,0 +1,27 @@
1
+ import { Observer } from 'rxjs';
2
+ export interface SubscriptionObserver<T> {
3
+ closed: boolean;
4
+ next(value: T): void;
5
+ error(errorValue: any): void;
6
+ complete(): void;
7
+ }
8
+ export declare enum CONTROL_MSG {
9
+ CONNECTION_CLOSED = "Connection closed",
10
+ CONNECTION_FAILED = "Connection failed",
11
+ REALTIME_SUBSCRIPTION_INIT_ERROR = "AppSync Realtime subscription init error",
12
+ SUBSCRIPTION_ACK = "Subscription ack",
13
+ TIMEOUT_DISCONNECT = "Timeout disconnect"
14
+ }
15
+ /** @enum {string} */
16
+ export declare enum ConnectionState {
17
+ Connected = "Connected",
18
+ ConnectedPendingNetwork = "ConnectedPendingNetwork",
19
+ ConnectionDisrupted = "ConnectionDisrupted",
20
+ ConnectionDisruptedPendingNetwork = "ConnectionDisruptedPendingNetwork",
21
+ Connecting = "Connecting",
22
+ ConnectedPendingDisconnect = "ConnectedPendingDisconnect",
23
+ Disconnected = "Disconnected",
24
+ ConnectedPendingKeepAlive = "ConnectedPendingKeepAlive"
25
+ }
26
+ export declare type PubSubContent = Record<string, unknown>;
27
+ export declare type PubSubContentObserver = Observer<PubSubContent>;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var CONTROL_MSG;
4
+ (function (CONTROL_MSG) {
5
+ CONTROL_MSG["CONNECTION_CLOSED"] = "Connection closed";
6
+ CONTROL_MSG["CONNECTION_FAILED"] = "Connection failed";
7
+ CONTROL_MSG["REALTIME_SUBSCRIPTION_INIT_ERROR"] = "AppSync Realtime subscription init error";
8
+ CONTROL_MSG["SUBSCRIPTION_ACK"] = "Subscription ack";
9
+ CONTROL_MSG["TIMEOUT_DISCONNECT"] = "Timeout disconnect";
10
+ })(CONTROL_MSG = exports.CONTROL_MSG || (exports.CONTROL_MSG = {}));
11
+ /** @enum {string} */
12
+ var ConnectionState;
13
+ (function (ConnectionState) {
14
+ /*
15
+ * The connection is alive and healthy
16
+ */
17
+ ConnectionState["Connected"] = "Connected";
18
+ /*
19
+ * The connection is alive, but the connection is offline
20
+ */
21
+ ConnectionState["ConnectedPendingNetwork"] = "ConnectedPendingNetwork";
22
+ /*
23
+ * The connection has been disconnected while in use
24
+ */
25
+ ConnectionState["ConnectionDisrupted"] = "ConnectionDisrupted";
26
+ /*
27
+ * The connection has been disconnected and the network is offline
28
+ */
29
+ ConnectionState["ConnectionDisruptedPendingNetwork"] = "ConnectionDisruptedPendingNetwork";
30
+ /*
31
+ * The connection is in the process of connecting
32
+ */
33
+ ConnectionState["Connecting"] = "Connecting";
34
+ /*
35
+ * The connection is not in use and is being disconnected
36
+ */
37
+ ConnectionState["ConnectedPendingDisconnect"] = "ConnectedPendingDisconnect";
38
+ /*
39
+ * The connection is not in use and has been disconnected
40
+ */
41
+ ConnectionState["Disconnected"] = "Disconnected";
42
+ /*
43
+ * The connection is alive, but a keep alive message has been missed
44
+ */
45
+ ConnectionState["ConnectedPendingKeepAlive"] = "ConnectedPendingKeepAlive";
46
+ })(ConnectionState = exports.ConnectionState || (exports.ConnectionState = {}));
@@ -1,16 +1,17 @@
1
+ import { AmplifyClassV6, ResourcesConfig } from '@aws-amplify/core';
1
2
  import { Source, DocumentNode, GraphQLError } from 'graphql';
2
3
  export { OperationTypeNode } from 'graphql';
3
- import { GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';
4
- export { GRAPHQL_AUTH_MODE };
5
- import { Observable } from 'zen-observable-ts';
6
- import { AWSAppSyncRealTimeProvider } from '@aws-amplify/pubsub';
4
+ import { Observable } from 'rxjs';
5
+ import { GraphQLAuthMode, DocumentType } from '@aws-amplify/core/internals/utils';
6
+ import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
7
+ export { CONTROL_MSG, ConnectionState } from './PubSub';
7
8
  /**
8
9
  * Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
9
10
  */
10
11
  export interface GraphQLOptions {
11
12
  query: string | DocumentNode;
12
- variables?: object;
13
- authMode?: keyof typeof GRAPHQL_AUTH_MODE;
13
+ variables?: Record<string, DocumentType>;
14
+ authMode?: GraphQLAuthMode;
14
15
  authToken?: string;
15
16
  /**
16
17
  * @deprecated This property should not be used
@@ -18,7 +19,7 @@ export interface GraphQLOptions {
18
19
  userAgentSuffix?: string;
19
20
  }
20
21
  export interface GraphQLResult<T = object> {
21
- data?: T;
22
+ data: T;
22
23
  errors?: GraphQLError[];
23
24
  extensions?: {
24
25
  [key: string]: any;
@@ -31,6 +32,66 @@ export declare type GraphQLQuery<T> = T & {
31
32
  export declare type GraphQLSubscription<T> = T & {
32
33
  readonly [queryType]: 'subscription';
33
34
  };
35
+ /**
36
+ * Accepts a code generated model type and returns a supertype that
37
+ * can accept return values from the relevant graphql operations.
38
+ *
39
+ * For example:
40
+ *
41
+ * ```typescript
42
+ * import { GraphQLReturnType } from 'aws-amplify/api';
43
+ * import { MyModel } from './API';
44
+ * import { getMyModel } from './graphql/queries';
45
+ *
46
+ * const [item, setItem] = useState<GraphQLReturnType<MyModel>>();
47
+ * setItem(await client.graphql({ query: getMyModel }).data.getMyModel!)
48
+ * ```
49
+ *
50
+ * Trying to assign the result of a `getMyModel` operation directly to a
51
+ * `MyModel` variables won't necessarily work, because normally-required related
52
+ * models might not be part of the selection set.
53
+ *
54
+ * This util simply makes related model properties optional recursively.
55
+ */
56
+ export declare type GraphQLReturnType<T> = T extends {} ? {
57
+ [K in keyof T]?: GraphQLReturnType<T[K]>;
58
+ } : T;
59
+ /**
60
+ * Describes a paged list result from AppSync, which can either
61
+ * live at the top query or property (e.g., related model) level.
62
+ */
63
+ declare type PagedList<T, TYPENAME> = {
64
+ __typename: TYPENAME;
65
+ nextToken?: string | null | undefined;
66
+ items: Array<T>;
67
+ };
68
+ /**
69
+ * Recursively looks through a result type and removes nulls and
70
+ * and undefined from `PagedList` types.
71
+ *
72
+ * Although a graphql response might contain empty values in an
73
+ * array, this will only be the case when we also have errors,
74
+ * which will then be *thrown*.
75
+ */
76
+ declare type WithListsFixed<T> = T extends PagedList<infer IT, infer NAME> ? PagedList<Exclude<IT, null | undefined>, NAME> : T extends {} ? {
77
+ [K in keyof T]: WithListsFixed<T[K]>;
78
+ } : T;
79
+ /**
80
+ * Returns an updated response type to always return a value.
81
+ */
82
+ declare type NeverEmpty<T> = {
83
+ [K in keyof T]-?: Exclude<WithListsFixed<T[K]>, undefined | null>;
84
+ };
85
+ /**
86
+ * Replaces all list result types in a query result with types to exclude
87
+ * nulls and undefined from list member unions.
88
+ *
89
+ * If empty members are present, there will also be errors present,
90
+ * and the response will instead be *thrown*.
91
+ */
92
+ declare type FixedQueryResult<T> = Exclude<T[keyof T], null | undefined> extends PagedList<any, any> ? {
93
+ [K in keyof T]-?: WithListsFixed<Exclude<T[K], null | undefined>>;
94
+ } : T;
34
95
  /**
35
96
  * The return value from a `graphql({query})` call when `query` is a subscription.
36
97
  *
@@ -69,10 +130,22 @@ export declare type GraphqlSubscriptionResult<T> = Observable<GraphqlSubscriptio
69
130
  * ```
70
131
  */
71
132
  export declare type GraphqlSubscriptionMessage<T> = {
72
- provider: AWSAppSyncRealTimeProvider;
73
- value: {
74
- data?: T;
133
+ data: T;
134
+ };
135
+ export interface AWSAppSyncRealTimeProviderOptions {
136
+ appSyncGraphqlEndpoint?: string;
137
+ authenticationType?: GraphQLAuthMode;
138
+ query?: string;
139
+ variables?: Record<string, unknown>;
140
+ apiKey?: string;
141
+ region?: string;
142
+ graphql_headers?: () => {} | (() => Promise<{}>);
143
+ additionalHeaders?: {
144
+ [key: string]: string;
75
145
  };
146
+ }
147
+ export declare type AWSAppSyncRealTimeProvider = {
148
+ subscribe(options?: AWSAppSyncRealTimeProviderOptions): Observable<Record<string, unknown>>;
76
149
  };
77
150
  export declare enum GraphQLAuthError {
78
151
  NO_API_KEY = "No api-key configured",
@@ -93,7 +166,7 @@ export declare type GraphQLOperation = Source | string;
93
166
  export interface GraphQLOptionsV6<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string> {
94
167
  query: TYPED_GQL_STRING | DocumentNode;
95
168
  variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
96
- authMode?: keyof typeof GRAPHQL_AUTH_MODE;
169
+ authMode?: GraphQLAuthMode;
97
170
  authToken?: string;
98
171
  /**
99
172
  * @deprecated This property should not be used
@@ -117,7 +190,7 @@ export declare type GraphQLVariablesV6<FALLBACK_TYPES = unknown, TYPED_GQL_STRIN
117
190
  * The expected return type with respect to the given `FALLBACK_TYPE`
118
191
  * and `TYPED_GQL_STRING`.
119
192
  */
120
- export declare type GraphQLResponseV6<FALLBACK_TYPE = unknown, TYPED_GQL_STRING extends string = string> = TYPED_GQL_STRING extends GeneratedQuery<infer IN, infer QUERY_OUT> ? Promise<GraphQLResult<QUERY_OUT>> : TYPED_GQL_STRING extends GeneratedMutation<infer IN, infer MUTATION_OUT> ? Promise<GraphQLResult<MUTATION_OUT>> : TYPED_GQL_STRING extends GeneratedSubscription<infer IN, infer SUB_OUT> ? GraphqlSubscriptionResult<SUB_OUT> : FALLBACK_TYPE extends GraphQLQuery<infer T> ? Promise<GraphQLResult<FALLBACK_TYPE>> : FALLBACK_TYPE extends GraphQLSubscription<infer T> ? GraphqlSubscriptionResult<FALLBACK_TYPE> : FALLBACK_TYPE extends GraphQLOperationType<infer IN, infer CUSTOM_OUT> ? CUSTOM_OUT : UnknownGraphQLResponse;
193
+ export declare type GraphQLResponseV6<FALLBACK_TYPE = unknown, TYPED_GQL_STRING extends string = string> = TYPED_GQL_STRING extends GeneratedQuery<infer IN, infer QUERY_OUT> ? Promise<GraphQLResult<FixedQueryResult<QUERY_OUT>>> : TYPED_GQL_STRING extends GeneratedMutation<infer IN, infer MUTATION_OUT> ? Promise<GraphQLResult<NeverEmpty<MUTATION_OUT>>> : TYPED_GQL_STRING extends GeneratedSubscription<infer IN, infer SUB_OUT> ? GraphqlSubscriptionResult<NeverEmpty<SUB_OUT>> : FALLBACK_TYPE extends GraphQLQuery<infer T> ? Promise<GraphQLResult<FALLBACK_TYPE>> : FALLBACK_TYPE extends GraphQLSubscription<infer T> ? GraphqlSubscriptionResult<FALLBACK_TYPE> : FALLBACK_TYPE extends GraphQLOperationType<infer IN, infer CUSTOM_OUT> ? CUSTOM_OUT : UnknownGraphQLResponse;
121
194
  /**
122
195
  * The shape customers can use to provide `T` to `graphql<T>()` to specify both
123
196
  * `IN` and `OUT` types (the type of `variables` and the return type, respectively).
@@ -200,3 +273,37 @@ export declare type GeneratedSubscription<InputType, OutputType> = string & {
200
273
  __generatedSubscriptionInput: InputType;
201
274
  __generatedSubscriptionOutput: OutputType;
202
275
  };
276
+ declare type FilteredKeys<T> = {
277
+ [P in keyof T]: T[P] extends never ? never : P;
278
+ }[keyof T];
279
+ declare type ExcludeNeverFields<O> = {
280
+ [K in FilteredKeys<O>]: O[K];
281
+ };
282
+ export declare const __amplify: unique symbol;
283
+ export declare type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
284
+ [__amplify]: AmplifyClassV6;
285
+ graphql: GraphQLMethod;
286
+ cancel: (promise: Promise<any>, message?: string) => boolean;
287
+ isCancelError: (error: any) => boolean;
288
+ }>;
289
+ export declare type V6ClientSSR<T extends Record<any, any> = never> = ExcludeNeverFields<{
290
+ [__amplify]: AmplifyClassV6;
291
+ graphql: GraphQLMethodSSR;
292
+ cancel: (promise: Promise<any>, message?: string) => boolean;
293
+ isCancelError: (error: any) => boolean;
294
+ }>;
295
+ export declare type GraphQLMethod = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
296
+ [key: string]: string;
297
+ } | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
298
+ export declare type GraphQLMethodSSR = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(contextSpec: AmplifyServer.ContextSpec, options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
299
+ [key: string]: string;
300
+ } | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
301
+ /**
302
+ * @private
303
+ *
304
+ * The knobs available for configuring `server/generateClient` internally.
305
+ */
306
+ export declare type ServerClientGenerationParams = {
307
+ amplify: null | ((fn: (amplify: any) => Promise<any>) => Promise<AmplifyClassV6>);
308
+ config: ResourcesConfig;
309
+ };
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var auth_1 = require("@aws-amplify/auth");
4
- exports.GRAPHQL_AUTH_MODE = auth_1.GRAPHQL_AUTH_MODE;
3
+ var PubSub_1 = require("./PubSub");
4
+ exports.CONTROL_MSG = PubSub_1.CONTROL_MSG;
5
+ exports.ConnectionState = PubSub_1.ConnectionState;
5
6
  var GraphQLAuthError;
6
7
  (function (GraphQLAuthError) {
7
8
  GraphQLAuthError["NO_API_KEY"] = "No api-key configured";
@@ -10,4 +11,4 @@ var GraphQLAuthError;
10
11
  GraphQLAuthError["NO_FEDERATED_JWT"] = "No federated jwt";
11
12
  GraphQLAuthError["NO_AUTH_TOKEN"] = "No auth token specified";
12
13
  })(GraphQLAuthError = exports.GraphQLAuthError || (exports.GraphQLAuthError = {}));
13
- //# sourceMappingURL=index.js.map
14
+ exports.__amplify = Symbol('amplify');
@@ -0,0 +1,41 @@
1
+ import { Observable } from 'rxjs';
2
+ import { ConnectionState } from '../types/PubSub';
3
+ declare type LinkedConnectionState = 'connected' | 'disconnected';
4
+ declare type LinkedHealthState = 'healthy' | 'unhealthy';
5
+ declare type LinkedConnectionStates = {
6
+ networkState: LinkedConnectionState;
7
+ connectionState: LinkedConnectionState | 'connecting';
8
+ intendedConnectionState: LinkedConnectionState;
9
+ keepAliveState: LinkedHealthState;
10
+ };
11
+ export declare const CONNECTION_CHANGE: {
12
+ [key in 'KEEP_ALIVE_MISSED' | 'KEEP_ALIVE' | 'CONNECTION_ESTABLISHED' | 'CONNECTION_FAILED' | 'CLOSING_CONNECTION' | 'OPENING_CONNECTION' | 'CLOSED' | 'ONLINE' | 'OFFLINE']: Partial<LinkedConnectionStates>;
13
+ };
14
+ export declare class ConnectionStateMonitor {
15
+ /**
16
+ * @private
17
+ */
18
+ private _linkedConnectionState;
19
+ private _linkedConnectionStateObservable;
20
+ private _linkedConnectionStateObserver;
21
+ private _networkMonitoringSubscription?;
22
+ private _initialNetworkStateSubscription?;
23
+ constructor();
24
+ /**
25
+ * Turn network state monitoring on if it isn't on already
26
+ */
27
+ private enableNetworkMonitoring;
28
+ /**
29
+ * Turn network state monitoring off if it isn't off already
30
+ */
31
+ private disableNetworkMonitoring;
32
+ /**
33
+ * Get the observable that allows us to monitor the connection state
34
+ *
35
+ * @returns {Observable<ConnectionState>} - The observable that emits ConnectionState updates
36
+ */
37
+ get connectionStateObservable(): Observable<ConnectionState>;
38
+ record(statusUpdates: Partial<LinkedConnectionStates>): void;
39
+ private connectionStatesTranslator;
40
+ }
41
+ export {};
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const rxjs_1 = require("rxjs");
6
+ const PubSub_1 = require("../types/PubSub");
7
+ const ReachabilityMonitor_1 = require("./ReachabilityMonitor");
8
+ exports.CONNECTION_CHANGE = {
9
+ KEEP_ALIVE_MISSED: { keepAliveState: 'unhealthy' },
10
+ KEEP_ALIVE: { keepAliveState: 'healthy' },
11
+ CONNECTION_ESTABLISHED: { connectionState: 'connected' },
12
+ CONNECTION_FAILED: {
13
+ intendedConnectionState: 'disconnected',
14
+ connectionState: 'disconnected',
15
+ },
16
+ CLOSING_CONNECTION: { intendedConnectionState: 'disconnected' },
17
+ OPENING_CONNECTION: {
18
+ intendedConnectionState: 'connected',
19
+ connectionState: 'connecting',
20
+ },
21
+ CLOSED: { connectionState: 'disconnected' },
22
+ ONLINE: { networkState: 'connected' },
23
+ OFFLINE: { networkState: 'disconnected' },
24
+ };
25
+ class ConnectionStateMonitor {
26
+ constructor() {
27
+ this._networkMonitoringSubscription = undefined;
28
+ this._linkedConnectionState = {
29
+ networkState: 'connected',
30
+ connectionState: 'disconnected',
31
+ intendedConnectionState: 'disconnected',
32
+ keepAliveState: 'healthy',
33
+ };
34
+ // Attempt to update the state with the current actual network state
35
+ this._initialNetworkStateSubscription = ReachabilityMonitor_1.ReachabilityMonitor().subscribe(({ online }) => {
36
+ this.record(online ? exports.CONNECTION_CHANGE.ONLINE : exports.CONNECTION_CHANGE.OFFLINE);
37
+ this._initialNetworkStateSubscription?.unsubscribe();
38
+ });
39
+ this._linkedConnectionStateObservable =
40
+ new rxjs_1.Observable(connectionStateObserver => {
41
+ connectionStateObserver.next(this._linkedConnectionState);
42
+ this._linkedConnectionStateObserver = connectionStateObserver;
43
+ });
44
+ }
45
+ /**
46
+ * Turn network state monitoring on if it isn't on already
47
+ */
48
+ enableNetworkMonitoring() {
49
+ // If no initial network state was discovered, stop trying
50
+ this._initialNetworkStateSubscription?.unsubscribe();
51
+ // Maintain the network state based on the reachability monitor
52
+ if (this._networkMonitoringSubscription === undefined) {
53
+ this._networkMonitoringSubscription = ReachabilityMonitor_1.ReachabilityMonitor().subscribe(({ online }) => {
54
+ this.record(online ? exports.CONNECTION_CHANGE.ONLINE : exports.CONNECTION_CHANGE.OFFLINE);
55
+ });
56
+ }
57
+ }
58
+ /**
59
+ * Turn network state monitoring off if it isn't off already
60
+ */
61
+ disableNetworkMonitoring() {
62
+ this._networkMonitoringSubscription?.unsubscribe();
63
+ this._networkMonitoringSubscription = undefined;
64
+ }
65
+ /**
66
+ * Get the observable that allows us to monitor the connection state
67
+ *
68
+ * @returns {Observable<ConnectionState>} - The observable that emits ConnectionState updates
69
+ */
70
+ get connectionStateObservable() {
71
+ let previous;
72
+ // The linked state aggregates state changes to any of the network, connection,
73
+ // intendedConnection and keepAliveHealth. Some states will change these independent
74
+ // states without changing the overall connection state.
75
+ // After translating from linked states to ConnectionState, then remove any duplicates
76
+ return this._linkedConnectionStateObservable
77
+ .pipe(rxjs_1.map(value => {
78
+ return this.connectionStatesTranslator(value);
79
+ }))
80
+ .pipe(rxjs_1.filter(current => {
81
+ const toInclude = current !== previous;
82
+ previous = current;
83
+ return toInclude;
84
+ }));
85
+ }
86
+ /*
87
+ * Updates local connection state and emits the full state to the observer.
88
+ */
89
+ record(statusUpdates) {
90
+ // Maintain the network monitor
91
+ if (statusUpdates.intendedConnectionState === 'connected') {
92
+ this.enableNetworkMonitoring();
93
+ }
94
+ else if (statusUpdates.intendedConnectionState === 'disconnected') {
95
+ this.disableNetworkMonitoring();
96
+ }
97
+ // Maintain the socket state
98
+ const newSocketStatus = {
99
+ ...this._linkedConnectionState,
100
+ ...statusUpdates,
101
+ };
102
+ this._linkedConnectionState = { ...newSocketStatus };
103
+ this._linkedConnectionStateObserver?.next(this._linkedConnectionState);
104
+ }
105
+ /*
106
+ * Translate the ConnectionState structure into a specific ConnectionState string literal union
107
+ */
108
+ connectionStatesTranslator({ connectionState, networkState, intendedConnectionState, keepAliveState, }) {
109
+ if (connectionState === 'connected' && networkState === 'disconnected')
110
+ return PubSub_1.ConnectionState.ConnectedPendingNetwork;
111
+ if (connectionState === 'connected' &&
112
+ intendedConnectionState === 'disconnected')
113
+ return PubSub_1.ConnectionState.ConnectedPendingDisconnect;
114
+ if (connectionState === 'disconnected' &&
115
+ intendedConnectionState === 'connected' &&
116
+ networkState === 'disconnected')
117
+ return PubSub_1.ConnectionState.ConnectionDisruptedPendingNetwork;
118
+ if (connectionState === 'disconnected' &&
119
+ intendedConnectionState === 'connected')
120
+ return PubSub_1.ConnectionState.ConnectionDisrupted;
121
+ if (connectionState === 'connected' && keepAliveState === 'unhealthy')
122
+ return PubSub_1.ConnectionState.ConnectedPendingKeepAlive;
123
+ // All remaining states directly correspond to the connection state
124
+ if (connectionState === 'connecting')
125
+ return PubSub_1.ConnectionState.Connecting;
126
+ if (connectionState === 'disconnected')
127
+ return PubSub_1.ConnectionState.Disconnected;
128
+ return PubSub_1.ConnectionState.Connected;
129
+ }
130
+ }
131
+ exports.ConnectionStateMonitor = ConnectionStateMonitor;
@@ -0,0 +1 @@
1
+ export declare const ReachabilityMonitor: () => any;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
+ // SPDX-License-Identifier: Apache-2.0
5
+ const utils_1 = require("@aws-amplify/core/internals/utils");
6
+ exports.ReachabilityMonitor = () => new utils_1.Reachability().networkMonitor();
@@ -0,0 +1 @@
1
+ export declare const ReachabilityMonitor: () => any;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
+ // SPDX-License-Identifier: Apache-2.0
6
+ const utils_1 = require("@aws-amplify/core/internals/utils");
7
+ const netinfo_1 = tslib_1.__importDefault(require("@react-native-community/netinfo"));
8
+ exports.ReachabilityMonitor = () => new utils_1.Reachability().networkMonitor(netinfo_1.default);
@@ -0,0 +1,29 @@
1
+ import { Observer } from 'rxjs';
2
+ export declare enum ReconnectEvent {
3
+ START_RECONNECT = "START_RECONNECT",
4
+ HALT_RECONNECT = "HALT_RECONNECT"
5
+ }
6
+ /**
7
+ * Captures the reconnect event logic used to determine when to reconnect to PubSub providers.
8
+ * Reconnnect attempts are delayed by 5 seconds to let the interface settle.
9
+ * Attempting to reconnect only once creates unrecoverable states when the network state isn't
10
+ * supported by the browser, so this keeps retrying every minute until halted.
11
+ */
12
+ export declare class ReconnectionMonitor {
13
+ private reconnectObservers;
14
+ private reconnectIntervalId?;
15
+ private reconnectSetTimeoutId?;
16
+ /**
17
+ * Add reconnect observer to the list of observers to alert on reconnect
18
+ */
19
+ addObserver(reconnectObserver: Observer<void>): void;
20
+ /**
21
+ * Given a reconnect event, start the appropriate behavior
22
+ */
23
+ record(event: ReconnectEvent): void;
24
+ /**
25
+ * Complete all reconnect observers
26
+ */
27
+ close(): void;
28
+ private _triggerReconnect;
29
+ }
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("../Providers/constants");
4
+ var ReconnectEvent;
5
+ (function (ReconnectEvent) {
6
+ ReconnectEvent["START_RECONNECT"] = "START_RECONNECT";
7
+ ReconnectEvent["HALT_RECONNECT"] = "HALT_RECONNECT";
8
+ })(ReconnectEvent = exports.ReconnectEvent || (exports.ReconnectEvent = {}));
9
+ /**
10
+ * Captures the reconnect event logic used to determine when to reconnect to PubSub providers.
11
+ * Reconnnect attempts are delayed by 5 seconds to let the interface settle.
12
+ * Attempting to reconnect only once creates unrecoverable states when the network state isn't
13
+ * supported by the browser, so this keeps retrying every minute until halted.
14
+ */
15
+ class ReconnectionMonitor {
16
+ constructor() {
17
+ this.reconnectObservers = [];
18
+ }
19
+ /**
20
+ * Add reconnect observer to the list of observers to alert on reconnect
21
+ */
22
+ addObserver(reconnectObserver) {
23
+ this.reconnectObservers.push(reconnectObserver);
24
+ }
25
+ /**
26
+ * Given a reconnect event, start the appropriate behavior
27
+ */
28
+ record(event) {
29
+ if (event === ReconnectEvent.START_RECONNECT) {
30
+ // If the reconnection hasn't been started
31
+ if (this.reconnectSetTimeoutId === undefined &&
32
+ this.reconnectIntervalId === undefined) {
33
+ this.reconnectSetTimeoutId = setTimeout(() => {
34
+ // Reconnect now
35
+ this._triggerReconnect();
36
+ // Retry reconnect every periodically until it works
37
+ this.reconnectIntervalId = setInterval(() => {
38
+ this._triggerReconnect();
39
+ }, constants_1.RECONNECT_INTERVAL);
40
+ }, constants_1.RECONNECT_DELAY);
41
+ }
42
+ }
43
+ if (event === ReconnectEvent.HALT_RECONNECT) {
44
+ if (this.reconnectIntervalId) {
45
+ clearInterval(this.reconnectIntervalId);
46
+ this.reconnectIntervalId = undefined;
47
+ }
48
+ if (this.reconnectSetTimeoutId) {
49
+ clearTimeout(this.reconnectSetTimeoutId);
50
+ this.reconnectSetTimeoutId = undefined;
51
+ }
52
+ }
53
+ }
54
+ /**
55
+ * Complete all reconnect observers
56
+ */
57
+ close() {
58
+ this.reconnectObservers.forEach(reconnectObserver => {
59
+ reconnectObserver.complete?.();
60
+ });
61
+ }
62
+ _triggerReconnect() {
63
+ this.reconnectObservers.forEach(reconnectObserver => {
64
+ reconnectObserver.next?.();
65
+ });
66
+ }
67
+ }
68
+ exports.ReconnectionMonitor = ReconnectionMonitor;
@@ -0,0 +1,7 @@
1
+ import { AmplifyError, AmplifyErrorParams } from '@aws-amplify/core/internals/utils';
2
+ /**
3
+ * @internal
4
+ */
5
+ export declare class APIError extends AmplifyError {
6
+ constructor(params: AmplifyErrorParams);
7
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const utils_1 = require("@aws-amplify/core/internals/utils");
6
+ /**
7
+ * @internal
8
+ */
9
+ class APIError extends utils_1.AmplifyError {
10
+ constructor(params) {
11
+ super(params);
12
+ // Hack for making the custom error class work when transpiled to es5
13
+ // TODO: Delete the following 2 lines after we change the build target to >= es2015
14
+ this.constructor = APIError;
15
+ Object.setPrototypeOf(this, APIError.prototype);
16
+ }
17
+ }
18
+ exports.APIError = APIError;
@@ -0,0 +1,5 @@
1
+ import { APIValidationErrorCode } from './validation';
2
+ /**
3
+ * @internal
4
+ */
5
+ export declare function assertValidationError(assertion: boolean, name: APIValidationErrorCode): asserts assertion;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const APIError_1 = require("./APIError");
6
+ const validation_1 = require("./validation");
7
+ /**
8
+ * @internal
9
+ */
10
+ function assertValidationError(assertion, name) {
11
+ const { message, recoverySuggestion } = validation_1.validationErrorMap[name];
12
+ if (!assertion) {
13
+ throw new APIError_1.APIError({ name, message, recoverySuggestion });
14
+ }
15
+ }
16
+ exports.assertValidationError = assertValidationError;
@@ -0,0 +1,3 @@
1
+ export { APIError } from './APIError';
2
+ export { assertValidationError } from './assertValidationError';
3
+ export { APIValidationErrorCode, validationErrorMap } from './validation';
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ var APIError_1 = require("./APIError");
6
+ exports.APIError = APIError_1.APIError;
7
+ var assertValidationError_1 = require("./assertValidationError");
8
+ exports.assertValidationError = assertValidationError_1.assertValidationError;
9
+ var validation_1 = require("./validation");
10
+ exports.APIValidationErrorCode = validation_1.APIValidationErrorCode;
11
+ exports.validationErrorMap = validation_1.validationErrorMap;
@@ -0,0 +1,7 @@
1
+ import { AmplifyErrorMap } from '@aws-amplify/core/internals/utils';
2
+ export declare enum APIValidationErrorCode {
3
+ NoAuthSession = "NoAuthSession",
4
+ NoRegion = "NoRegion",
5
+ NoCustomEndpoint = "NoCustomEndpoint"
6
+ }
7
+ export declare const validationErrorMap: AmplifyErrorMap<APIValidationErrorCode>;