@aws-amplify/core
Version:
Core category of aws-amplify
1 lines • 3.92 kB
Source Map (JSON)
{"version":3,"file":"serverContext.mjs","sources":["../../../../src/adapterCore/serverContext/serverContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { AmplifyClass } from '../../singleton';\nimport { AmplifyServerContextError } from '../error';\nimport { serverContextRegistry } from './serverContextRegistry';\n/**\n * Creates an Amplify server context.\n * @param amplifyConfig The Amplify resource config.\n * @param libraryOptions The Amplify library options.\n * @returns The Amplify server context spec.\n */\nexport const createAmplifyServerContext = (amplifyConfig, libraryOptions) => {\n const amplify = new AmplifyClass();\n amplify.configure(amplifyConfig, libraryOptions);\n return serverContextRegistry.register({\n amplify,\n });\n};\n/**\n * Returns an Amplify server context.\n * @param contextSpec The context spec used to get the Amplify server context.\n * @returns The Amplify server context.\n */\nexport const getAmplifyServerContext = (contextSpec) => {\n assertContextSpec(contextSpec);\n const context = serverContextRegistry.get(contextSpec);\n if (context) {\n return context;\n }\n throw new AmplifyServerContextError({\n message: 'Attempted to get the Amplify Server Context that may have been destroyed.',\n recoverySuggestion: 'Ensure always call Amplify APIs within `runWithAmplifyServerContext` function, and do not attempt to reuse `contextSpec` object.',\n });\n};\n/**\n * Destroys an Amplify server context.\n * @param contextSpec The context spec used to destroy the Amplify server context.\n */\nexport const destroyAmplifyServerContext = (contextSpec) => {\n serverContextRegistry.deregister(contextSpec);\n};\nconst assertContextSpec = (contextSpec) => {\n let invalid = false;\n if (!Object.prototype.hasOwnProperty.call(contextSpec, 'token')) {\n invalid = true;\n }\n else if (!Object.prototype.hasOwnProperty.call(contextSpec.token, 'value')) {\n invalid = true;\n }\n else if (Object.prototype.toString.call(contextSpec.token.value) !==\n '[object Symbol]') {\n invalid = true;\n }\n if (invalid) {\n throw new AmplifyServerContextError({\n message: 'Invalid `contextSpec`.',\n recoverySuggestion: 'Ensure to use the `contextSpec` object injected by `runWithAmplifyServerContext`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