Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add API for partially config Amplify #12513

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/aws-amplify/src/initSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
LegacyConfig,
parseAWSExports,
Lens,
} from '@aws-amplify/core/internals/utils';
import {
CognitoUserPoolsTokenProvider,
Expand Down Expand Up @@ -66,4 +67,12 @@ export const DefaultAmplify = {
getConfig(): ResourcesConfig {
return Amplify.getConfig();
},
updateConfig<T>(
selector: (
config: Lens<ResourcesConfig, ResourcesConfig>
) => Lens<ResourcesConfig, T>,
value: T
) {
Amplify.updateConfig(selector, value);
},
};
73 changes: 71 additions & 2 deletions packages/core/__tests__/singleton/Singleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Amplify } from '../../src/singleton';
import { AuthClass as Auth } from '../../src/singleton/Auth';
import { decodeJWT } from '../../src/singleton/Auth/utils';
import { AWSCredentialsAndIdentityId } from '../../src/singleton/Auth/types';
import { TextEncoder, TextDecoder } from 'util';
import { fetchAuthSession } from '../../src';
import { TextDecoder, TextEncoder } from 'util';
import { fetchAuthSession, ResourcesConfig } from '../../src';

Object.assign(global, { TextDecoder, TextEncoder });
type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any
? A
Expand All @@ -17,6 +18,74 @@ const MOCK_AUTH_CONFIG = {
},
};

describe('Partial configure', () => {
it('should update partial config', () => {
const mockConfig: ResourcesConfig = {
Auth: {
Cognito: {
allowGuestAccess: true,
identityPoolId: 'aws_cognito_identity_pool_id',
userPoolClientId: 'aws_user_pools_web_client_id',
userPoolId: 'aws_user_pools_id',
passwordFormat: {
minLength: 8,
requireLowercase: false,
requireNumbers: false,
requireSpecialCharacters: false,
requireUppercase: false,
},
},
},
};

Amplify.configure(mockConfig);
Amplify.updateConfig(
config =>
config
.atKey('Auth')
.atKey('Cognito')
.atKey('passwordFormat')
.atKey('minLength'),
12
);
expect(Amplify.getConfig()).toStrictEqual({
Auth: {
Cognito: {
allowGuestAccess: true,
identityPoolId: 'aws_cognito_identity_pool_id',
userPoolClientId: 'aws_user_pools_web_client_id',
userPoolId: 'aws_user_pools_id',
passwordFormat: {
minLength: 12,
requireLowercase: false,
requireNumbers: false,
requireSpecialCharacters: false,
requireUppercase: false,
},
},
},
});
});

it('should fail if update non existing field', () => {
const mockConfig: ResourcesConfig = {
Analytics: {
Pinpoint: {
appId: '123',
region: 'us-east-1',
},
},
};
Amplify.configure(mockConfig);
expect(() =>
Amplify.updateConfig(
config => config.atKey('Auth').atKey('Cognito').atKey('identityPoolId'),
'0000'
)
).toThrow();
});
});

describe('Amplify.configure() and Amplify.getConfig()', () => {
it('should take the legacy CLI shaped config object for configuring and return it from getConfig()', () => {
const mockLegacyConfig = {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/libraryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export { base64Decoder, base64Encoder } from './utils/convert';
export { getCrypto } from './utils/globalHelpers';
export { cryptoSecureRandomInt } from './utils/cryptoSecureRandomInt';
export { WordArray };
export { Lens } from './utils/Lens';

// Hub
export { HubInternal } from './Hub';
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/singleton/Amplify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Hub, AMPLIFY_SYMBOL } from '../Hub';
import { LegacyConfig, LibraryOptions, ResourcesConfig } from './types';
import { parseAWSExports } from '../parseAWSExports';
import { deepFreeze } from '../utils';
import { Lens } from '../utils/Lens';

export class AmplifyClass {
resourcesConfig: ResourcesConfig;
Expand Down Expand Up @@ -78,6 +79,23 @@ export class AmplifyClass {
getConfig(): Readonly<ResourcesConfig> {
return this.resourcesConfig;
}

/**
* Partially update the configuration
*
* @param selector Selector to define which field to set.
* @param value The value to set.
* */
updateConfig<T>(
selector: (
config: Lens<ResourcesConfig, ResourcesConfig>
) => Lens<ResourcesConfig, T>,
value: T
): void {
Amplify.configure(
selector(Lens.of<ResourcesConfig>()).set(value)(this.resourcesConfig)
);
}
}

/**
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/utils/Lens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

type Getter<W, P> = (w: W) => P;
type Setter<W, P> = (p: P) => (w: W) => W;

export class Lens<W, P> {
private readonly get: Getter<W, P>;

/** @internal */
readonly set: Setter<W, P>;

static of<T>(): Lens<T, T> {
return new Lens<T, T>(
t => t,
t => () => t
);
}

constructor(get: Getter<W, P>, set: Setter<W, P>) {
this.get = get;
this.set = set;
}

private compose<Q>(next: Lens<P, Q>): Lens<W, Q> {
return new Lens<W, Q>(
w => next.get(this.get(w)),
q => w => this.set(next.set(q)(this.get(w)))(w)
);
}

atKey<Q extends keyof P>(key: Q): Lens<W, P[Q]> {
return this.compose(
new Lens<P, P[Q]>(
p => p[key],
pq => p => ({
...p,
[key]: pq,
})
)
);
}
}
Loading