@middy/appconfig 4.2.7 → 5.0.0-alpha.0

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 (4) hide show
  1. package/index.cjs +35 -14
  2. package/index.d.ts +7 -7
  3. package/index.js +35 -14
  4. package/package.json +6 -6
package/index.cjs CHANGED
@@ -7,9 +7,9 @@ Object.defineProperty(module, "exports", {
7
7
  get: ()=>_default
8
8
  });
9
9
  const _util = require("@middy/util");
10
- const _clientAppconfig = require("@aws-sdk/client-appconfig");
10
+ const _clientAppconfigdata = require("@aws-sdk/client-appconfigdata");
11
11
  const defaults = {
12
- AwsClient: _clientAppconfig.AppConfigClient,
12
+ AwsClient: _clientAppconfigdata.AppConfigDataClient,
13
13
  awsClientOptions: {},
14
14
  awsClientAssumeRole: undefined,
15
15
  awsClientCapture: undefined,
@@ -25,22 +25,43 @@ const appConfigMiddleware = (opts = {})=>{
25
25
  ...defaults,
26
26
  ...opts
27
27
  };
28
+ const configurationTokenCache = {};
29
+ const configurationCache = {};
30
+ function fetchLatestConfiguration(configToken, internalKey) {
31
+ return client.send(new _clientAppconfigdata.GetLatestConfigurationCommand({
32
+ ConfigurationToken: configToken
33
+ })).then((configResp)=>{
34
+ configurationTokenCache[internalKey] = configResp.NextPollConfigurationToken;
35
+ if (configResp.Configuration.length === 0) {
36
+ return configurationCache[internalKey];
37
+ }
38
+ let value = String.fromCharCode.apply(null, configResp.Configuration);
39
+ if (contentTypePattern.test(configResp.ContentType)) {
40
+ value = (0, _util.jsonSafeParse)(value);
41
+ }
42
+ configurationCache[internalKey] = value;
43
+ return value;
44
+ }).catch((e)=>{
45
+ const value = (0, _util.getCache)(options.cacheKey).value ?? {};
46
+ value[internalKey] = undefined;
47
+ (0, _util.modifyCache)(options.cacheKey, value);
48
+ throw e;
49
+ });
50
+ }
28
51
  const fetch = (request, cachedValues = {})=>{
29
52
  const values = {};
30
53
  for (const internalKey of Object.keys(options.fetchData)){
31
54
  if (cachedValues[internalKey]) continue;
32
- values[internalKey] = client.send(new _clientAppconfig.GetConfigurationCommand(options.fetchData[internalKey])).then((resp)=>{
33
- let value = String.fromCharCode.apply(null, resp.Content);
34
- if (contentTypePattern.test(resp.ContentType)) {
35
- value = (0, _util.jsonSafeParse)(value);
36
- }
37
- return value;
38
- }).catch((e)=>{
39
- const value = (0, _util.getCache)(options.cacheKey).value ?? {};
40
- value[internalKey] = undefined;
41
- (0, _util.modifyCache)(options.cacheKey, value);
42
- throw e;
43
- });
55
+ if (configurationTokenCache[internalKey] == null) {
56
+ values[internalKey] = client.send(new _clientAppconfigdata.StartConfigurationSessionCommand(options.fetchData[internalKey])).then((configSessionResp)=>fetchLatestConfiguration(configSessionResp.InitialConfigurationToken, internalKey)).catch((e)=>{
57
+ const value = (0, _util.getCache)(options.cacheKey).value ?? {};
58
+ value[internalKey] = undefined;
59
+ (0, _util.modifyCache)(options.cacheKey, value);
60
+ throw e;
61
+ });
62
+ continue;
63
+ }
64
+ values[internalKey] = fetchLatestConfiguration(configurationTokenCache[internalKey], internalKey);
44
65
  }
45
66
  return values;
46
67
  };
package/index.d.ts CHANGED
@@ -2,16 +2,16 @@ import middy from '@middy/core'
2
2
  import { Options as MiddyOptions } from '@middy/util'
3
3
  import { Context as LambdaContext } from 'aws-lambda'
4
4
  import {
5
- AppConfigClient,
6
- AppConfigClientConfig,
7
- GetConfigurationRequest
8
- } from '@aws-sdk/client-appconfig'
5
+ AppConfigDataClient,
6
+ AppConfigDataClientConfig,
7
+ StartConfigurationSessionRequest
8
+ } from '@aws-sdk/client-appconfigdata'
9
9
 
10
- export type Options<AwsAppConfigClient = AppConfigClient>
11
- = Omit<MiddyOptions<AwsAppConfigClient, AppConfigClientConfig>, 'fetchData'>
10
+ export type Options<AwsAppConfigClient = AppConfigDataClient>
11
+ = Omit<MiddyOptions<AwsAppConfigClient, AppConfigDataClientConfig>, 'fetchData'>
12
12
  & {
13
13
  fetchData?: {
14
- [configurationRequestKey: string]: GetConfigurationRequest
14
+ [configurationRequestKey: string]: StartConfigurationSessionRequest
15
15
  }
16
16
  }
17
17
 
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
2
- import { AppConfigClient, GetConfigurationCommand } from '@aws-sdk/client-appconfig';
2
+ import { StartConfigurationSessionCommand, GetLatestConfigurationCommand, AppConfigDataClient } from '@aws-sdk/client-appconfigdata';
3
3
  const defaults = {
4
- AwsClient: AppConfigClient,
4
+ AwsClient: AppConfigDataClient,
5
5
  awsClientOptions: {},
6
6
  awsClientAssumeRole: undefined,
7
7
  awsClientCapture: undefined,
@@ -17,22 +17,43 @@ const appConfigMiddleware = (opts = {})=>{
17
17
  ...defaults,
18
18
  ...opts
19
19
  };
20
+ const configurationTokenCache = {};
21
+ const configurationCache = {};
22
+ function fetchLatestConfiguration(configToken, internalKey) {
23
+ return client.send(new GetLatestConfigurationCommand({
24
+ ConfigurationToken: configToken
25
+ })).then((configResp)=>{
26
+ configurationTokenCache[internalKey] = configResp.NextPollConfigurationToken;
27
+ if (configResp.Configuration.length === 0) {
28
+ return configurationCache[internalKey];
29
+ }
30
+ let value = String.fromCharCode.apply(null, configResp.Configuration);
31
+ if (contentTypePattern.test(configResp.ContentType)) {
32
+ value = jsonSafeParse(value);
33
+ }
34
+ configurationCache[internalKey] = value;
35
+ return value;
36
+ }).catch((e)=>{
37
+ const value = getCache(options.cacheKey).value ?? {};
38
+ value[internalKey] = undefined;
39
+ modifyCache(options.cacheKey, value);
40
+ throw e;
41
+ });
42
+ }
20
43
  const fetch = (request, cachedValues = {})=>{
21
44
  const values = {};
22
45
  for (const internalKey of Object.keys(options.fetchData)){
23
46
  if (cachedValues[internalKey]) continue;
24
- values[internalKey] = client.send(new GetConfigurationCommand(options.fetchData[internalKey])).then((resp)=>{
25
- let value = String.fromCharCode.apply(null, resp.Content);
26
- if (contentTypePattern.test(resp.ContentType)) {
27
- value = jsonSafeParse(value);
28
- }
29
- return value;
30
- }).catch((e)=>{
31
- const value = getCache(options.cacheKey).value ?? {};
32
- value[internalKey] = undefined;
33
- modifyCache(options.cacheKey, value);
34
- throw e;
35
- });
47
+ if (configurationTokenCache[internalKey] == null) {
48
+ values[internalKey] = client.send(new StartConfigurationSessionCommand(options.fetchData[internalKey])).then((configSessionResp)=>fetchLatestConfiguration(configSessionResp.InitialConfigurationToken, internalKey)).catch((e)=>{
49
+ const value = getCache(options.cacheKey).value ?? {};
50
+ value[internalKey] = undefined;
51
+ modifyCache(options.cacheKey, value);
52
+ throw e;
53
+ });
54
+ continue;
55
+ }
56
+ values[internalKey] = fetchLatestConfiguration(configurationTokenCache[internalKey], internalKey);
36
57
  }
37
58
  return values;
38
59
  };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@middy/appconfig",
3
- "version": "4.2.7",
3
+ "version": "5.0.0-alpha.0",
4
4
  "description": "AppConfig middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=18"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
@@ -64,13 +64,13 @@
64
64
  "url": "https://github.com/sponsors/willfarrell"
65
65
  },
66
66
  "dependencies": {
67
- "@middy/util": "4.2.7"
67
+ "@middy/util": "5.0.0-alpha.0"
68
68
  },
69
69
  "devDependencies": {
70
- "@aws-sdk/client-appconfig": "^3.0.0",
71
- "@middy/core": "4.2.7",
70
+ "@aws-sdk/client-appconfigdata": "^3.0.0",
71
+ "@middy/core": "5.0.0-alpha.0",
72
72
  "@types/aws-lambda": "^8.10.101",
73
73
  "aws-xray-sdk": "^3.3.3"
74
74
  },
75
- "gitHead": "f17e0e371d1d1c7a67365c895775a847aab5aa0b"
75
+ "gitHead": "08c35e3dba9efdad0b86666ce206ce302cc65d07"
76
76
  }