@prismatic-io/spectral
Version:
Utility library for building Prismatic connectors and code-native integrations
53 lines (52 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findUserDefinedComponentKey = void 0;
exports.runWithContext = runWithContext;
exports.requireContext = requireContext;
exports.runWithIntegrationContext = runWithIntegrationContext;
exports.requireIntegrationContext = requireIntegrationContext;
// Only import async_hooks in Node.js environments
const asyncHooks = typeof window === "undefined" ? require("node:async_hooks") : null;
const actionContextStorage = asyncHooks ? new asyncHooks.AsyncLocalStorage() : null;
const integrationContextStorage = asyncHooks ? new asyncHooks.AsyncLocalStorage() : null;
function runWithContext(context, fn) {
if (!actionContextStorage) {
return fn();
}
return actionContextStorage.run(context, fn);
}
function requireContext() {
const context = actionContextStorage.getStore();
if (!context) {
throw new Error("ActionContext not found. Ensure this code is wrapped via runWithContext.");
}
return context;
}
function runWithIntegrationContext(context, fn) {
if (!integrationContextStorage) {
console.warn("Creating integration without context. This may result in errors when generating component manifests.");
return fn();
}
return integrationContextStorage.run(context, fn);
}
function requireIntegrationContext() {
const context = integrationContextStorage.getStore();
if (!context) {
console.trace();
throw new Error("IntegrationContext not found. Ensure this code is wrapped via runWithIntegrationContext.");
}
return context;
}
const findUserDefinedComponentKey = (componentKey, isPublic, registry) => {
if (!registry) {
throw new Error("Error locating component registry. Is there a component registry defined on your integration?");
}
const userKey = Object.keys(registry).find((userKey) => {
return registry[userKey].key === componentKey && registry[userKey].public === isPublic;
});
if (!userKey) {
throw new Error(`Error locating component ${componentKey} with custom key ${userKey} in the component registry. Is this component properly installed with a correct public/private setting?`);
}
return userKey;
};
exports.findUserDefinedComponentKey = findUserDefinedComponentKey;