@middy/service-discovery 5.0.0-alpha.0 → 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/README.md CHANGED
@@ -19,8 +19,9 @@
19
19
  <a href="https://snyk.io/test/github/middyjs/middy">
20
20
  <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
21
21
  </a>
22
- <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23
- <img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
22
+ <a href="https://github.com/middyjs/middy/actions/workflows/sast.yml">
23
+ <img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg
24
+ ?branch=main&event=push" alt="CodeQL" style="max-width:100%;">
24
25
  </a>
25
26
  <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
27
  <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
package/index.d.ts CHANGED
@@ -4,10 +4,11 @@ import { Context as LambdaContext } from 'aws-lambda'
4
4
  import {
5
5
  ServiceDiscoveryClient,
6
6
  ServiceDiscoveryClientConfig,
7
+ DiscoverInstancesCommandInput,
7
8
  HttpInstanceSummary
8
9
  } from '@aws-sdk/client-servicediscovery'
9
10
 
10
- interface Options<AwsServiceDiscoveryClient = ServiceDiscoveryClient>
11
+ interface ServiceDiscoveryOptions<AwsServiceDiscoveryClient = ServiceDiscoveryClient>
11
12
  extends Pick<
12
13
  MiddyOptions<
13
14
  AwsServiceDiscoveryClient,
@@ -16,21 +17,34 @@ interface Options<AwsServiceDiscoveryClient = ServiceDiscoveryClient>
16
17
  | 'AwsClient'
17
18
  | 'awsClientOptions'
18
19
  | 'awsClientCapture'
19
- | 'fetchData'
20
20
  | 'disablePrefetch'
21
21
  | 'cacheKey'
22
22
  | 'cacheExpiry'
23
23
  | 'setToContext'
24
- > {}
25
-
26
- export type Context<TOptions extends Options | undefined> = TOptions extends {
27
- setToContext: true
24
+ > {
25
+ fetchData?: { [key: string]: DiscoverInstancesCommandInput }
28
26
  }
29
- ? LambdaContext & Record<keyof TOptions['fetchData'], HttpInstanceSummary>
30
- : LambdaContext
31
27
 
32
- declare function serviceDiscovery<TOptions extends Options | undefined> (
28
+ export type Context<TOptions extends ServiceDiscoveryOptions | undefined> =
29
+ TOptions extends { setToContext: true }
30
+ ? TOptions extends { fetchData: infer TFetchData }
31
+ ? LambdaContext & {
32
+ [Key in keyof TFetchData]: HttpInstanceSummary[]
33
+ }
34
+ : never
35
+ : LambdaContext
36
+
37
+ export type Internal<TOptions extends ServiceDiscoveryOptions | undefined> =
38
+ TOptions extends ServiceDiscoveryOptions
39
+ ? TOptions extends { fetchData: infer TFetchData }
40
+ ? {
41
+ [Key in keyof TFetchData]: HttpInstanceSummary[]
42
+ }
43
+ : {}
44
+ : {}
45
+
46
+ declare function serviceDiscovery<TOptions extends ServiceDiscoveryOptions | undefined> (
33
47
  options?: TOptions
34
- ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
48
+ ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
35
49
 
36
50
  export default serviceDiscovery
package/index.js CHANGED
@@ -8,6 +8,7 @@ const defaults = {
8
8
  fetchData: {},
9
9
  disablePrefetch: false,
10
10
  cacheKey: 'cloud-map',
11
+ cacheKeyExpiry: {},
11
12
  cacheExpiry: -1,
12
13
  setToContext: false
13
14
  };
@@ -29,22 +30,21 @@ const serviceDiscoveryMiddleware = (opts = {})=>{
29
30
  }
30
31
  return values;
31
32
  };
32
- let prefetch, client;
33
+ let client;
33
34
  if (canPrefetch(options)) {
34
35
  client = createPrefetchClient(options);
35
- prefetch = processCache(options, fetch);
36
+ processCache(options, fetch);
36
37
  }
37
38
  const serviceDiscoveryMiddlewareBefore = async (request)=>{
38
39
  if (!client) {
39
40
  client = await createClient(options, request);
40
41
  }
41
- const { value } = prefetch ?? processCache(options, fetch, request);
42
+ const { value } = processCache(options, fetch, request);
42
43
  Object.assign(request.internal, value);
43
44
  if (options.setToContext) {
44
45
  const data = await getInternal(Object.keys(options.fetchData), request);
45
46
  if (options.setToContext) Object.assign(request.context, data);
46
47
  }
47
- prefetch = null;
48
48
  };
49
49
  return {
50
50
  before: serviceDiscoveryMiddlewareBefore
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/service-discovery",
3
- "version": "5.0.0-alpha.0",
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": {
@@ -10,24 +10,18 @@
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "./index.cjs",
14
13
  "module": "./index.js",
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": {
18
17
  "types": "./index.d.ts",
19
18
  "default": "./index.js"
20
- },
21
- "require": {
22
- "types": "./index.d.ts",
23
- "default": "./index.cjs"
24
19
  }
25
20
  }
26
21
  },
27
22
  "types": "index.d.ts",
28
23
  "files": [
29
24
  "index.js",
30
- "index.cjs",
31
25
  "index.d.ts"
32
26
  ],
33
27
  "scripts": {
@@ -65,13 +59,13 @@
65
59
  "url": "https://github.com/sponsors/willfarrell"
66
60
  },
67
61
  "dependencies": {
68
- "@middy/util": "5.0.0-alpha.0"
62
+ "@middy/util": "5.0.0-alpha.2"
69
63
  },
70
64
  "devDependencies": {
71
65
  "@aws-sdk/client-servicediscovery": "^3.0.0",
72
- "@middy/core": "5.0.0-alpha.0",
66
+ "@middy/core": "5.0.0-alpha.2",
73
67
  "@types/aws-lambda": "^8.10.101",
74
68
  "aws-xray-sdk": "^3.3.3"
75
69
  },
76
- "gitHead": "08c35e3dba9efdad0b86666ce206ce302cc65d07"
70
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
77
71
  }
package/index.cjs DELETED
@@ -1,62 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: ()=>_default
8
- });
9
- const _util = require("@middy/util");
10
- const _clientServicediscovery = require("@aws-sdk/client-servicediscovery");
11
- const defaults = {
12
- AwsClient: _clientServicediscovery.ServiceDiscoveryClient,
13
- awsClientOptions: {},
14
- awsClientAssumeRole: undefined,
15
- awsClientCapture: undefined,
16
- fetchData: {},
17
- disablePrefetch: false,
18
- cacheKey: 'cloud-map',
19
- cacheExpiry: -1,
20
- setToContext: false
21
- };
22
- const serviceDiscoveryMiddleware = (opts = {})=>{
23
- const options = {
24
- ...defaults,
25
- ...opts
26
- };
27
- const fetch = (request, cachedValues = {})=>{
28
- const values = {};
29
- for (const internalKey of Object.keys(options.fetchData)){
30
- if (cachedValues[internalKey]) continue;
31
- values[internalKey] = client.send(new _clientServicediscovery.DiscoverInstancesCommand(options.fetchData[internalKey])).then((resp)=>resp.Instances).catch((e)=>{
32
- const value = (0, _util.getCache)(options.cacheKey).value ?? {};
33
- value[internalKey] = undefined;
34
- (0, _util.modifyCache)(options.cacheKey, value);
35
- throw e;
36
- });
37
- }
38
- return values;
39
- };
40
- let prefetch, client;
41
- if ((0, _util.canPrefetch)(options)) {
42
- client = (0, _util.createPrefetchClient)(options);
43
- prefetch = (0, _util.processCache)(options, fetch);
44
- }
45
- const serviceDiscoveryMiddlewareBefore = async (request)=>{
46
- if (!client) {
47
- client = await (0, _util.createClient)(options, request);
48
- }
49
- const { value } = prefetch ?? (0, _util.processCache)(options, fetch, request);
50
- Object.assign(request.internal, value);
51
- if (options.setToContext) {
52
- const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
53
- if (options.setToContext) Object.assign(request.context, data);
54
- }
55
- prefetch = null;
56
- };
57
- return {
58
- before: serviceDiscoveryMiddlewareBefore
59
- };
60
- };
61
- const _default = serviceDiscoveryMiddleware;
62
-