aws-amplify
Version:
AWS Amplify is a JavaScript library for Frontend and mobile developers building cloud-enabled applications.
1 lines • 11.2 kB
Source Map (JSON)
{"version":3,"file":"Amplify.mjs","sources":["../../src/Amplify.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { Hub, createAmplifyContext, getGlobalContext, hasGlobalContext, } from '@aws-amplify/core';\nimport { AMPLIFY_SYMBOL, parseAmplifyConfig, selectSsrKeyValueStorage, setGlobalContext, } from '@aws-amplify/core/internals/utils';\nimport { CognitoAWSCredentialsAndIdentityIdProvider, DefaultIdentityIdStore, cognitoCredentialsProvider, cognitoUserPoolsTokenProvider, } from './auth/cognito';\n/**\n * The `Amplify` singleton facade provides the v6-compatible, global-state\n * `configure()` / `getConfig()` API. It builds a branded, frozen\n * {@link AmplifyContext}, publishes it as the global context (so category APIs\n * can be called without an explicit context argument), and dispatches the\n * `configure` Hub event that side effects such as auth's OAuth listener depend\n * on.\n *\n * @example\n * ```ts\n * import { Amplify } from 'aws-amplify';\n * import config from './amplifyconfiguration.json';\n *\n * Amplify.configure(config);\n * ```\n */\nexport const Amplify = {\n /**\n * Configures Amplify with the {@link resourceConfig} and {@link libraryOptions},\n * publishing the resulting branded context as the global {@link AmplifyContext}.\n *\n * If `libraryOptions` is omitted on a reconfigure, the previously configured\n * non-Auth `libraryOptions` are preserved (mirroring the previous singleton\n * merge behavior); when provided they replace the previous ones. When\n * `resourceConfig.Auth` is present and the caller does not supply\n * `libraryOptions.Auth`, the default Cognito token/credentials providers are\n * wired using cookie storage (`ssr: true`) or `localStorage` (default), and\n * the singleton token provider's auth config is re-synced on every call so a\n * reconfigure with a new `userPoolId` retargets token refresh.\n *\n * @param resourceConfig The {@link ResourcesConfig} object that is typically imported from the\n * `amplifyconfiguration.json` / `amplify_outputs.json` file. It can also be an object literal\n * created inline when calling `Amplify.configure`.\n * @param libraryOptions The {@link LibraryOptions} additional options for the library.\n *\n * @example\n * import config from './amplifyconfiguration.json';\n *\n * Amplify.configure(config);\n */\n configure(resourceConfig, libraryOptions) {\n const resolvedResourceConfig = parseAmplifyConfig(resourceConfig);\n const previousLibraryOptions = hasGlobalContext()\n ? getGlobalContext().libraryOptions\n : undefined;\n const resolvedLibraryOptions = resolveLibraryOptions(resolvedResourceConfig, libraryOptions, previousLibraryOptions);\n // Build a branded + frozen context (with a fresh per-context AuthClass)\n // via the core factory, then publish it as the global context.\n const ctx = createAmplifyContext(resolvedResourceConfig, resolvedLibraryOptions);\n setGlobalContext(ctx);\n // Notify listeners (e.g. auth's OAuth completion side effect) that the\n // library has been (re)configured. The payload `data` carries the parsed\n // ResourcesConfig so listeners can read `data.Auth.Cognito`.\n Hub.dispatch('core', {\n event: 'configure',\n data: resolvedResourceConfig,\n }, 'Configure', AMPLIFY_SYMBOL);\n },\n /**\n * Returns the {@link ResourcesConfig} object passed in as the `resourceConfig` parameter when\n * calling `Amplify.configure`.\n *\n * If `configure()` has not been called yet, this warns and returns an empty\n * config object (`{}`) rather than throwing, preserving the historical\n * pre-context singleton behavior that callers (and release-gating e2e tests)\n * depend on.\n *\n * @returns An {@link ResourcesConfig} object.\n */\n getConfig() {\n if (!hasGlobalContext()) {\n // Preserve the historical unconfigured UX from the core singleton:\n // warn and return an empty config instead of throwing.\n // eslint-disable-next-line no-console\n console.warn(`Amplify has not been configured. Please call Amplify.configure() before using this service.`);\n return {};\n }\n return getGlobalContext().resourcesConfig;\n },\n /**\n * Fetches the current auth session from the global context.\n *\n * @throws If `configure()` has not been called yet.\n */\n fetchAuthSession(options) {\n return getGlobalContext().fetchAuthSession(options);\n },\n /**\n * Clears cached credentials in the global context.\n *\n * @throws If `configure()` has not been called yet.\n */\n clearCredentials() {\n return getGlobalContext().clearCredentials();\n },\n /**\n * Fetches auth tokens from the global context.\n *\n * @throws If `configure()` has not been called yet.\n */\n getTokens(options) {\n return getGlobalContext().getTokens(options);\n },\n};\n/**\n * Resolves the effective {@link LibraryOptions} for the global singleton,\n * preserving the previous `DefaultAmplify`-style default-provider\n * behavior (process-wide **singleton** Cognito providers, `setAuthConfig` /\n * `setKeyValueStorage` refresh).\n *\n * - No `Auth` in the resource config → pass options through unchanged\n * (preserving previously-resolved options when the caller supplies none).\n * - Caller-provided `libraryOptions.Auth` → take precedence (no defaults).\n * - Otherwise (Auth present, no caller-supplied Auth providers) → on **every**\n * call, including reconfigures with or without other `libraryOptions`, refresh\n * the singleton token provider's auth config + storage and inject the\n * singleton token/credentials providers. Non-Auth library options are still\n * carried forward from the previous configuration when the caller passes none.\n */\nfunction resolveLibraryOptions(resourceConfig, libraryOptions, previousLibraryOptions) {\n // Pass-through: with no Auth in the resource config the singleton token\n // provider is never involved, so simply preserve the previously configured\n // libraryOptions when the caller supplies none (the core AmplifyClass\n // singleton used to perform this merge), otherwise use what was given.\n if (!resourceConfig.Auth) {\n if (!libraryOptions && previousLibraryOptions) {\n return previousLibraryOptions;\n }\n return libraryOptions ?? {};\n }\n // Caller-provided Auth providers always take precedence; never touch the\n // singleton token provider in that case.\n if (libraryOptions?.Auth) {\n return libraryOptions;\n }\n // Auth is present and the caller did NOT supply their own Auth providers.\n // Carry non-Auth library options forward from the previous configuration\n // when the caller passes none (preserving prior behavior across\n // reconfigure), but ALWAYS re-sync the\n // singleton token provider below so the Auth providers never go stale.\n const baseLibraryOptions = libraryOptions ?? previousLibraryOptions ?? {};\n // Select the storage backing the default providers via core's shared\n // helper (ssr → fresh cookie storage, else the localStorage-backed\n // singleton). When ssr is set, reuse the SAME cookie storage instance for\n // the identity-id store so tokens and identity ids share one cookie jar.\n const resolvedKeyValueStorage = selectSsrKeyValueStorage(baseLibraryOptions.ssr);\n const resolvedCredentialsProvider = baseLibraryOptions.ssr\n ? new CognitoAWSCredentialsAndIdentityIdProvider(new DefaultIdentityIdStore(resolvedKeyValueStorage))\n : cognitoCredentialsProvider;\n // Re-push the resolved auth config + key-value storage into the process-wide\n // singleton token provider on EVERY configure call. AuthClass.getTokens\n // delegates to this provider's own authConfig, which is only ever updated\n // here; skipping this on a reconfigure (e.g. a new userPoolId with no\n // libraryOptions) would leave token refresh pinned to the previous pool\n // while getConfig()/credentials reflect the new one. Restores the #14819\n // contract.\n cognitoUserPoolsTokenProvider.setAuthConfig(resourceConfig.Auth);\n cognitoUserPoolsTokenProvider.setKeyValueStorage(\n // TODO: allow configure with a public interface\n resolvedKeyValueStorage);\n return {\n ...baseLibraryOptions,\n Auth: {\n tokenProvider: cognitoUserPoolsTokenProvider,\n credentialsProvider: resolvedCredentialsProvider,\n },\n };\n}\n"],"names":[],"mappings":";;;;AAAA;AACA;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,OAAO,GAAG;AACvB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,SAAS,CAAC,cAAc,EAAE,cAAc,EAAE;AAC9C,QAAQ,MAAM,sBAAsB,GAAG,kBAAkB,CAAC,cAAc,CAAC;AACzE,QAAQ,MAAM,sBAAsB,GAAG,gBAAgB;AACvD,cAAc,gBAAgB,EAAE,CAAC;AACjC,cAAc,SAAS;AACvB,QAAQ,MAAM,sBAAsB,GAAG,qBAAqB,CAAC,sBAAsB,EAAE,cAAc,EAAE,sBAAsB,CAAC;AAC5H;AACA;AACA,QAAQ,MAAM,GAAG,GAAG,oBAAoB,CAAC,sBAAsB,EAAE,sBAAsB,CAAC;AACxF,QAAQ,gBAAgB,CAAC,GAAG,CAAC;AAC7B;AACA;AACA;AACA,QAAQ,GAAG,CAAC,QAAQ,CAAC,MAAM,EAAE;AAC7B,YAAY,KAAK,EAAE,WAAW;AAC9B,YAAY,IAAI,EAAE,sBAAsB;AACxC,SAAS,EAAE,WAAW,EAAE,cAAc,CAAC;AACvC,IAAI,CAAC;AACL;AACA;AACA;AACA;AACA;AACA;AACA;A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