Skip to content

Commit

Permalink
Ensure TS inferred shape from external json does not throw type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwin Kumar committed Dec 27, 2024
1 parent 0ecded1 commit bacf67f
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 2 deletions.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module.exports = {
noImplicitAny: false,
types: ['jest', 'jsdom'],
},
diagnostics: {
warnOnly: false,
},
},
],
},
Expand Down
19 changes: 19 additions & 0 deletions packages/core/__tests__/singleton/Singleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { AuthClass as Auth } from '../../src/singleton/Auth';
import { decodeJWT } from '../../src/singleton/Auth/utils';
import { CredentialsAndIdentityId } from '../../src/singleton/Auth/types';
import { ResourcesConfig, fetchAuthSession } from '../../src';
import { AmplifyOutputs } from '../../src/libraryUtils';

import mockAmplifyOutputs from './utils/amplify_outputs.json';

Object.assign(global, { TextDecoder, TextEncoder });

Expand Down Expand Up @@ -164,6 +167,22 @@ describe('Amplify.configure() and Amplify.getConfig()', () => {
expect(result).toEqual(expectedResourceConfig);
});

it(`should successfully configure from a 'amplify_outputs.json' file`, () => {
type DeepRequired<T> = T extends object
? { [K in keyof T]-?: DeepRequired<NonNullable<T[K]>> }
: T;

const recreateObject = <T>(obj: T): DeepRequired<T> =>
obj as DeepRequired<T>;

// Ensures that all optional properties in mockAmplifyOutputs are defined
const _amplifyOutputs: DeepRequired<AmplifyOutputs> =
recreateObject(mockAmplifyOutputs);

// Configures Amplify with mockAmplifyOutputs, relying on its inferred type
Amplify.configure(mockAmplifyOutputs);
});

it('dispatches hub event with parsed ResourceConfig from the legacy config', () => {
Amplify.configure(mockLegacyConfig);

Expand Down
207 changes: 207 additions & 0 deletions packages/core/__tests__/singleton/utils/amplify_outputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"version": "1",
"auth": {
"aws_region": "us-west-2",
"user_pool_id": "mock-cup-id",
"user_pool_client_id": "mock-cup-client-id",
"identity_pool_id": "mock-idp-id",
"authentication_flow_type": "CUSTOM_AUTH",
"oauth": {
"identity_providers": [
"FACEBOOK",
"SIGN_IN_WITH_APPLE",
"GOOGLE",
"Auth0"
],
"domain": "mock-oauth-domain",
"scopes": ["phone"],
"redirect_sign_in_uri": ["mock-sign-in-uri"],
"redirect_sign_out_uri": ["mock-sign-out-uri"],
"response_type": "token"
},
"standard_required_attributes": ["address", "locale", "email"],
"username_attributes": ["phone_number", "email"],
"user_verification_types": ["email"],
"unauthenticated_identities_enabled": true,
"mfa_configuration": "OPTIONAL",
"mfa_methods": ["TOTP", "SMS"],
"password_policy": {
"require_lowercase": true,
"require_numbers": true,
"require_uppercase": true,
"require_symbols": true,
"min_length": 6
},
"groups": [
{
"ADMIN": { "precedence": 0 },
"USER": { "precedence": 1 }
},
{
"USER": { "precedence": 0 }
},
{
"TEST": { "precedence": 2 },
"USER": { "precedence": 1 },
"ADMIN": { "precedence": 0 }
}
]
},
"data": {
"aws_region": "us-west-2",
"url": "mock-data-url",
"api_key": "mock-data-api-key",
"default_authorization_type": "API_KEY",
"authorization_types": [],
"conflict_resolution_mode": "AUTOMERGE",
"model_introspection": {
"version": 1,
"models": {
"Todo": {
"name": "Todo",
"fields": {
"id": {
"name": "id",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"content": {
"name": "content",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"createdAt": {
"name": "createdAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
},
"updatedAt": {
"name": "updatedAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
}
},
"syncable": true,
"pluralName": "Todos",
"attributes": [
{
"type": "model",
"properties": {}
},
{
"type": "auth",
"properties": {
"rules": [
{
"provider": "userPools",
"ownerField": "owner",
"allow": "owner",
"identityClaim": "cognito:username",
"operations": ["create", "update", "delete", "read"]
},
{
"allow": "private",
"provider": "iam",
"operations": ["create", "update", "delete", "read"]
}
]
}
}
],
"primaryKeyInfo": {
"isCustomPrimaryKey": false,
"primaryKeyFieldName": "id",
"sortKeyFieldNames": []
}
}
},
"enums": {},
"nonModels": {}
}
},
"geo": {
"aws_region": "us-west-2",
"search_indices": {
"items": ["mock-geo-search-item", "mock-geo-search-item-alt"],
"default": "mock-geo-search-item"
},
"geofence_collections": {
"items": ["mock-geo-fence-item", "mock-geo-fence-item-alt"],
"default": "mock-geo-fence-item"
},

"maps": {
"items": {
"map51addb38-dev": {
"style": "VectorEsriStreets"
},
"map30grxcw31-prod": {
"style": "VectorEsriStreets"
}
},
"default": "map51addb38-dev"
}
},
"custom": {
"custom-prop": -51806024,
"custom-prop-alt": 87599986,
"events": {
"url": "mock-events-url",
"aws_region": "us-west-2",
"default_authorization_type": "API_KEY",
"api_key": "mock-events-api-key"
}
},
"notifications": {
"aws_region": "us-west-2",
"amazon_pinpoint_app_id": "mock-pinpoint-app-id",
"channels": ["IN_APP_MESSAGING", "APNS"]
},
"analytics": {
"amazon_pinpoint": {
"app_id": "mock-pinpoint-app-id",
"aws_region": "us-west-2"
}
},
"storage": {
"aws_region": "us-west-2",
"bucket_name": "mock-bucket-1",
"buckets": [
{
"name": "mock-bucket-1",
"bucket_name": "bucket1",
"aws_region": "us-west-2",
"paths": {
"sub/*": {
"authenticated": ["get", "list"]
},
"public/*": {
"guest": ["get", "list", "write", "delete"],
"authenticated": ["write", "get", "list", "delete"]
}
}
},
{
"name": "mock-bucket-2",
"bucket_name": "bucket2",
"aws_region": "us-west-2",
"paths": {
"public/*": {
"guest": ["get", "list", "write", "delete"],
"authenticated": ["write", "get", "list", "delete"]
}
}
}
]
}
}
4 changes: 2 additions & 2 deletions packages/core/src/singleton/AmplifyOutputs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type UserGroupName = string;
type UserGroupPrecedence = Record<string, number>;
export interface AmplifyOutputsAuthProperties {
aws_region: string;
authentication_flow_type?: 'USER_SRP_AUTH' | 'CUSTOM_AUTH';
authentication_flow_type?: string;
user_pool_id: string;
user_pool_client_id: string;
identity_pool_id?: string;
Expand All @@ -42,7 +42,7 @@ export interface AmplifyOutputsAuthProperties {
unauthenticated_identities_enabled?: boolean;
mfa_configuration?: string;
mfa_methods?: string[];
groups?: Partial<Record<UserGroupName, UserGroupPrecedence>>[];
groups?: Record<UserGroupName, UserGroupPrecedence>[];
}

export interface AmplifyOutputsStorageBucketProperties {
Expand Down

0 comments on commit bacf67f

Please sign in to comment.