@middy/appconfig 6.1.5 → 6.2.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.
- package/index.d.ts +49 -34
- package/index.js +114 -114
- package/package.json +65 -68
package/index.d.ts
CHANGED
|
@@ -1,44 +1,59 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from
|
|
1
|
+
import type {
|
|
2
|
+
AppConfigDataClient,
|
|
3
|
+
AppConfigDataClientConfig,
|
|
4
|
+
StartConfigurationSessionRequest,
|
|
5
|
+
} from "@aws-sdk/client-appconfigdata";
|
|
6
|
+
import type middy from "@middy/core";
|
|
7
|
+
import type { Options as MiddyOptions } from "@middy/util";
|
|
8
|
+
import type { Context as LambdaContext } from "aws-lambda";
|
|
9
9
|
|
|
10
|
-
export type ParamType<T> = StartConfigurationSessionRequest & {
|
|
11
|
-
|
|
10
|
+
export type ParamType<T> = StartConfigurationSessionRequest & {
|
|
11
|
+
__returnType?: T;
|
|
12
|
+
};
|
|
13
|
+
export declare function appConfigReq<T>(
|
|
14
|
+
req: StartConfigurationSessionRequest,
|
|
15
|
+
): ParamType<T>;
|
|
12
16
|
|
|
13
17
|
export interface AppConfigOptions<AwsAppConfigClient = AppConfigDataClient>
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
extends Omit<
|
|
19
|
+
MiddyOptions<AwsAppConfigClient, AppConfigDataClientConfig>,
|
|
20
|
+
"fetchData"
|
|
21
|
+
> {
|
|
22
|
+
fetchData?: {
|
|
23
|
+
[key: string]: StartConfigurationSessionRequest | ParamType<unknown>;
|
|
24
|
+
};
|
|
16
25
|
}
|
|
17
26
|
|
|
18
27
|
export type Context<TOptions extends AppConfigOptions | undefined> =
|
|
19
|
-
TOptions extends { setToContext: true }
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
TOptions extends { setToContext: true }
|
|
29
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
30
|
+
? LambdaContext & {
|
|
31
|
+
[Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
|
|
32
|
+
? T
|
|
33
|
+
: unknown;
|
|
34
|
+
}
|
|
35
|
+
: never
|
|
36
|
+
: LambdaContext;
|
|
28
37
|
|
|
29
38
|
export type Internal<TOptions extends AppConfigOptions | undefined> =
|
|
30
|
-
TOptions extends AppConfigOptions
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
TOptions extends AppConfigOptions
|
|
40
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
41
|
+
? {
|
|
42
|
+
[Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
|
|
43
|
+
? T
|
|
44
|
+
: unknown;
|
|
45
|
+
}
|
|
46
|
+
: {}
|
|
47
|
+
: {};
|
|
39
48
|
|
|
40
|
-
declare function appConfigMiddleware<TOptions extends AppConfigOptions>
|
|
41
|
-
|
|
42
|
-
): middy.MiddlewareObj<
|
|
49
|
+
declare function appConfigMiddleware<TOptions extends AppConfigOptions>(
|
|
50
|
+
options?: TOptions,
|
|
51
|
+
): middy.MiddlewareObj<
|
|
52
|
+
unknown,
|
|
53
|
+
any,
|
|
54
|
+
Error,
|
|
55
|
+
Context<TOptions>,
|
|
56
|
+
Internal<TOptions>
|
|
57
|
+
>;
|
|
43
58
|
|
|
44
|
-
export default appConfigMiddleware
|
|
59
|
+
export default appConfigMiddleware;
|
package/index.js
CHANGED
|
@@ -1,129 +1,129 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
getInternal,
|
|
7
|
-
processCache,
|
|
8
|
-
modifyCache,
|
|
9
|
-
jsonSafeParse,
|
|
10
|
-
catchInvalidSignatureException
|
|
11
|
-
} from '@middy/util'
|
|
2
|
+
AppConfigDataClient,
|
|
3
|
+
GetLatestConfigurationCommand,
|
|
4
|
+
StartConfigurationSessionCommand,
|
|
5
|
+
} from "@aws-sdk/client-appconfigdata";
|
|
12
6
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
canPrefetch,
|
|
8
|
+
catchInvalidSignatureException,
|
|
9
|
+
createClient,
|
|
10
|
+
createPrefetchClient,
|
|
11
|
+
getCache,
|
|
12
|
+
getInternal,
|
|
13
|
+
jsonSafeParse,
|
|
14
|
+
modifyCache,
|
|
15
|
+
processCache,
|
|
16
|
+
} from "@middy/util";
|
|
17
17
|
|
|
18
18
|
const defaults = {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
const contentTypePattern = /^application\/(.+\+)?json($|;.+)
|
|
19
|
+
AwsClient: AppConfigDataClient,
|
|
20
|
+
awsClientOptions: {},
|
|
21
|
+
awsClientAssumeRole: undefined,
|
|
22
|
+
awsClientCapture: undefined,
|
|
23
|
+
fetchData: {},
|
|
24
|
+
disablePrefetch: false,
|
|
25
|
+
cacheKey: "appconfig",
|
|
26
|
+
cacheKeyExpiry: {},
|
|
27
|
+
cacheExpiry: -1,
|
|
28
|
+
setToContext: false,
|
|
29
|
+
};
|
|
30
|
+
const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
|
|
31
31
|
const appConfigMiddleware = (opts = {}) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
const options = {
|
|
33
|
+
...defaults,
|
|
34
|
+
...opts,
|
|
35
|
+
};
|
|
36
|
+
const configurationTokenCache = {};
|
|
37
|
+
const configurationCache = {};
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
39
|
+
function fetchLatestConfigurationRequest(configToken, internalKey) {
|
|
40
|
+
const command = new GetLatestConfigurationCommand({
|
|
41
|
+
ConfigurationToken: configToken,
|
|
42
|
+
});
|
|
43
|
+
return client
|
|
44
|
+
.send(command)
|
|
45
|
+
.catch((e) => catchInvalidSignatureException(e, client, command))
|
|
46
|
+
.then((configResp) => {
|
|
47
|
+
configurationTokenCache[internalKey] =
|
|
48
|
+
configResp.NextPollConfigurationToken;
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
if (configResp.Configuration.length === 0) {
|
|
51
|
+
return configurationCache[internalKey];
|
|
52
|
+
}
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
54
|
+
let value = String.fromCharCode.apply(null, configResp.Configuration);
|
|
55
|
+
if (contentTypePattern.test(configResp.ContentType)) {
|
|
56
|
+
value = jsonSafeParse(value);
|
|
57
|
+
}
|
|
58
|
+
configurationCache[internalKey] = value;
|
|
59
|
+
return value;
|
|
60
|
+
})
|
|
61
|
+
.catch((e) => {
|
|
62
|
+
const value = getCache(options.cacheKey).value ?? {};
|
|
63
|
+
value[internalKey] = undefined;
|
|
64
|
+
modifyCache(options.cacheKey, value);
|
|
65
|
+
throw e;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
69
|
+
const fetchRequest = (request, cachedValues = {}) => {
|
|
70
|
+
const values = {};
|
|
71
|
+
for (const internalKey of Object.keys(options.fetchData)) {
|
|
72
|
+
if (cachedValues[internalKey]) continue;
|
|
73
|
+
if (configurationTokenCache[internalKey] == null) {
|
|
74
|
+
const command = new StartConfigurationSessionCommand(
|
|
75
|
+
options.fetchData[internalKey],
|
|
76
|
+
);
|
|
77
|
+
values[internalKey] = client
|
|
78
|
+
.send(command)
|
|
79
|
+
.catch((e) => catchInvalidSignatureException(e, client, command))
|
|
80
|
+
.then((configSessionResp) =>
|
|
81
|
+
fetchLatestConfigurationRequest(
|
|
82
|
+
configSessionResp.InitialConfigurationToken,
|
|
83
|
+
internalKey,
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
.catch((e) => {
|
|
87
|
+
const value = getCache(options.cacheKey).value ?? {};
|
|
88
|
+
value[internalKey] = undefined;
|
|
89
|
+
modifyCache(options.cacheKey, value);
|
|
90
|
+
throw e;
|
|
91
|
+
});
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
export default appConfigMiddleware
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
values[internalKey] = fetchLatestConfigurationRequest(
|
|
96
|
+
configurationTokenCache[internalKey],
|
|
97
|
+
internalKey,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return values;
|
|
101
|
+
};
|
|
102
|
+
let client;
|
|
103
|
+
if (canPrefetch(options)) {
|
|
104
|
+
client = createPrefetchClient(options);
|
|
105
|
+
processCache(options, fetchRequest);
|
|
106
|
+
}
|
|
107
|
+
const appConfigMiddlewareBefore = async (request) => {
|
|
108
|
+
if (!client) {
|
|
109
|
+
client = await createClient(options, request);
|
|
110
|
+
}
|
|
111
|
+
const { value } = processCache(options, fetchRequest, request);
|
|
112
|
+
Object.assign(request.internal, value);
|
|
113
|
+
if (options.setToContext) {
|
|
114
|
+
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
115
|
+
Object.assign(request.context, data);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return {
|
|
119
|
+
before: appConfigMiddlewareBefore,
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
export default appConfigMiddleware;
|
|
123
123
|
|
|
124
124
|
// used for TS type inference (see index.d.ts)
|
|
125
|
-
export function appConfigReq
|
|
126
|
-
|
|
125
|
+
export function appConfigReq(req) {
|
|
126
|
+
return req;
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
// # sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,70 +1,67 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
"aws-xray-sdk": "^3.3.3"
|
|
68
|
-
},
|
|
69
|
-
"gitHead": "4780887d818be31d9b6f1e2ced99c954d11a4a81"
|
|
2
|
+
"name": "@middy/appconfig",
|
|
3
|
+
"version": "6.2.0",
|
|
4
|
+
"description": "AppConfig middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"AppConfig"
|
|
43
|
+
],
|
|
44
|
+
"author": {
|
|
45
|
+
"name": "Middy contributors",
|
|
46
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
51
|
+
"directory": "packages/appconfig"
|
|
52
|
+
},
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://middy.js.org",
|
|
57
|
+
"funding": {
|
|
58
|
+
"type": "github",
|
|
59
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@aws-sdk/client-appconfigdata": "^3.0.0",
|
|
63
|
+
"@types/aws-lambda": "^8.10.101",
|
|
64
|
+
"aws-xray-sdk": "^3.3.3"
|
|
65
|
+
},
|
|
66
|
+
"gitHead": "4780887d818be31d9b6f1e2ced99c954d11a4a81"
|
|
70
67
|
}
|