@aws-amplify/api
Version:
Api category of aws-amplify
70 lines (68 loc) • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateClient = generateClient;
const internals_1 = require("@aws-amplify/api-graphql/internals");
const core_1 = require("@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 (0, core_1.hasGlobalContext)()
? (0, core_1.getGlobalContext)().resourcesConfig
: EMPTY_RESOURCES_CONFIG;
},
get libraryOptions() {
return (0, core_1.hasGlobalContext)() ? (0, core_1.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: (0, core_1.createAmplifyContextToken)(),
fetchAuthSession: options => (0, core_1.getGlobalContext)().fetchAuthSession(options),
clearCredentials: () => (0, core_1.getGlobalContext)().clearCredentials(),
getTokens: options => (0, core_1.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, core_1.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 = (0, core_1.isAmplifyContext)(ctxOrOptions)
? ctxOrOptions
: lazyGlobalContext;
const options = (0, core_1.isAmplifyContext)(ctxOrOptions) ? maybeOptions : ctxOrOptions;
return (0, internals_1.generateClient)({
...(options ?? {}),
amplify,
});
}
//# sourceMappingURL=API.js.map