@middy/appconfig 4.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2023 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ <div align="center">
2
+ <h1>Middy appconfig middleware</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
4
+ <p><strong>Secrets Manager middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
5
+ <p>
6
+ <a href="https://www.npmjs.com/package/@middy/appconfig?activeTab=versions">
7
+ <img src="https://badge.fury.io/js/%40middy%2Fappconfig.svg" alt="npm version" style="max-width:100%;">
8
+ </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/appconfig">
10
+ <img src="https://packagephobia.com/badge?p=@middy/appconfig" alt="npm install size" style="max-width:100%;">
11
+ </a>
12
+ <a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
13
+ <img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
14
+ </a>
15
+ <br/>
16
+ <a href="https://standardjs.com/">
17
+ <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
18
+ </a>
19
+ <a href="https://snyk.io/test/github/middyjs/middy">
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
+ </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%;">
24
+ </a>
25
+ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
+ <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
27
+ </a>
28
+ <br/>
29
+ <a href="https://gitter.im/middyjs/Lobby">
30
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
31
+ </a>
32
+ <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
33
+ <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
34
+ </a>
35
+ </p>
36
+ <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/appconfig">https://middy.js.org/docs/middlewares/appconfig</a></p>
37
+ </div>
38
+
39
+ ## License
40
+
41
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2023 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
42
+
43
+ <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
44
+ <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
45
+ </a>
package/index.cjs ADDED
@@ -0,0 +1,71 @@
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 _clientAppconfig = require("@aws-sdk/client-appconfig");
11
+ const defaults = {
12
+ AwsClient: _clientAppconfig.AppConfigClient,
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 fetch = (request, cachedValues = {})=>{
29
+ const values = {};
30
+ for (const internalKey of Object.keys(options.fetchData)){
31
+ 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
+ });
44
+ }
45
+ return values;
46
+ };
47
+ let prefetch, client;
48
+ if ((0, _util.canPrefetch)(options)) {
49
+ client = (0, _util.createPrefetchClient)(options);
50
+ prefetch = (0, _util.processCache)(options, fetch);
51
+ }
52
+ const appConfigMiddlewareBefore = async (request)=>{
53
+ if (!client) {
54
+ client = await (0, _util.createClient)(options, request);
55
+ }
56
+ const { value } = prefetch ?? (0, _util.processCache)(options, fetch, request);
57
+ Object.assign(request.internal, value);
58
+ if (options.setToContext) {
59
+ const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
60
+ Object.assign(request.context, data);
61
+ }
62
+ prefetch = null;
63
+ };
64
+ return {
65
+ before: appConfigMiddlewareBefore
66
+ };
67
+ };
68
+ const _default = appConfigMiddleware;
69
+
70
+
71
+ //# sourceMappingURL=index.cjs.map
package/index.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import middy from '@middy/core'
2
+ import { Options as MiddyOptions } from '@middy/util'
3
+ import { Context as LambdaContext } from 'aws-lambda'
4
+ import {
5
+ AppConfigClient,
6
+ AppConfigClientConfig,
7
+ GetConfigurationRequest
8
+ } from '@aws-sdk/client-appconfig'
9
+
10
+ export type Options<AwsAppConfigClient = AppConfigClient>
11
+ = Omit<MiddyOptions<AwsAppConfigClient, AppConfigClientConfig>, 'fetchData'>
12
+ & {
13
+ fetchData?: {
14
+ [configurationRequestKey: string]: GetConfigurationRequest
15
+ }
16
+ }
17
+
18
+ export type Context<TOptions extends Options | undefined> = TOptions extends {
19
+ setToContext: true
20
+ }
21
+ ? LambdaContext & Record<keyof TOptions['fetchData'], any>
22
+ : LambdaContext
23
+
24
+ declare function appConfigMiddleware<TOptions extends Options | undefined> (
25
+ options?: TOptions
26
+ ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
27
+
28
+ export default appConfigMiddleware
package/index.js ADDED
@@ -0,0 +1,63 @@
1
+ import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
2
+ import { AppConfigClient, GetConfigurationCommand } from '@aws-sdk/client-appconfig';
3
+ const defaults = {
4
+ AwsClient: AppConfigClient,
5
+ awsClientOptions: {},
6
+ awsClientAssumeRole: undefined,
7
+ awsClientCapture: undefined,
8
+ fetchData: {},
9
+ disablePrefetch: false,
10
+ cacheKey: 'appconfig',
11
+ cacheExpiry: -1,
12
+ setToContext: false
13
+ };
14
+ const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
15
+ const appConfigMiddleware = (opts = {})=>{
16
+ const options = {
17
+ ...defaults,
18
+ ...opts
19
+ };
20
+ const fetch = (request, cachedValues = {})=>{
21
+ const values = {};
22
+ for (const internalKey of Object.keys(options.fetchData)){
23
+ 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
+ });
36
+ }
37
+ return values;
38
+ };
39
+ let prefetch, client;
40
+ if (canPrefetch(options)) {
41
+ client = createPrefetchClient(options);
42
+ prefetch = processCache(options, fetch);
43
+ }
44
+ const appConfigMiddlewareBefore = async (request)=>{
45
+ if (!client) {
46
+ client = await createClient(options, request);
47
+ }
48
+ const { value } = prefetch ?? processCache(options, fetch, request);
49
+ Object.assign(request.internal, value);
50
+ if (options.setToContext) {
51
+ const data = await getInternal(Object.keys(options.fetchData), request);
52
+ Object.assign(request.context, data);
53
+ }
54
+ prefetch = null;
55
+ };
56
+ return {
57
+ before: appConfigMiddlewareBefore
58
+ };
59
+ };
60
+ export default appConfigMiddleware;
61
+
62
+
63
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@middy/appconfig",
3
+ "version": "4.1.0",
4
+ "description": "AppConfig middleware for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=16"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "./index.cjs",
14
+ "module": "./index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./index.d.ts",
19
+ "default": "./index.js"
20
+ },
21
+ "require": {
22
+ "types": "./index.d.ts",
23
+ "default": "./index.cjs"
24
+ }
25
+ }
26
+ },
27
+ "types": "index.d.ts",
28
+ "files": [
29
+ "index.js",
30
+ "index.cjs",
31
+ "index.d.ts"
32
+ ],
33
+ "scripts": {
34
+ "test": "npm run test:unit",
35
+ "test:unit": "ava",
36
+ "test:benchmark": "node __benchmarks__/index.js"
37
+ },
38
+ "license": "MIT",
39
+ "keywords": [
40
+ "Lambda",
41
+ "Middleware",
42
+ "Serverless",
43
+ "Framework",
44
+ "AWS",
45
+ "AWS Lambda",
46
+ "Middy",
47
+ "AppConfig"
48
+ ],
49
+ "author": {
50
+ "name": "Middy contributors",
51
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
52
+ },
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "github:middyjs/middy",
56
+ "directory": "packages/appconfig"
57
+ },
58
+ "bugs": {
59
+ "url": "https://github.com/middyjs/middy/issues"
60
+ },
61
+ "homepage": "https://middy.js.org",
62
+ "dependencies": {
63
+ "@middy/util": "4.1.0"
64
+ },
65
+ "devDependencies": {
66
+ "@aws-sdk/client-appconfig": "^3.186.0",
67
+ "@middy/core": "4.1.0",
68
+ "@types/aws-lambda": "^8.10.101",
69
+ "aws-xray-sdk": "^3.3.3"
70
+ },
71
+ "gitHead": "c5abbac9280c3a2f30758188233a7b27367901d1"
72
+ }