UNPKG

@middy/service-discovery

Version:

Service Discovery (Cloud Map) instances middleware for the middy framework

90 lines (76 loc) 2.23 kB
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors. // SPDX-License-Identifier: MIT import { DiscoverInstancesCommand, ServiceDiscoveryClient, } from "@aws-sdk/client-servicediscovery"; import { canPrefetch, catchInvalidSignatureException, createClient, createPrefetchClient, getCache, getInternal, modifyCache, processCache, } from "@middy/util"; const defaults = { AwsClient: ServiceDiscoveryClient, awsClientOptions: {}, awsClientAssumeRole: undefined, awsClientCapture: undefined, fetchData: {}, // { contextKey: {NamespaceName, ServiceName, HealthStatus} } disablePrefetch: false, cacheKey: "service-discovery", cacheKeyExpiry: {}, cacheExpiry: -1, setToContext: false, }; const serviceDiscoveryMiddleware = (opts = {}) => { const options = { ...defaults, ...opts }; const fetchDataKeys = Object.keys(options.fetchData); const fetchRequest = (request, cachedValues = {}) => { const values = {}; for (const internalKey of fetchDataKeys) { if (cachedValues[internalKey]) continue; const command = new DiscoverInstancesCommand( options.fetchData[internalKey], ); values[internalKey] = client .send(command) .catch((e) => catchInvalidSignatureException(e, client, command)) .then((resp) => resp.Instances) .catch((e) => { const value = getCache(options.cacheKey).value ?? {}; value[internalKey] = undefined; modifyCache(options.cacheKey, value); throw e; }); } return values; }; let client; if (canPrefetch(options)) { client = createPrefetchClient(options); processCache(options, fetchRequest); } const serviceDiscoveryMiddlewareBefore = async (request) => { if (!client) { client = await createClient(options, request); } const { value } = processCache(options, fetchRequest, request); Object.assign(request.internal, value); if (options.setToContext) { const data = await getInternal(fetchDataKeys, request); Object.assign(request.context, data); } }; return { before: serviceDiscoveryMiddlewareBefore, }; }; export default serviceDiscoveryMiddleware; // used for TS type inference (see index.d.ts) export function serviceDiscoveryParam(name) { return name; }