@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.
- package/README.md +3 -0
- package/dist/cjs/GraphQLAPI.js +66 -0
- package/dist/cjs/GraphQLAPI.js.map +1 -0
- package/dist/cjs/Providers/AWSAppSyncEventsProvider/index.js +117 -0
- package/dist/cjs/Providers/AWSAppSyncEventsProvider/index.js.map +1 -0
- package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js +96 -0
- package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -0
- package/dist/cjs/Providers/AWSWebSocketProvider/appsyncUrl.js +113 -0
- package/dist/cjs/Providers/AWSWebSocketProvider/appsyncUrl.js.map +1 -0
- package/dist/cjs/Providers/AWSWebSocketProvider/authHeaders.js +99 -0
- package/dist/cjs/Providers/AWSWebSocketProvider/authHeaders.js.map +1 -0
- package/dist/cjs/Providers/AWSWebSocketProvider/index.js +693 -0
- package/dist/cjs/Providers/AWSWebSocketProvider/index.js.map +1 -0
- package/dist/cjs/Providers/constants.js +149 -0
- package/dist/cjs/Providers/constants.js.map +1 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/internals/InternalGraphQLAPI.js +269 -0
- package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -0
- package/dist/cjs/internals/events/appsyncRequest.js +96 -0
- package/dist/cjs/internals/events/appsyncRequest.js.map +1 -0
- package/dist/cjs/internals/events/index.js +142 -0
- package/dist/cjs/internals/events/index.js.map +1 -0
- package/dist/cjs/internals/events/types.js +6 -0
- package/dist/cjs/internals/events/types.js.map +1 -0
- package/dist/cjs/internals/events/utils.js +57 -0
- package/dist/cjs/internals/events/utils.js.map +1 -0
- package/dist/cjs/internals/generateClient.js +86 -0
- package/dist/cjs/internals/generateClient.js.map +1 -0
- package/dist/cjs/internals/graphqlAuth.js +63 -0
- package/dist/cjs/internals/graphqlAuth.js.map +1 -0
- package/dist/cjs/internals/graphqlRequest.js +19 -0
- package/dist/cjs/internals/graphqlRequest.js.map +1 -0
- package/dist/cjs/internals/index.js +16 -0
- package/dist/cjs/internals/index.js.map +1 -0
- package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js +48 -0
- package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js.map +1 -0
- package/dist/cjs/internals/server/index.js +9 -0
- package/dist/cjs/internals/server/index.js.map +1 -0
- package/dist/cjs/internals/types.js +4 -0
- package/dist/cjs/internals/types.js.map +1 -0
- package/dist/cjs/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.js +10 -0
- package/dist/cjs/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.js.map +1 -0
- package/dist/cjs/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.js +10 -0
- package/dist/cjs/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.js.map +1 -0
- package/dist/cjs/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.js +14 -0
- package/dist/cjs/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.js.map +1 -0
- package/dist/cjs/internals/v6.js +155 -0
- package/dist/cjs/internals/v6.js.map +1 -0
- package/dist/cjs/server/generateClient.js +42 -0
- package/dist/cjs/server/generateClient.js.map +1 -0
- package/dist/cjs/server/index.js +9 -0
- package/dist/cjs/server/index.js.map +1 -0
- package/dist/cjs/types/PubSub.js +49 -0
- package/dist/cjs/types/PubSub.js.map +1 -0
- package/dist/cjs/types/index.js +34 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/utils/ConnectionStateMonitor.js +134 -0
- package/dist/cjs/utils/ConnectionStateMonitor.js.map +1 -0
- package/dist/cjs/utils/ReachabilityMonitor/index.js +10 -0
- package/dist/cjs/utils/ReachabilityMonitor/index.js.map +1 -0
- package/dist/cjs/utils/ReachabilityMonitor/index.native.js +12 -0
- package/dist/cjs/utils/ReachabilityMonitor/index.native.js.map +1 -0
- package/dist/cjs/utils/ReconnectionMonitor.js +71 -0
- package/dist/cjs/utils/ReconnectionMonitor.js.map +1 -0
- package/dist/cjs/utils/errors/GraphQLApiError.js +21 -0
- package/dist/cjs/utils/errors/GraphQLApiError.js.map +1 -0
- package/dist/cjs/utils/errors/assertValidationError.js +18 -0
- package/dist/cjs/utils/errors/assertValidationError.js.map +1 -0
- package/dist/cjs/utils/errors/constants.js +47 -0
- package/dist/cjs/utils/errors/constants.js.map +1 -0
- package/dist/cjs/utils/errors/createGraphQLResultWithError.js +15 -0
- package/dist/cjs/utils/errors/createGraphQLResultWithError.js.map +1 -0
- package/dist/cjs/utils/errors/index.js +16 -0
- package/dist/cjs/utils/errors/index.js.map +1 -0
- package/dist/cjs/utils/errors/repackageAuthError.js +38 -0
- package/dist/cjs/utils/errors/repackageAuthError.js.map +1 -0
- package/dist/cjs/utils/errors/validation.js +28 -0
- package/dist/cjs/utils/errors/validation.js.map +1 -0
- package/dist/cjs/utils/index.js +11 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/utils/resolveConfig.js +32 -0
- package/dist/cjs/utils/resolveConfig.js.map +1 -0
- package/dist/cjs/utils/resolveLibraryOptions.js +16 -0
- package/dist/cjs/utils/resolveLibraryOptions.js.map +1 -0
- package/dist/esm/GraphQLAPI.d.ts +37 -0
- package/dist/esm/GraphQLAPI.mjs +63 -0
- package/dist/esm/GraphQLAPI.mjs.map +1 -0
- package/dist/esm/Providers/AWSAppSyncEventsProvider/index.d.ts +49 -0
- package/dist/esm/Providers/AWSAppSyncEventsProvider/index.mjs +115 -0
- package/dist/esm/Providers/AWSAppSyncEventsProvider/index.mjs.map +1 -0
- package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +47 -0
- package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +94 -0
- package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -0
- package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.d.ts +21 -0
- package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.mjs +107 -0
- package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.mjs.map +1 -0
- package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.d.ts +8 -0
- package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.mjs +97 -0
- package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.mjs.map +1 -0
- package/dist/esm/Providers/AWSWebSocketProvider/index.d.ts +99 -0
- package/dist/esm/Providers/AWSWebSocketProvider/index.mjs +691 -0
- package/dist/esm/Providers/AWSWebSocketProvider/index.mjs.map +1 -0
- package/dist/esm/Providers/constants.d.ts +135 -0
- package/dist/esm/Providers/constants.mjs +147 -0
- package/dist/esm/Providers/constants.mjs.map +1 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.mjs +7 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/internals/InternalGraphQLAPI.d.ts +45 -0
- package/dist/esm/internals/InternalGraphQLAPI.mjs +270 -0
- package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -0
- package/dist/esm/internals/events/appsyncRequest.d.ts +14 -0
- package/dist/esm/internals/events/appsyncRequest.mjs +96 -0
- package/dist/esm/internals/events/appsyncRequest.mjs.map +1 -0
- package/dist/esm/internals/events/index.d.ts +58 -0
- package/dist/esm/internals/events/index.mjs +139 -0
- package/dist/esm/internals/events/index.mjs.map +1 -0
- package/dist/esm/internals/events/types.d.ts +93 -0
- package/dist/esm/internals/events/types.mjs +2 -0
- package/dist/esm/internals/events/types.mjs.map +1 -0
- package/dist/esm/internals/events/utils.d.ts +16 -0
- package/dist/esm/internals/events/utils.mjs +53 -0
- package/dist/esm/internals/events/utils.mjs.map +1 -0
- package/dist/esm/internals/generateClient.d.ts +12 -0
- package/dist/esm/internals/generateClient.mjs +85 -0
- package/dist/esm/internals/generateClient.mjs.map +1 -0
- package/dist/esm/internals/graphqlAuth.d.ts +3 -0
- package/dist/esm/internals/graphqlAuth.mjs +64 -0
- package/dist/esm/internals/graphqlAuth.mjs.map +1 -0
- package/dist/esm/internals/graphqlRequest.d.ts +3 -0
- package/dist/esm/internals/graphqlRequest.mjs +18 -0
- package/dist/esm/internals/graphqlRequest.mjs.map +1 -0
- package/dist/esm/internals/index.d.ts +4 -0
- package/dist/esm/internals/index.mjs +4 -0
- package/dist/esm/internals/index.mjs.map +1 -0
- package/dist/esm/internals/server/generateClientWithAmplifyInstance.d.ts +13 -0
- package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs +61 -0
- package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs.map +1 -0
- package/dist/esm/internals/server/index.d.ts +1 -0
- package/dist/esm/internals/server/index.mjs +2 -0
- package/dist/esm/internals/server/index.mjs.map +1 -0
- package/dist/esm/internals/types.d.ts +34 -0
- package/dist/esm/internals/types.mjs +2 -0
- package/dist/esm/internals/types.mjs.map +1 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.d.ts +2 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.mjs +8 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.mjs.map +1 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.d.ts +8 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.mjs +8 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.mjs.map +1 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.d.ts +2 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.mjs +12 -0
- package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.mjs.map +1 -0
- package/dist/esm/internals/v6.d.ts +100 -0
- package/dist/esm/internals/v6.mjs +152 -0
- package/dist/esm/internals/v6.mjs.map +1 -0
- package/dist/esm/server/generateClient.d.ts +21 -0
- package/dist/esm/server/generateClient.mjs +41 -0
- package/dist/esm/server/generateClient.mjs.map +1 -0
- package/dist/esm/server/index.d.ts +1 -0
- package/dist/esm/server/index.mjs +2 -0
- package/dist/esm/server/index.mjs.map +1 -0
- package/dist/esm/types/PubSub.d.ts +27 -0
- package/dist/esm/types/PubSub.mjs +47 -0
- package/dist/esm/types/PubSub.mjs.map +1 -0
- package/dist/esm/types/index.d.ts +369 -0
- package/dist/esm/types/index.mjs +30 -0
- package/dist/esm/types/index.mjs.map +1 -0
- package/dist/esm/utils/ConnectionStateMonitor.d.ts +69 -0
- package/dist/esm/utils/ConnectionStateMonitor.mjs +132 -0
- package/dist/esm/utils/ConnectionStateMonitor.mjs.map +1 -0
- package/dist/esm/utils/ReachabilityMonitor/index.d.ts +1 -0
- package/dist/esm/utils/ReachabilityMonitor/index.mjs +8 -0
- package/dist/esm/utils/ReachabilityMonitor/index.mjs.map +1 -0
- package/dist/esm/utils/ReachabilityMonitor/index.native.d.ts +1 -0
- package/dist/esm/utils/ReachabilityMonitor/index.native.mjs +9 -0
- package/dist/esm/utils/ReachabilityMonitor/index.native.mjs.map +1 -0
- package/dist/esm/utils/ReconnectionMonitor.d.ts +29 -0
- package/dist/esm/utils/ReconnectionMonitor.mjs +69 -0
- package/dist/esm/utils/ReconnectionMonitor.mjs.map +1 -0
- package/dist/esm/utils/errors/GraphQLApiError.d.ts +7 -0
- package/dist/esm/utils/errors/GraphQLApiError.mjs +19 -0
- package/dist/esm/utils/errors/GraphQLApiError.mjs.map +1 -0
- package/dist/esm/utils/errors/assertValidationError.d.ts +5 -0
- package/dist/esm/utils/errors/assertValidationError.mjs +17 -0
- package/dist/esm/utils/errors/assertValidationError.mjs.map +1 -0
- package/dist/esm/utils/errors/constants.d.ts +7 -0
- package/dist/esm/utils/errors/constants.mjs +46 -0
- package/dist/esm/utils/errors/constants.mjs.map +1 -0
- package/dist/esm/utils/errors/createGraphQLResultWithError.d.ts +2 -0
- package/dist/esm/utils/errors/createGraphQLResultWithError.mjs +13 -0
- package/dist/esm/utils/errors/createGraphQLResultWithError.mjs.map +1 -0
- package/dist/esm/utils/errors/index.d.ts +4 -0
- package/dist/esm/utils/errors/index.mjs +5 -0
- package/dist/esm/utils/errors/index.mjs.map +1 -0
- package/dist/esm/utils/errors/repackageAuthError.d.ts +7 -0
- package/dist/esm/utils/errors/repackageAuthError.mjs +36 -0
- package/dist/esm/utils/errors/repackageAuthError.mjs.map +1 -0
- package/dist/esm/utils/errors/validation.d.ts +7 -0
- package/dist/esm/utils/errors/validation.mjs +26 -0
- package/dist/esm/utils/errors/validation.mjs.map +1 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/index.mjs +3 -0
- package/dist/esm/utils/index.mjs.map +1 -0
- package/dist/esm/utils/resolveConfig.d.ts +12 -0
- package/dist/esm/utils/resolveConfig.mjs +33 -0
- package/dist/esm/utils/resolveConfig.mjs.map +1 -0
- package/dist/esm/utils/resolveLibraryOptions.d.ts +11 -0
- package/dist/esm/utils/resolveLibraryOptions.mjs +13 -0
- package/dist/esm/utils/resolveLibraryOptions.mjs.map +1 -0
- package/internals/package.json +8 -0
- package/internals/server/package.json +8 -0
- package/package.json +99 -0
- package/server/package.json +7 -0
- package/src/GraphQLAPI.ts +97 -0
- package/src/Providers/AWSAppSyncEventsProvider/index.ts +218 -0
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +185 -0
- package/src/Providers/AWSWebSocketProvider/appsyncUrl.ts +152 -0
- package/src/Providers/AWSWebSocketProvider/authHeaders.ts +146 -0
- package/src/Providers/AWSWebSocketProvider/index.ts +1028 -0
- package/src/Providers/constants.ts +154 -0
- package/src/index.ts +12 -0
- package/src/internals/InternalGraphQLAPI.ts +424 -0
- package/src/internals/events/appsyncRequest.ts +169 -0
- package/src/internals/events/index.ts +199 -0
- package/src/internals/events/types.ts +109 -0
- package/src/internals/events/utils.ts +77 -0
- package/src/internals/generateClient.ts +120 -0
- package/src/internals/graphqlAuth.ts +87 -0
- package/src/internals/graphqlRequest.ts +30 -0
- package/src/internals/index.ts +10 -0
- package/src/internals/server/generateClientWithAmplifyInstance.ts +73 -0
- package/src/internals/server/index.ts +4 -0
- package/src/internals/types.ts +42 -0
- package/src/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.ts +10 -0
- package/src/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.ts +13 -0
- package/src/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.ts +15 -0
- package/src/internals/v6.ts +189 -0
- package/src/server/generateClient.ts +68 -0
- package/src/server/index.ts +4 -0
- package/src/types/PubSub.ts +57 -0
- package/src/types/index.ts +524 -0
- package/src/utils/ConnectionStateMonitor.ts +195 -0
- package/src/utils/ReachabilityMonitor/index.native.ts +7 -0
- package/src/utils/ReachabilityMonitor/index.ts +5 -0
- package/src/utils/ReconnectionMonitor.ts +77 -0
- package/src/utils/errors/GraphQLApiError.ts +21 -0
- package/src/utils/errors/assertValidationError.ts +19 -0
- package/src/utils/errors/constants.ts +56 -0
- package/src/utils/errors/createGraphQLResultWithError.ts +15 -0
- package/src/utils/errors/index.ts +7 -0
- package/src/utils/errors/repackageAuthError.ts +43 -0
- package/src/utils/errors/validation.ts +26 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/resolveConfig.ts +46 -0
- package/src/utils/resolveLibraryOptions.ts +14 -0
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { AmplifyClassV6, ResourcesConfig } from '@aws-amplify/core';
|
|
4
|
+
import {
|
|
5
|
+
BaseClient,
|
|
6
|
+
ClientExtensions,
|
|
7
|
+
ClientExtensionsSSRCookies,
|
|
8
|
+
ClientExtensionsSSRRequest,
|
|
9
|
+
ClientInternals,
|
|
10
|
+
CustomHeaders,
|
|
11
|
+
ModelSortDirection,
|
|
12
|
+
} from '@aws-amplify/data-schema/runtime';
|
|
13
|
+
import { DocumentNode, GraphQLError, Source } from 'graphql';
|
|
14
|
+
import { Observable } from 'rxjs';
|
|
15
|
+
import {
|
|
16
|
+
DocumentType,
|
|
17
|
+
GraphQLAuthMode,
|
|
18
|
+
} from '@aws-amplify/core/internals/utils';
|
|
19
|
+
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
|
|
20
|
+
|
|
21
|
+
import { CommonPublicClientOptions } from '../internals/types';
|
|
22
|
+
|
|
23
|
+
export { OperationTypeNode } from 'graphql';
|
|
24
|
+
|
|
25
|
+
export { CONTROL_MSG, ConnectionState } from './PubSub';
|
|
26
|
+
|
|
27
|
+
export { SelectionSet } from '@aws-amplify/data-schema/runtime';
|
|
28
|
+
|
|
29
|
+
export type { CommonPublicClientOptions };
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
|
|
33
|
+
*/
|
|
34
|
+
export interface GraphQLOptions {
|
|
35
|
+
query: string | DocumentNode;
|
|
36
|
+
endpoint?: string;
|
|
37
|
+
variables?: Record<string, DocumentType>;
|
|
38
|
+
authMode?: GraphQLAuthMode;
|
|
39
|
+
authToken?: string;
|
|
40
|
+
apiKey?: string;
|
|
41
|
+
/**
|
|
42
|
+
* @deprecated This property should not be used
|
|
43
|
+
*/
|
|
44
|
+
userAgentSuffix?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface GraphQLResult<T = object | null> {
|
|
48
|
+
data: T;
|
|
49
|
+
errors?: GraphQLError[];
|
|
50
|
+
extensions?: Record<string, any>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Opaque type used for determining the graphql query type
|
|
54
|
+
declare const queryType: unique symbol;
|
|
55
|
+
|
|
56
|
+
export type GraphQLQuery<T> = T & { readonly [queryType]: 'query' };
|
|
57
|
+
export type GraphQLSubscription<T> = T & {
|
|
58
|
+
readonly [queryType]: 'subscription';
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Accepts a code generated model type and returns a supertype that
|
|
63
|
+
* can accept return values from the relevant graphql operations.
|
|
64
|
+
*
|
|
65
|
+
* For example:
|
|
66
|
+
*
|
|
67
|
+
* ```typescript
|
|
68
|
+
* import { GraphQLReturnType } from 'aws-amplify/api';
|
|
69
|
+
* import { MyModel } from './API';
|
|
70
|
+
* import { getMyModel } from './graphql/queries';
|
|
71
|
+
*
|
|
72
|
+
* const [item, setItem] = useState<GraphQLReturnType<MyModel>>();
|
|
73
|
+
* setItem(await client.graphql({ query: getMyModel }).data.getMyModel!)
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* Trying to assign the result of a `getMyModel` operation directly to a
|
|
77
|
+
* `MyModel` variables won't necessarily work, because normally-required related
|
|
78
|
+
* models might not be part of the selection set.
|
|
79
|
+
*
|
|
80
|
+
* This util simply makes related model properties optional recursively.
|
|
81
|
+
*/
|
|
82
|
+
export type GraphQLReturnType<T> =
|
|
83
|
+
T extends Record<string, unknown>
|
|
84
|
+
? {
|
|
85
|
+
[K in keyof T]?: GraphQLReturnType<T[K]>;
|
|
86
|
+
}
|
|
87
|
+
: T;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Describes a paged list result from AppSync, which can either
|
|
91
|
+
* live at the top query or property (e.g., related model) level.
|
|
92
|
+
*/
|
|
93
|
+
interface PagedList<T, TYPENAME> {
|
|
94
|
+
__typename: TYPENAME;
|
|
95
|
+
nextToken?: string | null | undefined;
|
|
96
|
+
items: T[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Recursively looks through a result type and removes nulls and
|
|
101
|
+
* and undefined from `PagedList` types.
|
|
102
|
+
*
|
|
103
|
+
* Although a graphql response might contain empty values in an
|
|
104
|
+
* array, this will only be the case when we also have errors,
|
|
105
|
+
* which will then be *thrown*.
|
|
106
|
+
*/
|
|
107
|
+
type WithListsFixed<T> =
|
|
108
|
+
T extends PagedList<infer IT, infer NAME>
|
|
109
|
+
? PagedList<Exclude<IT, null | undefined>, NAME>
|
|
110
|
+
: T extends Record<string, unknown>
|
|
111
|
+
? {
|
|
112
|
+
[K in keyof T]: WithListsFixed<T[K]>;
|
|
113
|
+
}
|
|
114
|
+
: T;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Returns an updated response type to always return a value.
|
|
118
|
+
*/
|
|
119
|
+
type NeverEmpty<T> = {
|
|
120
|
+
[K in keyof T]-?: Exclude<WithListsFixed<T[K]>, undefined | null>;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Replaces all list result types in a query result with types to exclude
|
|
125
|
+
* nulls and undefined from list member unions.
|
|
126
|
+
*
|
|
127
|
+
* If empty members are present, there will also be errors present,
|
|
128
|
+
* and the response will instead be *thrown*.
|
|
129
|
+
*/
|
|
130
|
+
type FixedQueryResult<T> =
|
|
131
|
+
Exclude<T[keyof T], null | undefined> extends PagedList<any, any>
|
|
132
|
+
? {
|
|
133
|
+
[K in keyof T]-?: WithListsFixed<Exclude<T[K], null | undefined>>;
|
|
134
|
+
}
|
|
135
|
+
: T;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The return value from a `graphql({query})` call when `query` is a subscription.
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* // |-- You are here
|
|
142
|
+
* // v
|
|
143
|
+
* const subResult: GraphqlSubscriptionResult<T> = client.graphql({
|
|
144
|
+
* query: onCreateWidget
|
|
145
|
+
* });
|
|
146
|
+
*
|
|
147
|
+
* const sub = subResult.subscribe({
|
|
148
|
+
* //
|
|
149
|
+
* // |-- You are here
|
|
150
|
+
* // v
|
|
151
|
+
* next(message: GraphqlSubscriptionMessage<OnCreateWidgetSubscription>) {
|
|
152
|
+
* handle(message.value); // <-- type OnCreateWidgetSubscription
|
|
153
|
+
* }
|
|
154
|
+
* })
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export type GraphqlSubscriptionResult<T> = Observable<
|
|
158
|
+
GraphqlSubscriptionMessage<T>
|
|
159
|
+
>;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The shape of messages passed to `next()` from a graphql subscription. E.g.,
|
|
163
|
+
*
|
|
164
|
+
* ```ts
|
|
165
|
+
* const sub = client.graphql({
|
|
166
|
+
* query: onCreateWidget,
|
|
167
|
+
* }).subscribe({
|
|
168
|
+
* //
|
|
169
|
+
* // |-- You are here
|
|
170
|
+
* // v
|
|
171
|
+
* next(message: GraphqlSubscriptionMessage<OnCreateWidgetSubscription>) {
|
|
172
|
+
* handle(message.value); // <-- type OnCreateWidgetSubscription
|
|
173
|
+
* }
|
|
174
|
+
* })
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
export interface GraphqlSubscriptionMessage<T> {
|
|
178
|
+
data: T;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface AWSAppSyncRealTimeProviderOptions {
|
|
182
|
+
appSyncGraphqlEndpoint?: string;
|
|
183
|
+
authenticationType?: GraphQLAuthMode;
|
|
184
|
+
query?: string;
|
|
185
|
+
variables?: Record<string, unknown>;
|
|
186
|
+
apiKey?: string;
|
|
187
|
+
region?: string;
|
|
188
|
+
libraryConfigHeaders?(): () => Promise<Record<string, unknown> | Headers>;
|
|
189
|
+
additionalHeaders?: CustomHeaders;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export interface AWSAppSyncRealTimeProvider {
|
|
193
|
+
subscribe(
|
|
194
|
+
options?: AWSAppSyncRealTimeProviderOptions,
|
|
195
|
+
): Observable<Record<string, unknown>>;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export enum GraphQLAuthError {
|
|
199
|
+
NO_API_KEY = 'No api-key configured',
|
|
200
|
+
NO_CURRENT_USER = 'No current user',
|
|
201
|
+
NO_CREDENTIALS = 'No credentials',
|
|
202
|
+
NO_FEDERATED_JWT = 'No federated jwt',
|
|
203
|
+
NO_AUTH_TOKEN = 'No auth token specified',
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* GraphQLSource or string, the type of the parameter for calling graphql.parse
|
|
208
|
+
* @see: https://graphql.org/graphql-js/language/#parse
|
|
209
|
+
*/
|
|
210
|
+
export type GraphQLOperation = Source | string;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* API V6 `graphql({options})` type that can leverage branded graphql `query`
|
|
214
|
+
* objects and fallback types.
|
|
215
|
+
*/
|
|
216
|
+
export type GraphQLOptionsV6<
|
|
217
|
+
FALLBACK_TYPES = unknown,
|
|
218
|
+
TYPED_GQL_STRING extends string = string,
|
|
219
|
+
Options extends CommonPublicClientOptions = object,
|
|
220
|
+
> = Options['endpoint'] extends string
|
|
221
|
+
? Options['apiKey'] extends string
|
|
222
|
+
? {
|
|
223
|
+
query: TYPED_GQL_STRING | DocumentNode;
|
|
224
|
+
variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
225
|
+
authMode?: GraphQLAuthMode;
|
|
226
|
+
apiKey?: string;
|
|
227
|
+
authToken?: string;
|
|
228
|
+
/**
|
|
229
|
+
* @deprecated This property should not be used
|
|
230
|
+
*/
|
|
231
|
+
userAgentSuffix?: string;
|
|
232
|
+
}
|
|
233
|
+
:
|
|
234
|
+
| {
|
|
235
|
+
query: TYPED_GQL_STRING | DocumentNode;
|
|
236
|
+
variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
237
|
+
authMode?: never;
|
|
238
|
+
apiKey?: never;
|
|
239
|
+
authToken?: string;
|
|
240
|
+
/**
|
|
241
|
+
* @deprecated This property should not be used
|
|
242
|
+
*/
|
|
243
|
+
userAgentSuffix?: string;
|
|
244
|
+
}
|
|
245
|
+
| {
|
|
246
|
+
query: TYPED_GQL_STRING | DocumentNode;
|
|
247
|
+
variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
248
|
+
authMode: 'apiKey';
|
|
249
|
+
apiKey: string;
|
|
250
|
+
authToken?: string;
|
|
251
|
+
/**
|
|
252
|
+
* @deprecated This property should not be used
|
|
253
|
+
*/
|
|
254
|
+
userAgentSuffix?: string;
|
|
255
|
+
}
|
|
256
|
+
| {
|
|
257
|
+
query: TYPED_GQL_STRING | DocumentNode;
|
|
258
|
+
variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
259
|
+
authMode: Exclude<GraphQLAuthMode, 'apiKey'>;
|
|
260
|
+
apiKey?: never;
|
|
261
|
+
authToken?: string;
|
|
262
|
+
/**
|
|
263
|
+
* @deprecated This property should not be used
|
|
264
|
+
*/
|
|
265
|
+
userAgentSuffix?: string;
|
|
266
|
+
}
|
|
267
|
+
: {
|
|
268
|
+
query: TYPED_GQL_STRING | DocumentNode;
|
|
269
|
+
variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
270
|
+
authMode?: GraphQLAuthMode;
|
|
271
|
+
apiKey?: string;
|
|
272
|
+
authToken?: string;
|
|
273
|
+
/**
|
|
274
|
+
* @deprecated This property should not be used
|
|
275
|
+
*/
|
|
276
|
+
userAgentSuffix?: string;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Result type for `graphql()` operations that don't include any specific
|
|
281
|
+
* type information. The result could be either a `Promise` or `Subscription`.
|
|
282
|
+
*
|
|
283
|
+
* Invoking code should either cast the result or use `?` and `!` operators to
|
|
284
|
+
* navigate the result objects.
|
|
285
|
+
*/
|
|
286
|
+
export type UnknownGraphQLResponse =
|
|
287
|
+
| Promise<GraphQLResult<any>>
|
|
288
|
+
| GraphqlSubscriptionResult<any>;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* The expected type for `variables` in a V6 `graphql()` operation with
|
|
292
|
+
* respect to the given `FALLBACK_TYPES` and `TYPED_GQL_STRING`.
|
|
293
|
+
*/
|
|
294
|
+
export type GraphQLVariablesV6<
|
|
295
|
+
FALLBACK_TYPES = unknown,
|
|
296
|
+
TYPED_GQL_STRING extends string = string,
|
|
297
|
+
> =
|
|
298
|
+
TYPED_GQL_STRING extends GeneratedQuery<infer IN, any>
|
|
299
|
+
? IN
|
|
300
|
+
: TYPED_GQL_STRING extends GeneratedMutation<infer IN, any>
|
|
301
|
+
? IN
|
|
302
|
+
: TYPED_GQL_STRING extends GeneratedSubscription<infer IN, any>
|
|
303
|
+
? IN
|
|
304
|
+
: FALLBACK_TYPES extends GraphQLOperationType<infer IN, any>
|
|
305
|
+
? IN
|
|
306
|
+
: any;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The expected return type with respect to the given `FALLBACK_TYPE`
|
|
310
|
+
* and `TYPED_GQL_STRING`.
|
|
311
|
+
*/
|
|
312
|
+
export type GraphQLResponseV6<
|
|
313
|
+
FALLBACK_TYPE = unknown,
|
|
314
|
+
TYPED_GQL_STRING extends string = string,
|
|
315
|
+
> =
|
|
316
|
+
TYPED_GQL_STRING extends GeneratedQuery<infer _, infer QUERY_OUT>
|
|
317
|
+
? Promise<GraphQLResult<FixedQueryResult<QUERY_OUT>>>
|
|
318
|
+
: TYPED_GQL_STRING extends GeneratedMutation<infer _, infer MUTATION_OUT>
|
|
319
|
+
? Promise<GraphQLResult<NeverEmpty<MUTATION_OUT>>>
|
|
320
|
+
: TYPED_GQL_STRING extends GeneratedSubscription<infer _, infer SUB_OUT>
|
|
321
|
+
? GraphqlSubscriptionResult<NeverEmpty<SUB_OUT>>
|
|
322
|
+
: FALLBACK_TYPE extends GraphQLQuery<infer _>
|
|
323
|
+
? Promise<GraphQLResult<FALLBACK_TYPE>>
|
|
324
|
+
: FALLBACK_TYPE extends GraphQLSubscription<infer _>
|
|
325
|
+
? GraphqlSubscriptionResult<FALLBACK_TYPE>
|
|
326
|
+
: FALLBACK_TYPE extends GraphQLOperationType<
|
|
327
|
+
infer _,
|
|
328
|
+
infer CUSTOM_OUT
|
|
329
|
+
>
|
|
330
|
+
? CUSTOM_OUT
|
|
331
|
+
: UnknownGraphQLResponse;
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* The shape customers can use to provide `T` to `graphql<T>()` to specify both
|
|
335
|
+
* `IN` and `OUT` types (the type of `variables` and the return type, respectively).
|
|
336
|
+
*
|
|
337
|
+
* I.E.,
|
|
338
|
+
*
|
|
339
|
+
* ```ts
|
|
340
|
+
* type MyVariablesType = { ... };
|
|
341
|
+
* type MyResultType = { ... };
|
|
342
|
+
* type MyOperationType = { variables: MyVariablesType, result: MyResultType };
|
|
343
|
+
*
|
|
344
|
+
* const result: MyResultType = graphql<MyOperationType>("graphql string", {
|
|
345
|
+
* variables: {
|
|
346
|
+
* // MyVariablesType
|
|
347
|
+
* }
|
|
348
|
+
* })
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
export interface GraphQLOperationType<
|
|
352
|
+
IN extends Record<string, DocumentType>,
|
|
353
|
+
OUT extends Record<string, DocumentType>,
|
|
354
|
+
> {
|
|
355
|
+
variables: IN;
|
|
356
|
+
result: OUT;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Nominal type for branding generated graphql query operation strings with
|
|
361
|
+
* input and output types.
|
|
362
|
+
*
|
|
363
|
+
* E.g.,
|
|
364
|
+
*
|
|
365
|
+
* ```ts
|
|
366
|
+
* export const getWidget = `...` as GeneratedQuery<
|
|
367
|
+
* GetWidgetQueryVariables,
|
|
368
|
+
* GetWidgetQuery
|
|
369
|
+
* >;
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
372
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
373
|
+
* assign types to the `variables` and result objects.
|
|
374
|
+
*/
|
|
375
|
+
export type GeneratedQuery<InputType, OutputType> = string & {
|
|
376
|
+
__generatedQueryInput: InputType;
|
|
377
|
+
__generatedQueryOutput: OutputType;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Nominal type for branding generated graphql mutation operation strings with
|
|
382
|
+
* input and output types.
|
|
383
|
+
*
|
|
384
|
+
* E.g.,
|
|
385
|
+
*
|
|
386
|
+
* ```ts
|
|
387
|
+
* export const createWidget = `...` as GeneratedQuery<
|
|
388
|
+
* CreateWidgetMutationVariables,
|
|
389
|
+
* CreateWidgetMutation
|
|
390
|
+
* >;
|
|
391
|
+
* ```
|
|
392
|
+
*
|
|
393
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
394
|
+
* assign types to the `variables` and result objects.
|
|
395
|
+
*/
|
|
396
|
+
export type GeneratedMutation<InputType, OutputType> = string & {
|
|
397
|
+
__generatedMutationInput: InputType;
|
|
398
|
+
__generatedMutationOutput: OutputType;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Nominal type for branding generated graphql mutation operation strings with
|
|
403
|
+
* input and output types.
|
|
404
|
+
*
|
|
405
|
+
* E.g.,
|
|
406
|
+
*
|
|
407
|
+
* ```ts
|
|
408
|
+
* export const createWidget = `...` as GeneratedMutation<
|
|
409
|
+
* CreateWidgetMutationVariables,
|
|
410
|
+
* CreateWidgetMutation
|
|
411
|
+
* >;
|
|
412
|
+
* ```
|
|
413
|
+
*
|
|
414
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
415
|
+
* assign types to the `variables` and result objects.
|
|
416
|
+
*/
|
|
417
|
+
export type GeneratedSubscription<InputType, OutputType> = string & {
|
|
418
|
+
__generatedSubscriptionInput: InputType;
|
|
419
|
+
__generatedSubscriptionOutput: OutputType;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
export const __amplify = Symbol('amplify');
|
|
423
|
+
export const __authMode = Symbol('authMode');
|
|
424
|
+
export const __authToken = Symbol('authToken');
|
|
425
|
+
export const __apiKey = Symbol('apiKey');
|
|
426
|
+
export const __headers = Symbol('headers');
|
|
427
|
+
export const __endpoint = Symbol('endpoint');
|
|
428
|
+
|
|
429
|
+
export function getInternals(client: BaseClient): ClientInternals & {
|
|
430
|
+
apiKey?: string;
|
|
431
|
+
endpoint?: string;
|
|
432
|
+
} {
|
|
433
|
+
const c = client as any;
|
|
434
|
+
|
|
435
|
+
return {
|
|
436
|
+
amplify: c[__amplify],
|
|
437
|
+
apiKey: c[__apiKey],
|
|
438
|
+
authMode: c[__authMode],
|
|
439
|
+
authToken: c[__authToken],
|
|
440
|
+
endpoint: c[__endpoint],
|
|
441
|
+
headers: c[__headers],
|
|
442
|
+
} as any;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
export type ClientWithModels =
|
|
446
|
+
| V6Client
|
|
447
|
+
| V6ClientSSRRequest
|
|
448
|
+
| V6ClientSSRCookies;
|
|
449
|
+
|
|
450
|
+
export type V6Client<
|
|
451
|
+
T extends Record<any, any> = never,
|
|
452
|
+
Options extends CommonPublicClientOptions = object,
|
|
453
|
+
> = {
|
|
454
|
+
graphql: GraphQLMethod<Options>;
|
|
455
|
+
cancel(promise: Promise<any>, message?: string): boolean;
|
|
456
|
+
isCancelError(error: any): boolean;
|
|
457
|
+
} & ClientExtensions<T>;
|
|
458
|
+
|
|
459
|
+
export type V6ClientSSRRequest<
|
|
460
|
+
T extends Record<any, any> = never,
|
|
461
|
+
Options extends CommonPublicClientOptions = object,
|
|
462
|
+
> = {
|
|
463
|
+
graphql: GraphQLMethodSSR<Options>;
|
|
464
|
+
cancel(promise: Promise<any>, message?: string): boolean;
|
|
465
|
+
isCancelError(error: any): boolean;
|
|
466
|
+
} & ClientExtensionsSSRRequest<T>;
|
|
467
|
+
|
|
468
|
+
export type V6ClientSSRCookies<
|
|
469
|
+
T extends Record<any, any> = never,
|
|
470
|
+
Options extends CommonPublicClientOptions = object,
|
|
471
|
+
> = {
|
|
472
|
+
graphql: GraphQLMethod<Options>;
|
|
473
|
+
cancel(promise: Promise<any>, message?: string): boolean;
|
|
474
|
+
isCancelError(error: any): boolean;
|
|
475
|
+
} & ClientExtensionsSSRCookies<T>;
|
|
476
|
+
|
|
477
|
+
export type GraphQLMethod<Options extends CommonPublicClientOptions> = <
|
|
478
|
+
FALLBACK_TYPES = unknown,
|
|
479
|
+
TYPED_GQL_STRING extends string = string,
|
|
480
|
+
>(
|
|
481
|
+
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING, Options>,
|
|
482
|
+
additionalHeaders?: CustomHeaders | undefined,
|
|
483
|
+
) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
484
|
+
|
|
485
|
+
export type GraphQLMethodSSR<Options extends CommonPublicClientOptions> = <
|
|
486
|
+
FALLBACK_TYPES = unknown,
|
|
487
|
+
TYPED_GQL_STRING extends string = string,
|
|
488
|
+
>(
|
|
489
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
490
|
+
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING, Options>,
|
|
491
|
+
additionalHeaders?: CustomHeaders | undefined,
|
|
492
|
+
) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* @private
|
|
496
|
+
*
|
|
497
|
+
* The knobs available for configuring `server/generateClient` internally.
|
|
498
|
+
*/
|
|
499
|
+
export interface ServerClientGenerationParams {
|
|
500
|
+
amplify:
|
|
501
|
+
| null // null expected when used with `generateServerClient`
|
|
502
|
+
// closure expected with `generateServerClientUsingCookies`
|
|
503
|
+
| ((fn: (amplify: AmplifyClassV6) => Promise<any>) => Promise<any>);
|
|
504
|
+
// global env-sourced config use for retrieving modelIntro
|
|
505
|
+
config: ResourcesConfig;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
export type QueryArgs = Record<string, unknown>;
|
|
509
|
+
|
|
510
|
+
export interface ListArgs extends Record<string, unknown> {
|
|
511
|
+
selectionSet?: string[];
|
|
512
|
+
filter?: Record<string, unknown>;
|
|
513
|
+
sortDirection?: ModelSortDirection;
|
|
514
|
+
headers?: CustomHeaders;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
export interface AuthModeParams extends Record<string, unknown> {
|
|
518
|
+
authMode?: GraphQLAuthMode;
|
|
519
|
+
authToken?: string;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export type GenerateServerClientParams = {
|
|
523
|
+
config: ResourcesConfig;
|
|
524
|
+
} & CommonPublicClientOptions;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { Observable, Observer, SubscriptionLike, filter, map } from 'rxjs';
|
|
5
|
+
|
|
6
|
+
import { ConnectionState } from '../types/PubSub';
|
|
7
|
+
|
|
8
|
+
import { ReachabilityMonitor } from './ReachabilityMonitor';
|
|
9
|
+
|
|
10
|
+
// Internal types for tracking different connection states
|
|
11
|
+
type LinkedConnectionState = 'connected' | 'disconnected';
|
|
12
|
+
type LinkedHealthState = 'healthy' | 'unhealthy';
|
|
13
|
+
interface LinkedConnectionStates {
|
|
14
|
+
networkState: LinkedConnectionState;
|
|
15
|
+
connectionState: LinkedConnectionState | 'connecting';
|
|
16
|
+
intendedConnectionState: LinkedConnectionState;
|
|
17
|
+
keepAliveState: LinkedHealthState;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const CONNECTION_CHANGE = {
|
|
21
|
+
KEEP_ALIVE_MISSED: { keepAliveState: 'unhealthy' },
|
|
22
|
+
KEEP_ALIVE: { keepAliveState: 'healthy' },
|
|
23
|
+
CONNECTION_ESTABLISHED: { connectionState: 'connected' },
|
|
24
|
+
CONNECTION_FAILED: {
|
|
25
|
+
intendedConnectionState: 'disconnected',
|
|
26
|
+
connectionState: 'disconnected',
|
|
27
|
+
},
|
|
28
|
+
CLOSING_CONNECTION: { intendedConnectionState: 'disconnected' },
|
|
29
|
+
OPENING_CONNECTION: {
|
|
30
|
+
intendedConnectionState: 'connected',
|
|
31
|
+
connectionState: 'connecting',
|
|
32
|
+
},
|
|
33
|
+
CLOSED: { connectionState: 'disconnected' },
|
|
34
|
+
ONLINE: { networkState: 'connected' },
|
|
35
|
+
OFFLINE: { networkState: 'disconnected' },
|
|
36
|
+
} as const;
|
|
37
|
+
|
|
38
|
+
export class ConnectionStateMonitor {
|
|
39
|
+
/**
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
private _linkedConnectionState: LinkedConnectionStates;
|
|
43
|
+
private _linkedConnectionStateObservable: Observable<LinkedConnectionStates>;
|
|
44
|
+
private _linkedConnectionStateObserver:
|
|
45
|
+
| Observer<LinkedConnectionStates>
|
|
46
|
+
| undefined;
|
|
47
|
+
|
|
48
|
+
private _networkMonitoringSubscription?: SubscriptionLike;
|
|
49
|
+
private _initialNetworkStateSubscription?: SubscriptionLike;
|
|
50
|
+
|
|
51
|
+
constructor() {
|
|
52
|
+
this._networkMonitoringSubscription = undefined;
|
|
53
|
+
this._linkedConnectionState = {
|
|
54
|
+
networkState: 'connected',
|
|
55
|
+
connectionState: 'disconnected',
|
|
56
|
+
intendedConnectionState: 'disconnected',
|
|
57
|
+
keepAliveState: 'healthy',
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Attempt to update the state with the current actual network state
|
|
61
|
+
this._initialNetworkStateSubscription = ReachabilityMonitor().subscribe(
|
|
62
|
+
({ online }) => {
|
|
63
|
+
this.record(
|
|
64
|
+
online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE,
|
|
65
|
+
);
|
|
66
|
+
this._initialNetworkStateSubscription?.unsubscribe();
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
this._linkedConnectionStateObservable =
|
|
71
|
+
new Observable<LinkedConnectionStates>(connectionStateObserver => {
|
|
72
|
+
connectionStateObserver.next(this._linkedConnectionState);
|
|
73
|
+
this._linkedConnectionStateObserver = connectionStateObserver;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Turn network state monitoring on if it isn't on already
|
|
79
|
+
*/
|
|
80
|
+
private enableNetworkMonitoring() {
|
|
81
|
+
// If no initial network state was discovered, stop trying
|
|
82
|
+
this._initialNetworkStateSubscription?.unsubscribe();
|
|
83
|
+
|
|
84
|
+
// Maintain the network state based on the reachability monitor
|
|
85
|
+
if (this._networkMonitoringSubscription === undefined) {
|
|
86
|
+
this._networkMonitoringSubscription = ReachabilityMonitor().subscribe(
|
|
87
|
+
({ online }) => {
|
|
88
|
+
this.record(
|
|
89
|
+
online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE,
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Turn network state monitoring off if it isn't off already
|
|
98
|
+
*/
|
|
99
|
+
private disableNetworkMonitoring() {
|
|
100
|
+
this._networkMonitoringSubscription?.unsubscribe();
|
|
101
|
+
this._networkMonitoringSubscription = undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Get the observable that allows us to monitor the connection state
|
|
106
|
+
*
|
|
107
|
+
* @returns {Observable<ConnectionState>} - The observable that emits ConnectionState updates
|
|
108
|
+
*/
|
|
109
|
+
public get connectionStateObservable(): Observable<ConnectionState> {
|
|
110
|
+
let previous: ConnectionState;
|
|
111
|
+
|
|
112
|
+
// The linked state aggregates state changes to any of the network, connection,
|
|
113
|
+
// intendedConnection and keepAliveHealth. Some states will change these independent
|
|
114
|
+
// states without changing the overall connection state.
|
|
115
|
+
|
|
116
|
+
// After translating from linked states to ConnectionState, then remove any duplicates
|
|
117
|
+
return this._linkedConnectionStateObservable
|
|
118
|
+
.pipe(
|
|
119
|
+
map(value => {
|
|
120
|
+
return this.connectionStatesTranslator(value);
|
|
121
|
+
}),
|
|
122
|
+
)
|
|
123
|
+
.pipe(
|
|
124
|
+
filter(current => {
|
|
125
|
+
const toInclude = current !== previous;
|
|
126
|
+
previous = current;
|
|
127
|
+
|
|
128
|
+
return toInclude;
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/*
|
|
134
|
+
* Updates local connection state and emits the full state to the observer.
|
|
135
|
+
*/
|
|
136
|
+
record(statusUpdates: Partial<LinkedConnectionStates>) {
|
|
137
|
+
// Maintain the network monitor
|
|
138
|
+
if (statusUpdates.intendedConnectionState === 'connected') {
|
|
139
|
+
this.enableNetworkMonitoring();
|
|
140
|
+
} else if (statusUpdates.intendedConnectionState === 'disconnected') {
|
|
141
|
+
this.disableNetworkMonitoring();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Maintain the socket state
|
|
145
|
+
const newSocketStatus = {
|
|
146
|
+
...this._linkedConnectionState,
|
|
147
|
+
...statusUpdates,
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
this._linkedConnectionState = { ...newSocketStatus };
|
|
151
|
+
|
|
152
|
+
this._linkedConnectionStateObserver?.next(this._linkedConnectionState);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/*
|
|
156
|
+
* Translate the ConnectionState structure into a specific ConnectionState string literal union
|
|
157
|
+
*/
|
|
158
|
+
private connectionStatesTranslator({
|
|
159
|
+
connectionState,
|
|
160
|
+
networkState,
|
|
161
|
+
intendedConnectionState,
|
|
162
|
+
keepAliveState,
|
|
163
|
+
}: LinkedConnectionStates): ConnectionState {
|
|
164
|
+
if (connectionState === 'connected' && networkState === 'disconnected')
|
|
165
|
+
return ConnectionState.ConnectedPendingNetwork;
|
|
166
|
+
|
|
167
|
+
if (
|
|
168
|
+
connectionState === 'connected' &&
|
|
169
|
+
intendedConnectionState === 'disconnected'
|
|
170
|
+
)
|
|
171
|
+
return ConnectionState.ConnectedPendingDisconnect;
|
|
172
|
+
|
|
173
|
+
if (
|
|
174
|
+
connectionState === 'disconnected' &&
|
|
175
|
+
intendedConnectionState === 'connected' &&
|
|
176
|
+
networkState === 'disconnected'
|
|
177
|
+
)
|
|
178
|
+
return ConnectionState.ConnectionDisruptedPendingNetwork;
|
|
179
|
+
|
|
180
|
+
if (
|
|
181
|
+
connectionState === 'disconnected' &&
|
|
182
|
+
intendedConnectionState === 'connected'
|
|
183
|
+
)
|
|
184
|
+
return ConnectionState.ConnectionDisrupted;
|
|
185
|
+
|
|
186
|
+
if (connectionState === 'connected' && keepAliveState === 'unhealthy')
|
|
187
|
+
return ConnectionState.ConnectedPendingKeepAlive;
|
|
188
|
+
|
|
189
|
+
// All remaining states directly correspond to the connection state
|
|
190
|
+
if (connectionState === 'connecting') return ConnectionState.Connecting;
|
|
191
|
+
if (connectionState === 'disconnected') return ConnectionState.Disconnected;
|
|
192
|
+
|
|
193
|
+
return ConnectionState.Connected;
|
|
194
|
+
}
|
|
195
|
+
}
|