UNPKG

@aws-amplify/api

Version:

Api category of aws-amplify

69 lines (66 loc) • 3.37 kB
import { generateClient as generateClient$1 } from '@aws-amplify/api-graphql/internals'; import { createAmplifyContextToken, isAmplifyContext, getGlobalContext, hasGlobalContext, AMPLIFY_CONTEXT_BRAND } from '@aws-amplify/core'; // Empty config returned by the lazy global-context fallback before // `Amplify.configure()` has run. Mirrors the previous `Amplify` singleton // fallback whose `getConfig()` returned `{}` pre-configure, so `generateClient()` // called before configuration still yields a client whose `models` rebuild on // the subsequent configure Hub event (rather than throwing). const EMPTY_RESOURCES_CONFIG = {}; /** * A lazy {@link AmplifyContext} that resolves the *current* global context on * every access instead of capturing a snapshot at client-generation time. * * `generateClient()` returns a long-lived client whose `graphql()` operations * read config/auth off the stored context per call. Binding a snapshot of * `getGlobalContext()` at generation time would go stale if the app calls * `Amplify.configure()` again — each configure installs a NEW frozen global * context. Delegating through getters keeps reconfiguration honored, preserving * the live-read behavior the previous `Amplify` singleton fallback provided via * `bridgeAmplifyClass`'s getters. Reads before configuration degrade to an empty * config instead of throwing (see {@link EMPTY_RESOURCES_CONFIG}). */ const lazyGlobalContext = { get resourcesConfig() { return hasGlobalContext() ? getGlobalContext().resourcesConfig : EMPTY_RESOURCES_CONFIG; }, get libraryOptions() { return hasGlobalContext() ? getGlobalContext().libraryOptions : {}; }, // Stable identity handle for this lazy handle itself (see // AmplifyContextToken). Deliberately NOT delegated to the global context: // the token identifies this long-lived wrapper, and reading it must not // throw pre-configure. token: createAmplifyContextToken(), fetchAuthSession: options => getGlobalContext().fetchAuthSession(options), clearCredentials: () => getGlobalContext().clearCredentials(), getTokens: options => getGlobalContext().getTokens(options), }; // Brand the lazy context so `internalGenerateClient` recognizes it as an // AmplifyContext (via `isAmplifyContext`) and does NOT attempt to bridge it as // an `AmplifyClass` instance. Object.defineProperty(lazyGlobalContext, AMPLIFY_CONTEXT_BRAND, { value: true, enumerable: false, configurable: false, writable: false, }); Object.freeze(lazyGlobalContext); function generateClient(ctxOrOptions, maybeOptions) { // When the first argument is a branded AmplifyContext, bind the client to it // and treat the second argument as the options. Otherwise fall back to the // lazy global context (resolved fresh per operation, honoring reconfigure) // and treat the first argument as the options. `isAmplifyContext` is inlined // in each ternary so its type guard narrows `ctxOrOptions` in place. const amplify = isAmplifyContext(ctxOrOptions) ? ctxOrOptions : lazyGlobalContext; const options = isAmplifyContext(ctxOrOptions) ? maybeOptions : ctxOrOptions; return generateClient$1({ ...(options ?? {}), amplify, }); } export { generateClient }; //# sourceMappingURL=API.mjs.map