@cloud-copilot/iam-lens
Version:
Visibility in IAM in and across AWS accounts
55 lines • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadCollectConfigs = loadCollectConfigs;
exports.getCollectClient = getCollectClient;
const iam_collect_1 = require("@cloud-copilot/iam-collect");
const module_1 = require("module");
const client_js_1 = require("./client.js");
const dynamicImport_js_1 = require("../utils/dynamicImport.js");
/**
* Load IAM collect configs from the specified paths.
*
* @param configPaths the paths to the config files
* @returns the top-level configs
*/
async function loadCollectConfigs(configPaths) {
return (0, iam_collect_1.loadConfigFiles)(configPaths);
}
/**
* Get a collect client for the specified partition using the provided configs.
*
* If a `clientFactoryPlugin` is provided, the factory function is called with
* `(store, clientOptions, data)` — i.e. the raw `AwsIamStore` and
* `IamCollectClientOptions` rather than a pre-built client — so the factory
* can construct whatever client subclass it needs without discarding an
* intermediate instance.
*
* @param configs - The top-level configs to use for storage.
* @param partition - Which partition to use (aws, aws-cn, aws-us-gov).
* @param options - Optional client options including a `clientFactoryPlugin`.
* @returns The iam-collect client to use for retrieving IAM resources.
*/
async function getCollectClient(configs, partition, options) {
const { clientFactoryPlugin, ...clientOptions } = options ?? {};
const store = (0, iam_collect_1.createStorageClient)(configs, partition, true);
if (!clientFactoryPlugin)
return new client_js_1.IamCollectClient(store, clientOptions);
const mod = await (0, dynamicImport_js_1.dynamicImport)(clientFactoryPlugin.module);
let factory = mod[clientFactoryPlugin.factoryExport];
// Some environments (e.g. vitest SSR) wrap import() results in a Proxy where
// Object.keys() returns the correct keys but property access returns undefined.
// When this happens, fall back to createRequire which bypasses the proxy.
if (!factory && Object.keys(mod).includes(clientFactoryPlugin.factoryExport)) {
const _require = (0, module_1.createRequire)(process.cwd() + '/');
const fallback = _require(clientFactoryPlugin.module);
factory = fallback[clientFactoryPlugin.factoryExport];
}
if (!factory) {
throw new Error(`Factory export '${clientFactoryPlugin.factoryExport}' not found in module '${clientFactoryPlugin.module}'`);
}
else if (typeof factory !== 'function') {
throw new Error(`Factory export '${clientFactoryPlugin.factoryExport}' in module '${clientFactoryPlugin.module}' is not a function`);
}
return factory(store, clientOptions, clientFactoryPlugin.data);
}
//# sourceMappingURL=collect.js.map