@aws-amplify/core 6.0.1-console-preview.d7e0545.0 → 6.0.1-console-preview.7739091.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/lib/Cache/Utils/CacheUtils.js +2 -4
- package/lib/Platform/version.d.ts +1 -1
- package/lib/Platform/version.js +1 -1
- package/lib/RNComponents/index.d.ts +1 -1
- package/lib/RNComponents/index.js +4 -4
- package/lib/ServiceWorker/ServiceWorker.js +1 -1
- package/lib/Util/JS.d.ts +1 -4
- package/lib/Util/JS.js +4 -11
- package/lib/Util/Reachability.js +0 -3
- package/lib/clients/handlers/fetch.d.ts +0 -1
- package/lib/clients/handlers/fetch.js +0 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +6 -8
- package/lib/libraryUtils.d.ts +1 -2
- package/lib/libraryUtils.js +3 -6
- package/lib/parseAWSExports.js +20 -1
- package/lib/providers/pinpoint/apis/record.d.ts +2 -2
- package/lib/providers/pinpoint/apis/record.js +3 -3
- package/lib/providers/pinpoint/apis/updateEndpoint.d.ts +2 -2
- package/lib/providers/pinpoint/types/pinpoint.d.ts +2 -2
- package/lib/singleton/API/types.d.ts +34 -0
- package/lib/singleton/API/types.js +2 -0
- package/lib/singleton/Auth/index.js +6 -16
- package/lib/singleton/Auth/types.d.ts +2 -2
- package/lib/singleton/Storage/types.d.ts +5 -0
- package/lib/singleton/types.d.ts +4 -1
- package/lib/{StorageHelper/cookieStorage.d.ts → storage/CookieStorage.d.ts} +4 -3
- package/lib/{StorageHelper/cookieStorage.js → storage/CookieStorage.js} +35 -83
- package/lib/storage/CookieStorage.native.d.ts +7 -0
- package/lib/storage/CookieStorage.native.js +32 -0
- package/lib/storage/DefaultStorage.d.ts +7 -0
- package/lib/storage/DefaultStorage.js +33 -0
- package/{lib-esm/StorageHelper/localStorage.d.ts → lib/storage/DefaultStorage.native.d.ts} +7 -6
- package/lib/{StorageHelper/sessionStorage.js → storage/DefaultStorage.native.js} +46 -43
- package/lib/storage/InMemoryStorage.d.ts +12 -0
- package/lib/storage/InMemoryStorage.js +41 -0
- package/lib/{StorageHelper/sessionStorage.d.ts → storage/KeyValueStorage.d.ts} +5 -4
- package/lib/{StorageHelper/localStorage.js → storage/KeyValueStorage.js} +13 -15
- package/lib/storage/SessionStorage.d.ts +7 -0
- package/lib/storage/SessionStorage.js +33 -0
- package/lib/storage/index.d.ts +7 -0
- package/lib/storage/index.js +14 -0
- package/lib/storage/utils.d.ts +10 -0
- package/lib/storage/utils.js +26 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/storage.d.ts +3 -0
- package/lib-esm/Cache/Utils/CacheUtils.js +2 -4
- package/lib-esm/Platform/version.d.ts +1 -1
- package/lib-esm/Platform/version.js +1 -1
- package/lib-esm/RNComponents/index.d.ts +1 -1
- package/lib-esm/RNComponents/index.js +5 -5
- package/lib-esm/ServiceWorker/ServiceWorker.js +2 -2
- package/lib-esm/Util/JS.d.ts +1 -4
- package/lib-esm/Util/JS.js +2 -9
- package/lib-esm/Util/Reachability.js +1 -4
- package/lib-esm/clients/handlers/fetch.d.ts +0 -1
- package/lib-esm/clients/handlers/fetch.js +0 -1
- package/lib-esm/index.d.ts +1 -1
- package/lib-esm/index.js +1 -1
- package/lib-esm/libraryUtils.d.ts +1 -2
- package/lib-esm/libraryUtils.js +1 -2
- package/lib-esm/parseAWSExports.js +20 -1
- package/lib-esm/providers/pinpoint/apis/record.d.ts +2 -2
- package/lib-esm/providers/pinpoint/apis/record.js +3 -3
- package/lib-esm/providers/pinpoint/apis/updateEndpoint.d.ts +2 -2
- package/lib-esm/providers/pinpoint/types/pinpoint.d.ts +2 -2
- package/lib-esm/singleton/API/types.d.ts +34 -0
- package/lib-esm/singleton/API/types.js +1 -0
- package/lib-esm/singleton/Auth/index.js +6 -16
- package/lib-esm/singleton/Auth/types.d.ts +2 -2
- package/lib-esm/singleton/Storage/types.d.ts +5 -0
- package/lib-esm/singleton/types.d.ts +4 -1
- package/lib-esm/{StorageHelper/cookieStorage.d.ts → storage/CookieStorage.d.ts} +4 -3
- package/lib-esm/{StorageHelper/cookieStorage.js → storage/CookieStorage.js} +36 -61
- package/lib-esm/storage/CookieStorage.native.d.ts +7 -0
- package/lib-esm/storage/CookieStorage.native.js +29 -0
- package/lib-esm/storage/DefaultStorage.d.ts +7 -0
- package/lib-esm/storage/DefaultStorage.js +30 -0
- package/{lib/StorageHelper/localStorage.d.ts → lib-esm/storage/DefaultStorage.native.d.ts} +7 -6
- package/lib-esm/{StorageHelper/sessionStorage.js → storage/DefaultStorage.native.js} +45 -42
- package/lib-esm/storage/InMemoryStorage.d.ts +12 -0
- package/lib-esm/storage/InMemoryStorage.js +38 -0
- package/lib-esm/{StorageHelper/sessionStorage.d.ts → storage/KeyValueStorage.d.ts} +5 -4
- package/lib-esm/{StorageHelper/localStorage.js → storage/KeyValueStorage.js} +12 -14
- package/lib-esm/storage/SessionStorage.d.ts +7 -0
- package/lib-esm/storage/SessionStorage.js +30 -0
- package/lib-esm/storage/index.d.ts +7 -0
- package/lib-esm/storage/index.js +10 -0
- package/lib-esm/storage/utils.d.ts +10 -0
- package/lib-esm/storage/utils.js +21 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/storage.d.ts +3 -0
- package/package.json +3 -5
- package/src/Cache/Utils/CacheUtils.ts +2 -4
- package/src/Platform/version.ts +1 -1
- package/src/RNComponents/index.ts +5 -5
- package/src/ServiceWorker/ServiceWorker.ts +2 -2
- package/src/Util/JS.ts +2 -13
- package/src/Util/Reachability.ts +1 -5
- package/src/clients/handlers/fetch.ts +0 -1
- package/src/index.ts +4 -6
- package/src/libraryUtils.ts +1 -2
- package/src/parseAWSExports.ts +26 -0
- package/src/providers/pinpoint/apis/record.ts +8 -8
- package/src/providers/pinpoint/apis/updateEndpoint.ts +2 -2
- package/src/providers/pinpoint/types/pinpoint.ts +7 -6
- package/src/singleton/API/types.ts +31 -0
- package/src/singleton/Auth/index.ts +1 -10
- package/src/singleton/Auth/types.ts +2 -2
- package/src/singleton/Storage/types.ts +6 -2
- package/src/singleton/types.ts +4 -1
- package/src/storage/CookieStorage.native.ts +13 -0
- package/src/storage/CookieStorage.ts +72 -0
- package/src/storage/DefaultStorage.native.ts +78 -0
- package/src/storage/DefaultStorage.ts +14 -0
- package/src/storage/InMemoryStorage.ts +36 -0
- package/src/{StorageHelper/sessionStorage.ts → storage/KeyValueStorage.ts} +10 -13
- package/src/storage/SessionStorage.ts +14 -0
- package/src/storage/index.ts +13 -0
- package/src/storage/utils.ts +22 -0
- package/src/types/storage.ts +4 -0
- package/lib/OAuthHelper/FacebookOAuth.d.ts +0 -6
- package/lib/OAuthHelper/FacebookOAuth.js +0 -124
- package/lib/OAuthHelper/GoogleOAuth.d.ts +0 -6
- package/lib/OAuthHelper/GoogleOAuth.js +0 -134
- package/lib/OAuthHelper/index.d.ts +0 -4
- package/lib/OAuthHelper/index.js +0 -9
- package/lib/StorageHelper/inMemoryStorage.d.ts +0 -10
- package/lib/StorageHelper/inMemoryStorage.js +0 -82
- package/lib/StorageHelper/index.d.ts +0 -45
- package/lib/StorageHelper/index.js +0 -83
- package/lib/StorageHelper/reactnative.d.ts +0 -83
- package/lib/StorageHelper/reactnative.js +0 -250
- package/lib-esm/OAuthHelper/FacebookOAuth.d.ts +0 -6
- package/lib-esm/OAuthHelper/FacebookOAuth.js +0 -121
- package/lib-esm/OAuthHelper/GoogleOAuth.d.ts +0 -6
- package/lib-esm/OAuthHelper/GoogleOAuth.js +0 -131
- package/lib-esm/OAuthHelper/index.d.ts +0 -4
- package/lib-esm/OAuthHelper/index.js +0 -6
- package/lib-esm/StorageHelper/inMemoryStorage.d.ts +0 -10
- package/lib-esm/StorageHelper/inMemoryStorage.js +0 -79
- package/lib-esm/StorageHelper/index.d.ts +0 -45
- package/lib-esm/StorageHelper/index.js +0 -76
- package/lib-esm/StorageHelper/reactnative.d.ts +0 -83
- package/lib-esm/StorageHelper/reactnative.js +0 -244
- package/src/OAuthHelper/FacebookOAuth.ts +0 -83
- package/src/OAuthHelper/GoogleOAuth.ts +0 -97
- package/src/OAuthHelper/index.ts +0 -7
- package/src/StorageHelper/cookieStorage.ts +0 -99
- package/src/StorageHelper/inMemoryStorage.ts +0 -32
- package/src/StorageHelper/index.ts +0 -78
- package/src/StorageHelper/localStorage.ts +0 -60
- package/src/StorageHelper/reactnative.ts +0 -182
package/src/libraryUtils.ts
CHANGED
|
@@ -7,7 +7,7 @@ utils for use throughout the library.
|
|
|
7
7
|
*/
|
|
8
8
|
// JS utilities
|
|
9
9
|
export {
|
|
10
|
-
|
|
10
|
+
isBrowser,
|
|
11
11
|
filenameToContentType,
|
|
12
12
|
generateRandomString,
|
|
13
13
|
isEmpty,
|
|
@@ -96,7 +96,6 @@ export {
|
|
|
96
96
|
AmplifyError,
|
|
97
97
|
AmplifyErrorString,
|
|
98
98
|
} from './Util/Errors';
|
|
99
|
-
export { FacebookOAuth, GoogleOAuth } from './OAuthHelper';
|
|
100
99
|
export { AppState, AsyncStorage, Linking } from './RNComponents';
|
|
101
100
|
export { ErrorParams, AmplifyErrorMap, ServiceError } from './types';
|
|
102
101
|
export {
|
package/src/parseAWSExports.ts
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
import { OAuthConfig } from './singleton/Auth/types';
|
|
4
4
|
import { ResourcesConfig } from './singleton/types';
|
|
5
5
|
|
|
6
|
+
const authTypeMapping: Record<any, any> = {
|
|
7
|
+
API_KEY: 'apiKey',
|
|
8
|
+
AWS_IAM: 'iam',
|
|
9
|
+
AMAZON_COGNITO_USER_POOLS: 'jwt',
|
|
10
|
+
};
|
|
11
|
+
|
|
6
12
|
/**
|
|
7
13
|
* This utility converts the `aws-exports.js` file generated by the Amplify CLI into a {@link ResourcesConfig} object
|
|
8
14
|
* consumable by Amplify.
|
|
@@ -11,10 +17,15 @@ import { ResourcesConfig } from './singleton/types';
|
|
|
11
17
|
*
|
|
12
18
|
* @returns A {@link ResourcesConfig} object.
|
|
13
19
|
*/
|
|
20
|
+
|
|
14
21
|
export const parseAWSExports = (
|
|
15
22
|
config: Record<string, any> = {}
|
|
16
23
|
): ResourcesConfig => {
|
|
17
24
|
const {
|
|
25
|
+
aws_appsync_apiKey,
|
|
26
|
+
aws_appsync_authenticationType,
|
|
27
|
+
aws_appsync_graphqlEndpoint,
|
|
28
|
+
aws_appsync_region,
|
|
18
29
|
aws_cognito_identity_pool_id,
|
|
19
30
|
aws_cognito_sign_up_verification_method,
|
|
20
31
|
aws_mandatory_sign_in,
|
|
@@ -40,6 +51,21 @@ export const parseAWSExports = (
|
|
|
40
51
|
};
|
|
41
52
|
}
|
|
42
53
|
|
|
54
|
+
// TODO: Need to support all API configurations
|
|
55
|
+
// API
|
|
56
|
+
if (aws_appsync_graphqlEndpoint) {
|
|
57
|
+
amplifyConfig.API = {
|
|
58
|
+
AppSync: {
|
|
59
|
+
defaultAuthMode: {
|
|
60
|
+
type: authTypeMapping[aws_appsync_authenticationType],
|
|
61
|
+
apiKey: aws_appsync_apiKey,
|
|
62
|
+
},
|
|
63
|
+
endpoint: aws_appsync_graphqlEndpoint,
|
|
64
|
+
region: aws_appsync_region,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
43
69
|
// Auth
|
|
44
70
|
if (aws_cognito_identity_pool_id || aws_user_pools_id) {
|
|
45
71
|
amplifyConfig.Auth = {
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import { v4 as uuid } from 'uuid';
|
|
5
|
-
import {
|
|
5
|
+
import { PinpointRecordInput, PinpointSession } from '../types';
|
|
6
6
|
import { getEndpointId } from '../utils';
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
8
|
BUFFER_SIZE,
|
|
9
9
|
FLUSH_INTERVAL,
|
|
10
10
|
FLUSH_SIZE,
|
|
@@ -28,11 +28,11 @@ export const record = async ({
|
|
|
28
28
|
identityId,
|
|
29
29
|
region,
|
|
30
30
|
userAgentValue,
|
|
31
|
-
}:
|
|
31
|
+
}: PinpointRecordInput): Promise<void> => {
|
|
32
32
|
const timestampISOString = new Date().toISOString();
|
|
33
33
|
const eventId = uuid();
|
|
34
34
|
let endpointId = await getEndpointId(appId, category);
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
// Prepare event buffer if required
|
|
37
37
|
const buffer = getEventBuffer({
|
|
38
38
|
appId,
|
|
@@ -43,7 +43,7 @@ export const record = async ({
|
|
|
43
43
|
identityId,
|
|
44
44
|
region,
|
|
45
45
|
resendLimit: RESEND_LIMIT,
|
|
46
|
-
userAgentValue
|
|
46
|
+
userAgentValue,
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
// Prepare a Pinpoint endpoint via updateEndpoint if one does not already exist, which will generate and cache an
|
|
@@ -64,7 +64,7 @@ export const record = async ({
|
|
|
64
64
|
if (!endpointId) {
|
|
65
65
|
throw new AmplifyError({
|
|
66
66
|
name: 'ENDPOINT_NOT_CREATED',
|
|
67
|
-
message: 'Endpoint was not created.'
|
|
67
|
+
message: 'Endpoint was not created.',
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
|
|
@@ -77,7 +77,7 @@ export const record = async ({
|
|
|
77
77
|
StartTimestamp: timestampISOString,
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
// Push event to buffer
|
|
82
82
|
buffer.push({
|
|
83
83
|
eventId,
|
|
@@ -85,6 +85,6 @@ export const record = async ({
|
|
|
85
85
|
event,
|
|
86
86
|
session,
|
|
87
87
|
timestamp: timestampISOString,
|
|
88
|
-
resendLimit: RESEND_LIMIT
|
|
88
|
+
resendLimit: RESEND_LIMIT,
|
|
89
89
|
});
|
|
90
90
|
};
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
updateEndpoint as clientUpdateEndpoint,
|
|
8
8
|
UpdateEndpointInput,
|
|
9
9
|
} from '../../../AwsClients/Pinpoint';
|
|
10
|
-
import {
|
|
10
|
+
import { PinpointUpdateEndpointInput } from '../types';
|
|
11
11
|
import { cacheEndpointId, getEndpointId } from '../utils';
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -25,7 +25,7 @@ export const updateEndpoint = async ({
|
|
|
25
25
|
userId,
|
|
26
26
|
userProfile,
|
|
27
27
|
userAgentValue,
|
|
28
|
-
}:
|
|
28
|
+
}: PinpointUpdateEndpointInput): Promise<void> => {
|
|
29
29
|
const endpointId = await getEndpointId(appId, category);
|
|
30
30
|
// only generate a new endpoint id if one was not found in cache
|
|
31
31
|
const createdEndpointId = !endpointId ? uuidv4() : undefined;
|
|
@@ -44,12 +44,13 @@ type PinpointCommonParameters = {
|
|
|
44
44
|
userAgentValue?: string;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
export type
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
export type PinpointUpdateEndpointInput = PinpointCommonParameters &
|
|
48
|
+
PinpointServiceOptions & {
|
|
49
|
+
channelType?: SupportedChannelType;
|
|
50
|
+
userId?: string;
|
|
51
|
+
userProfile?: UserProfile;
|
|
52
|
+
};
|
|
52
53
|
|
|
53
|
-
export type
|
|
54
|
+
export type PinpointRecordInput = PinpointCommonParameters & {
|
|
54
55
|
event: PinpointAnalyticsEvent;
|
|
55
56
|
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
export type LibraryAPIOptions = {
|
|
4
|
+
AppSync: {
|
|
5
|
+
query: string;
|
|
6
|
+
variables?: object;
|
|
7
|
+
authMode?: any;
|
|
8
|
+
authToken?: string;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated This property should not be used
|
|
11
|
+
*/
|
|
12
|
+
userAgentSuffix?: string;
|
|
13
|
+
};
|
|
14
|
+
customHeaders: Function;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type APIConfig = {
|
|
18
|
+
AppSync?: {
|
|
19
|
+
defaultAuthMode?: GraphQLAuthMode;
|
|
20
|
+
region?: string;
|
|
21
|
+
endpoint?: string;
|
|
22
|
+
modelIntrospectionSchema?: any;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type GraphQLAuthMode =
|
|
27
|
+
| { type: 'apiKey'; apiKey: string }
|
|
28
|
+
| { type: 'jwt'; token: 'id' | 'access' }
|
|
29
|
+
| { type: 'iam' }
|
|
30
|
+
| { type: 'lambda' }
|
|
31
|
+
| { type: 'custom' };
|
|
@@ -8,8 +8,6 @@ import {
|
|
|
8
8
|
FetchAuthSessionOptions,
|
|
9
9
|
LibraryAuthOptions,
|
|
10
10
|
} from './types';
|
|
11
|
-
import { asserts } from '../../Util/errors/AssertError';
|
|
12
|
-
import { AUTH_CONFING_EXCEPTION } from '../../Util/Constants';
|
|
13
11
|
|
|
14
12
|
export function isTokenExpired({
|
|
15
13
|
expiresAt,
|
|
@@ -53,13 +51,6 @@ export class AuthClass {
|
|
|
53
51
|
let credentialsAndIdentityId: AWSCredentialsAndIdentityId | undefined;
|
|
54
52
|
let userSub: string | undefined;
|
|
55
53
|
|
|
56
|
-
asserts(!!this.authConfig, {
|
|
57
|
-
name: AUTH_CONFING_EXCEPTION,
|
|
58
|
-
message: 'AuthConfig is required',
|
|
59
|
-
recoverySuggestion:
|
|
60
|
-
'call Amplify.configure in your app with a valid AuthConfig',
|
|
61
|
-
});
|
|
62
|
-
|
|
63
54
|
// Get tokens will throw if session cannot be refreshed (network or service error) or return null if not available
|
|
64
55
|
tokens =
|
|
65
56
|
(await this.authOptions?.tokenProvider?.getTokens(options)) ?? undefined;
|
|
@@ -77,7 +68,7 @@ export class AuthClass {
|
|
|
77
68
|
forceRefresh: options.forceRefresh,
|
|
78
69
|
}
|
|
79
70
|
);
|
|
80
|
-
} else
|
|
71
|
+
} else {
|
|
81
72
|
// getCredentialsAndIdentityId will throw if cannot get credentials (network or service error)
|
|
82
73
|
credentialsAndIdentityId =
|
|
83
74
|
await this.authOptions?.credentialsProvider?.getCredentialsAndIdentityId(
|
|
@@ -152,14 +152,14 @@ export type GetCredentialsOptions =
|
|
|
152
152
|
type GetCredentialsAuthenticatedUser = {
|
|
153
153
|
authenticated: true;
|
|
154
154
|
forceRefresh?: boolean;
|
|
155
|
-
authConfig: AuthConfig;
|
|
155
|
+
authConfig: AuthConfig | undefined;
|
|
156
156
|
tokens: AuthTokens;
|
|
157
157
|
};
|
|
158
158
|
|
|
159
159
|
type GetCredentialsUnauthenticatedUser = {
|
|
160
160
|
authenticated: false;
|
|
161
161
|
forceRefresh?: boolean;
|
|
162
|
-
authConfig: AuthConfig;
|
|
162
|
+
authConfig: AuthConfig | undefined;
|
|
163
163
|
tokens?: never;
|
|
164
164
|
};
|
|
165
165
|
|
|
@@ -7,8 +7,13 @@ export interface StorageConfig {
|
|
|
7
7
|
S3: {
|
|
8
8
|
bucket?: string;
|
|
9
9
|
region?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Internal-only configuration for testing purpose. You should not use this.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
10
15
|
dangerouslyConnectToHttpEndpointForTesting?: string;
|
|
11
|
-
}
|
|
16
|
+
};
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
type StoragePrefixResolver = (params: {
|
|
@@ -16,7 +21,6 @@ type StoragePrefixResolver = (params: {
|
|
|
16
21
|
targetIdentityId?: string;
|
|
17
22
|
}) => Promise<string>;
|
|
18
23
|
|
|
19
|
-
// TODO[AllanZhengYP]: need to finalize the decision whether to move defaultAccessLevel to StorageConfig
|
|
20
24
|
export interface LibraryStorageOptions {
|
|
21
25
|
S3: {
|
|
22
26
|
prefixResolver?: StoragePrefixResolver;
|
package/src/singleton/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
+
import { APIConfig, LibraryAPIOptions } from './API/types';
|
|
4
5
|
import { AnalyticsConfig } from './Analytics/types';
|
|
5
6
|
import {
|
|
6
7
|
AuthConfig,
|
|
@@ -18,7 +19,7 @@ import {
|
|
|
18
19
|
} from './Storage/types';
|
|
19
20
|
|
|
20
21
|
export type ResourcesConfig = {
|
|
21
|
-
|
|
22
|
+
API?: APIConfig;
|
|
22
23
|
Analytics?: AnalyticsConfig;
|
|
23
24
|
Auth?: AuthConfig;
|
|
24
25
|
// Cache?: CacheConfig;
|
|
@@ -31,12 +32,14 @@ export type ResourcesConfig = {
|
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
export type LibraryOptions = {
|
|
35
|
+
API?: LibraryAPIOptions;
|
|
34
36
|
Auth?: LibraryAuthOptions;
|
|
35
37
|
Storage?: LibraryStorageOptions;
|
|
36
38
|
ssr?: boolean;
|
|
37
39
|
};
|
|
38
40
|
|
|
39
41
|
export {
|
|
42
|
+
APIConfig,
|
|
40
43
|
AuthConfig,
|
|
41
44
|
AuthUserPoolConfig,
|
|
42
45
|
AuthIdentityPoolConfig,
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { KeyValueStorage } from './KeyValueStorage';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export class CookieStorage extends KeyValueStorage {
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
get as getJsCookie,
|
|
6
|
+
remove as removeJsCookie,
|
|
7
|
+
set as setJsCookie,
|
|
8
|
+
} from 'js-cookie';
|
|
9
|
+
import {
|
|
10
|
+
CookieStorageData,
|
|
11
|
+
KeyValueStorageInterface,
|
|
12
|
+
SameSite,
|
|
13
|
+
} from '../types';
|
|
14
|
+
|
|
15
|
+
export class CookieStorage implements KeyValueStorageInterface {
|
|
16
|
+
path: string;
|
|
17
|
+
domain?: string;
|
|
18
|
+
expires?: number;
|
|
19
|
+
sameSite?: SameSite;
|
|
20
|
+
secure?: boolean;
|
|
21
|
+
|
|
22
|
+
constructor(data: CookieStorageData = {}) {
|
|
23
|
+
const { path, domain, expires, sameSite, secure } = data;
|
|
24
|
+
this.domain = domain;
|
|
25
|
+
this.path = path ? path : '/';
|
|
26
|
+
this.expires = data.hasOwnProperty('expires') ? expires : 365;
|
|
27
|
+
this.secure = data.hasOwnProperty('secure') ? secure : true;
|
|
28
|
+
|
|
29
|
+
if (data.hasOwnProperty('sameSite')) {
|
|
30
|
+
if (!sameSite || !['strict', 'lax', 'none'].includes(sameSite)) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
'The sameSite value of cookieStorage must be "lax", "strict" or "none".'
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
if (sameSite === 'none' && !this.secure) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
'sameSite = None requires the Secure attribute in latest browser versions.'
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
this.sameSite = sameSite;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async setItem(key: string, value: string) {
|
|
45
|
+
setJsCookie(key, value, this.getData());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async getItem(key: string) {
|
|
49
|
+
const item = getJsCookie(key);
|
|
50
|
+
return item ?? null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async removeItem(key: string) {
|
|
54
|
+
removeJsCookie(key, this.getData());
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async clear() {
|
|
58
|
+
const cookie = getJsCookie();
|
|
59
|
+
const promises = Object.keys(cookie).map(key => this.removeItem(key));
|
|
60
|
+
await Promise.all(promises);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private getData(): CookieStorageData {
|
|
64
|
+
return {
|
|
65
|
+
path: this.path,
|
|
66
|
+
expires: this.expires,
|
|
67
|
+
domain: this.domain,
|
|
68
|
+
secure: this.secure,
|
|
69
|
+
...(this.sameSite && { sameSite: this.sameSite }),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { AsyncStorageStatic } from '@react-native-async-storage/async-storage';
|
|
5
|
+
import { AmplifyError } from '../libraryUtils';
|
|
6
|
+
import { KeyValueStorageInterface } from '../types';
|
|
7
|
+
|
|
8
|
+
const ASYNC_STORAGE_MODULE = '@react-native-async-storage/async-storage';
|
|
9
|
+
const MEMORY_KEY_PREFIX = '@MemoryStorage:';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export class DefaultStorage implements KeyValueStorageInterface {
|
|
15
|
+
private asyncStorage?: AsyncStorageStatic;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* This is used to set a specific item in storage
|
|
19
|
+
* @param {string} key - the key for the item
|
|
20
|
+
* @param {object} value - the value
|
|
21
|
+
* @returns {string} value that was set
|
|
22
|
+
*/
|
|
23
|
+
setItem(key: string, value: string) {
|
|
24
|
+
this.assertModule(this.asyncStorage);
|
|
25
|
+
return this.asyncStorage.setItem(`${MEMORY_KEY_PREFIX}${key}`, value);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* This is used to get a specific key from storage
|
|
30
|
+
* @param {string} key - the key for the item
|
|
31
|
+
* This is used to clear the storage
|
|
32
|
+
* @returns {string} the data item
|
|
33
|
+
*/
|
|
34
|
+
getItem(key: string) {
|
|
35
|
+
this.assertModule(this.asyncStorage);
|
|
36
|
+
return this.asyncStorage.getItem(`${MEMORY_KEY_PREFIX}${key}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* This is used to remove an item from storage
|
|
41
|
+
* @param {string} key - the key being set
|
|
42
|
+
* @returns {string} value - value that was deleted
|
|
43
|
+
*/
|
|
44
|
+
removeItem(key: string): Promise<void> {
|
|
45
|
+
this.assertModule(this.asyncStorage);
|
|
46
|
+
return this.asyncStorage.removeItem(`${MEMORY_KEY_PREFIX}${key}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* This is used to clear the storage
|
|
51
|
+
* @returns {string} nothing
|
|
52
|
+
*/
|
|
53
|
+
async clear(): Promise<void> {
|
|
54
|
+
this.assertModule(this.asyncStorage);
|
|
55
|
+
const allKeys = await this.asyncStorage.getAllKeys();
|
|
56
|
+
return this.asyncStorage.multiRemove(
|
|
57
|
+
allKeys.filter(key => key.startsWith(MEMORY_KEY_PREFIX))
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private assertModule(
|
|
62
|
+
asyncStorage?: AsyncStorageStatic
|
|
63
|
+
): asserts asyncStorage {
|
|
64
|
+
if (!!asyncStorage) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
this.asyncStorage = require(ASYNC_STORAGE_MODULE)
|
|
69
|
+
.default as AsyncStorageStatic;
|
|
70
|
+
} catch (err) {
|
|
71
|
+
throw new AmplifyError({
|
|
72
|
+
name: 'NativeModuleException',
|
|
73
|
+
message: `Unable to find ${ASYNC_STORAGE_MODULE}`,
|
|
74
|
+
recoverySuggestion: `Make sure to install ${ASYNC_STORAGE_MODULE}`,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { KeyValueStorage } from './KeyValueStorage';
|
|
5
|
+
import { getDefaultStorageWithFallback } from './utils';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export class DefaultStorage extends KeyValueStorage {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(getDefaultStorageWithFallback());
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export class InMemoryStorage implements Storage {
|
|
8
|
+
storage = new Map<string, string>();
|
|
9
|
+
|
|
10
|
+
get length() {
|
|
11
|
+
return this.storage.size;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
key(index: number) {
|
|
15
|
+
if (index > this.length - 1) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return Array.from(this.storage.keys())[index];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
setItem(key: string, value: string) {
|
|
22
|
+
this.storage.set(key, value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getItem(key: string) {
|
|
26
|
+
return this.storage.get(key) ?? null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
removeItem(key: string) {
|
|
30
|
+
this.storage.delete(key);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
clear() {
|
|
34
|
+
this.storage.clear();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -4,15 +4,14 @@
|
|
|
4
4
|
import { PlatformNotSupportedError } from '../Util/Errors';
|
|
5
5
|
import { KeyValueStorageInterface } from '../types';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export class KeyValueStorage implements KeyValueStorageInterface {
|
|
8
11
|
storage?: Storage;
|
|
9
12
|
|
|
10
|
-
constructor() {
|
|
11
|
-
|
|
12
|
-
try {
|
|
13
|
-
this.storage = window?.sessionStorage;
|
|
14
|
-
} catch (error) {}
|
|
15
|
-
}
|
|
13
|
+
constructor(storage?: Storage) {
|
|
14
|
+
this.storage = storage;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
/**
|
|
@@ -21,7 +20,7 @@ class SessionStorageClass implements KeyValueStorageInterface {
|
|
|
21
20
|
* @param {object} value - the value
|
|
22
21
|
* @returns {string} value that was set
|
|
23
22
|
*/
|
|
24
|
-
async setItem(key: string, value: string)
|
|
23
|
+
async setItem(key: string, value: string) {
|
|
25
24
|
if (!this.storage) throw PlatformNotSupportedError;
|
|
26
25
|
this.storage.setItem(key, value);
|
|
27
26
|
}
|
|
@@ -32,7 +31,7 @@ class SessionStorageClass implements KeyValueStorageInterface {
|
|
|
32
31
|
* This is used to clear the storage
|
|
33
32
|
* @returns {string} the data item
|
|
34
33
|
*/
|
|
35
|
-
async getItem(key: string)
|
|
34
|
+
async getItem(key: string) {
|
|
36
35
|
if (!this.storage) throw PlatformNotSupportedError;
|
|
37
36
|
return this.storage.getItem(key);
|
|
38
37
|
}
|
|
@@ -42,7 +41,7 @@ class SessionStorageClass implements KeyValueStorageInterface {
|
|
|
42
41
|
* @param {string} key - the key being set
|
|
43
42
|
* @returns {string} value - value that was deleted
|
|
44
43
|
*/
|
|
45
|
-
async removeItem(key: string)
|
|
44
|
+
async removeItem(key: string) {
|
|
46
45
|
if (!this.storage) throw PlatformNotSupportedError;
|
|
47
46
|
this.storage.removeItem(key);
|
|
48
47
|
}
|
|
@@ -51,10 +50,8 @@ class SessionStorageClass implements KeyValueStorageInterface {
|
|
|
51
50
|
* This is used to clear the storage
|
|
52
51
|
* @returns {string} nothing
|
|
53
52
|
*/
|
|
54
|
-
async clear()
|
|
53
|
+
async clear() {
|
|
55
54
|
if (!this.storage) throw PlatformNotSupportedError;
|
|
56
55
|
this.storage.clear();
|
|
57
56
|
}
|
|
58
57
|
}
|
|
59
|
-
|
|
60
|
-
export const SessionStorage = new SessionStorageClass();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { KeyValueStorage } from './KeyValueStorage';
|
|
5
|
+
import { getSessionStorageWithFallback } from './utils';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export class SessionStorage extends KeyValueStorage {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(getSessionStorageWithFallback());
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { DefaultStorage } from './DefaultStorage';
|
|
5
|
+
import { InMemoryStorage } from './InMemoryStorage';
|
|
6
|
+
import { KeyValueStorage } from './KeyValueStorage';
|
|
7
|
+
import { SessionStorage } from './SessionStorage';
|
|
8
|
+
|
|
9
|
+
export { CookieStorage } from './CookieStorage';
|
|
10
|
+
|
|
11
|
+
export const defaultStorage = new DefaultStorage();
|
|
12
|
+
export const sessionStorage = new SessionStorage();
|
|
13
|
+
export const sharedInMemoryStorage = new KeyValueStorage(new InMemoryStorage());
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { InMemoryStorage } from './InMemoryStorage';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
* @returns Either a reference to window.localStorage or an in-memory storage as fallback
|
|
9
|
+
*/
|
|
10
|
+
export const getDefaultStorageWithFallback = (): Storage =>
|
|
11
|
+
typeof window !== 'undefined' && window.localStorage
|
|
12
|
+
? window.localStorage
|
|
13
|
+
: new InMemoryStorage();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @internal
|
|
17
|
+
* @returns Either a reference to window.sessionStorage or an in-memory storage as fallback
|
|
18
|
+
*/
|
|
19
|
+
export const getSessionStorageWithFallback = (): Storage =>
|
|
20
|
+
typeof window !== 'undefined' && window.sessionStorage
|
|
21
|
+
? window.sessionStorage
|
|
22
|
+
: new InMemoryStorage();
|
package/src/types/storage.ts
CHANGED