diff --git a/packages/api-graphql/__tests__/fixtures/modeled/amplifyconfiguration.js b/packages/api-graphql/__tests__/fixtures/modeled/amplifyconfiguration.js index e7a66e357f3..b9262629e6b 100644 --- a/packages/api-graphql/__tests__/fixtures/modeled/amplifyconfiguration.js +++ b/packages/api-graphql/__tests__/fixtures/modeled/amplifyconfiguration.js @@ -4,6 +4,7 @@ * Cognito fields etc. omitted. */ const amplifyConfig = { + aws_project_region: 'us-east-2', aws_appsync_graphqlEndpoint: 'https://localhost/graphql', aws_appsync_region: 'us-west-1', aws_appsync_authenticationType: 'API_KEY', diff --git a/packages/aws-amplify/__tests__/adapterCore/storageFactories/createKeyValueStorageFromCookieStorageAdapter.ts b/packages/aws-amplify/__tests__/adapterCore/storageFactories/createKeyValueStorageFromCookieStorageAdapter.test.ts similarity index 100% rename from packages/aws-amplify/__tests__/adapterCore/storageFactories/createKeyValueStorageFromCookieStorageAdapter.ts rename to packages/aws-amplify/__tests__/adapterCore/storageFactories/createKeyValueStorageFromCookieStorageAdapter.test.ts diff --git a/packages/aws-amplify/__tests__/exports.test.ts b/packages/aws-amplify/__tests__/exports.test.ts index 2732f332d47..4412cdd26ec 100644 --- a/packages/aws-amplify/__tests__/exports.test.ts +++ b/packages/aws-amplify/__tests__/exports.test.ts @@ -39,6 +39,7 @@ describe('aws-amplify Exports', () => { 'ServiceWorker', 'CookieStorage', 'defaultStorage', + 'parseAmplifyConfig', 'sessionStorage', 'sharedInMemoryStorage', ].sort() diff --git a/packages/aws-amplify/jest.config.js b/packages/aws-amplify/jest.config.js index 9f65be3c87a..7365a413e7c 100644 --- a/packages/aws-amplify/jest.config.js +++ b/packages/aws-amplify/jest.config.js @@ -3,7 +3,7 @@ module.exports = { coverageThreshold: { global: { branches: 85, - functions: 44, + functions: 66, lines: 90, statements: 91, }, diff --git a/packages/aws-amplify/package.json b/packages/aws-amplify/package.json index dd6f1baec36..7e1062c5cbc 100644 --- a/packages/aws-amplify/package.json +++ b/packages/aws-amplify/package.json @@ -289,7 +289,7 @@ "name": "[Analytics] record (Kinesis)", "path": "./dist/esm/analytics/kinesis/index.mjs", "import": "{ record }", - "limit": "44.05 kB" + "limit": "44.10 kB" }, { "name": "[Analytics] record (Kinesis Firehose)", @@ -427,7 +427,7 @@ "name": "[Auth] signInWithRedirect (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ signInWithRedirect }", - "limit": "19.24 kB" + "limit": "19.30 kB" }, { "name": "[Auth] fetchUserAttributes (Cognito)", @@ -439,55 +439,55 @@ "name": "[Auth] Basic Auth Flow (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ signIn, signOut, fetchAuthSession, confirmSignIn }", - "limit": "27.44 kB" + "limit": "27.60 kB" }, { "name": "[Auth] OAuth Auth Flow (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ signInWithRedirect, signOut, fetchAuthSession }", - "limit": "19.68 kB" + "limit": "19.80 kB" }, { "name": "[Storage] copy (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ copy }", - "limit": "12.98 kB" + "limit": "13.10 kB" }, { "name": "[Storage] downloadData (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ downloadData }", - "limit": "13.76 kB" + "limit": "13.80 kB" }, { "name": "[Storage] getProperties (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ getProperties }", - "limit": "13.01 kB" + "limit": "13.10 kB" }, { "name": "[Storage] getUrl (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ getUrl }", - "limit": "14.09 kB" + "limit": "14.10 kB" }, { "name": "[Storage] list (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ list }", - "limit": "13.39 kB" + "limit": "13.50 kB" }, { "name": "[Storage] remove (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ remove }", - "limit": "12.82 kB" + "limit": "12.90 kB" }, { "name": "[Storage] uploadData (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ uploadData }", - "limit": "18.09 kB" + "limit": "18.10 kB" } ] } diff --git a/packages/aws-amplify/src/utils/index.ts b/packages/aws-amplify/src/utils/index.ts index 87ff39d973f..52367f4911a 100644 --- a/packages/aws-amplify/src/utils/index.ts +++ b/packages/aws-amplify/src/utils/index.ts @@ -17,3 +17,5 @@ export { sharedInMemoryStorage, KeyValueStorageInterface, } from '@aws-amplify/core'; + +export { parseAWSExports as parseAmplifyConfig } from '@aws-amplify/core/internals/utils'; diff --git a/packages/core/__tests__/parseAWSExports.test.ts b/packages/core/__tests__/parseAWSExports.test.ts index 29ebf893475..9b50376bd38 100644 --- a/packages/core/__tests__/parseAWSExports.test.ts +++ b/packages/core/__tests__/parseAWSExports.test.ts @@ -144,6 +144,7 @@ describe('parseAWSExports', () => { }; expect( parseAWSExports({ + aws_project_region: 'us-west-2', aws_cognito_identity_pool_id: identityPoolId, aws_cognito_sign_up_verification_method: signUpVerificationMethod, aws_cognito_username_attributes: ['PHONE_NUMBER'], @@ -190,6 +191,7 @@ describe('parseAWSExports', () => { it('should fallback to IAM auth mode if Appsync auth type is invalid', () => { expect( parseAWSExports({ + aws_project_region: 'us-west-2', aws_appsync_graphqlEndpoint: appsyncEndpoint, aws_appsync_apiKey: apiKey, aws_appsync_region: region, @@ -210,6 +212,7 @@ describe('parseAWSExports', () => { it('should handle missing `redirectSignIn` or `redirectSignOut` configuration', () => { expect( parseAWSExports({ + aws_project_region: 'us-west-2', aws_user_pools_id: userPoolId, oauth: { domain: oAuthDomain, @@ -244,4 +247,19 @@ describe('parseAWSExports', () => { }, }); }); + + it('should throw an error when passing a `ResourceConfig` object as the config parameter', () => { + const testConfig: ResourcesConfig = { + Auth: { + Cognito: { + userPoolClientId: 'userPoolClientId', + userPoolId: 'userPoolId', + }, + }, + }; + + expect(() => parseAWSExports(testConfig)).toThrow( + 'Invalid config parameter.' + ); + }); }); diff --git a/packages/core/src/parseAWSExports.ts b/packages/core/src/parseAWSExports.ts index 1823cb44275..dcb466ddbbb 100644 --- a/packages/core/src/parseAWSExports.ts +++ b/packages/core/src/parseAWSExports.ts @@ -1,6 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { ConsoleLogger } from './Logger'; +import { AmplifyError } from './errors'; import { AuthConfigUserAttributes, LegacyUserAttributeKey, @@ -21,23 +22,31 @@ const authTypeMapping: Record = { }; /** - * This utility converts the `aws-exports.js` file generated by the Amplify CLI into a {@link ResourcesConfig} object - * consumable by Amplify. + * Converts the object imported from `aws-exports.js` or `amplifyconfiguration.json` files generated by + * the Amplify CLI into an object that conforms to the {@link ResourcesConfig}. * - * @param config A configuration object from `aws-exports.js`. + * @param config A configuration object imported from `aws-exports.js` or `amplifyconfiguration.json`. * - * @returns A {@link ResourcesConfig} object. + * @returns An object that conforms to the {@link ResourcesConfig} . */ export const parseAWSExports = ( config: Record = {} ): ResourcesConfig => { + if (!Object.prototype.hasOwnProperty.call(config, 'aws_project_region')) { + throw new AmplifyError({ + name: 'InvalidParameterException', + message: 'Invalid config parameter.', + recoverySuggestion: + 'Ensure passing the config object imported from `amplifyconfiguration.json`.', + }); + } + const { aws_appsync_apiKey, aws_appsync_authenticationType, aws_appsync_graphqlEndpoint, aws_appsync_region, - aws_bots, aws_bots_config, aws_cognito_identity_pool_id, aws_cognito_sign_up_verification_method, diff --git a/packages/geo/__tests__/testData.ts b/packages/geo/__tests__/testData.ts index 8d6c712b621..e2d243d6cca 100644 --- a/packages/geo/__tests__/testData.ts +++ b/packages/geo/__tests__/testData.ts @@ -22,6 +22,7 @@ export const credentials = { }; export const awsConfig = { + aws_project_region: 'us-east-2', geo: { amazon_location_service: { maps: {