UNPKG

@aws-amplify/core

Version:
69 lines (67 loc) 2.81 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.destroyAmplifyServerContext = exports.getAmplifyServerContext = exports.createAmplifyServerContext = void 0; const singleton_1 = require("../../singleton"); const error_1 = require("../error"); const serverContextRegistry_1 = require("./serverContextRegistry"); /** * Creates an Amplify server context. * @param amplifyConfig The Amplify resource config. * @param libraryOptions The Amplify library options. * @returns The Amplify server context spec. */ const createAmplifyServerContext = (amplifyConfig, libraryOptions) => { const amplify = new singleton_1.AmplifyClass(); amplify.configure(amplifyConfig, libraryOptions); return serverContextRegistry_1.serverContextRegistry.register({ amplify, }); }; exports.createAmplifyServerContext = createAmplifyServerContext; /** * Returns an Amplify server context. * @param contextSpec The context spec used to get the Amplify server context. * @returns The Amplify server context. */ const getAmplifyServerContext = (contextSpec) => { assertContextSpec(contextSpec); const context = serverContextRegistry_1.serverContextRegistry.get(contextSpec); if (context) { return context; } throw new error_1.AmplifyServerContextError({ message: 'Attempted to get the Amplify Server Context that may have been destroyed.', recoverySuggestion: 'Ensure always call Amplify APIs within `runWithAmplifyServerContext` function, and do not attempt to reuse `contextSpec` object.', }); }; exports.getAmplifyServerContext = getAmplifyServerContext; /** * Destroys an Amplify server context. * @param contextSpec The context spec used to destroy the Amplify server context. */ const destroyAmplifyServerContext = (contextSpec) => { serverContextRegistry_1.serverContextRegistry.deregister(contextSpec); }; exports.destroyAmplifyServerContext = destroyAmplifyServerContext; const assertContextSpec = (contextSpec) => { let invalid = false; if (!Object.prototype.hasOwnProperty.call(contextSpec, 'token')) { invalid = true; } else if (!Object.prototype.hasOwnProperty.call(contextSpec.token, 'value')) { invalid = true; } else if (Object.prototype.toString.call(contextSpec.token.value) !== '[object Symbol]') { invalid = true; } if (invalid) { throw new error_1.AmplifyServerContextError({ message: 'Invalid `contextSpec`.', recoverySuggestion: 'Ensure to use the `contextSpec` object injected by `runWithAmplifyServerContext` function.', }); } }; //# sourceMappingURL=serverContext.js.map