@aws-amplify/core
Version:
Core category of aws-amplify
90 lines (77 loc) • 2.51 kB
text/typescript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { AuthClass } from './Auth';
import { Hub, AMPLIFY_SYMBOL } from '../Hub';
import { LegacyConfig, LibraryOptions, ResourcesConfig } from './types';
import { parseAWSExports } from '../parseAWSExports';
import { deepFreeze } from '../utils';
export class AmplifyClass {
resourcesConfig: ResourcesConfig;
libraryOptions: LibraryOptions;
/**
* Cross-category Auth utilities.
*
* @internal
*/
public readonly Auth: AuthClass;
constructor() {
this.resourcesConfig = {};
this.libraryOptions = {};
this.Auth = new AuthClass();
}
/**
* Configures Amplify for use with your back-end resources.
*
* @remarks
* This API does not perform any merging of either `resourcesConfig` or `libraryOptions`. The most recently
* provided values will be used after configuration.
*
* @remarks
* `configure` can be used to specify additional library options where available for supported categories.
*
* @param resourceConfig - Back-end resource configuration. Typically provided via the `aws-exports.js` file.
* @param libraryOptions - Additional options for customizing the behavior of the library.
*/
configure(
resourcesConfig: ResourcesConfig | LegacyConfig,
libraryOptions?: LibraryOptions
): void {
let resolvedResourceConfig: ResourcesConfig;
if (Object.keys(resourcesConfig).some(key => key.startsWith('aws_'))) {
resolvedResourceConfig = parseAWSExports(resourcesConfig);
} else {
resolvedResourceConfig = resourcesConfig as ResourcesConfig;
}
this.resourcesConfig = resolvedResourceConfig;
if (libraryOptions) {
this.libraryOptions = libraryOptions;
}
// Make resource config immutable
this.resourcesConfig = deepFreeze(this.resourcesConfig);
this.Auth.configure(this.resourcesConfig.Auth!, this.libraryOptions.Auth);
Hub.dispatch(
'core',
{
event: 'configure',
data: resourcesConfig,
},
'Configure',
AMPLIFY_SYMBOL
);
}
/**
* Provides access to the current back-end resource configuration for the Library.
*
* @returns Returns the immutable back-end resource configuration.
*/
getConfig(): Readonly<ResourcesConfig> {
return this.resourcesConfig;
}
}
/**
* The `Amplify` utility is used to configure the library.
*
* @remarks
* `Amplify` is responsible for orchestrating cross-category communication within the library.
*/
export const Amplify = new AmplifyClass();