@aws-amplify/api-graphql 4.3.3 → 4.3.4-events.81620c6.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.
- package/dist/cjs/GraphQLAPI.js +15 -1
- package/dist/cjs/GraphQLAPI.js.map +1 -1
- package/dist/cjs/Providers/AWSAppSyncEventsProvider/index.js +88 -0
- package/dist/cjs/Providers/AWSAppSyncEventsProvider/index.js.map +1 -0
- package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js +32 -735
- package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
- package/dist/cjs/Providers/AWSWebSocketProvider/appsyncUrl.js +112 -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 +626 -0
- package/dist/cjs/Providers/AWSWebSocketProvider/index.js.map +1 -0
- package/dist/cjs/Providers/constants.js +22 -2
- package/dist/cjs/Providers/constants.js.map +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/internals/InternalGraphQLAPI.js +8 -67
- package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -1
- package/dist/cjs/internals/events/appsyncRequest.js +97 -0
- package/dist/cjs/internals/events/appsyncRequest.js.map +1 -0
- package/dist/cjs/internals/events/index.js +109 -0
- package/dist/cjs/internals/events/index.js.map +1 -0
- package/dist/cjs/internals/graphqlAuth.js +64 -0
- package/dist/cjs/internals/graphqlAuth.js.map +1 -0
- package/dist/cjs/internals/graphqlRequest.js +20 -0
- package/dist/cjs/internals/graphqlRequest.js.map +1 -0
- package/dist/cjs/utils/ReconnectionMonitor.js +1 -1
- package/dist/cjs/utils/ReconnectionMonitor.js.map +1 -1
- package/dist/cjs/utils/resolveConfig.js +18 -1
- package/dist/cjs/utils/resolveConfig.js.map +1 -1
- package/dist/esm/GraphQLAPI.mjs +15 -1
- package/dist/esm/GraphQLAPI.mjs.map +1 -1
- package/dist/esm/Providers/AWSAppSyncEventsProvider/index.d.ts +37 -0
- package/dist/esm/Providers/AWSAppSyncEventsProvider/index.mjs +86 -0
- package/dist/esm/Providers/AWSAppSyncEventsProvider/index.mjs.map +1 -0
- package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +20 -71
- package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +32 -735
- package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -1
- package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.d.ts +21 -0
- package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.mjs +106 -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 +79 -0
- package/dist/esm/Providers/AWSWebSocketProvider/index.mjs +624 -0
- package/dist/esm/Providers/AWSWebSocketProvider/index.mjs.map +1 -0
- package/dist/esm/Providers/constants.d.ts +23 -3
- package/dist/esm/Providers/constants.mjs +22 -2
- package/dist/esm/Providers/constants.mjs.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/internals/InternalGraphQLAPI.d.ts +0 -1
- package/dist/esm/internals/InternalGraphQLAPI.mjs +9 -68
- package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -1
- 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 +31 -0
- package/dist/esm/internals/events/index.mjs +105 -0
- package/dist/esm/internals/events/index.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/utils/ConnectionStateMonitor.d.ts +29 -1
- package/dist/esm/utils/ReconnectionMonitor.d.ts +1 -1
- package/dist/esm/utils/ReconnectionMonitor.mjs +1 -1
- package/dist/esm/utils/ReconnectionMonitor.mjs.map +1 -1
- package/dist/esm/utils/resolveConfig.d.ts +9 -0
- package/dist/esm/utils/resolveConfig.mjs +17 -1
- package/dist/esm/utils/resolveConfig.mjs.map +1 -1
- package/package.json +5 -5
- package/src/GraphQLAPI.ts +34 -3
- package/src/Providers/AWSAppSyncEventsProvider/index.ts +174 -0
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +52 -1055
- package/src/Providers/AWSWebSocketProvider/appsyncUrl.ts +151 -0
- package/src/Providers/AWSWebSocketProvider/authHeaders.ts +146 -0
- package/src/Providers/AWSWebSocketProvider/index.ts +923 -0
- package/src/Providers/constants.ts +22 -2
- package/src/index.ts +1 -0
- package/src/internals/InternalGraphQLAPI.ts +15 -102
- package/src/internals/events/appsyncRequest.ts +173 -0
- package/src/internals/events/index.ts +209 -0
- package/src/internals/graphqlAuth.ts +87 -0
- package/src/internals/graphqlRequest.ts +30 -0
- package/src/utils/ConnectionStateMonitor.ts +2 -13
- package/src/utils/ReconnectionMonitor.ts +1 -1
- package/src/utils/resolveConfig.ts +23 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { DocumentType, GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
|
|
3
|
+
interface EventsOptions {
|
|
4
|
+
authMode?: GraphQLAuthMode;
|
|
5
|
+
authToken?: string;
|
|
6
|
+
}
|
|
7
|
+
interface SubscriptionObserver<T> {
|
|
8
|
+
next(value: T): void;
|
|
9
|
+
error(errorValue: any): void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param channelName
|
|
14
|
+
* @param options
|
|
15
|
+
*/
|
|
16
|
+
declare function connect(channelName: string, options?: EventsOptions): Promise<{
|
|
17
|
+
subscribe: (observer: SubscriptionObserver<any>, subOptions?: EventsOptions) => Observable<any>;
|
|
18
|
+
}>;
|
|
19
|
+
interface PublishedEvent {
|
|
20
|
+
identifier: string;
|
|
21
|
+
index: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @param channelName - publish destination
|
|
26
|
+
* @param event - JSON-serializable value or an array of values
|
|
27
|
+
* @param options
|
|
28
|
+
*/
|
|
29
|
+
declare function post(channelName: string, event: DocumentType | DocumentType[], options?: EventsOptions): Promise<void | PublishedEvent[]>;
|
|
30
|
+
declare function closeAll(): void;
|
|
31
|
+
export { connect, post, closeAll };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Amplify } from '@aws-amplify/core';
|
|
2
|
+
import { AWSAppSyncEventProvider } from '../../Providers/AWSAppSyncEventsProvider/index.mjs';
|
|
3
|
+
import { appsyncRequest } from './appsyncRequest.mjs';
|
|
4
|
+
|
|
5
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
6
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
7
|
+
const eventProvider = new AWSAppSyncEventProvider();
|
|
8
|
+
const normalizeAuth = (explicitAuthMode, defaultAuthMode) => {
|
|
9
|
+
if (!explicitAuthMode) {
|
|
10
|
+
return defaultAuthMode;
|
|
11
|
+
}
|
|
12
|
+
if (explicitAuthMode === 'identityPool') {
|
|
13
|
+
return 'iam';
|
|
14
|
+
}
|
|
15
|
+
return explicitAuthMode;
|
|
16
|
+
};
|
|
17
|
+
const configure = () => {
|
|
18
|
+
const config = Amplify.getConfig();
|
|
19
|
+
// TODO - get this correct
|
|
20
|
+
const eventsConfig = config.API?.GraphQL?.events ?? config.data?.events;
|
|
21
|
+
if (!eventsConfig) {
|
|
22
|
+
throw new Error('Amplify configuration is missing. Have you called Amplify.configure()');
|
|
23
|
+
}
|
|
24
|
+
const configAuthMode = normalizeAuth(eventsConfig.defaultAuthMode ?? eventsConfig.default_authorization_type, 'apiKey');
|
|
25
|
+
const options = {
|
|
26
|
+
appSyncGraphqlEndpoint: eventsConfig.url,
|
|
27
|
+
region: eventsConfig.region ?? eventsConfig.aws_region,
|
|
28
|
+
authenticationType: configAuthMode,
|
|
29
|
+
apiKey: eventsConfig.apiKey ?? eventsConfig.api_key,
|
|
30
|
+
};
|
|
31
|
+
return options;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param channelName
|
|
36
|
+
* @param options
|
|
37
|
+
*/
|
|
38
|
+
async function connect(channelName, options) {
|
|
39
|
+
const providerOptions = configure();
|
|
40
|
+
providerOptions.authenticationType = normalizeAuth(options?.authMode, providerOptions.authenticationType);
|
|
41
|
+
await eventProvider.connect(providerOptions);
|
|
42
|
+
const sub = (observer, subOptions) => {
|
|
43
|
+
const subscribeOptions = { ...providerOptions, query: channelName };
|
|
44
|
+
subscribeOptions.authenticationType = normalizeAuth(subOptions?.authMode, subscribeOptions.authenticationType);
|
|
45
|
+
const _sub = eventProvider.subscribe(subscribeOptions);
|
|
46
|
+
_sub.subscribe(observer);
|
|
47
|
+
return _sub;
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
// WS publish is not enabled in the service yet, will be a follow up feature
|
|
51
|
+
// publish: pub,
|
|
52
|
+
subscribe: sub,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Event API expects and array of JSON strings
|
|
57
|
+
*
|
|
58
|
+
* @param events - JSON-serializable value or an array of values
|
|
59
|
+
* @returns array of JSON strings
|
|
60
|
+
*/
|
|
61
|
+
const serializeEvents = (events) => {
|
|
62
|
+
if (Array.isArray(events)) {
|
|
63
|
+
return events.map((ev, idx) => {
|
|
64
|
+
const eventJson = JSON.stringify(ev);
|
|
65
|
+
if (eventJson === undefined) {
|
|
66
|
+
throw new Error(`Event must be a valid JSON value. Received ${ev} at index ${idx}`);
|
|
67
|
+
}
|
|
68
|
+
return eventJson;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const eventJson = JSON.stringify(events);
|
|
72
|
+
if (eventJson === undefined) {
|
|
73
|
+
throw new Error(`Event must be a valid JSON value. Received ${events}`);
|
|
74
|
+
}
|
|
75
|
+
return [eventJson];
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param channelName - publish destination
|
|
80
|
+
* @param event - JSON-serializable value or an array of values
|
|
81
|
+
* @param options
|
|
82
|
+
*/
|
|
83
|
+
async function post(channelName, event, options) {
|
|
84
|
+
const providerOptions = configure();
|
|
85
|
+
providerOptions.authenticationType = normalizeAuth(options?.authMode, providerOptions.authenticationType);
|
|
86
|
+
// trailing slash required in publish
|
|
87
|
+
const normalizedChannelName = channelName[0] === '/' ? channelName : `/${channelName}`;
|
|
88
|
+
const publishOptions = {
|
|
89
|
+
...providerOptions,
|
|
90
|
+
query: normalizedChannelName,
|
|
91
|
+
variables: serializeEvents(event),
|
|
92
|
+
authToken: options?.authToken,
|
|
93
|
+
};
|
|
94
|
+
const abortController = new AbortController();
|
|
95
|
+
const res = await appsyncRequest(Amplify, publishOptions, {}, abortController);
|
|
96
|
+
if (res.failed?.length > 0) {
|
|
97
|
+
return res.failed;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function closeAll() {
|
|
101
|
+
eventProvider.close();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { closeAll, connect, post };
|
|
105
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../../src/internals/events/index.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { Amplify } from '@aws-amplify/core';\nimport { AWSAppSyncEventProvider } from '../../Providers/AWSAppSyncEventsProvider';\nimport { appsyncRequest } from './appsyncRequest';\nconst eventProvider = new AWSAppSyncEventProvider();\nconst normalizeAuth = (explicitAuthMode, defaultAuthMode) => {\n if (!explicitAuthMode) {\n return defaultAuthMode;\n }\n if (explicitAuthMode === 'identityPool') {\n return 'iam';\n }\n return explicitAuthMode;\n};\nconst configure = () => {\n const config = Amplify.getConfig();\n // TODO - get this correct\n const eventsConfig = config.API?.GraphQL?.events ?? config.data?.events;\n if (!eventsConfig) {\n throw new Error('Amplify configuration is missing. Have you called Amplify.configure()');\n }\n const configAuthMode = normalizeAuth(eventsConfig.defaultAuthMode ?? eventsConfig.default_authorization_type, 'apiKey');\n const options = {\n appSyncGraphqlEndpoint: eventsConfig.url,\n region: eventsConfig.region ?? eventsConfig.aws_region,\n authenticationType: configAuthMode,\n apiKey: eventsConfig.apiKey ?? eventsConfig.api_key,\n };\n return options;\n};\n/**\n *\n * @param channelName\n * @param options\n */\nasync function connect(channelName, options) {\n const providerOptions = configure();\n providerOptions.authenticationType = normalizeAuth(options?.authMode, providerOptions.authenticationType);\n await eventProvider.connect(providerOptions);\n const sub = (observer, subOptions) => {\n const subscribeOptions = { ...providerOptions, query: channelName };\n subscribeOptions.authenticationType = normalizeAuth(subOptions?.authMode, subscribeOptions.authenticationType);\n const _sub = eventProvider.subscribe(subscribeOptions);\n _sub.subscribe(observer);\n return _sub;\n };\n // WS publish is not enabled in the service yet. It will be a follow up feature\n const _pub = async (event, pubOptions) => {\n const publishOptions = {\n ...providerOptions,\n query: channelName,\n variables: event,\n };\n publishOptions.authenticationType = normalizeAuth(pubOptions?.authMode, publishOptions.authenticationType);\n return eventProvider.publish(publishOptions);\n };\n return {\n // WS publish is not enabled in the service yet, will be a follow up feature\n // publish: pub,\n subscribe: sub,\n };\n}\n/**\n * Event API expects and array of JSON strings\n *\n * @param events - JSON-serializable value or an array of values\n * @returns array of JSON strings\n */\nconst serializeEvents = (events) => {\n if (Array.isArray(events)) {\n return events.map((ev, idx) => {\n const eventJson = JSON.stringify(ev);\n if (eventJson === undefined) {\n throw new Error(`Event must be a valid JSON value. Received ${ev} at index ${idx}`);\n }\n return eventJson;\n });\n }\n const eventJson = JSON.stringify(events);\n if (eventJson === undefined) {\n throw new Error(`Event must be a valid JSON value. Received ${events}`);\n }\n return [eventJson];\n};\n/**\n *\n * @param channelName - publish destination\n * @param event - JSON-serializable value or an array of values\n * @param options\n */\nasync function post(channelName, event, options) {\n const providerOptions = configure();\n providerOptions.authenticationType = normalizeAuth(options?.authMode, providerOptions.authenticationType);\n // trailing slash required in publish\n const normalizedChannelName = channelName[0] === '/' ? channelName : `/${channelName}`;\n const publishOptions = {\n ...providerOptions,\n query: normalizedChannelName,\n variables: serializeEvents(event),\n authToken: options?.authToken,\n };\n const abortController = new AbortController();\n const res = await appsyncRequest(Amplify, publishOptions, {}, abortController);\n if (res.failed?.length > 0) {\n return res.failed;\n }\n}\nfunction closeAll() {\n eventProvider.close();\n}\nexport { connect, post, closeAll };\n"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
|
|
2
|
+
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
3
|
+
export declare function headerBasedAuth(amplify: AmplifyClassV6, authMode: GraphQLAuthMode, apiKey: string | undefined, additionalHeaders?: Record<string, string>): Promise<{}>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { GraphQLApiError } from '../utils/errors/GraphQLApiError.mjs';
|
|
2
|
+
import '../utils/errors/validation.mjs';
|
|
3
|
+
import 'graphql';
|
|
4
|
+
import { NO_AUTH_TOKEN_HEADER, NO_SIGNED_IN_USER, NO_VALID_AUTH_TOKEN, NO_VALID_CREDENTIALS, NO_API_KEY } from '../utils/errors/constants.mjs';
|
|
5
|
+
|
|
6
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
7
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
8
|
+
async function headerBasedAuth(amplify, authMode, apiKey, additionalHeaders = {}) {
|
|
9
|
+
let headers = {};
|
|
10
|
+
switch (authMode) {
|
|
11
|
+
case 'apiKey':
|
|
12
|
+
if (!apiKey) {
|
|
13
|
+
throw new GraphQLApiError(NO_API_KEY);
|
|
14
|
+
}
|
|
15
|
+
headers = {
|
|
16
|
+
'X-Api-Key': apiKey,
|
|
17
|
+
};
|
|
18
|
+
break;
|
|
19
|
+
case 'iam': {
|
|
20
|
+
const session = await amplify.Auth.fetchAuthSession();
|
|
21
|
+
if (session.credentials === undefined) {
|
|
22
|
+
throw new GraphQLApiError(NO_VALID_CREDENTIALS);
|
|
23
|
+
}
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case 'oidc':
|
|
27
|
+
case 'userPool': {
|
|
28
|
+
let token;
|
|
29
|
+
try {
|
|
30
|
+
token = (await amplify.Auth.fetchAuthSession()).tokens?.accessToken.toString();
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
// fetchAuthSession failed
|
|
34
|
+
throw new GraphQLApiError({
|
|
35
|
+
...NO_SIGNED_IN_USER,
|
|
36
|
+
underlyingError: e,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
// `fetchAuthSession()` succeeded but didn't return `tokens`.
|
|
40
|
+
// This may happen when unauthenticated access is enabled and there is
|
|
41
|
+
// no user signed in.
|
|
42
|
+
if (!token) {
|
|
43
|
+
throw new GraphQLApiError(NO_VALID_AUTH_TOKEN);
|
|
44
|
+
}
|
|
45
|
+
headers = {
|
|
46
|
+
Authorization: token,
|
|
47
|
+
};
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case 'lambda':
|
|
51
|
+
if (typeof additionalHeaders === 'object' &&
|
|
52
|
+
!additionalHeaders.Authorization) {
|
|
53
|
+
throw new GraphQLApiError(NO_AUTH_TOKEN_HEADER);
|
|
54
|
+
}
|
|
55
|
+
headers = {
|
|
56
|
+
Authorization: additionalHeaders.Authorization,
|
|
57
|
+
};
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
return headers;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { headerBasedAuth };
|
|
64
|
+
//# sourceMappingURL=graphqlAuth.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphqlAuth.mjs","sources":["../../../src/internals/graphqlAuth.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { GraphQLApiError } from '../utils/errors';\nimport { NO_API_KEY, NO_AUTH_TOKEN_HEADER, NO_SIGNED_IN_USER, NO_VALID_AUTH_TOKEN, NO_VALID_CREDENTIALS, } from '../utils/errors/constants';\nexport async function headerBasedAuth(amplify, authMode, apiKey, additionalHeaders = {}) {\n let headers = {};\n switch (authMode) {\n case 'apiKey':\n if (!apiKey) {\n throw new GraphQLApiError(NO_API_KEY);\n }\n headers = {\n 'X-Api-Key': apiKey,\n };\n break;\n case 'iam': {\n const session = await amplify.Auth.fetchAuthSession();\n if (session.credentials === undefined) {\n throw new GraphQLApiError(NO_VALID_CREDENTIALS);\n }\n break;\n }\n case 'oidc':\n case 'userPool': {\n let token;\n try {\n token = (await amplify.Auth.fetchAuthSession()).tokens?.accessToken.toString();\n }\n catch (e) {\n // fetchAuthSession failed\n throw new GraphQLApiError({\n ...NO_SIGNED_IN_USER,\n underlyingError: e,\n });\n }\n // `fetchAuthSession()` succeeded but didn't return `tokens`.\n // This may happen when unauthenticated access is enabled and there is\n // no user signed in.\n if (!token) {\n throw new GraphQLApiError(NO_VALID_AUTH_TOKEN);\n }\n headers = {\n Authorization: token,\n };\n break;\n }\n case 'lambda':\n if (typeof additionalHeaders === 'object' &&\n !additionalHeaders.Authorization) {\n throw new GraphQLApiError(NO_AUTH_TOKEN_HEADER);\n }\n headers = {\n Authorization: additionalHeaders.Authorization,\n };\n break;\n case 'none':\n break;\n default:\n break;\n }\n return headers;\n}\n"],"names":[],"mappings":";;;;;AAAA;AACA;AAGO,eAAe,eAAe,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,iBAAiB,GAAG,EAAE,EAAE;AACzF,IAAI,IAAI,OAAO,GAAG,EAAE,CAAC;AACrB,IAAI,QAAQ,QAAQ;AACpB,QAAQ,KAAK,QAAQ;AACrB,YAAY,IAAI,CAAC,MAAM,EAAE;AACzB,gBAAgB,MAAM,IAAI,eAAe,CAAC,UAAU,CAAC,CAAC;AACtD,aAAa;AACb,YAAY,OAAO,GAAG;AACtB,gBAAgB,WAAW,EAAE,MAAM;AACnC,aAAa,CAAC;AACd,YAAY,MAAM;AAClB,QAAQ,KAAK,KAAK,EAAE;AACpB,YAAY,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC;AAClE,YAAY,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,EAAE;AACnD,gBAAgB,MAAM,IAAI,eAAe,CAAC,oBAAoB,CAAC,CAAC;AAChE,aAAa;AACb,YAAY,MAAM;AAClB,SAAS;AACT,QAAQ,KAAK,MAAM,CAAC;AACpB,QAAQ,KAAK,UAAU,EAAE;AACzB,YAAY,IAAI,KAAK,CAAC;AACtB,YAAY,IAAI;AAChB,gBAAgB,KAAK,GAAG,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAE,WAAW,CAAC,QAAQ,EAAE,CAAC;AAC/F,aAAa;AACb,YAAY,OAAO,CAAC,EAAE;AACtB;AACA,gBAAgB,MAAM,IAAI,eAAe,CAAC;AAC1C,oBAAoB,GAAG,iBAAiB;AACxC,oBAAoB,eAAe,EAAE,CAAC;AACtC,iBAAiB,CAAC,CAAC;AACnB,aAAa;AACb;AACA;AACA;AACA,YAAY,IAAI,CAAC,KAAK,EAAE;AACxB,gBAAgB,MAAM,IAAI,eAAe,CAAC,mBAAmB,CAAC,CAAC;AAC/D,aAAa;AACb,YAAY,OAAO,GAAG;AACtB,gBAAgB,aAAa,EAAE,KAAK;AACpC,aAAa,CAAC;AACd,YAAY,MAAM;AAClB,SAAS;AACT,QAAQ,KAAK,QAAQ;AACrB,YAAY,IAAI,OAAO,iBAAiB,KAAK,QAAQ;AACrD,gBAAgB,CAAC,iBAAiB,CAAC,aAAa,EAAE;AAClD,gBAAgB,MAAM,IAAI,eAAe,CAAC,oBAAoB,CAAC,CAAC;AAChE,aAAa;AACb,YAAY,OAAO,GAAG;AACtB,gBAAgB,aAAa,EAAE,iBAAiB,CAAC,aAAa;AAC9D,aAAa,CAAC;AACd,YAAY,MAAM;AAKlB,KAAK;AACL,IAAI,OAAO,OAAO,CAAC;AACnB;;;;"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
2
|
+
import { post } from '@aws-amplify/api-rest/internals';
|
|
3
|
+
export declare function graphqlRequest(amplify: AmplifyClassV6, url: string, options: any, abortController: AbortController, _post?: typeof post): Promise<import("@aws-amplify/core/internals/utils").DocumentType>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AmplifyUrl } from '@aws-amplify/core/internals/utils';
|
|
2
|
+
import { post } from '@aws-amplify/api-rest/internals';
|
|
3
|
+
|
|
4
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
async function graphqlRequest(amplify, url, options, abortController, _post) {
|
|
7
|
+
const p = _post ?? post;
|
|
8
|
+
const { body: responseBody } = await p(amplify, {
|
|
9
|
+
url: new AmplifyUrl(url),
|
|
10
|
+
options,
|
|
11
|
+
abortController,
|
|
12
|
+
});
|
|
13
|
+
const response = await responseBody.json();
|
|
14
|
+
return response;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { graphqlRequest };
|
|
18
|
+
//# sourceMappingURL=graphqlRequest.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphqlRequest.mjs","sources":["../../../src/internals/graphqlRequest.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { AmplifyUrl } from '@aws-amplify/core/internals/utils';\nimport { \n// cancel as cancelREST,\npost,\n// updateRequestToBeCancellable,\n } from '@aws-amplify/api-rest/internals';\nexport async function graphqlRequest(amplify, url, options, abortController, _post) {\n const p = _post ?? post;\n const { body: responseBody } = await p(amplify, {\n url: new AmplifyUrl(url),\n options,\n abortController,\n });\n const response = await responseBody.json();\n return response;\n}\n"],"names":[],"mappings":";;;AAAA;AACA;AAOO,eAAe,cAAc,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE;AACpF,IAAI,MAAM,CAAC,GAAG,KAAK,IAAI,IAAI,CAAC;AAC5B,IAAI,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC,CAAC,OAAO,EAAE;AACpD,QAAQ,GAAG,EAAE,IAAI,UAAU,CAAC,GAAG,CAAC;AAChC,QAAQ,OAAO;AACf,QAAQ,eAAe;AACvB,KAAK,CAAC,CAAC;AACP,IAAI,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC,IAAI,EAAE,CAAC;AAC/C,IAAI,OAAO,QAAQ,CAAC;AACpB;;;;"}
|
|
@@ -9,7 +9,35 @@ interface LinkedConnectionStates {
|
|
|
9
9
|
keepAliveState: LinkedHealthState;
|
|
10
10
|
}
|
|
11
11
|
export declare const CONNECTION_CHANGE: {
|
|
12
|
-
|
|
12
|
+
readonly KEEP_ALIVE_MISSED: {
|
|
13
|
+
readonly keepAliveState: "unhealthy";
|
|
14
|
+
};
|
|
15
|
+
readonly KEEP_ALIVE: {
|
|
16
|
+
readonly keepAliveState: "healthy";
|
|
17
|
+
};
|
|
18
|
+
readonly CONNECTION_ESTABLISHED: {
|
|
19
|
+
readonly connectionState: "connected";
|
|
20
|
+
};
|
|
21
|
+
readonly CONNECTION_FAILED: {
|
|
22
|
+
readonly intendedConnectionState: "disconnected";
|
|
23
|
+
readonly connectionState: "disconnected";
|
|
24
|
+
};
|
|
25
|
+
readonly CLOSING_CONNECTION: {
|
|
26
|
+
readonly intendedConnectionState: "disconnected";
|
|
27
|
+
};
|
|
28
|
+
readonly OPENING_CONNECTION: {
|
|
29
|
+
readonly intendedConnectionState: "connected";
|
|
30
|
+
readonly connectionState: "connecting";
|
|
31
|
+
};
|
|
32
|
+
readonly CLOSED: {
|
|
33
|
+
readonly connectionState: "disconnected";
|
|
34
|
+
};
|
|
35
|
+
readonly ONLINE: {
|
|
36
|
+
readonly networkState: "connected";
|
|
37
|
+
};
|
|
38
|
+
readonly OFFLINE: {
|
|
39
|
+
readonly networkState: "disconnected";
|
|
40
|
+
};
|
|
13
41
|
};
|
|
14
42
|
export declare class ConnectionStateMonitor {
|
|
15
43
|
/**
|
|
@@ -5,7 +5,7 @@ export declare enum ReconnectEvent {
|
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
7
|
* Captures the reconnect event logic used to determine when to reconnect to PubSub providers.
|
|
8
|
-
*
|
|
8
|
+
* Reconnect attempts are delayed by 5 seconds to let the interface settle.
|
|
9
9
|
* Attempting to reconnect only once creates unrecoverable states when the network state isn't
|
|
10
10
|
* supported by the browser, so this keeps retrying every minute until halted.
|
|
11
11
|
*/
|
|
@@ -7,7 +7,7 @@ var ReconnectEvent;
|
|
|
7
7
|
})(ReconnectEvent || (ReconnectEvent = {}));
|
|
8
8
|
/**
|
|
9
9
|
* Captures the reconnect event logic used to determine when to reconnect to PubSub providers.
|
|
10
|
-
*
|
|
10
|
+
* Reconnect attempts are delayed by 5 seconds to let the interface settle.
|
|
11
11
|
* Attempting to reconnect only once creates unrecoverable states when the network state isn't
|
|
12
12
|
* supported by the browser, so this keeps retrying every minute until halted.
|
|
13
13
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ReconnectionMonitor.mjs","sources":["../../../src/utils/ReconnectionMonitor.ts"],"sourcesContent":["import { RECONNECT_DELAY, RECONNECT_INTERVAL } from '../Providers/constants';\nexport var ReconnectEvent;\n(function (ReconnectEvent) {\n ReconnectEvent[\"START_RECONNECT\"] = \"START_RECONNECT\";\n ReconnectEvent[\"HALT_RECONNECT\"] = \"HALT_RECONNECT\";\n})(ReconnectEvent || (ReconnectEvent = {}));\n/**\n * Captures the reconnect event logic used to determine when to reconnect to PubSub providers.\n *
|
|
1
|
+
{"version":3,"file":"ReconnectionMonitor.mjs","sources":["../../../src/utils/ReconnectionMonitor.ts"],"sourcesContent":["import { RECONNECT_DELAY, RECONNECT_INTERVAL } from '../Providers/constants';\nexport var ReconnectEvent;\n(function (ReconnectEvent) {\n ReconnectEvent[\"START_RECONNECT\"] = \"START_RECONNECT\";\n ReconnectEvent[\"HALT_RECONNECT\"] = \"HALT_RECONNECT\";\n})(ReconnectEvent || (ReconnectEvent = {}));\n/**\n * Captures the reconnect event logic used to determine when to reconnect to PubSub providers.\n * Reconnect attempts are delayed by 5 seconds to let the interface settle.\n * Attempting to reconnect only once creates unrecoverable states when the network state isn't\n * supported by the browser, so this keeps retrying every minute until halted.\n */\nexport class ReconnectionMonitor {\n constructor() {\n this.reconnectObservers = [];\n }\n /**\n * Add reconnect observer to the list of observers to alert on reconnect\n */\n addObserver(reconnectObserver) {\n this.reconnectObservers.push(reconnectObserver);\n }\n /**\n * Given a reconnect event, start the appropriate behavior\n */\n record(event) {\n if (event === ReconnectEvent.START_RECONNECT) {\n // If the reconnection hasn't been started\n if (this.reconnectSetTimeoutId === undefined &&\n this.reconnectIntervalId === undefined) {\n this.reconnectSetTimeoutId = setTimeout(() => {\n // Reconnect now\n this._triggerReconnect();\n // Retry reconnect every periodically until it works\n this.reconnectIntervalId = setInterval(() => {\n this._triggerReconnect();\n }, RECONNECT_INTERVAL);\n }, RECONNECT_DELAY);\n }\n }\n if (event === ReconnectEvent.HALT_RECONNECT) {\n if (this.reconnectIntervalId) {\n clearInterval(this.reconnectIntervalId);\n this.reconnectIntervalId = undefined;\n }\n if (this.reconnectSetTimeoutId) {\n clearTimeout(this.reconnectSetTimeoutId);\n this.reconnectSetTimeoutId = undefined;\n }\n }\n }\n /**\n * Complete all reconnect observers\n */\n close() {\n this.reconnectObservers.forEach(reconnectObserver => {\n reconnectObserver.complete?.();\n });\n }\n _triggerReconnect() {\n this.reconnectObservers.forEach(reconnectObserver => {\n reconnectObserver.next?.();\n });\n }\n}\n"],"names":[],"mappings":";;AACU,IAAC,eAAe;AAC1B,CAAC,UAAU,cAAc,EAAE;AAC3B,IAAI,cAAc,CAAC,iBAAiB,CAAC,GAAG,iBAAiB,CAAC;AAC1D,IAAI,cAAc,CAAC,gBAAgB,CAAC,GAAG,gBAAgB,CAAC;AACxD,CAAC,EAAE,cAAc,KAAK,cAAc,GAAG,EAAE,CAAC,CAAC,CAAC;AAC5C;AACA;AACA;AACA;AACA;AACA;AACO,MAAM,mBAAmB,CAAC;AACjC,IAAI,WAAW,GAAG;AAClB,QAAQ,IAAI,CAAC,kBAAkB,GAAG,EAAE,CAAC;AACrC,KAAK;AACL;AACA;AACA;AACA,IAAI,WAAW,CAAC,iBAAiB,EAAE;AACnC,QAAQ,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;AACxD,KAAK;AACL;AACA;AACA;AACA,IAAI,MAAM,CAAC,KAAK,EAAE;AAClB,QAAQ,IAAI,KAAK,KAAK,cAAc,CAAC,eAAe,EAAE;AACtD;AACA,YAAY,IAAI,IAAI,CAAC,qBAAqB,KAAK,SAAS;AACxD,gBAAgB,IAAI,CAAC,mBAAmB,KAAK,SAAS,EAAE;AACxD,gBAAgB,IAAI,CAAC,qBAAqB,GAAG,UAAU,CAAC,MAAM;AAC9D;AACA,oBAAoB,IAAI,CAAC,iBAAiB,EAAE,CAAC;AAC7C;AACA,oBAAoB,IAAI,CAAC,mBAAmB,GAAG,WAAW,CAAC,MAAM;AACjE,wBAAwB,IAAI,CAAC,iBAAiB,EAAE,CAAC;AACjD,qBAAqB,EAAE,kBAAkB,CAAC,CAAC;AAC3C,iBAAiB,EAAE,eAAe,CAAC,CAAC;AACpC,aAAa;AACb,SAAS;AACT,QAAQ,IAAI,KAAK,KAAK,cAAc,CAAC,cAAc,EAAE;AACrD,YAAY,IAAI,IAAI,CAAC,mBAAmB,EAAE;AAC1C,gBAAgB,aAAa,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;AACxD,gBAAgB,IAAI,CAAC,mBAAmB,GAAG,SAAS,CAAC;AACrD,aAAa;AACb,YAAY,IAAI,IAAI,CAAC,qBAAqB,EAAE;AAC5C,gBAAgB,YAAY,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;AACzD,gBAAgB,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;AACvD,aAAa;AACb,SAAS;AACT,KAAK;AACL;AACA;AACA;AACA,IAAI,KAAK,GAAG;AACZ,QAAQ,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,iBAAiB,IAAI;AAC7D,YAAY,iBAAiB,CAAC,QAAQ,IAAI,CAAC;AAC3C,SAAS,CAAC,CAAC;AACX,KAAK;AACL,IAAI,iBAAiB,GAAG;AACxB,QAAQ,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,iBAAiB,IAAI;AAC7D,YAAY,iBAAiB,CAAC,IAAI,IAAI,CAAC;AACvC,SAAS,CAAC,CAAC;AACX,KAAK;AACL;;;;"}
|
|
@@ -10,3 +10,12 @@ export declare const resolveConfig: (amplify: AmplifyClassV6) => {
|
|
|
10
10
|
endpoint: string | undefined;
|
|
11
11
|
region: string | undefined;
|
|
12
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare const resolveEventsConfig: (amplify: AmplifyClassV6) => {
|
|
17
|
+
apiKey: string | undefined;
|
|
18
|
+
defaultAuthMode: import("@aws-amplify/core/internals/utils").GraphQLAuthMode | undefined;
|
|
19
|
+
appSyncGraphqlEndpoint: string | undefined;
|
|
20
|
+
region: string | undefined;
|
|
21
|
+
};
|
|
@@ -28,6 +28,22 @@ const resolveConfig = (amplify) => {
|
|
|
28
28
|
region,
|
|
29
29
|
};
|
|
30
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
const resolveEventsConfig = (amplify) => {
|
|
35
|
+
const config = amplify.getConfig();
|
|
36
|
+
if (!config.API?.GraphQL?.events) {
|
|
37
|
+
logger.warn('The Events configuration is missing. This is likely due to Amplify.configure() not being called prior to using events.connect() or events.post().');
|
|
38
|
+
}
|
|
39
|
+
const { apiKey, url, defaultAuthMode, region } = config.API?.GraphQL?.events ?? {};
|
|
40
|
+
return {
|
|
41
|
+
apiKey,
|
|
42
|
+
defaultAuthMode,
|
|
43
|
+
appSyncGraphqlEndpoint: url,
|
|
44
|
+
region,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
31
47
|
|
|
32
|
-
export { resolveConfig };
|
|
48
|
+
export { resolveConfig, resolveEventsConfig };
|
|
33
49
|
//# sourceMappingURL=resolveConfig.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveConfig.mjs","sources":["../../../src/utils/resolveConfig.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger } from '@aws-amplify/core';\nimport { APIValidationErrorCode, assertValidationError } from './errors';\nconst logger = new ConsoleLogger('GraphQLAPI resolveConfig');\n/**\n * @internal\n */\nexport const resolveConfig = (amplify) => {\n const config = amplify.getConfig();\n if (!config.API?.GraphQL) {\n logger.warn('The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient().');\n }\n const { apiKey, customEndpoint, customEndpointRegion, defaultAuthMode, endpoint, region, } = config.API?.GraphQL ?? {};\n // TODO: re-enable when working in all test environments:\n // assertValidationError(!!endpoint, APIValidationErrorCode.NoEndpoint);\n assertValidationError(!(!customEndpoint && customEndpointRegion), APIValidationErrorCode.NoCustomEndpoint);\n return {\n apiKey,\n customEndpoint,\n customEndpointRegion,\n defaultAuthMode,\n endpoint,\n region,\n };\n};\n"],"names":[],"mappings":";;;;;;AAAA;AACA;AAGA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,0BAA0B,CAAC,CAAC;AAC7D;AACA;AACA;AACY,MAAC,aAAa,GAAG,CAAC,OAAO,KAAK;AAC1C,IAAI,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;AACvC,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE;AAC9B,QAAQ,MAAM,CAAC,IAAI,CAAC,yHAAyH,CAAC,CAAC;AAC/I,KAAK;AACL,IAAI,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,oBAAoB,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,EAAE,OAAO,IAAI,EAAE,CAAC;AAC3H;AACA;AACA,IAAI,qBAAqB,CAAC,EAAE,CAAC,cAAc,IAAI,oBAAoB,CAAC,EAAE,sBAAsB,CAAC,gBAAgB,CAAC,CAAC;AAC/G,IAAI,OAAO;AACX,QAAQ,MAAM;AACd,QAAQ,cAAc;AACtB,QAAQ,oBAAoB;AAC5B,QAAQ,eAAe;AACvB,QAAQ,QAAQ;AAChB,QAAQ,MAAM;AACd,KAAK,CAAC;AACN;;;;"}
|
|
1
|
+
{"version":3,"file":"resolveConfig.mjs","sources":["../../../src/utils/resolveConfig.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger } from '@aws-amplify/core';\nimport { APIValidationErrorCode, assertValidationError } from './errors';\nconst logger = new ConsoleLogger('GraphQLAPI resolveConfig');\n/**\n * @internal\n */\nexport const resolveConfig = (amplify) => {\n const config = amplify.getConfig();\n if (!config.API?.GraphQL) {\n logger.warn('The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient().');\n }\n const { apiKey, customEndpoint, customEndpointRegion, defaultAuthMode, endpoint, region, } = config.API?.GraphQL ?? {};\n // TODO: re-enable when working in all test environments:\n // assertValidationError(!!endpoint, APIValidationErrorCode.NoEndpoint);\n assertValidationError(!(!customEndpoint && customEndpointRegion), APIValidationErrorCode.NoCustomEndpoint);\n return {\n apiKey,\n customEndpoint,\n customEndpointRegion,\n defaultAuthMode,\n endpoint,\n region,\n };\n};\n/**\n * @internal\n */\nexport const resolveEventsConfig = (amplify) => {\n const config = amplify.getConfig();\n if (!config.API?.GraphQL?.events) {\n logger.warn('The Events configuration is missing. This is likely due to Amplify.configure() not being called prior to using events.connect() or events.post().');\n }\n const { apiKey, url, defaultAuthMode, region } = config.API?.GraphQL?.events ?? {};\n return {\n apiKey,\n defaultAuthMode,\n appSyncGraphqlEndpoint: url,\n region,\n };\n};\n"],"names":[],"mappings":";;;;;;AAAA;AACA;AAGA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,0BAA0B,CAAC,CAAC;AAC7D;AACA;AACA;AACY,MAAC,aAAa,GAAG,CAAC,OAAO,KAAK;AAC1C,IAAI,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;AACvC,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE;AAC9B,QAAQ,MAAM,CAAC,IAAI,CAAC,yHAAyH,CAAC,CAAC;AAC/I,KAAK;AACL,IAAI,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,oBAAoB,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,EAAE,OAAO,IAAI,EAAE,CAAC;AAC3H;AACA;AACA,IAAI,qBAAqB,CAAC,EAAE,CAAC,cAAc,IAAI,oBAAoB,CAAC,EAAE,sBAAsB,CAAC,gBAAgB,CAAC,CAAC;AAC/G,IAAI,OAAO;AACX,QAAQ,MAAM;AACd,QAAQ,cAAc;AACtB,QAAQ,oBAAoB;AAC5B,QAAQ,eAAe;AACvB,QAAQ,QAAQ;AAChB,QAAQ,MAAM;AACd,KAAK,CAAC;AACN,EAAE;AACF;AACA;AACA;AACY,MAAC,mBAAmB,GAAG,CAAC,OAAO,KAAK;AAChD,IAAI,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;AACvC,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE;AACtC,QAAQ,MAAM,CAAC,IAAI,CAAC,mJAAmJ,CAAC,CAAC;AACzK,KAAK;AACL,IAAI,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,eAAe,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,IAAI,EAAE,CAAC;AACvF,IAAI,OAAO;AACX,QAAQ,MAAM;AACd,QAAQ,eAAe;AACvB,QAAQ,sBAAsB,EAAE,GAAG;AACnC,QAAQ,MAAM;AACd,KAAK,CAAC;AACN;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/api-graphql",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.4-events.81620c6.0+81620c6",
|
|
4
4
|
"description": "Api-graphql category of aws-amplify",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.mjs",
|
|
@@ -84,9 +84,9 @@
|
|
|
84
84
|
"server"
|
|
85
85
|
],
|
|
86
86
|
"dependencies": {
|
|
87
|
-
"@aws-amplify/api-rest": "4.0.
|
|
88
|
-
"@aws-amplify/core": "6.4.
|
|
89
|
-
"@aws-amplify/data-schema": "^1.
|
|
87
|
+
"@aws-amplify/api-rest": "4.0.51-events.81620c6.0+81620c6",
|
|
88
|
+
"@aws-amplify/core": "6.4.4-events.81620c6.0+81620c6",
|
|
89
|
+
"@aws-amplify/data-schema": "^1.7.0",
|
|
90
90
|
"@aws-sdk/types": "3.387.0",
|
|
91
91
|
"graphql": "15.8.0",
|
|
92
92
|
"rxjs": "^7.8.1",
|
|
@@ -101,5 +101,5 @@
|
|
|
101
101
|
"limit": "91.7 kB"
|
|
102
102
|
}
|
|
103
103
|
],
|
|
104
|
-
"gitHead": "
|
|
104
|
+
"gitHead": "81620c6dbe7e277a2f401fe967a1fb9c556b9341"
|
|
105
105
|
}
|
package/src/GraphQLAPI.ts
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
ApiAction,
|
|
6
|
+
Category,
|
|
7
|
+
CustomUserAgentDetails,
|
|
8
|
+
} from '@aws-amplify/core/internals/utils';
|
|
9
|
+
import {
|
|
10
|
+
CustomHeaders,
|
|
11
|
+
INTERNAL_USER_AGENT_OVERRIDE,
|
|
12
|
+
} from '@aws-amplify/data-schema/runtime';
|
|
6
13
|
import { Observable } from 'rxjs';
|
|
7
14
|
|
|
8
15
|
import { GraphQLOptions, GraphQLResult } from './types';
|
|
9
16
|
import { InternalGraphQLAPIClass } from './internals/InternalGraphQLAPI';
|
|
10
17
|
|
|
18
|
+
function isGraphQLOptionsWithOverride(
|
|
19
|
+
options: GraphQLOptions,
|
|
20
|
+
): options is GraphQLOptions & {
|
|
21
|
+
[INTERNAL_USER_AGENT_OVERRIDE]: CustomUserAgentDetails;
|
|
22
|
+
} {
|
|
23
|
+
return INTERNAL_USER_AGENT_OVERRIDE in options;
|
|
24
|
+
}
|
|
25
|
+
|
|
11
26
|
export const graphqlOperation = (
|
|
12
27
|
query: any,
|
|
13
28
|
variables = {},
|
|
@@ -38,9 +53,25 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
|
|
|
38
53
|
options: GraphQLOptions,
|
|
39
54
|
additionalHeaders?: CustomHeaders,
|
|
40
55
|
): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
|
|
41
|
-
|
|
56
|
+
const userAgentDetails: CustomUserAgentDetails = {
|
|
42
57
|
category: Category.API,
|
|
43
58
|
action: ApiAction.GraphQl,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
if (isGraphQLOptionsWithOverride(options)) {
|
|
62
|
+
const {
|
|
63
|
+
[INTERNAL_USER_AGENT_OVERRIDE]: internalUserAgentOverride,
|
|
64
|
+
...cleanOptions
|
|
65
|
+
} = options;
|
|
66
|
+
|
|
67
|
+
return super.graphql(amplify, cleanOptions, additionalHeaders, {
|
|
68
|
+
...userAgentDetails,
|
|
69
|
+
...internalUserAgentOverride,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return super.graphql(amplify, options, additionalHeaders, {
|
|
74
|
+
...userAgentDetails,
|
|
44
75
|
});
|
|
45
76
|
}
|
|
46
77
|
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import {
|
|
4
|
+
CustomUserAgentDetails,
|
|
5
|
+
DocumentType,
|
|
6
|
+
GraphQLAuthMode,
|
|
7
|
+
USER_AGENT_HEADER,
|
|
8
|
+
getAmplifyUserAgent,
|
|
9
|
+
} from '@aws-amplify/core/internals/utils';
|
|
10
|
+
import { CustomHeaders } from '@aws-amplify/data-schema/runtime';
|
|
11
|
+
|
|
12
|
+
import { MESSAGE_TYPES } from '../constants';
|
|
13
|
+
import { AWSWebSocketProvider } from '../AWSWebSocketProvider';
|
|
14
|
+
import { awsRealTimeHeaderBasedAuth } from '../AWSWebSocketProvider/authHeaders';
|
|
15
|
+
// resolved/actual AuthMode values. identityPool gets resolves to IAM upstream in InternalGraphQLAPI._graphqlSubscribe
|
|
16
|
+
type ResolvedGraphQLAuthModes = Exclude<GraphQLAuthMode, 'identityPool'>;
|
|
17
|
+
|
|
18
|
+
interface AWSAppSyncEventProviderOptions {
|
|
19
|
+
appSyncGraphqlEndpoint?: string;
|
|
20
|
+
authenticationType?: ResolvedGraphQLAuthModes;
|
|
21
|
+
query?: string;
|
|
22
|
+
variables?: DocumentType;
|
|
23
|
+
apiKey?: string;
|
|
24
|
+
region?: string;
|
|
25
|
+
libraryConfigHeaders?(): Promise<Record<string, unknown> | Headers>;
|
|
26
|
+
additionalHeaders?: CustomHeaders;
|
|
27
|
+
additionalCustomHeaders?: Record<string, string>;
|
|
28
|
+
authToken?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface DataPayload {
|
|
32
|
+
id: string;
|
|
33
|
+
event: string;
|
|
34
|
+
type: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface DataResponse {
|
|
38
|
+
id: string;
|
|
39
|
+
payload: string;
|
|
40
|
+
type: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const PROVIDER_NAME = 'AWSAppSyncEventsProvider';
|
|
44
|
+
|
|
45
|
+
export class AWSAppSyncEventProvider extends AWSWebSocketProvider {
|
|
46
|
+
constructor() {
|
|
47
|
+
super(PROVIDER_NAME);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async connect(options: AWSAppSyncEventProviderOptions) {
|
|
51
|
+
super.connect(options);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public subscribe(
|
|
55
|
+
options?: AWSAppSyncEventProviderOptions,
|
|
56
|
+
customUserAgentDetails?: CustomUserAgentDetails,
|
|
57
|
+
) {
|
|
58
|
+
return super.subscribe(options, customUserAgentDetails).pipe();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public async publish(
|
|
62
|
+
options: AWSAppSyncEventProviderOptions,
|
|
63
|
+
customUserAgentDetails?: CustomUserAgentDetails,
|
|
64
|
+
) {
|
|
65
|
+
super.publish(options, customUserAgentDetails);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
protected async _prepareSubscriptionPayload({
|
|
69
|
+
options,
|
|
70
|
+
subscriptionId,
|
|
71
|
+
customUserAgentDetails,
|
|
72
|
+
additionalCustomHeaders,
|
|
73
|
+
libraryConfigHeaders,
|
|
74
|
+
publish,
|
|
75
|
+
}: {
|
|
76
|
+
options: AWSAppSyncEventProviderOptions;
|
|
77
|
+
subscriptionId: string;
|
|
78
|
+
customUserAgentDetails: CustomUserAgentDetails | undefined;
|
|
79
|
+
additionalCustomHeaders: Record<string, string>;
|
|
80
|
+
libraryConfigHeaders: Record<string, string>;
|
|
81
|
+
publish?: boolean;
|
|
82
|
+
}): Promise<string> {
|
|
83
|
+
const {
|
|
84
|
+
appSyncGraphqlEndpoint,
|
|
85
|
+
authenticationType,
|
|
86
|
+
query,
|
|
87
|
+
variables,
|
|
88
|
+
apiKey,
|
|
89
|
+
region,
|
|
90
|
+
} = options;
|
|
91
|
+
|
|
92
|
+
// This will be needed for WS publish
|
|
93
|
+
// const data = {
|
|
94
|
+
// events: [variables],
|
|
95
|
+
// };
|
|
96
|
+
|
|
97
|
+
const serializedData = JSON.stringify([variables]);
|
|
98
|
+
|
|
99
|
+
const headers = {
|
|
100
|
+
...(await awsRealTimeHeaderBasedAuth({
|
|
101
|
+
apiKey,
|
|
102
|
+
appSyncGraphqlEndpoint,
|
|
103
|
+
authenticationType,
|
|
104
|
+
payload: serializedData,
|
|
105
|
+
canonicalUri: '',
|
|
106
|
+
region,
|
|
107
|
+
additionalCustomHeaders,
|
|
108
|
+
})),
|
|
109
|
+
...libraryConfigHeaders,
|
|
110
|
+
...additionalCustomHeaders,
|
|
111
|
+
[USER_AGENT_HEADER]: getAmplifyUserAgent(customUserAgentDetails),
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Commented out code will be needed for WS publish
|
|
115
|
+
const subscriptionMessage = {
|
|
116
|
+
id: subscriptionId,
|
|
117
|
+
channel: query,
|
|
118
|
+
// events: [JSON.stringify(variables)],
|
|
119
|
+
authorization: {
|
|
120
|
+
...headers,
|
|
121
|
+
},
|
|
122
|
+
// payload: {
|
|
123
|
+
// events: serializedData,
|
|
124
|
+
// extensions: {
|
|
125
|
+
// authorization: {
|
|
126
|
+
// ...headers,
|
|
127
|
+
// },
|
|
128
|
+
// },
|
|
129
|
+
// },
|
|
130
|
+
type: publish
|
|
131
|
+
? MESSAGE_TYPES.EVENT_PUBLISH
|
|
132
|
+
: MESSAGE_TYPES.EVENT_SUBSCRIBE,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const serializedSubscriptionMessage = JSON.stringify(subscriptionMessage);
|
|
136
|
+
|
|
137
|
+
return serializedSubscriptionMessage;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
protected _handleSubscriptionData(
|
|
141
|
+
message: MessageEvent,
|
|
142
|
+
): [boolean, DataResponse] {
|
|
143
|
+
this.logger.debug(
|
|
144
|
+
`subscription message from AWS AppSync Events: ${message.data}`,
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
const {
|
|
148
|
+
id = '',
|
|
149
|
+
event: payload,
|
|
150
|
+
type,
|
|
151
|
+
}: DataPayload = JSON.parse(String(message.data));
|
|
152
|
+
|
|
153
|
+
const {
|
|
154
|
+
observer = null,
|
|
155
|
+
query = '',
|
|
156
|
+
variables = {},
|
|
157
|
+
} = this.subscriptionObserverMap.get(id) || {};
|
|
158
|
+
|
|
159
|
+
this.logger.debug({ id, observer, query, variables });
|
|
160
|
+
|
|
161
|
+
if (type === MESSAGE_TYPES.DATA && payload) {
|
|
162
|
+
const deserializedEvent = JSON.parse(payload);
|
|
163
|
+
if (observer) {
|
|
164
|
+
observer.next(deserializedEvent);
|
|
165
|
+
} else {
|
|
166
|
+
this.logger.debug(`observer not found for id: ${id}`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return [true, { id, type, payload: deserializedEvent }];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return [false, { id, type, payload }];
|
|
173
|
+
}
|
|
174
|
+
}
|