From 5395f613d0f5e3afeceb08fb12bc00951fb8d73b Mon Sep 17 00:00:00 2001 From: elorzafe Date: Tue, 9 Apr 2024 16:10:32 -0700 Subject: [PATCH] Config: relax types to fix issues with TypeScript --- packages/core/__tests__/amplify_outputs.json | 72 ++++++++++++ .../__tests__/parseAmplifyOutputs.test.ts | 107 +++++++++++++++++- packages/core/src/parseAmplifyOutputs.ts | 37 ++---- .../src/singleton/AmplifyOutputs/types.ts | 39 ++----- 4 files changed, 197 insertions(+), 58 deletions(-) create mode 100644 packages/core/__tests__/amplify_outputs.json diff --git a/packages/core/__tests__/amplify_outputs.json b/packages/core/__tests__/amplify_outputs.json new file mode 100644 index 00000000000..3ab7633d1a7 --- /dev/null +++ b/packages/core/__tests__/amplify_outputs.json @@ -0,0 +1,72 @@ +{ + "$schema": "adipisicing cillum", + "version": "1", + "auth": { + "aws_region": "non proident exercitation anim fugiat", + "user_pool_id": "sit velit dolor magna est", + "user_pool_client_id": "voluptate", + "identity_pool_id": "Lorem", + "oauth": { + "identity_providers": ["FACEBOOK", "SIGN_IN_WITH_APPLE", "GOOGLE"], + "domain": "proident dolore do mollit ad", + "scopes": ["incididunt proident"], + "redirect_sign_in_uri": ["Duis", "ipsum velit in dolore"], + "redirect_sign_out_uri": [ + "Excepteur pariatur cillum officia", + "incididunt in Ut Excepteur commodo" + ], + "response_type": "token" + }, + "standard_required_attributes": [ + "address", + "locale", + "family_name", + "sub", + "email" + ], + "username_attributes": ["phone_number", "email"], + "user_verification_types": ["email", "email"], + "unauthenticated_identities_enabled": true, + "mfa_configuration": "OPTIONAL", + "mfa_methods": ["TOTP", "TOTP", "SMS", "TOTP", "TOTP"] + }, + "data": { + "aws_region": "regasd", + "url": "dolore dolor do cillum nulla", + "api_key": "non", + "default_authorization_type": "API_KEY", + "authorization_types": [] + }, + "geo": { + "aws_region": "tempor", + "search_indices": { + "items": [ + "commodo Lorem", + "reprehenderit consequat", + "amet", + "aliquip deserunt", + "ea dolor in proident" + ], + "default": "exercitation fugiat ut dolor sed" + }, + "geofence_collections": { + "items": [ + "fugiat ea irure dolor", + "Ut", + "culpa ut enim exercitation", + "labore", + "ex pariatur est ullamco" + ], + "default": "ullamco incididunt aliquip" + } + }, + "custom": { + "occaecat_4_": -51806024, + "dolorc": 87599986 + }, + "notifications": { + "aws_region": "labore nisi ad", + "amazon_pinpoint_app_id": "in dolor veniam reprehenderit", + "channels": ["EMAIL"] + } +} diff --git a/packages/core/__tests__/parseAmplifyOutputs.test.ts b/packages/core/__tests__/parseAmplifyOutputs.test.ts index d226874a493..39d2012565b 100644 --- a/packages/core/__tests__/parseAmplifyOutputs.test.ts +++ b/packages/core/__tests__/parseAmplifyOutputs.test.ts @@ -1,9 +1,106 @@ import { AmplifyOutputs, parseAmplifyOutputs } from '../src/libraryUtils'; - +import amplifyOutputs from './amplify_outputs.json'; describe('parseAmplifyOutputs tests', () => { describe('auth tests', () => { + + it('should parse from amplify-outputs.json', async () => { + const result = parseAmplifyOutputs(amplifyOutputs); + + expect(result).toEqual({ + "API": { + "GraphQL": { + "apiKey": "non", + "defaultAuthMode": "apiKey", + "endpoint": "dolore dolor do cillum nulla", + "modelIntrospection": undefined, + "region": "regasd", + }, + }, + "Auth": { + "Cognito": { + "allowGuestAccess": true, + "identityPoolId": "Lorem", + "loginWith": { + "email": true, + "oauth": { + "domain": "proident dolore do mollit ad", + "providers": [ + "Facebook", + "Apple", + "Google", + ], + "redirectSignIn": [ + "Duis", + "ipsum velit in dolore", + ], + "redirectSignOut": [ + "Excepteur pariatur cillum officia", + "incididunt in Ut Excepteur commodo", + ], + "responseType": "token", + "scopes": [ + "incididunt proident", + ], + }, + "phone": true, + }, + "mfa": { + "smsEnabled": true, + "status": "optional", + "totpEnabled": true, + }, + "userAttributes": { + "address": { + "required": true, + }, + "email": { + "required": true, + }, + "family_name": { + "required": true, + }, + "locale": { + "required": true, + }, + "sub": { + "required": true, + }, + }, + "userPoolClientId": "voluptate", + "userPoolId": "sit velit dolor magna est", + }, + }, + "Geo": { + "LocationService": { + "geofenceCollections": { + "default": "ullamco incididunt aliquip", + "items": [ + "fugiat ea irure dolor", + "Ut", + "culpa ut enim exercitation", + "labore", + "ex pariatur est ullamco", + ], + }, + "maps": undefined, + "region": "tempor", + "searchIndices": { + "default": "exercitation fugiat ut dolor sed", + "items": [ + "commodo Lorem", + "reprehenderit consequat", + "amet", + "aliquip deserunt", + "ea dolor in proident", + ], + }, + }, + }, + }); + }); + it('should parse auth happy path (all enabled)', () => { - const amplifyOutputs: AmplifyOutputs = { + const amplifyOutputs = { 'version': '1', 'auth': { 'user_pool_id': 'us-east-1:', @@ -26,8 +123,8 @@ describe('parseAmplifyOutputs tests', () => { 'require_numbers': true }, 'standard_required_attributes': ['email'], - 'username_attributes': ['EMAIL'], - 'user_verification_mechanisms': ['EMAIL'], + 'username_attributes': ['email'], + 'user_verification_types': ['email'], 'unauthenticated_identities_enabled': true, 'mfa_configuration': 'OPTIONAL', 'mfa_methods': ['SMS'] @@ -224,7 +321,7 @@ describe('parseAmplifyOutputs tests', () => { 'version': '1', 'notifications': { aws_region: 'us-west-2', - pinpoint_app_id: 'appid123', + amazon_pinpoint_app_id: 'appid123', channels: ['APNS', 'EMAIL', 'FCM', 'IN_APP_MESSAGING', 'SMS'], } }; diff --git a/packages/core/src/parseAmplifyOutputs.ts b/packages/core/src/parseAmplifyOutputs.ts index badd70aef02..09c62e5d4c7 100644 --- a/packages/core/src/parseAmplifyOutputs.ts +++ b/packages/core/src/parseAmplifyOutputs.ts @@ -20,14 +20,11 @@ import { NotificationsConfig } from './singleton/Notifications/types'; import { AmplifyOutputs, AmplifyOutputsAnalyticsProperties, - AmplifyOutputsAuthMFAConfiguration, AmplifyOutputsAuthProperties, AmplifyOutputsDataProperties, AmplifyOutputsGeoProperties, AmplifyOutputsNotificationsProperties, - AmplifyOutputsOAuthIdentityProvider, AmplifyOutputsStorageProperties, - AuthType, } from './singleton/AmplifyOutputs/types'; import { AnalyticsConfig, @@ -130,34 +127,27 @@ function parseAuth( domain: oauth.domain, redirectSignIn: oauth.redirect_sign_in_uri, redirectSignOut: oauth.redirect_sign_out_uri, - responseType: oauth.response_type, + responseType: oauth.response_type === 'token' ? 'token' : 'code', scopes: oauth.scopes, providers: getOAuthProviders(oauth.identity_providers), }, }; } - if (username_attributes?.includes('EMAIL')) { + if (username_attributes?.includes('email')) { authConfig.Cognito.loginWith = { ...authConfig.Cognito.loginWith, email: true, }; } - if (username_attributes?.includes('PHONE_NUMBER')) { + if (username_attributes?.includes('phone_number')) { authConfig.Cognito.loginWith = { ...authConfig.Cognito.loginWith, phone: true, }; } - if (username_attributes?.includes('USERNAME')) { - authConfig.Cognito.loginWith = { - ...authConfig.Cognito.loginWith, - username: true, - }; - } - if (standard_required_attributes) { authConfig.Cognito.userAttributes = standard_required_attributes.reduce( (acc, curr) => ({ ...acc, [curr]: { required: true } }), @@ -240,7 +230,7 @@ function parseNotifications( return undefined; } - const { aws_region, channels, pinpoint_app_id } = + const { aws_region, channels, amazon_pinpoint_app_id } = amplifyOutputsNotificationsProperties; const hasInAppMessaging = channels.includes('IN_APP_MESSAGING'); @@ -257,7 +247,7 @@ function parseNotifications( if (hasInAppMessaging) { notificationsConfig.InAppMessaging = { Pinpoint: { - appId: pinpoint_app_id, + appId: amazon_pinpoint_app_id, region: aws_region, }, }; @@ -266,7 +256,7 @@ function parseNotifications( if (hasPushNotification) { notificationsConfig.PushNotification = { Pinpoint: { - appId: pinpoint_app_id, + appId: amazon_pinpoint_app_id, region: aws_region, }, }; @@ -309,7 +299,7 @@ export function parseAmplifyOutputs( return resourcesConfig; } -const authModeNames: Record = { +const authModeNames: Record = { AMAZON_COGNITO_USER_POOLS: 'userPool', API_KEY: 'apiKey', AWS_IAM: 'iam', @@ -317,28 +307,23 @@ const authModeNames: Record = { OPENID_CONNECT: 'oidc', }; -function getGraphQLAuthMode(authType: AuthType): GraphQLAuthMode { +function getGraphQLAuthMode(authType: string): GraphQLAuthMode { return authModeNames[authType]; } -const providerNames: Record< - AmplifyOutputsOAuthIdentityProvider, - OAuthProvider -> = { +const providerNames: Record = { GOOGLE: 'Google', LOGIN_WITH_AMAZON: 'Amazon', FACEBOOK: 'Facebook', SIGN_IN_WITH_APPLE: 'Apple', }; -function getOAuthProviders( - providers: AmplifyOutputsOAuthIdentityProvider[] = [], -): OAuthProvider[] { +function getOAuthProviders(providers: string[] = []): OAuthProvider[] { return providers.map(provider => providerNames[provider]); } function getMfaStatus( - mfaConfiguration: AmplifyOutputsAuthMFAConfiguration, + mfaConfiguration: string, ): CognitoUserPoolConfigMfaStatus { if (mfaConfiguration === 'OPTIONAL') return 'optional'; if (mfaConfiguration === 'REQUIRED') return 'on'; diff --git a/packages/core/src/singleton/AmplifyOutputs/types.ts b/packages/core/src/singleton/AmplifyOutputs/types.ts index c28b2c53513..d3476f87723 100644 --- a/packages/core/src/singleton/AmplifyOutputs/types.ts +++ b/packages/core/src/singleton/AmplifyOutputs/types.ts @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 import { ModelIntrospectionSchema } from '../API/types'; -import { AuthStandardAttributeKey } from '../Auth/types'; export type AmplifyOutputsOAuthIdentityProvider = | 'GOOGLE' @@ -10,13 +9,6 @@ export type AmplifyOutputsOAuthIdentityProvider = | 'LOGIN_WITH_AMAZON' | 'SIGN_IN_WITH_APPLE'; -type AmplifyOutputsAuthUsernameAttribute = - | 'EMAIL' - | 'PHONE_NUMBER' - | 'USERNAME'; - -type AmplifyOutputsAuthUserVerificationMethod = 'EMAIL' | 'PHONE_NUMBER'; - export type AmplifyOutputsAuthMFAConfiguration = | 'OPTIONAL' | 'REQUIRED' @@ -38,19 +30,19 @@ export interface AmplifyOutputsAuthProperties { require_symbols: boolean; }; oauth?: { - identity_providers: AmplifyOutputsOAuthIdentityProvider[]; + identity_providers: string[]; domain: string; scopes: string[]; redirect_sign_in_uri: string[]; redirect_sign_out_uri: string[]; - response_type: 'code' | 'token'; + response_type: string; }; - standard_required_attributes?: AuthStandardAttributeKey[]; - username_attributes?: AmplifyOutputsAuthUsernameAttribute[]; - user_verification_mechanisms?: AmplifyOutputsAuthUserVerificationMethod[]; + standard_required_attributes?: string[]; + username_attributes?: string[]; + user_verification_types?: string[]; unauthenticated_identities_enabled?: boolean; - mfa_configuration?: AmplifyOutputsAuthMFAConfiguration; - mfa_methods?: AmplifyOutputsAuthMFAMethod[]; + mfa_configuration?: string; + mfa_methods?: string[]; } export interface AmplifyOutputsStorageProperties { @@ -85,24 +77,17 @@ export type AuthType = export interface AmplifyOutputsDataProperties { aws_region: string; url: string; - default_authorization_type: AuthType; - authorization_types: AuthType[]; + default_authorization_type: string; + authorization_types: string[]; model_introspection?: ModelIntrospectionSchema; api_key?: string; - conflict_resolution_mode?: 'AUTO_MERGE' | 'OPTIMISTIC_CONCURRENCY' | 'LAMBDA'; + conflict_resolution_mode?: string; } -type AmplifyOutputsNotificationChannel = - | 'IN_APP_MESSAGING' - | 'FCM' - | 'APNS' - | 'EMAIL' - | 'SMS'; - export interface AmplifyOutputsNotificationsProperties { aws_region: string; - pinpoint_app_id: string; - channels: AmplifyOutputsNotificationChannel[]; + amazon_pinpoint_app_id: string; + channels: string[]; } export interface AmplifyOutputs {