diff --git a/packages/adapter-nextjs/__tests__/api/generateServerClient.test.ts b/packages/adapter-nextjs/__tests__/api/generateServerClient.test.ts index dfc038de44a..3cd577d8eaa 100644 --- a/packages/adapter-nextjs/__tests__/api/generateServerClient.test.ts +++ b/packages/adapter-nextjs/__tests__/api/generateServerClient.test.ts @@ -1,10 +1,10 @@ import { ResourcesConfig } from '@aws-amplify/core'; +import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils'; import { generateServerClientUsingCookies, generateServerClientUsingReqRes, } from '../../src/api'; import { - getAmplifyConfig, createRunWithAmplifyServerContext, } from '../../src/utils'; import { NextApiRequestMock, NextApiResponseMock } from '../mocks/headers'; @@ -33,13 +33,16 @@ const mockAmplifyConfig: ResourcesConfig = { jest.mock('../../src/utils', () => ({ createRunWithAmplifyServerContext: jest.fn(() => jest.fn()), - getAmplifyConfig: jest.fn(() => mockAmplifyConfig), createCookieStorageAdapterFromNextServerContext: jest.fn(), })); +jest.mock('@aws-amplify/core/internals/utils', () => ({ + ...jest.requireActual('@aws-amplify/core/internals/utils'), + parseAmplifyConfig: jest.fn(() => mockAmplifyConfig), +})); jest.mock('aws-amplify/adapter-core'); -const mockGetAmplifyConfig = getAmplifyConfig as jest.Mock; +const mockParseAmplifyConfig = parseAmplifyConfig as jest.Mock; const mockCreateRunWithAmplifyServerContext = createRunWithAmplifyServerContext as jest.Mock; @@ -76,7 +79,7 @@ describe('generateServerClient', () => { it('should call getAmlifyConfig', async () => { generateServerClientUsingReqRes({ config: mockAmplifyConfig }); - expect(mockGetAmplifyConfig).toHaveBeenCalled(); + expect(mockParseAmplifyConfig).toHaveBeenCalled(); }); // TODO: figure out proper mocks and unskip diff --git a/packages/adapter-nextjs/__tests__/createServerRunner.test.ts b/packages/adapter-nextjs/__tests__/createServerRunner.test.ts index 17ff383720f..3b67894077b 100644 --- a/packages/adapter-nextjs/__tests__/createServerRunner.test.ts +++ b/packages/adapter-nextjs/__tests__/createServerRunner.test.ts @@ -30,7 +30,7 @@ jest.mock( describe('createServerRunner', () => { let createServerRunner: any; - const mockParseAWSExports = jest.fn(); + const mockParseAmplifyConfig = jest.fn(); const mockCreateAWSCredentialsAndIdentityIdProvider = jest.fn(); const mockCreateKeyValueStorageFromCookieStorageAdapter = jest.fn(); const mockCreateUserPoolsTokenProvider = jest.fn(); @@ -47,23 +47,23 @@ describe('createServerRunner', () => { runWithAmplifyServerContext: mockRunWithAmplifyServerContextCore, })); jest.doMock('@aws-amplify/core/internals/utils', () => ({ - parseAWSExports: mockParseAWSExports, + parseAmplifyConfig: mockParseAmplifyConfig, })); createServerRunner = require('../src').createServerRunner; }); afterEach(() => { - mockParseAWSExports.mockClear(); + mockParseAmplifyConfig.mockClear(); mockCreateAWSCredentialsAndIdentityIdProvider.mockClear(); mockCreateKeyValueStorageFromCookieStorageAdapter.mockClear(); mockCreateUserPoolsTokenProvider.mockClear(); mockRunWithAmplifyServerContextCore.mockClear(); }); - it('calls parseAWSExports when the config object is imported from amplify configuration file', () => { + it('calls parseAmplifyConfig when the config object is imported from amplify configuration file', () => { createServerRunner({ config: { aws_project_region: 'us-west-2' } }); - expect(mockParseAWSExports).toHaveBeenCalled(); + expect(mockParseAmplifyConfig).toHaveBeenCalled(); }); it('returns runWithAmplifyServerContext function', () => { @@ -76,7 +76,7 @@ describe('createServerRunner', () => { describe('runWithAmplifyServerContext', () => { describe('when amplifyConfig.Auth is not defined', () => { it('should call runWithAmplifyServerContextCore without Auth library options', () => { - const mockAmplifyConfig: ResourcesConfig = { + const mockAmplifyConfigAnalytics: ResourcesConfig = { Analytics: { Pinpoint: { appId: 'app-id', @@ -84,13 +84,16 @@ describe('createServerRunner', () => { }, }, }; + + mockParseAmplifyConfig.mockReturnValue(mockAmplifyConfigAnalytics); + const { runWithAmplifyServerContext } = createServerRunner({ - config: mockAmplifyConfig, + config: mockAmplifyConfigAnalytics, }); const operation = jest.fn(); runWithAmplifyServerContext({ operation, nextServerContext: null }); expect(mockRunWithAmplifyServerContextCore).toHaveBeenCalledWith( - mockAmplifyConfig, + mockAmplifyConfigAnalytics, {}, operation, ); @@ -98,6 +101,10 @@ describe('createServerRunner', () => { }); describe('when amplifyConfig.Auth is defined', () => { + beforeEach(() => { + mockParseAmplifyConfig.mockReturnValue(mockAmplifyConfig); + }) + describe('when nextServerContext is null (opt-in unauthenticated role)', () => { it('should create auth providers with sharedInMemoryStorage', () => { const { runWithAmplifyServerContext } = createServerRunner({ diff --git a/packages/adapter-nextjs/__tests__/utils/getAmplifyConfig.test.ts b/packages/adapter-nextjs/__tests__/utils/getAmplifyConfig.test.ts deleted file mode 100644 index 14532cec5b3..00000000000 --- a/packages/adapter-nextjs/__tests__/utils/getAmplifyConfig.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -import { getAmplifyConfig } from '../../src/utils/getAmplifyConfig'; - -describe('getAmplifyConfig', () => { - const mockLegacyConfig = { - aws_project_region: 'us-west-2', - aws_cognito_identity_pool_id: '123', - aws_cognito_region: 'aws_cognito_region', - aws_user_pools_id: 'abc', - aws_user_pools_web_client_id: 'def', - oauth: {}, - aws_cognito_username_attributes: [], - aws_cognito_social_providers: [], - aws_cognito_signup_attributes: [], - aws_cognito_mfa_configuration: 'OFF', - aws_cognito_mfa_types: ['SMS'], - aws_cognito_password_protection_settings: { - passwordPolicyMinLength: 8, - passwordPolicyCharacters: [], - }, - aws_cognito_verification_mechanisms: ['PHONE_NUMBER'], - aws_user_files_s3_bucket: 'bucket', - aws_user_files_s3_bucket_region: 'us-east-1', - }; - const mockAmplifyConfig = { - Auth: { - Cognito: { - identityPoolId: '123', - userPoolId: 'abc', - userPoolClientId: 'def', - }, - }, - Storage: { - S3: { - bucket: 'bucket', - region: 'us-east-1', - }, - }, - }; - - it('returns config object that conforms to ResourcesConfig', () => { - expect(getAmplifyConfig(mockLegacyConfig)).toMatchObject(mockAmplifyConfig); - }); -}); diff --git a/packages/adapter-nextjs/src/api/createServerRunnerForAPI.ts b/packages/adapter-nextjs/src/api/createServerRunnerForAPI.ts index 94434a8a481..3c5ae6ad97a 100644 --- a/packages/adapter-nextjs/src/api/createServerRunnerForAPI.ts +++ b/packages/adapter-nextjs/src/api/createServerRunnerForAPI.ts @@ -2,8 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 import { ResourcesConfig } from '@aws-amplify/core'; +import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils'; -import { createRunWithAmplifyServerContext, getAmplifyConfig } from '../utils'; +import { createRunWithAmplifyServerContext } from '../utils'; import { NextServer } from '../types'; export const createServerRunnerForAPI = ({ @@ -11,7 +12,7 @@ export const createServerRunnerForAPI = ({ }: NextServer.CreateServerRunnerInput): NextServer.CreateServerRunnerOutput & { resourcesConfig: ResourcesConfig; } => { - const amplifyConfig = getAmplifyConfig(config); + const amplifyConfig = parseAmplifyConfig(config); return { runWithAmplifyServerContext: createRunWithAmplifyServerContext({ diff --git a/packages/adapter-nextjs/src/api/generateServerClient.ts b/packages/adapter-nextjs/src/api/generateServerClient.ts index 7d8723fd716..e1c5ab09816 100644 --- a/packages/adapter-nextjs/src/api/generateServerClient.ts +++ b/packages/adapter-nextjs/src/api/generateServerClient.ts @@ -11,10 +11,12 @@ import { V6ClientSSRCookies, V6ClientSSRRequest, } from '@aws-amplify/api-graphql'; -import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils'; +import { + GraphQLAuthMode, + parseAmplifyConfig, +} from '@aws-amplify/core/internals/utils'; import { NextServer } from '../types'; -import { getAmplifyConfig } from '../utils'; import { createServerRunnerForAPI } from './createServerRunnerForAPI'; @@ -98,7 +100,7 @@ export function generateServerClientUsingCookies< export function generateServerClientUsingReqRes< T extends Record = never, >({ config, authMode, authToken }: ReqClientParams): V6ClientSSRRequest { - const amplifyConfig = getAmplifyConfig(config); + const amplifyConfig = parseAmplifyConfig(config); return generateClient({ config: amplifyConfig, diff --git a/packages/adapter-nextjs/src/createServerRunner.ts b/packages/adapter-nextjs/src/createServerRunner.ts index 78d419af99b..576356fba3e 100644 --- a/packages/adapter-nextjs/src/createServerRunner.ts +++ b/packages/adapter-nextjs/src/createServerRunner.ts @@ -2,8 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 import { ResourcesConfig } from 'aws-amplify'; +import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils'; -import { createRunWithAmplifyServerContext, getAmplifyConfig } from './utils'; +import { createRunWithAmplifyServerContext } from './utils'; import { NextServer } from './types'; /** @@ -27,7 +28,7 @@ import { NextServer } from './types'; export const createServerRunner: NextServer.CreateServerRunner = ({ config, }) => { - const amplifyConfig = getAmplifyConfig(config); + const amplifyConfig = parseAmplifyConfig(config); return { runWithAmplifyServerContext: createRunWithAmplifyServerContext({ diff --git a/packages/adapter-nextjs/src/types/NextServer.ts b/packages/adapter-nextjs/src/types/NextServer.ts index 107bab823e7..5c3d093b795 100644 --- a/packages/adapter-nextjs/src/types/NextServer.ts +++ b/packages/adapter-nextjs/src/types/NextServer.ts @@ -4,7 +4,7 @@ import { GetServerSidePropsContext as NextGetServerSidePropsContext } from 'next'; import { NextRequest, NextResponse } from 'next/server.js'; import { cookies } from 'next/headers.js'; -import { LegacyConfig } from 'aws-amplify/adapter-core'; +import { AmplifyOutputs, LegacyConfig } from 'aws-amplify/adapter-core'; import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core'; import { ResourcesConfig } from '@aws-amplify/core'; @@ -74,7 +74,7 @@ export declare namespace NextServer { ) => Promise; export interface CreateServerRunnerInput { - config: ResourcesConfig | LegacyConfig; + config: ResourcesConfig | LegacyConfig | AmplifyOutputs; } export interface CreateServerRunnerOutput { diff --git a/packages/adapter-nextjs/src/utils/getAmplifyConfig.ts b/packages/adapter-nextjs/src/utils/getAmplifyConfig.ts deleted file mode 100644 index a8ab9f1d22b..00000000000 --- a/packages/adapter-nextjs/src/utils/getAmplifyConfig.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -import { ResourcesConfig } from 'aws-amplify'; -import { LegacyConfig } from 'aws-amplify/adapter-core'; -import { parseAWSExports } from '@aws-amplify/core/internals/utils'; - -export const getAmplifyConfig = ( - config: ResourcesConfig | LegacyConfig, -): ResourcesConfig => - Object.keys(config).some(key => key.startsWith('aws_')) - ? parseAWSExports(config) - : (config as ResourcesConfig); diff --git a/packages/adapter-nextjs/src/utils/index.ts b/packages/adapter-nextjs/src/utils/index.ts index e5fd9bb5f87..68ab6cdf55c 100644 --- a/packages/adapter-nextjs/src/utils/index.ts +++ b/packages/adapter-nextjs/src/utils/index.ts @@ -1,5 +1,4 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -export { getAmplifyConfig } from './getAmplifyConfig'; export { createRunWithAmplifyServerContext } from './createRunWithAmplifyServerContext'; diff --git a/packages/aws-amplify/jest.config.js b/packages/aws-amplify/jest.config.js index 7365a413e7c..5254f524623 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: 66, + functions: 65.5, lines: 90, statements: 91, }, diff --git a/packages/aws-amplify/package.json b/packages/aws-amplify/package.json index 8706c23c601..df7111b459f 100644 --- a/packages/aws-amplify/package.json +++ b/packages/aws-amplify/package.json @@ -293,31 +293,31 @@ "name": "[Analytics] record (Pinpoint)", "path": "./dist/esm/analytics/index.mjs", "import": "{ record }", - "limit": "16.50 kB" + "limit": "17.02 kB" }, { "name": "[Analytics] record (Kinesis)", "path": "./dist/esm/analytics/kinesis/index.mjs", "import": "{ record }", - "limit": "45.50 kB" + "limit": "48.56 kB" }, { "name": "[Analytics] record (Kinesis Firehose)", "path": "./dist/esm/analytics/kinesis-firehose/index.mjs", "import": "{ record }", - "limit": "42.50 kB" + "limit": "45.68 kB" }, { "name": "[Analytics] record (Personalize)", "path": "./dist/esm/analytics/personalize/index.mjs", "import": "{ record }", - "limit": "46.50 kB" + "limit": "49.50 kB" }, { "name": "[Analytics] identifyUser (Pinpoint)", "path": "./dist/esm/analytics/index.mjs", "import": "{ identifyUser }", - "limit": "15.00 kB" + "limit": "15.52 kB" }, { "name": "[Analytics] enable", @@ -335,7 +335,7 @@ "name": "[API] generateClient (AppSync)", "path": "./dist/esm/api/index.mjs", "import": "{ generateClient }", - "limit": "38.00 kB" + "limit": "38.58 kB" }, { "name": "[API] REST API handlers", @@ -353,13 +353,13 @@ "name": "[Auth] resetPassword (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ resetPassword }", - "limit": "9.02 kB" + "limit": "12.44 kB" }, { "name": "[Auth] confirmResetPassword (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ confirmResetPassword }", - "limit": "9.00 kB" + "limit": "12.39 kB" }, { "name": "[Auth] signIn (Cognito)", @@ -371,7 +371,7 @@ "name": "[Auth] resendSignUpCode (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ resendSignUpCode }", - "limit": "9.00 kB" + "limit": "12.40 kB" }, { "name": "[Auth] confirmSignUp (Cognito)", @@ -383,31 +383,31 @@ "name": "[Auth] confirmSignIn (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ confirmSignIn }", - "limit": "26.40 kB" + "limit": "27.63 kB" }, { "name": "[Auth] updateMFAPreference (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ updateMFAPreference }", - "limit": "8.6 kB" + "limit": "11.74 kB" }, { "name": "[Auth] fetchMFAPreference (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ fetchMFAPreference }", - "limit": "8.35 kB" + "limit": "11.78 kB" }, { "name": "[Auth] verifyTOTPSetup (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ verifyTOTPSetup }", - "limit": "9.18 kB" + "limit": "12.59 kB" }, { "name": "[Auth] updatePassword (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ updatePassword }", - "limit": "9.19 kB" + "limit": "12.63 kB" }, { "name": "[Auth] setUpTOTP (Cognito)", @@ -419,85 +419,85 @@ "name": "[Auth] updateUserAttributes (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ updateUserAttributes }", - "limit": "8.46 kB" + "limit": "11.87 kB" }, { "name": "[Auth] getCurrentUser (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ getCurrentUser }", - "limit": "4.32 kB" + "limit": "7.75 kB" }, { "name": "[Auth] confirmUserAttribute (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ confirmUserAttribute }", - "limit": "9.19 kB" + "limit": "12.61 kB" }, { "name": "[Auth] signInWithRedirect (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ signInWithRedirect }", - "limit": "19.40 kB" + "limit": "20.98 kB" }, { "name": "[Auth] fetchUserAttributes (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ fetchUserAttributes }", - "limit": "8.27 kB" + "limit": "11.69 kB" }, { "name": "[Auth] Basic Auth Flow (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ signIn, signOut, fetchAuthSession, confirmSignIn }", - "limit": "28.50 kB" + "limit": "29.83 kB" }, { "name": "[Auth] OAuth Auth Flow (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ signInWithRedirect, signOut, fetchAuthSession }", - "limit": "19.80 kB" + "limit": "21.42 kB" }, { "name": "[Storage] copy (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ copy }", - "limit": "13.69 kB" + "limit": "14.54 kB" }, { "name": "[Storage] downloadData (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ downloadData }", - "limit": "14.32 kB" + "limit": "15.17 kB" }, { "name": "[Storage] getProperties (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ getProperties }", - "limit": "13.56 kB" + "limit": "14.43 kB" }, { "name": "[Storage] getUrl (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ getUrl }", - "limit": "14.65 kB" + "limit": "15.51 kB" }, { "name": "[Storage] list (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ list }", - "limit": "14.2 kB" + "limit": "14.94 kB" }, { "name": "[Storage] remove (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ remove }", - "limit": "13.50 kB" + "limit": "14.29 kB" }, { "name": "[Storage] uploadData (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ uploadData }", - "limit": "18.79 kB" + "limit": "19.64 kB" } ] } diff --git a/packages/aws-amplify/src/adapterCore/index.ts b/packages/aws-amplify/src/adapterCore/index.ts index 81f099b9353..755f8c12b42 100644 --- a/packages/aws-amplify/src/adapterCore/index.ts +++ b/packages/aws-amplify/src/adapterCore/index.ts @@ -7,7 +7,10 @@ export { createAWSCredentialsAndIdentityIdProvider, createUserPoolsTokenProvider, } from './authProvidersFactories/cognito'; -export { LegacyConfig } from '@aws-amplify/core/internals/utils'; +export { + LegacyConfig, + AmplifyOutputs, +} from '@aws-amplify/core/internals/utils'; export { AmplifyServer, CookieStorage, diff --git a/packages/aws-amplify/src/initSingleton.ts b/packages/aws-amplify/src/initSingleton.ts index fd6d29e2635..b5de7deb56a 100644 --- a/packages/aws-amplify/src/initSingleton.ts +++ b/packages/aws-amplify/src/initSingleton.ts @@ -10,9 +10,7 @@ import { import { AmplifyOutputs, LegacyConfig, - isAmplifyOutputs, - parseAWSExports, - parseAmplifyOutputs, + parseAmplifyConfig, } from '@aws-amplify/core/internals/utils'; import { @@ -37,15 +35,7 @@ export const DefaultAmplify = { resourceConfig: ResourcesConfig | LegacyConfig | AmplifyOutputs, libraryOptions?: LibraryOptions, ): void { - let resolvedResourceConfig: ResourcesConfig; - - if (Object.keys(resourceConfig).some(key => key.startsWith('aws_'))) { - resolvedResourceConfig = parseAWSExports(resourceConfig); - } else if (isAmplifyOutputs(resourceConfig)) { - resolvedResourceConfig = parseAmplifyOutputs(resourceConfig); - } else { - resolvedResourceConfig = resourceConfig as ResourcesConfig; - } + const resolvedResourceConfig = parseAmplifyConfig(resourceConfig); // If no Auth config is provided, no special handling will be required, configure as is. // Otherwise, we can assume an Auth config is provided from here on. diff --git a/packages/aws-amplify/src/utils/index.ts b/packages/aws-amplify/src/utils/index.ts index 52367f4911a..35093e8e864 100644 --- a/packages/aws-amplify/src/utils/index.ts +++ b/packages/aws-amplify/src/utils/index.ts @@ -18,4 +18,4 @@ export { KeyValueStorageInterface, } from '@aws-amplify/core'; -export { parseAWSExports as parseAmplifyConfig } from '@aws-amplify/core/internals/utils'; +export { parseAmplifyConfig } from '@aws-amplify/core/internals/utils'; diff --git a/packages/core/__tests__/utils/parseAmplifyConfig.test.ts b/packages/core/__tests__/utils/parseAmplifyConfig.test.ts new file mode 100644 index 00000000000..2394e0b1918 --- /dev/null +++ b/packages/core/__tests__/utils/parseAmplifyConfig.test.ts @@ -0,0 +1,72 @@ +import { ResourcesConfig } from '../../src'; +import { parseAmplifyConfig } from '../../src/libraryUtils'; +import { parseAWSExports } from '../../src/parseAWSExports'; +import { isAmplifyOutputs, parseAmplifyOutputs } from '../../src/parseAmplifyOutputs'; + +jest.mock('../../src/parseAWSExports'); +jest.mock('../../src/parseAmplifyOutputs'); + +const testAmplifyOutputs = { + 'version': '1', + 'auth': { + 'user_pool_id': 'us-east-1:', + 'user_pool_client_id': 'xxxx', + 'aws_region': 'us-east-1', + }, +} + +const testLegacyConfig = { + aws_project_region: 'us-west-2', + aws_user_pools_id: 'user-pool-id', + aws_user_pools_web_client_id: 'user-pool-client-id' +} + +const testResourcesConfig: ResourcesConfig = { + Auth: { + Cognito: { + userPoolId: 'us-east-1:xxx', + userPoolClientId: 'xxxx', + identityPoolId: 'test' + } + } +}; + +describe('parseAmplifyConfig', () => { + const mockParseAWSExports = parseAWSExports as jest.Mock; + const mockParseAmplifyOutputs = parseAmplifyOutputs as jest.Mock; + const mockIsAmplifyOutputs = isAmplifyOutputs as unknown as jest.Mock; + + beforeEach(() => { + jest.clearAllMocks(); + mockParseAWSExports.mockReturnValue(testResourcesConfig); + mockParseAmplifyOutputs.mockReturnValue(testResourcesConfig); + mockIsAmplifyOutputs.mockReturnValue(false); + }); + + it('returns a ResourceConfig when one is provided', () => { + const parsedConfig = parseAmplifyConfig(testResourcesConfig); + + // Verify that a provided ResourceConfig is returned back unmodified + expect(parsedConfig).toEqual(testResourcesConfig) + }); + + it('parses legacy config objects into ResourcesConfig', () => { + const parsedConfig = parseAmplifyConfig(testLegacyConfig); + + // Verify that a provided legacy config is parsed into a ResourcesConfig + expect(parsedConfig).toEqual(testResourcesConfig); + expect(mockParseAWSExports).toHaveBeenCalledTimes(1); + expect(mockParseAWSExports).toHaveBeenCalledWith(testLegacyConfig) + }); + + it('parses Gen2 config objects into ResourcesConfig', () => { + mockIsAmplifyOutputs.mockReturnValueOnce(true); + const parsedConfig = parseAmplifyConfig(testAmplifyOutputs); + + // Verify that a provided Gen2 config is parsed into a ResourcesConfig + expect(parsedConfig).toEqual(testResourcesConfig); + expect(mockParseAmplifyOutputs).toHaveBeenCalledTimes(1); + expect(mockIsAmplifyOutputs).toHaveBeenCalledTimes(1); + expect(mockParseAmplifyOutputs).toHaveBeenCalledWith(testAmplifyOutputs); + }); +}) \ No newline at end of file diff --git a/packages/core/src/libraryUtils.ts b/packages/core/src/libraryUtils.ts index 4e1f576834e..df38e8b3476 100644 --- a/packages/core/src/libraryUtils.ts +++ b/packages/core/src/libraryUtils.ts @@ -27,6 +27,7 @@ export { AmplifyOutputs } from './singleton/AmplifyOutputs/types'; export { ADD_OAUTH_LISTENER } from './singleton/constants'; export { amplifyUuid } from './utils/amplifyUuid'; export { AmplifyUrl, AmplifyUrlSearchParams } from './utils/amplifyUrl'; +export { parseAmplifyConfig } from './utils/parseAmplifyConfig'; // Auth utilities export { diff --git a/packages/core/src/parseAmplifyOutputs.ts b/packages/core/src/parseAmplifyOutputs.ts index 09c62e5d4c7..930f36df2de 100644 --- a/packages/core/src/parseAmplifyOutputs.ts +++ b/packages/core/src/parseAmplifyOutputs.ts @@ -11,6 +11,7 @@ import { APIConfig, APIGraphQLConfig, GraphQLAuthMode, + ModelIntrospectionSchema, } from './singleton/API/types'; import { CognitoUserPoolConfigMfaStatus, @@ -215,7 +216,7 @@ function parseData( defaultAuthMode: getGraphQLAuthMode(default_authorization_type), region: aws_region, apiKey: api_key, - modelIntrospection: model_introspection, + modelIntrospection: model_introspection as ModelIntrospectionSchema, }; return { diff --git a/packages/core/src/singleton/Amplify.ts b/packages/core/src/singleton/Amplify.ts index 87611d6c4cd..d3bcd33a2a4 100644 --- a/packages/core/src/singleton/Amplify.ts +++ b/packages/core/src/singleton/Amplify.ts @@ -1,10 +1,11 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { AMPLIFY_SYMBOL, Hub } from '../Hub'; -import { parseAWSExports } from '../parseAWSExports'; import { deepFreeze } from '../utils'; +import { parseAmplifyConfig } from '../libraryUtils'; import { + AmplifyOutputs, AuthConfig, LegacyConfig, LibraryOptions, @@ -48,16 +49,10 @@ export class AmplifyClass { * @param libraryOptions - Additional options for customizing the behavior of the library. */ configure( - resourcesConfig: ResourcesConfig | LegacyConfig, + resourcesConfig: ResourcesConfig | LegacyConfig | AmplifyOutputs, libraryOptions?: LibraryOptions, ): void { - let resolvedResourceConfig: ResourcesConfig; - - if (Object.keys(resourcesConfig).some(key => key.startsWith('aws_'))) { - resolvedResourceConfig = parseAWSExports(resourcesConfig); - } else { - resolvedResourceConfig = resourcesConfig as ResourcesConfig; - } + const resolvedResourceConfig = parseAmplifyConfig(resourcesConfig); this.resourcesConfig = resolvedResourceConfig; diff --git a/packages/core/src/singleton/AmplifyOutputs/types.ts b/packages/core/src/singleton/AmplifyOutputs/types.ts index d3476f87723..a09bf792267 100644 --- a/packages/core/src/singleton/AmplifyOutputs/types.ts +++ b/packages/core/src/singleton/AmplifyOutputs/types.ts @@ -1,8 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { ModelIntrospectionSchema } from '../API/types'; - export type AmplifyOutputsOAuthIdentityProvider = | 'GOOGLE' | 'FACEBOOK' @@ -79,7 +77,7 @@ export interface AmplifyOutputsDataProperties { url: string; default_authorization_type: string; authorization_types: string[]; - model_introspection?: ModelIntrospectionSchema; + model_introspection?: object; api_key?: string; conflict_resolution_mode?: string; } diff --git a/packages/core/src/singleton/types.ts b/packages/core/src/singleton/types.ts index 423ac96d23f..e2acbeb6611 100644 --- a/packages/core/src/singleton/types.ts +++ b/packages/core/src/singleton/types.ts @@ -26,6 +26,8 @@ import { import { NotificationsConfig } from './Notifications/types'; import { InteractionsConfig } from './Interactions/types'; +export { AmplifyOutputs } from './AmplifyOutputs/types'; + /** * Compatibility type representing the Amplify Gen 1 configuration file schema. This type should not be used directly. */ diff --git a/packages/core/src/utils/parseAmplifyConfig.ts b/packages/core/src/utils/parseAmplifyConfig.ts new file mode 100644 index 00000000000..424f71a7102 --- /dev/null +++ b/packages/core/src/utils/parseAmplifyConfig.ts @@ -0,0 +1,26 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { ResourcesConfig } from '../index'; +import { AmplifyOutputs } from '../singleton/AmplifyOutputs/types'; +import { LegacyConfig } from '../singleton/types'; +import { parseAWSExports } from '../parseAWSExports'; +import { isAmplifyOutputs, parseAmplifyOutputs } from '../parseAmplifyOutputs'; + +/** + * Parses the variety of configuration shapes that Amplify can accept into a ResourcesConfig. + * + * @param amplifyConfig An Amplify configuration object conforming to one of the supported schemas. + * @return A ResourcesConfig for the provided configuration object. + */ +export const parseAmplifyConfig = ( + amplifyConfig: ResourcesConfig | LegacyConfig | AmplifyOutputs, +): ResourcesConfig => { + if (Object.keys(amplifyConfig).some(key => key.startsWith('aws_'))) { + return parseAWSExports(amplifyConfig); + } else if (isAmplifyOutputs(amplifyConfig)) { + return parseAmplifyOutputs(amplifyConfig); + } else { + return amplifyConfig as ResourcesConfig; + } +}; diff --git a/packages/interactions/package.json b/packages/interactions/package.json index c2603233939..c756c5b9695 100644 --- a/packages/interactions/package.json +++ b/packages/interactions/package.json @@ -88,19 +88,19 @@ "name": "Interactions (default to Lex v2)", "path": "./dist/esm/index.mjs", "import": "{ Interactions }", - "limit": "52.00 kB" + "limit": "52.52 kB" }, { "name": "Interactions (Lex v2)", "path": "./dist/esm/lex-v2/index.mjs", "import": "{ Interactions }", - "limit": "52.00 kB" + "limit": "52.52 kB" }, { "name": "Interactions (Lex v1)", "path": "./dist/esm/lex-v1/index.mjs", "import": "{ Interactions }", - "limit": "47.00 kB" + "limit": "47.33 kB" } ] }