Skip to content

Commit

Permalink
chore(core|aws-amplify): export parseAmplifyConfig util function
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Dec 5, 2023
1 parent 829723b commit 887cd52
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
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: 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: 'InvalidInputForParseAmplifyConfig',
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

0 comments on commit 887cd52

Please sign in to comment.