@middy/cloudformation-router 6.1.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/README.md +46 -0
  2. package/index.d.ts +13 -0
  3. package/index.js +57 -0
  4. package/package.json +70 -0
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ <div align="center">
2
+ <h1>Middy cloudformation-router lambda handler</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
4
+ <p><strong>CloudFormation Custom Response router 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/cloudformation-router?activeTab=versions">
7
+ <img src="https://badge.fury.io/js/%40middy%cloudformation-router.svg" alt="npm version" style="max-width:100%;">
8
+ </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/cloudformation-router">
10
+ <img src="https://packagephobia.com/badge?p=@middy/cloudformation-router" 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://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%;">
25
+ </a>
26
+ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
27
+ <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
28
+ </a>
29
+ <br/>
30
+ <a href="https://gitter.im/middyjs/Lobby">
31
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
32
+ </a>
33
+ <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
34
+ <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
35
+ </a>
36
+ </p>
37
+ <p>You can read the documentation at: <a href="https://middy.js.org/docs/routers/cloudformation-router">https://middy.js.org/docs/routers/cloudformation-router</a></p>
38
+ </div>
39
+
40
+ ## License
41
+
42
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2024 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
43
+
44
+ <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
45
+ <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
46
+ </a>
package/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import middy from '@middy/core'
2
+ import { CloudFormationCustomResourceHandler } from 'aws-lambda'
3
+
4
+ interface Route<T = never> {
5
+ requestType: string
6
+ handler: CloudFormationCustomResourceHandler<T>
7
+ }
8
+
9
+ declare function cloudformationRouterHandler (
10
+ routes: Route[]
11
+ ): middy.MiddyfiedHandler
12
+
13
+ export default cloudformationRouterHandler
package/index.js ADDED
@@ -0,0 +1,57 @@
1
+ const defaults = {
2
+ routes: [],
3
+ notFoundResponse: ({ requestType }) => {
4
+ const err = new Error('Route does not exist', {
5
+ casue: {
6
+ package: '@middy/cloudformation-router',
7
+ data: { requestType }
8
+ }
9
+ })
10
+ throw err
11
+ }
12
+ }
13
+ const cloudformationCustomResourceRouteHandler = (opts = {}) => {
14
+ if (Array.isArray(opts)) {
15
+ opts = { routes: opts }
16
+ }
17
+ const { routes, notFoundResponse } = { ...defaults, ...opts }
18
+
19
+ const routesStatic = {}
20
+ for (const route of routes) {
21
+ const { requestType, handler } = route
22
+
23
+ // Static
24
+ routesStatic[requestType] = handler
25
+ }
26
+
27
+ const requestTypes = {
28
+ Create: true,
29
+ Update: true,
30
+ Delete: true
31
+ }
32
+ return (event, context, abort) => {
33
+ const { RequestType: requestType } = event
34
+ if (
35
+ !requestType ||
36
+ !Object.hasOwnProperty.call(requestTypes, requestType)
37
+ ) {
38
+ throw new Error('Unknown CloudFormation Custom Response event format', {
39
+ cause: {
40
+ package: '@middy/cloudformation-router',
41
+ data: { requestType }
42
+ }
43
+ })
44
+ }
45
+
46
+ // Static
47
+ if (Object.hasOwnProperty.call(routesStatic, requestType)) {
48
+ const handler = routesStatic[requestType]
49
+ return handler(event, context, abort)
50
+ }
51
+
52
+ // Not Found
53
+ return notFoundResponse({ requestType })
54
+ }
55
+ }
56
+
57
+ export default cloudformationCustomResourceRouteHandler
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@middy/cloudformation-router",
3
+ "version": "6.1.1",
4
+ "description": "CloudFormation Custom Response event router 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": [
27
+ "index.js",
28
+ "index.d.ts"
29
+ ],
30
+ "scripts": {
31
+ "test": "npm run test:unit",
32
+ "test:unit": "node --test __tests__/index.js",
33
+ "test:benchmark": "node __benchmarks__/index.js"
34
+ },
35
+ "license": "MIT",
36
+ "keywords": [
37
+ "Lambda",
38
+ "Middleware",
39
+ "Serverless",
40
+ "Framework",
41
+ "AWS",
42
+ "AWS Lambda",
43
+ "Middy",
44
+ "CloudFormation",
45
+ "Custom Response",
46
+ "router"
47
+ ],
48
+ "author": {
49
+ "name": "Middy contributors",
50
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
51
+ },
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "git+https://github.com/middyjs/middy.git",
55
+ "directory": "packages/cloudformation-router"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/middyjs/middy/issues"
59
+ },
60
+ "homepage": "https://middy.js.org",
61
+ "funding": {
62
+ "type": "github",
63
+ "url": "https://github.com/sponsors/willfarrell"
64
+ },
65
+ "devDependencies": {
66
+ "@middy/core": "6.1.0",
67
+ "@types/aws-lambda": "^8.10.100"
68
+ },
69
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
70
+ }