@middy/appconfig 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 +3 -2
- package/index.d.ts +28 -12
- package/index.js +8 -4
- package/package.json +4 -10
- package/index.cjs +0 -90
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://
|
|
23
|
-
<img src="https://
|
|
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
|
@@ -7,22 +7,38 @@ import {
|
|
|
7
7
|
StartConfigurationSessionRequest
|
|
8
8
|
} from '@aws-sdk/client-appconfigdata'
|
|
9
9
|
|
|
10
|
-
export type
|
|
11
|
-
|
|
12
|
-
& {
|
|
13
|
-
fetchData?: {
|
|
14
|
-
[configurationRequestKey: string]: StartConfigurationSessionRequest
|
|
15
|
-
}
|
|
16
|
-
}
|
|
10
|
+
export type ParamType<T> = StartConfigurationSessionRequest & { __returnType?: T }
|
|
11
|
+
export declare function appConfigReq<T> (req: StartConfigurationSessionRequest): ParamType<T>
|
|
17
12
|
|
|
18
|
-
export
|
|
19
|
-
|
|
13
|
+
export interface AppConfigOptions<AwsAppConfigClient = AppConfigDataClient>
|
|
14
|
+
extends Omit<MiddyOptions<AwsAppConfigClient, AppConfigDataClientConfig>, 'fetchData'> {
|
|
15
|
+
fetchData?: { [key: string]: StartConfigurationSessionRequest | ParamType<unknown> }
|
|
20
16
|
}
|
|
21
|
-
|
|
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
|
-
|
|
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
|
@@ -8,6 +8,7 @@ const defaults = {
|
|
|
8
8
|
fetchData: {},
|
|
9
9
|
disablePrefetch: false,
|
|
10
10
|
cacheKey: 'appconfig',
|
|
11
|
+
cacheKeyExpiry: {},
|
|
11
12
|
cacheExpiry: -1,
|
|
12
13
|
setToContext: false
|
|
13
14
|
};
|
|
@@ -57,26 +58,29 @@ const appConfigMiddleware = (opts = {})=>{
|
|
|
57
58
|
}
|
|
58
59
|
return values;
|
|
59
60
|
};
|
|
60
|
-
let
|
|
61
|
+
let client;
|
|
61
62
|
if (canPrefetch(options)) {
|
|
62
63
|
client = createPrefetchClient(options);
|
|
63
|
-
|
|
64
|
+
processCache(options, fetch);
|
|
64
65
|
}
|
|
65
66
|
const appConfigMiddlewareBefore = async (request)=>{
|
|
66
67
|
if (!client) {
|
|
67
68
|
client = await createClient(options, request);
|
|
68
69
|
}
|
|
69
|
-
const { value
|
|
70
|
+
const { value } = processCache(options, fetch, request);
|
|
70
71
|
Object.assign(request.internal, value);
|
|
71
72
|
if (options.setToContext) {
|
|
72
73
|
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
73
74
|
Object.assign(request.context, data);
|
|
74
75
|
}
|
|
75
|
-
prefetch = null;
|
|
76
76
|
};
|
|
77
77
|
return {
|
|
78
78
|
before: appConfigMiddlewareBefore
|
|
79
79
|
};
|
|
80
80
|
};
|
|
81
81
|
export default appConfigMiddleware;
|
|
82
|
+
// used for TS type inference (see index.d.ts)
|
|
83
|
+
export function appConfigReq(req) {
|
|
84
|
+
return req;
|
|
85
|
+
} // # sourceMappingURL=index.js.map
|
|
82
86
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/appconfig",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.2",
|
|
4
4
|
"description": "AppConfig 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": {
|
|
@@ -64,13 +58,13 @@
|
|
|
64
58
|
"url": "https://github.com/sponsors/willfarrell"
|
|
65
59
|
},
|
|
66
60
|
"dependencies": {
|
|
67
|
-
"@middy/util": "5.0.0-alpha.
|
|
61
|
+
"@middy/util": "5.0.0-alpha.2"
|
|
68
62
|
},
|
|
69
63
|
"devDependencies": {
|
|
70
64
|
"@aws-sdk/client-appconfigdata": "^3.0.0",
|
|
71
|
-
"@middy/core": "5.0.0-alpha.
|
|
65
|
+
"@middy/core": "5.0.0-alpha.2",
|
|
72
66
|
"@types/aws-lambda": "^8.10.101",
|
|
73
67
|
"aws-xray-sdk": "^3.3.3"
|
|
74
68
|
},
|
|
75
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
76
70
|
}
|
package/index.cjs
DELETED
|
@@ -1,90 +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 _clientAppconfigdata = require("@aws-sdk/client-appconfigdata");
|
|
11
|
-
const defaults = {
|
|
12
|
-
AwsClient: _clientAppconfigdata.AppConfigDataClient,
|
|
13
|
-
awsClientOptions: {},
|
|
14
|
-
awsClientAssumeRole: undefined,
|
|
15
|
-
awsClientCapture: undefined,
|
|
16
|
-
fetchData: {},
|
|
17
|
-
disablePrefetch: false,
|
|
18
|
-
cacheKey: 'appconfig',
|
|
19
|
-
cacheExpiry: -1,
|
|
20
|
-
setToContext: false
|
|
21
|
-
};
|
|
22
|
-
const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
|
|
23
|
-
const appConfigMiddleware = (opts = {})=>{
|
|
24
|
-
const options = {
|
|
25
|
-
...defaults,
|
|
26
|
-
...opts
|
|
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
|
-
}
|
|
51
|
-
const fetch = (request, cachedValues = {})=>{
|
|
52
|
-
const values = {};
|
|
53
|
-
for (const internalKey of Object.keys(options.fetchData)){
|
|
54
|
-
if (cachedValues[internalKey]) continue;
|
|
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);
|
|
65
|
-
}
|
|
66
|
-
return values;
|
|
67
|
-
};
|
|
68
|
-
let prefetch, client;
|
|
69
|
-
if ((0, _util.canPrefetch)(options)) {
|
|
70
|
-
client = (0, _util.createPrefetchClient)(options);
|
|
71
|
-
prefetch = (0, _util.processCache)(options, fetch);
|
|
72
|
-
}
|
|
73
|
-
const appConfigMiddlewareBefore = async (request)=>{
|
|
74
|
-
if (!client) {
|
|
75
|
-
client = await (0, _util.createClient)(options, request);
|
|
76
|
-
}
|
|
77
|
-
const { value } = prefetch ?? (0, _util.processCache)(options, fetch, request);
|
|
78
|
-
Object.assign(request.internal, value);
|
|
79
|
-
if (options.setToContext) {
|
|
80
|
-
const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
|
|
81
|
-
Object.assign(request.context, data);
|
|
82
|
-
}
|
|
83
|
-
prefetch = null;
|
|
84
|
-
};
|
|
85
|
-
return {
|
|
86
|
-
before: appConfigMiddlewareBefore
|
|
87
|
-
};
|
|
88
|
-
};
|
|
89
|
-
const _default = appConfigMiddleware;
|
|
90
|
-
|