@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.
- package/index.js +51 -73
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -1,76 +1,54 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
},
|