@aws-amplify/core
Version:
Core category of aws-amplify
85 lines (83 loc) • 3.32 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.Amplify = exports.AmplifyClass = void 0;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const Hub_1 = require("../Hub");
const utils_1 = require("../utils");
const libraryUtils_1 = require("../libraryUtils");
const Auth_1 = require("./Auth");
const constants_1 = require("./constants");
class AmplifyClass {
constructor() {
this.oAuthListener = undefined;
this.resourcesConfig = {};
this.libraryOptions = {};
this.Auth = new Auth_1.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, libraryOptions) {
const resolvedResourceConfig = (0, libraryUtils_1.parseAmplifyConfig)(resourcesConfig);
this.resourcesConfig = resolvedResourceConfig;
if (libraryOptions) {
this.libraryOptions = libraryOptions;
}
// Make resource config immutable
this.resourcesConfig = (0, utils_1.deepFreeze)(this.resourcesConfig);
this.Auth.configure(this.resourcesConfig.Auth, this.libraryOptions.Auth);
Hub_1.Hub.dispatch('core', {
event: 'configure',
data: this.resourcesConfig,
}, 'Configure', Hub_1.AMPLIFY_SYMBOL);
this.notifyOAuthListener();
}
/**
* Provides access to the current back-end resource configuration for the Library.
*
* @returns Returns the immutable back-end resource configuration.
*/
getConfig() {
return this.resourcesConfig;
}
/** @internal */
[constants_1.ADD_OAUTH_LISTENER](listener) {
if (this.resourcesConfig.Auth?.Cognito.loginWith?.oauth) {
// when Amplify has been configured with a valid OAuth config while adding the listener, run it directly
listener(this.resourcesConfig.Auth?.Cognito);
}
else {
// otherwise register the listener and run it later when Amplify gets configured with a valid oauth config
this.oAuthListener = listener;
}
}
notifyOAuthListener() {
if (!this.resourcesConfig.Auth?.Cognito.loginWith?.oauth ||
!this.oAuthListener) {
return;
}
this.oAuthListener(this.resourcesConfig.Auth?.Cognito);
// the listener should only be notified once with a valid oauth config
this.oAuthListener = undefined;
}
}
exports.AmplifyClass = AmplifyClass;
/**
* The `Amplify` utility is used to configure the library.
*
* @remarks
* `Amplify` orchestrates cross-category communication within the library.
*/
exports.Amplify = new AmplifyClass();
//# sourceMappingURL=Amplify.js.map