@middy/appconfig 4.6.5 → 5.0.0-alpha.1

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.d.ts +32 -16
  2. package/index.js +122 -58
  3. package/package.json +6 -12
  4. package/index.cjs +0 -72
package/index.d.ts CHANGED
@@ -2,27 +2,43 @@ 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'>
12
- & {
13
- fetchData?: {
14
- [configurationRequestKey: string]: GetConfigurationRequest
15
- }
16
- }
10
+ export type ParamType<T> = StartConfigurationSessionRequest & { __returnType?: T }
11
+ export declare function appConfigReq<T> (req: StartConfigurationSessionRequest): ParamType<T>
17
12
 
18
- export type Context<TOptions extends Options | undefined> = TOptions extends {
19
- setToContext: true
13
+ export interface AppConfigOptions<AwsAppConfigClient = AppConfigDataClient>
14
+ extends Omit<MiddyOptions<AwsAppConfigClient, AppConfigDataClientConfig>, 'fetchData'> {
15
+ fetchData?: { [key: string]: StartConfigurationSessionRequest | ParamType<unknown> }
20
16
  }
21
- ? LambdaContext & Record<keyof TOptions['fetchData'], any>
17
+
18
+ export type Context<TOptions extends AppConfigOptions | undefined> =
19
+ TOptions extends { setToContext: true }
20
+ ? TOptions extends { fetchData: infer TFetchData }
21
+ ? LambdaContext & {
22
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
23
+ ? T
24
+ : unknown
25
+ }
26
+ : never
22
27
  : LambdaContext
23
28
 
24
- declare function appConfigMiddleware<TOptions extends Options | undefined> (
29
+ export type Internal<TOptions extends AppConfigOptions | undefined> =
30
+ TOptions extends AppConfigOptions
31
+ ? TOptions extends { fetchData: infer TFetchData }
32
+ ? {
33
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
34
+ ? T
35
+ : unknown
36
+ }
37
+ : {}
38
+ : {}
39
+
40
+ declare function appConfigMiddleware<TOptions extends AppConfigOptions> (
25
41
  options?: TOptions
26
- ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
42
+ ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
27
43
 
28
44
  export default appConfigMiddleware
package/index.js CHANGED
@@ -1,62 +1,126 @@
1
- import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
2
- import { AppConfigClient, GetConfigurationCommand } from '@aws-sdk/client-appconfig';
1
+ import {
2
+ canPrefetch,
3
+ createPrefetchClient,
4
+ createClient,
5
+ getCache,
6
+ getInternal,
7
+ processCache,
8
+ modifyCache,
9
+ jsonSafeParse
10
+ } from '@middy/util'
11
+ import {
12
+ StartConfigurationSessionCommand,
13
+ GetLatestConfigurationCommand,
14
+ AppConfigDataClient
15
+ } from '@aws-sdk/client-appconfigdata'
16
+
3
17
  const defaults = {
4
- AwsClient: AppConfigClient,
5
- awsClientOptions: {},
6
- awsClientAssumeRole: undefined,
7
- awsClientCapture: undefined,
8
- fetchData: {},
9
- disablePrefetch: false,
10
- cacheKey: 'appconfig',
11
- cacheKeyExpiry: {},
12
- cacheExpiry: -1,
13
- setToContext: false
14
- };
15
- const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
16
- const appConfigMiddleware = (opts = {})=>{
17
- const options = {
18
- ...defaults,
19
- ...opts
20
- };
21
- const fetch = (request, cachedValues = {})=>{
22
- const values = {};
23
- for (const internalKey of Object.keys(options.fetchData)){
24
- if (cachedValues[internalKey]) continue;
25
- values[internalKey] = client.send(new GetConfigurationCommand(options.fetchData[internalKey])).then((resp)=>{
26
- let value = String.fromCharCode.apply(null, resp.Content);
27
- if (contentTypePattern.test(resp.ContentType)) {
28
- value = jsonSafeParse(value);
29
- }
30
- return value;
31
- }).catch((e)=>{
32
- const value = getCache(options.cacheKey).value ?? {};
33
- value[internalKey] = undefined;
34
- modifyCache(options.cacheKey, value);
35
- throw e;
36
- });
37
- }
38
- return values;
39
- };
40
- let client;
41
- if (canPrefetch(options)) {
42
- client = createPrefetchClient(options);
43
- processCache(options, fetch);
44
- }
45
- const appConfigMiddlewareBefore = async (request)=>{
46
- if (!client) {
47
- client = await createClient(options, request);
18
+ AwsClient: AppConfigDataClient,
19
+ awsClientOptions: {},
20
+ awsClientAssumeRole: undefined,
21
+ awsClientCapture: undefined,
22
+ fetchData: {},
23
+ disablePrefetch: false,
24
+ cacheKey: 'appconfig',
25
+ cacheKeyExpiry: {},
26
+ cacheExpiry: -1,
27
+ setToContext: false
28
+ }
29
+ const contentTypePattern = /^application\/(.+\+)?json($|;.+)/
30
+ const appConfigMiddleware = (opts = {}) => {
31
+ const options = {
32
+ ...defaults,
33
+ ...opts
34
+ }
35
+ const configurationTokenCache = {}
36
+ const configurationCache = {}
37
+
38
+ function fetchLatestConfiguration (configToken, internalKey) {
39
+ return client
40
+ .send(
41
+ new GetLatestConfigurationCommand({
42
+ ConfigurationToken: configToken
43
+ })
44
+ )
45
+ .then((configResp) => {
46
+ configurationTokenCache[internalKey] =
47
+ configResp.NextPollConfigurationToken
48
+
49
+ if (configResp.Configuration.length === 0) {
50
+ return configurationCache[internalKey]
48
51
  }
49
- const { value } = processCache(options, fetch, request);
50
- Object.assign(request.internal, value);
51
- if (options.setToContext) {
52
- const data = await getInternal(Object.keys(options.fetchData), request);
53
- Object.assign(request.context, data);
52
+
53
+ let value = String.fromCharCode.apply(null, configResp.Configuration)
54
+ if (contentTypePattern.test(configResp.ContentType)) {
55
+ value = jsonSafeParse(value)
54
56
  }
55
- };
56
- return {
57
- before: appConfigMiddlewareBefore
58
- };
59
- };
60
- export default appConfigMiddleware // # sourceMappingURL=index.js.map
61
- ;
57
+ configurationCache[internalKey] = value
58
+ return value
59
+ })
60
+ .catch((e) => {
61
+ const value = getCache(options.cacheKey).value ?? {}
62
+ value[internalKey] = undefined
63
+ modifyCache(options.cacheKey, value)
64
+ throw e
65
+ })
66
+ }
67
+
68
+ const fetch = (request, cachedValues = {}) => {
69
+ const values = {}
70
+ for (const internalKey of Object.keys(options.fetchData)) {
71
+ if (cachedValues[internalKey]) continue
72
+ if (configurationTokenCache[internalKey] == null) {
73
+ values[internalKey] = client
74
+ .send(
75
+ new StartConfigurationSessionCommand(options.fetchData[internalKey])
76
+ )
77
+ .then((configSessionResp) =>
78
+ fetchLatestConfiguration(
79
+ configSessionResp.InitialConfigurationToken,
80
+ internalKey
81
+ )
82
+ )
83
+ .catch((e) => {
84
+ const value = getCache(options.cacheKey).value ?? {}
85
+ value[internalKey] = undefined
86
+ modifyCache(options.cacheKey, value)
87
+ throw e
88
+ })
89
+
90
+ continue
91
+ }
92
+ values[internalKey] = fetchLatestConfiguration(
93
+ configurationTokenCache[internalKey],
94
+ internalKey
95
+ )
96
+ }
97
+ return values
98
+ }
99
+ let client
100
+ if (canPrefetch(options)) {
101
+ client = createPrefetchClient(options)
102
+ processCache(options, fetch)
103
+ }
104
+ const appConfigMiddlewareBefore = async (request) => {
105
+ if (!client) {
106
+ client = await createClient(options, request)
107
+ }
108
+ const { value } = processCache(options, fetch, request)
109
+ Object.assign(request.internal, value)
110
+ if (options.setToContext) {
111
+ const data = await getInternal(Object.keys(options.fetchData), request)
112
+ Object.assign(request.context, data)
113
+ }
114
+ }
115
+ return {
116
+ before: appConfigMiddlewareBefore
117
+ }
118
+ }
119
+ export default appConfigMiddleware
120
+
121
+ // used for TS type inference (see index.d.ts)
122
+ export function appConfigReq (req) {
123
+ return req
124
+ }
62
125
 
126
+ // # sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,33 +1,27 @@
1
1
  {
2
2
  "name": "@middy/appconfig",
3
- "version": "4.6.5",
3
+ "version": "5.0.0-alpha.1",
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": {
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": {
@@ -64,13 +58,13 @@
64
58
  "url": "https://github.com/sponsors/willfarrell"
65
59
  },
66
60
  "dependencies": {
67
- "@middy/util": "4.6.5"
61
+ "@middy/util": "5.0.0-alpha.1"
68
62
  },
69
63
  "devDependencies": {
70
- "@aws-sdk/client-appconfig": "^3.0.0",
71
- "@middy/core": "4.6.5",
64
+ "@aws-sdk/client-appconfigdata": "^3.0.0",
65
+ "@middy/core": "5.0.0-alpha.1",
72
66
  "@types/aws-lambda": "^8.10.101",
73
67
  "aws-xray-sdk": "^3.3.3"
74
68
  },
75
- "gitHead": "573d7b0bb243d8c5a9bcb00cf29d031aa7a0c606"
69
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
76
70
  }
package/index.cjs DELETED
@@ -1,72 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports" // # sourceMappingURL=index.js.map
6
- , {
7
- enumerable: true,
8
- get: function() {
9
- return _default;
10
- }
11
- });
12
- const _util = require("@middy/util");
13
- const _clientappconfig = require("@aws-sdk/client-appconfig");
14
- const defaults = {
15
- AwsClient: _clientappconfig.AppConfigClient,
16
- awsClientOptions: {},
17
- awsClientAssumeRole: undefined,
18
- awsClientCapture: undefined,
19
- fetchData: {},
20
- disablePrefetch: false,
21
- cacheKey: 'appconfig',
22
- cacheKeyExpiry: {},
23
- cacheExpiry: -1,
24
- setToContext: false
25
- };
26
- const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
27
- const appConfigMiddleware = (opts = {})=>{
28
- const options = {
29
- ...defaults,
30
- ...opts
31
- };
32
- const fetch = (request, cachedValues = {})=>{
33
- const values = {};
34
- for (const internalKey of Object.keys(options.fetchData)){
35
- if (cachedValues[internalKey]) continue;
36
- values[internalKey] = client.send(new _clientappconfig.GetConfigurationCommand(options.fetchData[internalKey])).then((resp)=>{
37
- let value = String.fromCharCode.apply(null, resp.Content);
38
- if (contentTypePattern.test(resp.ContentType)) {
39
- value = (0, _util.jsonSafeParse)(value);
40
- }
41
- return value;
42
- }).catch((e)=>{
43
- const value = (0, _util.getCache)(options.cacheKey).value ?? {};
44
- value[internalKey] = undefined;
45
- (0, _util.modifyCache)(options.cacheKey, value);
46
- throw e;
47
- });
48
- }
49
- return values;
50
- };
51
- let client;
52
- if ((0, _util.canPrefetch)(options)) {
53
- client = (0, _util.createPrefetchClient)(options);
54
- (0, _util.processCache)(options, fetch);
55
- }
56
- const appConfigMiddlewareBefore = async (request)=>{
57
- if (!client) {
58
- client = await (0, _util.createClient)(options, request);
59
- }
60
- const { value } = (0, _util.processCache)(options, fetch, request);
61
- Object.assign(request.internal, value);
62
- if (options.setToContext) {
63
- const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
64
- Object.assign(request.context, data);
65
- }
66
- };
67
- return {
68
- before: appConfigMiddlewareBefore
69
- };
70
- };
71
- const _default = appConfigMiddleware;
72
-