UNPKG

@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.

55 lines (54 loc) 3.35 kB
import { __assign, __awaiter, __generator } from "tslib"; import { HttpRequest } from "@aws-sdk/protocol-http"; import { getCacheKey } from "./getCacheKey"; import { updateDiscoveredEndpointInCache } from "./updateDiscoveredEndpointInCache"; export var endpointDiscoveryMiddleware = function (config, middlewareConfig) { return function (next, context) { return function (args) { return __awaiter(void 0, void 0, void 0, function () { var endpointDiscoveryCommandCtor, isDiscoveredEndpointRequired, identifiers, clientName, commandName, isEndpointDiscoveryEnabled, cacheKey, request, endpoint; return __generator(this, function (_a) { switch (_a.label) { case 0: if (config.isCustomEndpoint) { if (config.isClientEndpointDiscoveryEnabled) { throw new Error("Custom endpoint is supplied; endpointDiscoveryEnabled must not be true."); } return [2, next(args)]; } endpointDiscoveryCommandCtor = config.endpointDiscoveryCommandCtor; isDiscoveredEndpointRequired = middlewareConfig.isDiscoveredEndpointRequired, identifiers = middlewareConfig.identifiers; clientName = context.clientName, commandName = context.commandName; return [4, config.endpointDiscoveryEnabled()]; case 1: isEndpointDiscoveryEnabled = _a.sent(); return [4, getCacheKey(commandName, config, { identifiers: identifiers })]; case 2: cacheKey = _a.sent(); if (!isDiscoveredEndpointRequired) return [3, 4]; if (isEndpointDiscoveryEnabled === false) { throw new Error("Endpoint Discovery is disabled but " + commandName + " on " + clientName + " requires it." + " Please check your configurations."); } return [4, updateDiscoveredEndpointInCache(config, __assign(__assign({}, middlewareConfig), { commandName: commandName, cacheKey: cacheKey, endpointDiscoveryCommandCtor: endpointDiscoveryCommandCtor }))]; case 3: _a.sent(); return [3, 5]; case 4: if (isEndpointDiscoveryEnabled) { updateDiscoveredEndpointInCache(config, __assign(__assign({}, middlewareConfig), { commandName: commandName, cacheKey: cacheKey, endpointDiscoveryCommandCtor: endpointDiscoveryCommandCtor })); } _a.label = 5; case 5: request = args.request; if (cacheKey && HttpRequest.isInstance(request)) { endpoint = config.endpointCache.getEndpoint(cacheKey); if (endpoint) { request.hostname = endpoint; } } return [2, next(args)]; } }); }); }; }; };