@softchef/cdk-iot-device-management
Version:
IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.
49 lines (48 loc) • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.endpointDiscoveryMiddleware = void 0;
const protocol_http_1 = require("@aws-sdk/protocol-http");
const getCacheKey_1 = require("./getCacheKey");
const updateDiscoveredEndpointInCache_1 = require("./updateDiscoveredEndpointInCache");
const endpointDiscoveryMiddleware = (config, middlewareConfig) => (next, context) => async (args) => {
if (config.isCustomEndpoint) {
if (config.isClientEndpointDiscoveryEnabled) {
throw new Error(`Custom endpoint is supplied; endpointDiscoveryEnabled must not be true.`);
}
return next(args);
}
const { endpointDiscoveryCommandCtor } = config;
const { isDiscoveredEndpointRequired, identifiers } = middlewareConfig;
const { clientName, commandName } = context;
const isEndpointDiscoveryEnabled = await config.endpointDiscoveryEnabled();
const cacheKey = await getCacheKey_1.getCacheKey(commandName, config, { identifiers });
if (isDiscoveredEndpointRequired) {
if (isEndpointDiscoveryEnabled === false) {
throw new Error(`Endpoint Discovery is disabled but ${commandName} on ${clientName} requires it.` +
` Please check your configurations.`);
}
await updateDiscoveredEndpointInCache_1.updateDiscoveredEndpointInCache(config, {
...middlewareConfig,
commandName,
cacheKey,
endpointDiscoveryCommandCtor,
});
}
else if (isEndpointDiscoveryEnabled) {
updateDiscoveredEndpointInCache_1.updateDiscoveredEndpointInCache(config, {
...middlewareConfig,
commandName,
cacheKey,
endpointDiscoveryCommandCtor,
});
}
const { request } = args;
if (cacheKey && protocol_http_1.HttpRequest.isInstance(request)) {
const endpoint = config.endpointCache.getEndpoint(cacheKey);
if (endpoint) {
request.hostname = endpoint;
}
}
return next(args);
};
exports.endpointDiscoveryMiddleware = endpointDiscoveryMiddleware;