Skip to content

Commit

Permalink
chore(aws-amplify): export parseAmplifyConfig (#12669)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF authored Dec 7, 2023
1 parent 0aed4ba commit f21f8f1
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions packages/aws-amplify/__tests__/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('aws-amplify Exports', () => {
'ServiceWorker',
'CookieStorage',
'defaultStorage',
'parseAmplifyConfig',
'sessionStorage',
'sharedInMemoryStorage',
].sort()
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-amplify/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 85,
functions: 44,
functions: 66,
lines: 90,
statements: 91,
},
Expand Down
22 changes: 11 additions & 11 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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)",
Expand All @@ -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"
}
]
}
2 changes: 2 additions & 0 deletions packages/aws-amplify/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export {
sharedInMemoryStorage,
KeyValueStorageInterface,
} from '@aws-amplify/core';

export { parseAWSExports as parseAmplifyConfig } from '@aws-amplify/core/internals/utils';
18 changes: 18 additions & 0 deletions packages/core/__tests__/parseAWSExports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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.'
);
});
});
19 changes: 14 additions & 5 deletions packages/core/src/parseAWSExports.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -21,23 +22,31 @@ const authTypeMapping: Record<any, any> = {
};

/**
* 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<string, any> = {}
): 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,
Expand Down
1 change: 1 addition & 0 deletions packages/geo/__tests__/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const credentials = {
};

export const awsConfig = {
aws_project_region: 'us-east-2',
geo: {
amazon_location_service: {
maps: {
Expand Down

0 comments on commit f21f8f1

Please sign in to comment.