@middy/service-discovery 5.0.0-alpha.1 → 5.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +51 -73
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -1,76 +1,54 @@
1
- import {
2
- canPrefetch,
3
- createPrefetchClient,
4
- createClient,
5
- getCache,
6
- getInternal,
7
- processCache,
8
- modifyCache
9
- } from '@middy/util'
10
- import {
11
- ServiceDiscoveryClient,
12
- DiscoverInstancesCommand
13
- } from '@aws-sdk/client-servicediscovery'
14
-
1
+ import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache } from '@middy/util';
2
+ import { ServiceDiscoveryClient, DiscoverInstancesCommand } from '@aws-sdk/client-servicediscovery';
15
3
  const defaults = {
16
- AwsClient: ServiceDiscoveryClient,
17
- awsClientOptions: {},
18
- awsClientAssumeRole: undefined,
19
- awsClientCapture: undefined,
20
- fetchData: {}, // { contextKey: {NamespaceName, ServiceName, HealthStatus} }
21
- disablePrefetch: false,
22
- cacheKey: 'cloud-map',
23
- cacheKeyExpiry: {},
24
- cacheExpiry: -1,
25
- setToContext: false
26
- }
27
-
28
- const serviceDiscoveryMiddleware = (opts = {}) => {
29
- const options = { ...defaults, ...opts }
30
-
31
- const fetch = (request, cachedValues = {}) => {
32
- const values = {}
33
-
34
- for (const internalKey of Object.keys(options.fetchData)) {
35
- if (cachedValues[internalKey]) continue
36
-
37
- values[internalKey] = client
38
- .send(new DiscoverInstancesCommand(options.fetchData[internalKey]))
39
- .then((resp) => resp.Instances)
40
- .catch((e) => {
41
- const value = getCache(options.cacheKey).value ?? {}
42
- value[internalKey] = undefined
43
- modifyCache(options.cacheKey, value)
44
- throw e
45
- })
46
- }
47
-
48
- return values
49
- }
50
-
51
- let client
52
- if (canPrefetch(options)) {
53
- client = createPrefetchClient(options)
54
- processCache(options, fetch)
55
- }
56
-
57
- const serviceDiscoveryMiddlewareBefore = async (request) => {
58
- if (!client) {
59
- client = await createClient(options, request)
60
- }
61
-
62
- const { value } = processCache(options, fetch, request)
63
-
64
- Object.assign(request.internal, value)
65
-
66
- if (options.setToContext) {
67
- const data = await getInternal(Object.keys(options.fetchData), request)
68
- if (options.setToContext) Object.assign(request.context, data)
4
+ AwsClient: ServiceDiscoveryClient,
5
+ awsClientOptions: {},
6
+ awsClientAssumeRole: undefined,
7
+ awsClientCapture: undefined,
8
+ fetchData: {},
9
+ disablePrefetch: false,
10
+ cacheKey: 'cloud-map',
11
+ cacheKeyExpiry: {},
12
+ cacheExpiry: -1,
13
+ setToContext: false
14
+ };
15
+ const serviceDiscoveryMiddleware = (opts = {})=>{
16
+ const options = {
17
+ ...defaults,
18
+ ...opts
19
+ };
20
+ const fetch = (request, cachedValues = {})=>{
21
+ const values = {};
22
+ for (const internalKey of Object.keys(options.fetchData)){
23
+ if (cachedValues[internalKey]) continue;
24
+ values[internalKey] = client.send(new DiscoverInstancesCommand(options.fetchData[internalKey])).then((resp)=>resp.Instances).catch((e)=>{
25
+ const value = getCache(options.cacheKey).value ?? {};
26
+ value[internalKey] = undefined;
27
+ modifyCache(options.cacheKey, value);
28
+ throw e;
29
+ });
30
+ }
31
+ return values;
32
+ };
33
+ let client;
34
+ if (canPrefetch(options)) {
35
+ client = createPrefetchClient(options);
36
+ processCache(options, fetch);
69
37
  }
70
- }
38
+ const serviceDiscoveryMiddlewareBefore = async (request)=>{
39
+ if (!client) {
40
+ client = await createClient(options, request);
41
+ }
42
+ const { value } = processCache(options, fetch, request);
43
+ Object.assign(request.internal, value);
44
+ if (options.setToContext) {
45
+ const data = await getInternal(Object.keys(options.fetchData), request);
46
+ if (options.setToContext) Object.assign(request.context, data);
47
+ }
48
+ };
49
+ return {
50
+ before: serviceDiscoveryMiddlewareBefore
51
+ };
52
+ };
53
+ export default serviceDiscoveryMiddleware;
71
54
 
72
- return {
73
- before: serviceDiscoveryMiddlewareBefore
74
- }
75
- }
76
- export default serviceDiscoveryMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/service-discovery",
3
- "version": "5.0.0-alpha.1",
3
+ "version": "5.0.0-alpha.2",
4
4
  "description": "Service Discovery (Cloud Map) instances middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -59,11 +59,11 @@
59
59
  "url": "https://github.com/sponsors/willfarrell"
60
60
  },
61
61
  "dependencies": {
62
- "@middy/util": "5.0.0-alpha.1"
62
+ "@middy/util": "5.0.0-alpha.2"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@aws-sdk/client-servicediscovery": "^3.0.0",
66
- "@middy/core": "5.0.0-alpha.1",
66
+ "@middy/core": "5.0.0-alpha.2",
67
67
  "@types/aws-lambda": "^8.10.101",
68
68
  "aws-xray-sdk": "^3.3.3"
69
69
  },