{"_id":"next-api-middleware","_rev":"34-1fe3e604624af90f19e3055506f4f857","name":"next-api-middleware","dist-tags":{"latest":"3.0.0","canary":"2.0.0-canary.0","beta":"2.1.0-beta.0"},"versions":{"0.1.0":{"name":"next-api-middleware","version":"0.1.0","description":"Middleware solution for Next.js API routes","main":"index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","gitHead":"663babe4a901e75d75403ee2cbbad85ec186a5d0","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.1.0","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-Fo6S69bBYTfoSHSzJy4MiAxhE1YYgLmQa7ybU+mQEeI+U3SfiZ4ZC9Hz2zNY77FohGZr4xqgXCaV66lOx4+ytw==","shasum":"5ce8048e6136a96ef477d000afd9a8ecac50416e","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.1.0.tgz","fileCount":1,"unpackedSize":288,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxCrnCRA9TVsSAnZWagAAKmwP/1VLu4ULNsEQ9E4+JSl9\ng0lK2YagpdmnJe37Be5+LqbUQZ1L8e3vuxzhbIGUSOCMHf3FSDof//nfW3VV\n33WrFq74jdd2CYSoRHpckDcU7gk+HpuaHNA4sL/LwppVpE02G+sAYv61YNOe\nc315W7YgWy00d7DSW1ljZ0t+bsoxGFSBHSAv3+NIm5YBYTuQlsXi6yT2SS4G\nfp1IEpVziu4+9mQ8c8k166WhhJowaTed6LHkZPdXjKxloa/xXkHoGdjlxT1d\nGdF8lWbePyZKDbGa+1a+oQDtWWSYO4AkgH8ukFDFU4lYma9XAs3foyElZSla\n8Z3P4Xsm03TdrO8pcXrUtDfaSabnlVsm0k/sRbfVJBwgwc2liVcp7ypE5W4T\nNJCCLZdWvV747185VpYpw1BfIosNHZM7Z4H4iE6YOTUhKwrO0Xqr5H+pikGL\njiXMyvPM0eoYCEhg3lrkV7f28nV0u+wXDZM8MstkCMY5rKh2YIXYGh0efq0t\nfXa8k8/Eb2uX8FqrkbVlkL8oeCPVMk4Qp3wWVzQ0uLnKucjiIl+gyIp2ilqx\nyKI270EGwQF6ua+s7Mf5HlX0BSWNa8G4Bjhtprdd41C2axqF21wdxrispqoZ\naNzae7nLxl9HQgiRLL4Q3oiY3Z839EQviH2hbUw2neGJhNgogbY5tku5YJOb\nrNUT\r\n=EWMb\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDQHrnT4DOv3ZR56qmPIJlej2R+ua/1PYCuTXBfSPioTgIgELl3eCX0TkGQKUMqeoB53duWDJRjcAyckGAqOoAuBeQ="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.1.0_1606691559247_0.10391159091432378"},"_hasShrinkwrap":false},"0.2.0":{"name":"next-api-middleware","version":"0.2.0","description":"Middleware solution for Next.js API routes","main":"index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","scripts":{"build":"tsc","test":"jest"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"56f8ac73d25e61601247104f78c21d67d9e48673","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.2.0","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-HNQUXUkzwftiDW7ZkD8QuJGTB2RbrEB24Y4a7+Zjoj2LiV+F/eqJYMjRWD6isSfJIjKf2pXCZFWJMcbg4GynfQ==","shasum":"6f3bebc0c12b5dbd60cff721d07b73148524967f","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.2.0.tgz","fileCount":6,"unpackedSize":8270,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxU0JCRA9TVsSAnZWagAA0kcP+wddtutTmCzc82YZ0KFN\nJbcq6ElVLuGfOIrhJLwsHK6kyxQ7RuYIglF+uNJijL2oB/5WSMxAWZP3ei7M\nCR3p4XUiSeXjvy8lRQiJuxbOB/2uFglA+YZxJP7ZQ6EEKvypp+mTYvkNcuOE\npk4PBmFRteTU3XWNMn4tqBP1CVvK7IudiM+p2UMa6nx/wAn/k2/cqwSKHQuM\n5L0KCqLygGGMHxH0guYJ6tyMTrx1CPkauDM333I7lULaEYYKibpGmpxnNYaw\n7cSyQ7qao8eKwjCV6OKU1o5yU/9Avy8V+tpQT0hfpwJz7IS5uLsrOpevOIhC\nkv+j2FxaV1eH/3o/46RAXXY7Co+jqAJ9PVxahD4r2XoSTUPcRu0xLQHZ3LGo\nh0Qe4YUn0rY34snp0c12yvFRbuWkW2XvyyFesdrTubk6gEmofOo+yOD0cbTx\niddT6yeK84KwFzvo8a+MO3D/u6EWCGi+wqDpqMmXVDVHkBWnlJok/ohG9ynd\nDUgMY/OCgneSeIdanfzHLI6BCKBJgsObU1+NQKCQ4wvSXoxGJ7T/gGH5xQZJ\nN4qhTA9XD8P0PiN8I70M7MlMNofWaXA5EeNMCcVASTZFXHFfCZDYQZfrE03O\nkIQ2z4ZZ+YpP7rquZx7bYjL1fHvYZd58fbHrPAXBRlc3871RFpv3LznOBtln\nRFYZ\r\n=kLQn\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCyCZQo39SMAQRMjnVH5q1TAtUOseug58JESMKzND/dlQIhAPIDDZA/jwVsfOU8eaZoeUlubdMi44hgMP3IIigWaVwQ"}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.2.0_1606765833352_0.30801697409615736"},"_hasShrinkwrap":false},"0.2.1":{"name":"next-api-middleware","version":"0.2.1","description":"Middleware solution for Next.js API routes","main":"index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","scripts":{"build":"tsc","test":"jest","prepublish":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"5de91f9dee9c99d2a3dc43fee3e3669462f6f2a2","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.2.1","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-8+NpC0CeF3gTRiTBmDcxonaruIUWY6U0q+prJ0M79UryKfwL+kZNhYxUdFI5vYC9x/pbkuWKMblcKECb/feYaA==","shasum":"2cabc116baef9189094440ddbc1fed77f993b209","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.2.1.tgz","fileCount":6,"unpackedSize":8496,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxU3HCRA9TVsSAnZWagAAZrsP/1Mo1ebt8oX9k78MxDW9\nOaFSX/OJzIGCaimLaMVY/noZ8P07Aq9zC+4XzHKDklRT7+k/PoO8UENnNe7l\nfEUqPoJm3y1l4ccjc9pT1vL2ezKMvP7Ifrc7gu1fIi/NcTTzDDA8x0aT8YJz\nE/rMgdLOybsVgN7coK7bVKEtRUVXR1z4EKJiAqkiF8Q7ujvmt+neo1fVYdR9\nplQVHciCm7MqiMys7xWELI0dUsXkALVcrMzGL5dtt6nWt6K2n5DxXjA944xQ\nAUJCQEVbykOt44mKKz0bE8gNsAIxChJOPfn0s3+E1TvnXFU5TNo4AEobAO+v\nh7RUxKaQ/L9s5Yx4bWHKpvmk5SSVnCwz2gMOF8HjFr0mpTBcq0UP+USLcYr3\nGlgOBdjj4XKBGCw6LMJVLgGy0QdEfqPzZrGhwaY0aVIuz6C3fuHy/79rZDDp\n8iyUaBlGmUjKOSunomRwmyGGpPuEzvtLfO5oPAEYv/9lbIv/BYgWAqv6L/7/\noE+2scT1+dDEl7kg457b5qB4yWbZqZWPmrKstFOlTcgmFk0lyXci97clxka4\nn0XTZI3WXGG31vhQZa0Z5lccOhwT40hXXH2isvnUQ9NH3zwIDOkUkvlW0d6h\npk3atzFoectvk3Wn9Qqm1ReUqF6IRq3mjDDDOpvs3gU16z239+qqHrvyDB7b\nqq09\r\n=Zyun\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDeubaPoBUWERJXW7ZOV8WdbWCC6pvHeNE/nlizjUeP0gIhAIJ7EFVqGOeAS6jGqRfnAUyAW9a0wIXL0qNVGylMP7cv"}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.2.1_1606766022924_0.4130353076352842"},"_hasShrinkwrap":false},"0.2.2":{"name":"next-api-middleware","version":"0.2.2","description":"Middleware solution for Next.js API routes","main":"index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","keywords":["next","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"7239302cb204059104a6244d26bd211c1d000246","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.2.2","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-AXnMqEebrqxSzA24YmBRPdbrYK6D89ixaZNU6AEmWQbif01GbWe31T6OfwXjgG7eppNeimr2g27ajqrLS0/K0g==","shasum":"662195c1cf27d030ca3d493cb8f7dbe64af6cf21","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.2.2.tgz","fileCount":6,"unpackedSize":8572,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxU7qCRA9TVsSAnZWagAACTkQAI2Vl3bnGEWVMWdKbbb+\nqvJqgmu0LxHXNloJ2H1rvZpWcu9DJG+EU6zLJGbv/stipeL655YCJrh5feOR\nsplPoA3Qnr/vpRR9VXy7T9wHJZ/DtP2XxQ3m06PTY5peCC45Hj0UOgyCXAQ8\nxQ7L6k5Sgfa/sxNydXECijOC5Zz7y6TYmOLTWzldt6eoMomAHa0lQkkHzg6T\n7DV6dMVyItLt4rvtC4Pm+jP40pjXKUAhH2ztTxCTLNtbhQEl59DphrqB2C6f\n4iw9JKEPMm9I9TSgHaSq7crsJdoFPTFYhwuASZsBlOqmi/H7n3GlgBHgqQYI\nye56UTok3d+yyOABTvqUFGj1+GDPKwQTx1fS/92QfwPSusxowDP2SJOQUeV3\neF01x1oJqUU0cMTPBWwq1BBWA95mbXfaeGXW1KgApKVsjky91AubrUvgRzvO\nv+n4kbSgCEAk/oc5dp7S4Y+apnCpccU/SyuSmmYO7nxfoV93Rn9zl5PDgkhn\nCX018SSwsEqFB2MgfcyM1c8kcURPd43mcxQURoTLPhTu/jzzKFcQuMdorf3T\nc6EIo9hR6n/YwL8Fn7VW3/XTx1C5jizr5tqCTEDOTscSOwjDhRdNK+RVc7it\nPn6Oa2tZW5019gNa0M5LPizmd2zx69YtoZX4qwurfHf2wa5qWCbcvPJD8GVV\nuhtw\r\n=doKw\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFYHumFSkgD5SaebM9woJwy3yOAznMApCnYFfdgD8r5hAiEAlcXo4i4rlpQjbElwA5qFeyn0t02MbaSzFTZjSS857FM="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.2.2_1606766313625_0.8318080644413675"},"_hasShrinkwrap":false},"0.2.3":{"name":"next-api-middleware","version":"0.2.3","description":"Middleware solution for Next.js API routes","main":"index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","types":"dist/index.d.ts","keywords":["next","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"6fb853098ae27ff85998fc002feb0f3fab53b905","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.2.3","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-BpJbwYYZeMJcSq5TAO6BJ3ROrOXQZWMGkqZGVMYFuhl++ZFfnbK/hJW3AsIeyVSwPXUemaH/YNVrRJNDRgwmww==","shasum":"db156e3e477ab08974531e57308b2ab6cde33854","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.2.3.tgz","fileCount":6,"unpackedSize":8602,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxVBkCRA9TVsSAnZWagAAGDQP/08GTU98C6pygAru9syH\nUAqE6hWizZCUhN+eRPj7iQClYwx2dvJPsSfv5o/PVp58b4uNQZr0+8feWoYM\nFLnaNu7ORx/bkJjxgjynSKpwtqCpFibsULOUEHGJbF9ZeRBq4U2agrDzcxG1\n11q30wmqAKWEGnkwhXmcldqE/78utoLH2yP26j59lt7o5s3Pgl0dJlY/0XOP\nwIk0oWEmwU23XiEpI9Qqu8sko5jwwG69YZ8mV4XJv1QFr9ejTvHH83PEkPPj\nuU8ypgBiljr0H9K6qj1Cbbt+RVsFteb5jY2znu7PgFEugVbaKrh1XgoceWkX\nxWGM/v5+Uo6qwAs5pBI45GHMJDkHrjs8le9co9pH+LKVBbtm7dwDMipkS8fY\neBUS0hW/PjWd8NGRDTc5AipEtuSlJYDADaZEb0X9i2QwSsU7BR9oww9HXu16\nT9kmefZPKDwleLK4xxvaNQbUIpXJEYj4rhmCekI36eC639gpd+9ELGzvsV+U\nIRNo3AfwYk/5TKvnYPazOa/ZLy993nf3hcuk4GScAQcwVoQgQF3zae4lb0qO\nhKOf8AOGk3hiVYf9AfIHDmfrejpt0rQ9pTyHkpB6unK+SfmEA+0bby0HUNfZ\nlCT3CU3i6Gd/csCWuLgswilIfeVSvCzsA0GXBQP/P4MxRtXYdBcAlwwhqu2m\n+THX\r\n=U3ik\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCpqECsF2HLi+elj3oCLDEz0+Jrvs6mElBReWu5M1KGbAIhAPUKdzH9UApqnGfZ4n9sPMFZe+Ww5zNsGq2GLWA8y00z"}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.2.3_1606766691929_0.0396498869466233"},"_hasShrinkwrap":false},"0.2.4":{"name":"next-api-middleware","version":"0.2.4","description":"Middleware solution for Next.js API routes","main":"dist/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","types":"dist/index.d.ts","keywords":["next","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"cd6f7f30c8717cb76466896c5277ce8cd43f9a34","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.2.4","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-zVLdPLAu7NklPxJkvDyv5tkNepYxNkpW2jhl+jmNq8PAk8RVVqSw6GbTOjYILO/oi54uIeee6gAsZB2905q1CA==","shasum":"9a75aa3c886cd9ebd75f55fe65a322c45c419806","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.2.4.tgz","fileCount":6,"unpackedSize":8607,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxVEYCRA9TVsSAnZWagAAC/gP/2BjMQq+/oCt9+oaEwZf\nDyd+kqIJU702Le5JjHElSAbfOM1smnCp/roKeTYSlvUvKjrxdEKTNCmLiYTf\nPGN+KTCxlgvuCKEZ6ixrCo7fTpmJFxjafNh8oVpRwnd4K98iZVCl2hk85Wwv\nvr6RHPZo7yB/6MAFAGJPotoyOFbTFtN4+stS8BcW6LWTrAmr5p2w9U4ISfmZ\nFfr7sMYsydymIsbeECwex3OXh72R/buM1zB3UZf+PEuGFcl4ms67MTiEu6qC\n8FbgeZYym0M1A3sssGQMYzEA+tmbE9ekwebnUBSj9SPZayha1MJ0CGhrGulJ\ndUBFX/hDlP/BuRckaBp4UEHJ5r5dwIJh7HxRHknVlzoL7bS8wdDp2Pk6u4q9\n1sm2fqfKDb7o+RwodW6FTaJC0Pk53rRsNfuf1YF/LQMagApDhWk78qkiKPvJ\ni5FUJcO7rgita5tGtoCiUiL77n5527GFi0zEp2UGOkCw7oWRNznC1FqI/30d\nuVtfaP0o7cMnosMLswnR7N08RgAaAY0hncq+LRRVmW9NOb8Z3M1Tdcp9eaWl\n+p6B6UGpUs67DlKgy1mN4fUjU4NKsfX3a9y4DvO/SIGvyGxCXi8mqwplmKBG\nEH2u8v4la1Gl58IQnKVXR3S2OKKbAl5eE434RMZzgpxQrPQl7lscj6NXutg7\nq20j\r\n=Ft0L\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDfAWR3KF6p7tZ96ZglJydVaJ3bYX5VeTppZ/e+OH8LOAIgcroG6+/HXjJom44mt2sNzHOXEkb5SI6iFXIergTE24o="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.2.4_1606766872405_0.12054249706159137"},"_hasShrinkwrap":false},"0.3.0":{"name":"next-api-middleware","version":"0.3.0","description":"Middleware solution for Next.js API routes","main":"dist/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","types":"dist/index.d.ts","keywords":["next","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"41d0676adf5d23b6794c1c7cc1925752cc4ad298","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.3.0","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-/QBfxQSHv7kjN6Ane4Gfra4kklSqKBL4KyIDZ9jImoqWH5KQcfncKmu4P6CQUb8C2qMJ8/SSRnI5TqgAgTSvXA==","shasum":"082d1036dbf823c42ddbee772235dd38a126b854","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.3.0.tgz","fileCount":6,"unpackedSize":8728,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxVVUCRA9TVsSAnZWagAAIKkP/j+zKSQTz2869cRr2hBW\n7EDtruunH2gfVVCZetM5VeoxfH0rE4tnIlDZAWMrPe5yTfolbB+6Asn3qput\nk1YcmO0gi8e4Wgi+K+/aTY1hG5B8h/vYOMGnEVXWWt488W0uBB248oD5YwHh\ntX/gsnOF0sVaWkDwud5wQpma5WQ+TIlt+dtSMBJ4nzms1J9NK91ADbGMmnKH\noR03pAvr0d2IPbm3QeWYrCECLHcW/Yh3aW/8DSsDLPJ+QMj1zF+i/A0Q+nTY\nz9+VOr5f0W4Mj4wdyPPNQT7cCfHUYYTXrQ89Km60yoBsyNIA2QgGrw8Kpzfz\nD9NmUjVjzpvtZXkefRnmUVWWpX+isyFCf57gd7mjYToUZm1q9Ia00eXdg33E\nL9fb3nulmysPBVOwBF6Kc+HgYOZWRNs5VHBNwOq3Kb+i+FdwVuVeSKOHRMG8\nzrMfPWKCIqvkEWosthWjxvIbQT+1I+QsDkFlGP/LCDL8Y9JUrtsl/3h59E9R\nNZ2qsd5I+7nawY1AX12Hm3UZYYhKtBxfGOHfL9Xag8cQ6qzX3dmmvXAuiFvr\nQWotit7SiK2232xrffoqGCsm8ZqtT4iNRqmR9wFNMBG186JOKCc3teMsMfyI\nxTWO9Wj4tKT1FoFDBtpn5KlKRYZif/mhyAPEwzOxtz6E1HlbGthw+ZQCCjNy\nt246\r\n=MPnW\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFcxmvVTH1B/7cdyxhbpFk1VSHsq5mDouYXF/r97sAOEAiBfUaT4Z1IgW37+vIC0E2/ekEu24iMGN9PsULvP6iypBA=="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.3.0_1606767955729_0.7578661447415036"},"_hasShrinkwrap":false},"0.3.1":{"name":"next-api-middleware","version":"0.3.1","description":"Middleware solution for Next.js API routes","main":"dist/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"AGPL-3.0-or-later","types":"dist/index.d.ts","keywords":["next","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"b84096a0659f01826d8a49d0388d7112e7f27b1a","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.3.1","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-heqH+prObHZhL28QCc3grcJ6Ij/v9PAl5NaxUx1rM+m5Y/un0f3ZHsDNzMyXNChjRSHYKGsi6rDJ80AdYVMzoA==","shasum":"ebfe1fb25258e2d9be192ac5169ddf3d9e8684f5","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.3.1.tgz","fileCount":7,"unpackedSize":44934,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxWOACRA9TVsSAnZWagAAqyQP/1pDmLJTZsJ7FF9guNoT\nqpfRbZfXFy9z+SLjPIt3D3v3PVxHEc2z2XIEFqEi2vwxOsiXNT5BP6DVicW5\njvzg0v2eXv82FePWemWimO8J9JiBsrtL2ipJOqbr59XAR24fZEEuosDOUPF8\n5i1kF4u4s70L/BtdcdJdT9QIp+rotohAscIVQ9l70zQ6IBgB8etWJNRCM6nr\nAowZfB2hs43lFKTY9X3DuzHwEQW2IAx+0TASPpSQsPCDTn1CrNURtpRpH/sc\nTUHDBbw8lhjU+POzI9DrhcyvUAc02zwCuARt0P00JWDT/6VTTbx33qb2r9BG\nS/YKcRncLlR07gzsK1FJf/2L/K2O2jO7obTuXCYV9Fowd+rzRef9QY3iiift\niVhuXN2bjul2WL/BjMrFwCqZRQbsJyLs+/v5fR5ad3RrRITQkPz1Z7Qd0VFR\n3M7OJcI6TofoZmrfCw2/zrt813epEngic/mpqjXvOyyGOr2VFQTD37EW05Pv\nGltysSA2WvzkXJm56YHLMeH25FzKZ3J5gUcJzmB9o/O763Y9BUnDHbZZbeBt\nJydpi5vsFXIecLuXFgCyFiv1sI2u608NyIlq6sIz3ckk3JZYrAb5uPtWxKlV\nTPUwSeI1QobnbAfJfkQGB30Z9hu7lpAVSBTjYfpwdXsaLzem+ogXhIosUBoR\nXje8\r\n=17HS\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC3lijDrrsl+IDfjvYqMcQaSgXS3JUcGU04Izo+Z3HoqQIhAMCm8H++rPuMDs5QcsRxrT6eF25EMKRCNKm5U1arYOrV"}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.3.1_1606771584329_0.1320331544844855"},"_hasShrinkwrap":false},"0.3.2":{"name":"next-api-middleware","version":"0.3.2","description":"Middleware solution for Next.js API routes","main":"dist/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"AGPL-3.0-or-later","types":"dist/index.d.ts","keywords":["next","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"51f1e9124cdda3d2e0c882b240dfbeb735abe87f","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.3.2","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-T2IMNjmsZEVwn7rQnE04wbSFMpY42X0grFiPW+1Nx+HiyuJzShX0Q6Mvbo/RnyqwVqV5GZoxHmgAkoFkqb0q2A==","shasum":"6817c4b791da6c3de99756ee887944df52363f3a","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.3.2.tgz","fileCount":7,"unpackedSize":45008,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxWTMCRA9TVsSAnZWagAAE64P/jMEkU4bumsgf5DV6BUz\nqnNt28qwr2r+9rwnyDJ3oiGYsEjm3U00yGaRDE3XgMSxM6hVfOXxSKgswVkZ\nzrqoTiJZGQkZjBDy5+7vic1iGvUcJxF4c5b12Hjm90txPoWTzl3xiNp+afcC\ncBoCqKmPTUNN60YAZHkw5aXLXEbFk3Qtigu2s9Z6Vr2WMsKuVFEef16/J6zd\neiTErkpOdJI3reCHfEK0bI+mSaDUjCNJdqmhieYFPn2z3Df79WoDNRyEURpD\n7bKj6u3XY6ccFa4Z/Jetv5gKqyEZb6Ni0/ShriipcUJI7ZzW3M7SI3OFjTdE\nio7O+jJKklCk0U7rharcakehGeNWXZaql2tP9BLa3b4WOxH2yCUyHZ3Bl6Ok\n+P8LNOm1QL5UTTzbEZ9AXy9ffk24gtoDmsHJf0uQHpzLMQq4WHHKWtiSjANT\ngn9YOm2UqXljRGBxuIwYhlf4cFH1G2DF5rmuiowJx0UzAtjqenuu/hPPGItb\nmObTPPS1AAKNmRXh1ksrtng+nga9sVuAJaG/Wpah6zOJUyraaOuFMzvHHVOV\neLe9ICdnTcP6Rjmnne1syRu9DJylD7WO0GTFL85b84i9u+CjpHMruPHrw67Z\nCfuD5ou5xvu6EP4n43DKF/PyRwPlNMMRKbuRH5C4HSYOGK62mWkN7UIemD5U\nHyvK\r\n=SI/J\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEAFRXBm3akNveGq/ctdEi9ddgwBMYcjxL1VL40gBPlWAiBUbjftexSK0BTN0bxpQS+8Xr9KImo1sHYza6D/AICLwA=="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.3.2_1606771916165_0.9415546519681031"},"_hasShrinkwrap":false},"0.3.3":{"name":"next-api-middleware","version":"0.3.3","description":"Middleware solution for Next.js API routes","main":"dist/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"AGPL-3.0-or-later","types":"dist/index.d.ts","keywords":["next","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"aece8c7e21b7f26760a03162a6483d4f3b81fb1a","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.3.3","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-yNvOqJCRfMMkp4Op0KkS9kyn22a8bqh7l1Vg4nTeQ7sp7OLHTIk/sLS8lzczkpPOK71jCg4vY+wtNDWPzuXlhQ==","shasum":"fc7ae4d5e5c4036ad882940b6af493b08165e2dd","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.3.3.tgz","fileCount":5,"unpackedSize":45943,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxYBRCRA9TVsSAnZWagAAKQUQAIw9Zpmgqdy2mtfMryIX\ngbY5qrGpzTEZCU6y/TPodhFvAyT4Gz19SMzI5jVcpDxdsKbrHLt0/IV9nwQ0\n5bnO4Y/dL4ItBHJzjZsl64WntF16/J+iPpeMRss8+6UTcy3vUbjvfJXAS6yH\nzz3RL9ZoLP9TQcpVksjgbVMmh1DdJmqygcrzu2jOGIoUqzOzYTySCc3cLhW/\nAcjVQk7zFMq4X3829rjZNBbWUIICxIEccf+kUeiuACroXHRseLH1Hnmml3BW\nyVx/hhwnCmyhvEiJAbGjsLp+G4RtF+LpexJ7QjPEcd54GohoFSg5Ny2cc+0R\ncN5zoNd47EUmDaEkGUPELUQ1ay1acJksBAsXd7PwchYiH194aqQXCISkRLeJ\nIl/PP1ZrryC9cikw7yV77l64DPuvAIRig3+/3slF+ctBHohnSVmHcch6t67W\nl2NBkQT/1rNGujPWbGYBXHgIVSDdPCk5BcOGZIjfW70d8NQhLGDce3iWlHAG\nyu9Brq4Cz/J3YQsdQHEJQGHYmFM9b/i2FAmDz7SpvbLHOq3C2QIPI8FH6tiA\nIjwWaKPj0gLOhh8Ocy4LqhLiYspgECfJyotdb3a9qeKNPTD8QmIAfUThjzEb\nngeWiqrsX3RCwSqlGDBgofpJwA5cHjpUMXru6J2b9bG8sAJr4CdHC2f70vqp\nW3mC\r\n=XEpu\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIExAE+aTBFjxWWljYXLex7EA2UQMEpXudH3p6Do32OhLAiEAtp5NtoBmNH8KtkjobLd16D9877mhi2OQwCVUeJJj1qo="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.3.3_1606778961311_0.5453778250879842"},"_hasShrinkwrap":false},"0.3.4":{"name":"next-api-middleware","version":"0.3.4","description":"Middleware solution for Next.js API routes","main":"dist/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"AGPL-3.0-or-later","types":"dist/index.d.ts","keywords":["next","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"795dd54560a90030a4e0b33b54d557688bd14ac1","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.3.4","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-QrJNrEVBpyDITjAg3wwdKhwJScJDWro598FAmTVVz5vg6q/NLplO56r0Fsa4zKYpZDR2b+t3j5h141bkA0bkTg==","shasum":"20cacff64709ee4b8d9b95010541de6b5aead8fe","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.3.4.tgz","fileCount":5,"unpackedSize":46567,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxYj6CRA9TVsSAnZWagAAOKwP/1w4fWg6Gp2mQBxApcRX\ns9cb7/54B+z0jNQreJF94TdFipdgYKNHJQteIS9SW8/ZxfQXqeRLwIEhHHSn\nKjA4RtZl0UbdUkkydgGsqfFpkynJ0xywj7xCaOcSocskIoy3Z0du9VpxZ0b+\nQ/9aYixe+Lwnato9a9OY1RNOXWgC3uI21defuV8dv1/AfbQetBj2SW9xwpYN\nwNrT9SrKUlXudnuBeHrFzW3bL/paeh2ZQplubuvY3o/5lPjEaT0LboDWv8Ev\nKQ0FVWO3PzYB2/PqvVhtNt7uFzAelEtU1WLbb14RJ3qKbcAwvLHnrXyak1tz\nRdvyFmqJrXXQk90QWGzf7dGyw/CzcH4wNplVKwAekjGMFa5LPBUNieZBQ5jr\nnfD5MRBZJLV6VMJtwyQkl+JAJ0fwZAmOC0R0v5OtX7ASzXIWdiIZE9H7dpkJ\nJNeBW+1Rv2EwqNT5P7J5ZNblxG9n2n4QMW2ud6TpdPmz2uJmGf1aABRICn14\n9FpSJyQB5CwSfOQvGRHbu315eiOc5BHzpIWK0nPm3lX1R69XVrNBDaZR1teC\nN+61NGRfL+VoCjyt3HjBpUVNyOw2R655jC+q/GdFnEhWaYnFkq90yYXMY7/6\nxJtU2qFgiAigrtj2czGU/5CAwpFh+t532sH4b2tjjgsb4tbaGUVaBAkZQBL9\nMMK7\r\n=3+ou\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIE3W/8dpOX4JFrhAjsNCF3o6k8HOv8on/fcMB+nMQCgAAiBg9+A8Aljxnb5dZFBoLOgk7oKQO8ULhIsy6Lmp3KPwMA=="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.3.4_1606781178297_0.24126563684165747"},"_hasShrinkwrap":false},"0.4.0":{"name":"next-api-middleware","version":"0.4.0","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.x"},"keywords":["next","next.js","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"1906a2beab0ec83e742ff771ee896d161c6fab95","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.4.0","_nodeVersion":"12.20.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-HBcXMqgaHcaEMCIHX+uyWchyPQWd6iKpQyfZHGcLhdt7WCdcXQSvm0DQgCGNctPJnBy0jmokoboTORAKzMHr/A==","shasum":"1ea064e215cb71715576d96fbf75adec0e602716","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.4.0.tgz","fileCount":5,"unpackedSize":10469,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxovMCRA9TVsSAnZWagAAH0IP/2Z3QOzFETZv4t8iIXFg\nemMsor7kiSQv0zS8bVnqEa+94piCvENshdz3c1H8BmzcT3kvj9CAfhQRCeqU\nmyoQHLYfdGElHM4hezFywpqFx0cznFaaljjq5x/bTDiPLuYvFgfoNTBBEezW\nlmCOXxmwrLqpuhq4u+7xUt3B2lgaS93AzqkZ8WvIP9Z4znDeBiEWfCto673v\nChLnYVG8imFMV3T7AZslq+KHjHbmWq+RUBSY9t2bh//reaFIz8m+uT+5Dgz9\nGiASHF9GSxPyQ2RB7BNfJRKdWxoYZHADKz2rGimogwAbpkSdyLKS5L/ym4yv\nGdtFtDJnutj9CD9yZ38iOJMGyUEqcm7hRwpQvPqRb79LxXACUn5exSr8Tffj\nj3AsmPDPHiSyyLghWL+duSi4odMGkQWVYW9BZA4fkIEKTnSN1oxJOADPGzY3\nQRjpStgZMBcHoFyrdIOnfeZlcaOwT3YZ+3tGFNVODxrVkT4iXTNU0M2SSOrb\nQTJyZbMP9y976hIDoHv99LeDkRdYlcyobYeoVzrgWsBQvAWU4xawr1gA3X+4\nRF6AqA92a+Uy1lWqRoz/9AjSsYvbZrDiLgtdRp+vIdnmbRaaRyfyZra51Rvs\nhPKA8vfi3+s4siEwZheoOrVypZgNKw+/sJR3RqbrtLhDzCbcznWIjxvXgZFr\nNbw+\r\n=G0Md\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDdbag3R20LL7+JNBeRXdV+LqKPkWUowStTF/0R/xpWhgIgZgR24BJxwRKU1umFxRi28BLnb9bmQobD2PnQZdUlcUM="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.4.0_1606847435498_0.956916207932734"},"_hasShrinkwrap":false},"0.5.0":{"name":"next-api-middleware","version":"0.5.0","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.x"},"keywords":["next","next.js","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"f84d7b4e68529159f6f53821f101c69647737050","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.5.0","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-Gb2WUZ5+wlt5xdXJpa31J6vlJPB0Va2OkCsVKMDdBs+Uo8pKOX+MjdkJ8MImHSMokZy8fcuxyncKRmwitA2GaA==","shasum":"ce1d4e8a36eff6010cb3cbd95b9920d0913cb267","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.5.0.tgz","fileCount":5,"unpackedSize":11366,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxpxUCRA9TVsSAnZWagAAfgsP/1emmTvcg88MESU1uNV0\ni5sT5GW5IUaSLa7q55LCLvjpe0pVtW4RDoExP0vs2xRfd/semBikE0h515jK\nCKMhHxFLNDWn4UZ+H0qzoFXUOeFZdxtWRjl2HvCD7pdO9c5zZSHQ9N7mlX5m\nnUFLu3rxzEHaE6ddHe69HdUmvoUGjFXB2CyHDid6RgAgDb1bvJGURBDE/L6s\nnWtb8z5NtzNz5mNoIgaNv0PW95Nv0d8dmMYMt4mRKRtCZfY2vAP2VbZOkfGJ\nUqmQ9TaK1uFPxu90tLcVUfrX3zQf7LW0D0wQRZuOF3qRVHkUgSPClU8u+nF3\nnl4lpaHO+h/0LmPfc85A0Sj5KfCXvInGAzlR5tjnsW6KVjk70IRUvst+nHho\nKqmQ28kboilJWQDvS41c7Jl811XJ60a+YB2aMV3UWRf0HG1cXDxoyiFl5YJ5\n2XrSgln7pX1SWCn+vCm9V/tqVyktKT95SeDNaNMGmnCCFeO/MAsdspZ/nOwj\n12rTIbSoR044NwW1ezXu5K62LAKml5xz4DSmiV41Jco6Rt8UJk6cT7aA7luc\nsDrjzsFC5DooI0/S+8IwIGumcy7uVNrrJmM8cw9jEBT5RFEUd7Ds7qYRXbsh\n7qc1vL8yajoaAAJYkYiie2GG7oxENd/orFtifUEbH2ky4GPxh7DvB5/7lWZy\nlFcu\r\n=2EhX\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIE/Yk05ooHczgRPAMNFkXR9zyDaEKSbRV76/WgUf3IigAiEA1aX1gt+9UEHcr6ZBvwCuiqLhj7zfYOXFIYYKRneLWj8="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.5.0_1606851667740_0.9659757084416847"},"_hasShrinkwrap":false},"0.5.1":{"name":"next-api-middleware","version":"0.5.1","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.x"},"keywords":["next","next.js","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"5b064353b9d663915ccd7ce0d43e331e7c39f515","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.5.1","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-AJ85cO6MzB+2f4cMBoa7zJrxeGLr9vWLfaaI4TVBSCD5/UUU/EjYtBHmPFKZdh/vqW+JTbCB1ZHDOvkbS7Pt5Q==","shasum":"53354b05d10247f0fb800955bd78c819bf5b1ca4","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.5.1.tgz","fileCount":5,"unpackedSize":11490,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfx9ZNCRA9TVsSAnZWagAAahQP/ihKIyri6CYhsjZ1TkO4\ntWRoVaID3dl488xo6EFxqIq0cHLGVrwp6P2iY4x4AbYNXdeC1iuH4NyZVJv8\nzXHYjY8BvWOryY6AuF7k9NMp7Phpklo6oQ0DP2QB4eJabq5jBTpzFKDaPTaw\njTcDbvfh+1vaTmTKwdJ3AhD1F0bfGOZVYKf1Sm0JvYrPiHFAOWJHKmmGKMIN\nXIvqxYavr6CUFB6J+Hvl+1gPDSyEVbmWzgnoCKVairb8rd9E8wbHcjibhaoF\nrWqIdwQhob1HIYB/wI5ic0q/2Czr5Lt7e7AkX7EVYsDsC2e3t+7Z7HTj/PkV\nhHzy3Ft63IzwTkms/+SmmgWVKXdBibv6vNxfG9x2DCnudqmxcnSRpEUY1cXn\ngjIPXMsyPcHJI7PrusdMabsDaNzmrvPvIS/lg27mWYm2XTo3UuFARBPv1LVX\nxvbMZrVDUX2Tp4Df+ahddZZnufPhFHw4F65mjQDmQJrv6mVwgs4FFK6Fyz6L\nwBa7+EsTrD0h/D17if+qgcEiVHC2d7UX/cdMtJMCpg57UyfVvyDGMvsKPOA2\nWFUugpDfhemUhQEXR7HVFxcv9ZaqD/qrCN2mAqX3E6baitxuTBqjXGT6gePq\ne+Rhcl30De985bZJNoTntRkWWHoUKQZZSGtrYgqu4dDKnVTFzWXdmYeofVPd\nkd1I\r\n=3r3g\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDXaQca7Z1SgLU6LW41NnpYeRXAizXtgVI/5zxQU40+WgIgRx/jAzgYw1r8bDQOeaNOgAP58nSSA+MqcgGEKCZY/6Q="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.5.1_1606932045455_0.005982665519293162"},"_hasShrinkwrap":false},"0.5.2":{"name":"next-api-middleware","version":"0.5.2","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.x"},"keywords":["next","next.js","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"c1b69ba1ab00a4dbfbebb491ac7c28d34c43a074","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.5.2","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-aD1AWcgWc7LNgz1SEJN16NeA+FrL5Dm3T6sBi/OkPLfjWdbP0khnt8qx1TlmfvUsQarqw4WfJQJxyxDLkoC7zA==","shasum":"380eb37f589d3a846eb86ac2737a11d7525c95ee","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.5.2.tgz","fileCount":5,"unpackedSize":12140,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfx/3CCRA9TVsSAnZWagAAFsIP/2axpT2weth65TeHrUSo\n7ViIpz9Cgwir/UuOv7+7p8mb4GSreyoH2APRE1mH6/pxEvkxNpkJbPMfoM5z\nDjceyea9J4sBeL0haUEN8RzkFTQfeeDdnTwI+yX1xNHojEHC2qvItPeK/Lvy\nRuAHucMh6oMugFe8zOC+CWvpVK9xP5bszZI+KVsSBM3hbmmykv0hSH7xlLhB\nJbNdFdLLwKgAGZPJvdjf/VO0kvglHftZSZ8lGPqnMsK9FuCitEnW8e2YWHyD\nlUiW8txgi5vgZq6i35IMlikXW8r1N7pqCJYBnvC+/zB+vc1MBBXETB98BdCZ\n4IPkD2rNZvmVxYJ+IjvqdCC+mHn39aWsHA84lyPMHn72LzS+Ffv0XZ11LE/p\neTxExUj6I83qVgXYuvBg771OqjWt0uoQ6ZKHd1au6vzKxMPH1BAPBt8wH7ou\nUpIcd4QyN1Qe7byD3bUy1ZX9vP1sv9kltnCtlw8tSOz9i5MakC2nnFWNBFYX\nn106SNmOS75awJ4qy9BCP2mxSQegNzewZYUUauwpaK6XULqpFiFbUWjXWggC\nqOYMMkXeX4qLkLyCap5iumgAaFC8+WFXGYP2ry9/f+t59F2Q6xY0ypakKqYv\nKiBJvnmp7mNCaCACQG4emVkd64QWRhzuewVAekE2F2tW9iJx//KTX29a1xgh\nlQ/7\r\n=l5oO\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFupHYOp0rs21O9yBAPGRgSsyTKP2Td5t4oVvD3njCoUAiEAul/FbtgG0WvLvxDNEOcKFArt/eYlk8MEgjEQhaHU2+0="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.5.2_1606942146517_0.9941395448560106"},"_hasShrinkwrap":false},"0.6.0":{"name":"next-api-middleware","version":"0.6.0","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.x"},"keywords":["next","next.js","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"2d976d9c097ded93da2097d4283dcc657d16682a","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.6.0","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-JS728zfCLvRFQZCtc06DF7CHDvL2x0k25Lj6by5IV8Y1EJ/cvHE1q0hFEi2atKsZp01sxgSuiKS0PAMVfNcMdg==","shasum":"7c6f05be8eeb2b1e2a6b4830d384cfb66ff82a50","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.6.0.tgz","fileCount":5,"unpackedSize":12155,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfyA/mCRA9TVsSAnZWagAAyLIP/2XB9bt9CSgxoz+ck/fx\n2Ut/RxNcs7dE6wPdXwCWVtYCuRvETvGkhAvrW/QKiwj2tounfUCxwRdqG307\nM58Gp5r0ctYZlW8886QFh3Wr1k/Rihz3bPqxB5RvHfoIfw2fVWt4SCzqBVVx\noVZCUhkgdfVDz0vt3G7b92XGxKsHRd8M+CXUNEsMCAuRZ+RsqL8QXFuI2xVO\nCZHXW8pFpTSrRSi8WMOj3w+DqRSHUhF8v/gooDx9YrlNJpG/zlUgYFMq0/Jt\nM+AFiyb2EbGpMkoFLfMbSTaCOa4XWfAp/+N4plRngxXiD/Dm/y/8kM+EbhEf\nxSGFT5iFPGMl40qSQt4AlU34pLSvC2Nwqp4YA5oe6KbfxbWCARsJNMJGX6zx\nrKOHGmWFn8vAsQpR5jyJ6Uahe4pEesI4VPprKEbVg0lV98usukFK+ca3Z1/3\nO8zb4da2USleFCL3jLqa+8liqRcoL1iowQLtjUQmGQ57wf9PF5nv7sYRTsF5\n4bZWIWuQoll7Mipt588tultyNJE/BzB9dOUWE8Oyg4RWXFamDW7+Kva7Lc65\nwzyXt/nLWtEuQ7GkeBFaGaYnkDaTiZNg9MZYvaSBnmZLQ73bZ797wS4g+bG+\nu97cWZmxIyxa/Mpi36pnlAQPgEQYK5Ac3T+/Wv5aX7QaP7acd56+sQhAVshC\n5QIH\r\n=auev\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCHDYi8hS3i033VFzRYtn11jdnZlEm7rH8wsQY6dHfBlAIhAN5bJq0D93ci1ZAvxpKWnOtKc7TFo87T5PO4Q6k3n25B"}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.6.0_1606946789881_0.09886699552530098"},"_hasShrinkwrap":false},"0.7.0-canary.0":{"name":"next-api-middleware","version":"0.7.0-canary.0","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.x"},"keywords":["next","next.js","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"readme":"# Next.js API Middleware\n\n<p>\n  <a aria-label=\"npm\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm\" src=\"https://img.shields.io/npm/v/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"tests\" src=\"https://img.shields.io/github/workflow/status/htunnicliff/next-api-middleware/Test?style=for-the-badge&labelColor=000000&label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?style=for-the-badge&labelColor=000000&token=XI7G8L08TY\">\n  </a>\n<p>\n\n## Introduction\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares) :thumbsdown:. This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use. :tada:\n\n## Quick Start\n\n```js\nimport { use } from \"next-api-middleware\";\n\nconst middleware = use(\n  async (req, res, next) => {\n    console.log(\"Do work before the request\");\n    await next();\n    console.log(\"Clean up\");\n  },\n\n  async (req, res, next) => {\n    console.log(\"Do more work\");\n    await next();\n    console.log(\"Clean up more\");\n  },\n\n  (req, res, next) => {\n    console.log(\"Store user in request\");\n    req.locals = {\n      user: {\n        name: \"Alice\",\n        email: \"alice@example.com\",\n      },\n    };\n\n    return next();\n  }\n);\n\nconst apiRouteHandler = async (req, res) => {\n  const { name } = req.locals.user;\n\n  res.status(200);\n  res.send(`Hello, ${name}!`);\n};\n\nexport default middleware(apiRouteHandler);\n\n/**\n * Console output:\n *\n *   Do work before the request\n *   Do more work\n *   Store user in request\n *   Clean up more\n *   Clean up\n */\n```\n\n## Usage\n\n### :one: Create Middleware Functions\n\n```ts\nimport type { Middleware } from \"next-api-middleware\";\n\nexport const addRequestUUID: Middleware = (req, res, next) => {\n  res.setHeader(\"X-Response-ID\", uuid());\n  return next();\n};\n\nexport const addRequestTiming: Middleware = async (req, res, next) => {\n  res.setHeader(\"X-Timing-Start\", new Date().getTime());\n  await next();\n  res.setHeader(\"X-Timing-End\", new Date().getTime());\n};\n\nexport const logErrorsWithACME: Middleware = async (req, res, next) => {\n  try {\n    await next();\n  } catch (error) {\n    Acme.captureException(error);\n    res.status(500);\n    res.json({ error: error.message });\n  }\n};\n```\n\n### :two: Compose Reusable Groups\n\n```js\nimport { use } from \"next-api-middleware\";\nimport {\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n} from \"../helpers\";\nimport { connectDatabase, loadUsers } from \"../users\";\n\nexport const authMiddleware = use(\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n  connectDatabase,\n  loadUsers\n);\n\nexport const guestMiddleware = use(\n  isProduction ? [addRequestTiming, logErrorsWithACME] : [],\n  addRequestUUID\n);\n\nexport const otherMiddleware = use(\n  [addRequestUUID, connectDatabase, loadUsers],\n  [addRequestTiming, logErrorsWithACME]\n);\n```\n\nThe `use` method creates a higher order function that applies middleware to an API route. `use` accepts a list of values that evaluate to middleware functions. It also accepts arrays of middleware functions, which are flattened at runtime (order is preserved).\n\nAnother available method for grouping middleware is `label`. Here's how it works:\n\n```ts\nimport { label } from \"next-api-middleware\";\nimport {\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n} from \"../helpers\";\n\nconst middleware = label(\n  timing: addRequestTiming,\n  logErrors: logErrorsWithACME,\n  uuids: addRequestUUID,\n  all: [addRequestTiming, logErrorsWithACME, addRequestUUID]\n);\n\nconst apiRouteHandler = async (req, res) => {\n  const { name } = req.locals.user;\n\n  res.status(200);\n  res.send(`Hello, ${name}!`);\n};\n\n// Invokes `addRequestTiming` and `logErrorsWithACME`\nexport default middleware([\"timing\", \"logErrors\"])(apiRouteHandler);\n```\n\nUsing `label` creates a middleware group that, by default, doesn't invoke any middleware. Instead, it allows choosing specific middleware by supplying labels as arguments in the API route.\n\n### :three: Apply Middleware to API Routes\n\nTo apply a middleware group to an API route, just import it and provide the API route handler as an argument:\n\n```js\nimport { withGuestMiddleware } from \"../../middleware/groups\";\n\nconst apiHandler = async (req, res) => {\n  res.status(200);\n  res.send(\"Hello, world!\");\n};\n\nexport default withGuestMiddleware(apiHandler);\n```\n\n## Advanced\n\n### Middleware Factories\n\nSince `use` accepts values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { use, Middleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): Middleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const withAuthMiddleware = use(\n  httpMethod(\"POST\"),\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID\n);\n```\n","readmeFilename":"README.md","gitHead":"f89921ccef6f9869cf76454bcfa4530f30d1c587","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.7.0-canary.0","_nodeVersion":"15.2.1","_npmVersion":"7.0.8","dist":{"integrity":"sha512-MF+D5gOnubSuE7wjv/MgpyUL7kHCHa2cYzdNnxEgHt0+AtQ0u/76tFhbObU+/o8kApOgRyjcaZGk+UMqm3DYcw==","shasum":"4fee6e7edebbe3f048ed41fe5ae47172d5e7e42f","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.7.0-canary.0.tgz","fileCount":5,"unpackedSize":12735,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgJtjuCRA9TVsSAnZWagAA7a0P/0qCNJk24K66Jfe1kVSP\nP8St0zZZl9ZZWMEZ9z89YEGBcjIq45rJ+BNw4o8oYEUYhGBbwxJqdOKE5NXJ\nnGr8ggLtFOs5Qhp5gAG4bGwrzJooxXbYJmwuaXHAO+1MxMkfB6j9OhYrvYGW\nR+3X+sRlg30hQFi0/yI1VH6YRPGPW+LArzkliVyc4MYEQ0Gc9ip5oE5oRBcV\n/FFa4Cxtr98G4oL4Fr38Ripn7/3m1VtkzdqwBAZ/lsjlZZzlfazksE7XGriX\nmj2WgoYmfxK0gytQ9nBURB5BQj+BMycw1IGnruwXYtvYEr9O70hSFKFi4y5K\nedGKSvWSJiQOUl1VWQZZQvHGDHjrrYmv5uBo5TFKDu9om0ARR3lHnizMcSID\nw0EykNP1wIn7R2Rn2mep7t9YKr21iQAfLRGosVZICz46GE6z+yN8O9v8Ok+c\nbw5YNsr1HkNDZohR7lMvUXX/SGmT55aIjwma9u8tzXtHDrjuMWP1ApRTzX+6\neeZ8+X0cEc7Wefv+/H0l5OqiGecIkPb2tgM2yJ8wrQp6y64E9TAyS/xWPkh4\nN6e34BE8c0Dl3wRnU01YHphGAsbIH3AXWyxBVUlFT6FoEfcohyn5wOpqqw3W\n9YY5lZuTJ6T4EFX6o1UfZJS8Ab1eR2MPxa3ZHG713sSccYsRhNcgce9LIS0r\nhZb0\r\n=rE7w\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGxhHtadnFvJjfkrK597O9w05pVONX5YArxktA5ragkmAiEAjyO5TzFHRBvdSweTB+h8FzbIP38MOOqiQWi5lptEZug="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.7.0-canary.0_1613158637957_0.8807572127750061"},"_hasShrinkwrap":false},"0.7.0-canary.1":{"name":"next-api-middleware","version":"0.7.0-canary.1","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","test":"jest","preversion":"npm test","version":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/cors":"^2.8.10","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","cors":"^2.8.5","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"c2e85593f86e1c8187988954cb6c95cf5f5a6e0d","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.7.0-canary.1","_nodeVersion":"15.2.1","_npmVersion":"7.0.8","dist":{"integrity":"sha512-xrDAHiRG5NZfj9gLUE0Fi8iCJMPNdGT2QT3GSAvYs0SbWI145Vno6eUAp0sP6jxfqXUAlv9pdvioBCQ+6u8MoA==","shasum":"83c3b4509df200a59221a8e83b7a98ae3192f4ad","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.7.0-canary.1.tgz","fileCount":6,"unpackedSize":15203,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgMAMkCRA9TVsSAnZWagAA848P/RmvBBbsKToYxW0u3+fb\nYYzBiymkzPOZEj2B79G+fPd3d9Nx4UnrS2A7CqyRsKd6+hjS2oKx67kkYkr2\nane4jSslNZUvDaY83tk/cmOB67pGDrtcwpgVbx9gpbPdStJ83OHWqOu+BkcN\nSDZiJbej+Oa3R1Ynby7Cn1e0wkgTXegg9/5c0nyP932Dn1Ahs38wQgOzwmFv\nREXp6Xb5uiZLYNJZlhG4m8ewHZ1cm5/rugp5rVWEK/nf1w4FeXNNNaGKnF5Q\nDdWn5bV6iPg1alw0cYxZ12al23yb3cs9ZFxzJF6kleLPkQtowQA9qTT7XBU5\nyTVFfuuvPQyAfwrcp2T6wiZbjpVUd5qSaCj00KJZdp9ourXR5mc2YrpHlKD0\nc1nn9QXpCwd7EBpX+qqgZILdPRONOU8NwhtebGy13Pw/bUyhp0viUn+BSeid\nCGnHvUMRUp7mchLygLjIxF2kR6aL2IGI8JHrzJrupaPYRKAdlac0dNJjjQQc\n+F1xlccc3vtJ1NKNhp/A1Onp6ANE/vRoiEYSDyCh9TgWi1jkxkYaLRzGRkpT\nfDTvrrRbi+Cnves7DrLq55qMmty0cQIAb4iJfmQq+x1mmtffGQcHuTOnjJ7A\nIq9A4XE7jZ7DHr2EFRwBwtgUTieXj4Ql/rGIA35v46loqklOzcEKTdfVXmK1\n+cy8\r\n=p0dg\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCiQltAeJY53pWLxOS350NAkrZeFHrxnKmm7d5o4XNz9AIhAMlV9V+rIspruWT9F7h3/ge+w7ziL/wXWiQyVQ0lkBVH"}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.7.0-canary.1_1613759267609_0.7968019666837376"},"_hasShrinkwrap":false},"0.6.1":{"name":"next-api-middleware","version":"0.6.1","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.x"},"keywords":["next","next.js","middleware","API routes","koa"],"scripts":{"build":"tsc","test":"jest","prepare":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"54c108e49b0d97b667083bed7c440956ca9b1868","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.6.1","_nodeVersion":"15.2.1","_npmVersion":"7.0.8","dist":{"integrity":"sha512-iwsHjyTinZSkUFhEXq6TSJmOs8xNI/EiRXWFbwAwRv7XacmL1BN3Z/Xd1JXSSxvqxoAjA9hTEP6rGb5n3/veZQ==","shasum":"20dabb1d00522da76fbcc5a568034ac838794743","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.6.1.tgz","fileCount":5,"unpackedSize":12155,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgMAPQCRA9TVsSAnZWagAAee0P+wZv/vo+0yHx7YLIBB5Y\n6NLIR7J6pv81YtbMIjRlSjVw3MKZ5M6pcWcRJaFGgbZdjeJDzNz4aicg7HQL\n4iRHKxOpeJG5T11GIuXZWpQmsDgyBSt/amXRLr4dluxq2+62fsGjKGKzl+iK\nTUtSVuYJkSSuQC0JzTmt6whs39ifm7C+Ch1GeoeO/GQJgDJhB78XCcIouyoM\nURg8yHrONULkB6fWPOs/EAm/Uc1xxMaOMpiZcC/Eh0lNLAsyS8DL4fUqtYbC\nXY28jA0mhvSBn4iz479NWOyZ1ftKVp6ZniipdY8unMM7oDChlAhilYd7ifoC\nt+3i1Plm8FFStgyfLs7Nx30Idpqm0vNdlTgl2+OepuKuobaDQLPAB5DFsm7Q\n9eEPNDzQkv5eyEEu9bzy4SA1woiFoLmbCOmO62hnJAzbvhpxb3p7C3CDAqqu\n79xNx3eEq0LyyBOic7fcVSx6LhJj5th8f/Urg6lh7zsbokYpmN4mfmQg8etV\nIAFnFpbXqDtLyJZ3a43dO9MeR1TxHVctDxC6LBOUkv/LAw7C5TdEX8VSpV1P\nb1oGALogjk+eZseDqTu7YgNG36h0Qsk/8Vpy/BQPee8GwS4pwONeJLwvB8Hv\n4g16xW2afYvh4TbltsmOiyzXV7GLTTrpD6mF42cd+0/u5TX3xDyhEfe6U23x\nzTKW\r\n=eGAs\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBO0KFn3JmW4/cc4bx4Wu5Hnok+kgIKyZZzlxxy+eJZQAiEAgxQeSZJdAmapvrdn5tRMdMtKJX4D40Pv+Pq2yFX94Rs="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.6.1_1613759440193_0.7180650945963094"},"_hasShrinkwrap":false},"0.7.0":{"name":"next-api-middleware","version":"0.7.0","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","test":"jest","preversion":"npm test","version":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.12.9","@babel/preset-env":"^7.12.7","@babel/preset-typescript":"^7.12.7","@types/cors":"^2.8.10","@types/jest":"^26.0.15","@types/node":"^14.14.10","@types/react":"^17.0.0","@types/react-dom":"^17.0.0","babel-jest":"^26.6.3","cors":"^2.8.5","jest":"^26.6.3","next":"^10.0.3","ts-node":"^9.0.0","typescript":"^4.1.2"},"gitHead":"2636ba7cbfa4e392bf3911ef99a909de79494e9b","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.7.0","_nodeVersion":"15.2.1","_npmVersion":"7.0.8","dist":{"integrity":"sha512-3DmnBwSl85AFtzvgZJUu2oTKOVzh8D8S84LwH8xxpnuBrkHfIFvsZ1CtOnSKDEUTyTVvs05641lOzlesP82bvQ==","shasum":"bb183f95906ac9a9cd7f9c1b3e600582602a72b9","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.7.0.tgz","fileCount":6,"unpackedSize":17291,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgNtuKCRA9TVsSAnZWagAA13EP/iBTiHQcopHe/b6outHu\nDn3siVgg5J+l7x1qEgk7VBc9U5z4B5K4zczLHcuOYOrsecOHB+QIN8jXb6yK\nxkrLdu+53TciDuMJJBkGG4KL5ubmKoDYjXxgMdAMPd0yML3xL1Z+lxaaih9x\nnfnYEmwjcShPEudHwZ0D4W0YI9nyUgDS0MFJVXgctbpvsl2eQIhpP19HEYeP\njvg9K9VZ7syt4Ve35DlTszgzqmbFGXQMwGQOlKIj0dUSFu7/Vtix3AlPduUc\nSMA2mYbrTpYNNoMP1B3uaganA/h3GX29w5M3RJvUfF19kmHdtBSMqHSxzBx5\ndz6VdXbqEHKDxj+oqpGo1eZI9XXGuvRFd5tw9IRmBInM7UzOdoh5tR7u/2PM\nyjnh8mM9ylWhTNeIXOCo37eVvbA75i0mZGf6yDIJIZeYm5Du5sVOYCkjPZiW\nWJxowOqtJMZG4mapq4AuJNfbLT1utohgWZ+edUsehAbMgFoufb8xkOTsnz6a\nEIjLkI5dw8H3Tx0KB0P8bJNV6WQWCaKsDE1+9armZTlCpP5MDUZtSwksU17Y\nxy8F9PopG1DwXcn8hB3IiIO6h0gbOU8/Yr/NO4p65gRYBb67UPzmtq4nsFZD\n8BSaou0liSa0frrY72qMATNvE2mCVHsTmVJFYER5EnsXAXOWwof3ZiuZF4UQ\nkJhB\r\n=njVk\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCvGLFs/K4SLhf20SKHohbdmvaNOAeBOv3K5ECu/vwE3gIhAM/PlviGy6me39O6J0ToZlAKhxV3SavzCUBx9tShCOw1"}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.7.0_1614207881630_0.5513378191345468"},"_hasShrinkwrap":false},"0.7.1":{"name":"next-api-middleware","version":"0.7.1","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+ssh://git@github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","test":"jest","preversion":"npm test","version":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.13.15","@babel/preset-env":"^7.13.15","@babel/preset-typescript":"^7.13.0","@types/cors":"^2.8.10","@types/jest":"^26.0.22","@types/node":"^14.14.37","@types/react":"^17.0.3","@types/react-dom":"^17.0.3","babel-jest":"^26.6.3","cors":"^2.8.5","jest":"^26.6.3","next":"^10.1.3","ts-node":"^9.1.1","typescript":"^4.2.4"},"gitHead":"1b9cc645c161386864a04d5e33b8787f81df03ef","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@0.7.1","_nodeVersion":"15.2.1","_npmVersion":"7.0.8","dist":{"integrity":"sha512-HdAFPgOJcunHV8t+zQoGoXgpPGQkxXq+R7yFCvzt4kRSay5Y0/UVaCNRb0VWQcIMd30mp1CbWOjM/6Z0eFNuAw==","shasum":"cdf887f8f1cc216401799eeeba768286bd049788","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-0.7.1.tgz","fileCount":6,"unpackedSize":17293,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgdNFmCRA9TVsSAnZWagAAvyUQAIP6P0Te0cVElCfqdo/g\nizblCAIsGFJ+ocH1Y6lEikmlHnT7VgUE8MeyBjlnEtgwIJupaC7WDyKVPdKE\nXuEPIsuBO7+T3NR4qpKJaUL7J6qd2+uTA3C6eSHMtJZgdRO6JjT4pA1P/++2\nViK/QCCDLAulOtE+I+hg2QY28zmCdhTjycYgdWhrVjPMwgGHlFhHbCfbSAhz\nXLrxoiSdFr4vuaSz2as+JLkv64FNHMfatexYi9SoRU1xX6GvX28jybCkmoR3\n3XBLfdpIYWzB7u6IoFV9KwreihznzgrL5CoQSXzo7qhFyg4zxJlBRafp7V/v\neuaf4jF3SAapp/Fq6sRz6d+EgP+Q/uh1K0tZ/A7v0X43HmiKjCjCSqOqGKC/\nTDKmuGwa1cxde5LQt9GkV57KeY1KU64ydYViB+Cscou6M55cqHza+yOdAQq8\nEwWFMfxY4UxH1C6FWMwJ57TdshsURYMDQYhi8f5sJhLX3uO7ygfDYgouShl4\nSFdG62AHfoNHdJjbTmbC4UzdPFUYTqc7PtUBHNdqNsDKmbG3XSHsRLDTYCMn\nfNuaS7xuKkBMYdVLr/IYYl0BoTeIap13xf9TuKRuwqKhN2SrU2zX1E4vu+ks\ndeelzQYlK6143WOpvhOqE7HgjkD3piWRuRJVuxG4RaPC8uZzl2UAdTGTWA5r\nCk1R\r\n=jGO9\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICQaLBl2B8kILzVIVye1KkgXLtD1HvyP9xgOCnc4+MPZAiBF/VVBN4LOUu9UiMudcY4a/rAAD1OvX+/qf0/67Rl/WA=="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_0.7.1_1618268517551_0.7448854666857891"},"_hasShrinkwrap":false},"1.0.0-canary.0":{"name":"next-api-middleware","version":"1.0.0-canary.0","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git@github.com:htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","test":"jest","preversion":"npm test","version":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.13.15","@babel/preset-env":"^7.13.15","@babel/preset-typescript":"^7.13.0","@types/cors":"^2.8.10","@types/jest":"^26.0.24","@types/node":"^15.0.1","@types/react":"^17.0.3","@types/react-dom":"^17.0.3","babel-jest":"^27.0.2","cors":"^2.8.5","jest":"^27.0.4","jest-extended":"^0.11.5","next":"^10.1.3","ts-node":"^10.0.0","typescript":"^4.2.4"},"dependencies":{},"readmeFilename":"README.md","readme":"# Next.js API Middleware\n\n<p>\n  <a aria-label=\"npm\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm\" src=\"https://img.shields.io/npm/v/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"tests\" src=\"https://img.shields.io/github/workflow/status/htunnicliff/next-api-middleware/Test?style=for-the-badge&labelColor=000000&label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?style=for-the-badge&labelColor=000000&token=XI7G8L08TY\">\n  </a>\n<p>\n\n## Introduction\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares) :thumbsdown:. This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use. :tada:\n\n## Quick Start\n\n```js\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\n// Create a wrapper function that executes middleware\n// before and after an API route request\nconst withMiddleware = use(\n  async (req, res, next) => {\n    console.log(\"Do work before the request\");\n    await next();\n    console.log(\"Clean up\");\n  },\n  async (req, res, next) => {\n    console.log(\"Do more work\");\n    await next();\n    console.log(\"Clean up more\");\n  },\n  cors(), // <---- Invoke Express/Connect middleware like any other middleware\n  async (req, res, next) => {\n    console.log(\"Store user in request\");\n    req.locals = {\n      user: {\n        name: \"Alice\",\n        email: \"alice@example.com\",\n      },\n    };\n\n    await next();\n  }\n);\n\n// Create a normal Next.js API route handler\nconst apiRouteHandler = async (req, res) => {\n  const { name } = req.locals.user;\n\n  res.status(200);\n  res.send(`Hello, ${name}!`);\n};\n\n// Export the route handler wrapped inside the middleware function\nexport default withMiddleware(apiRouteHandler);\n\n/**\n * Console output:\n *\n *   Do work before the request\n *   Do more work\n *   cors\n *   Store user in request\n *   Clean up more\n *   Clean up\n */\n```\n\n## Usage\n\n### Writing Async Middleware\n\n```ts\nimport type { NextMiddleware } from \"next-api-middleware\";\n\n// Middleware that does work before a request\nexport const addRequestUUID: NextMiddleware = async (req, res, next) => {\n  // Set a custom header\n  res.setHeader(\"X-Response-ID\", uuid());\n\n  // Execute the remaining middleware\n  await next();\n};\n\n// Middleware that does work before *and* after a request\nexport const addRequestTiming: NextMiddleware = async (req, res, next) => {\n  // Set a custom header before other middleware and the route handler\n  res.setHeader(\"X-Timing-Start\", new Date().getTime());\n\n  // Execute the remaining middleware\n  await next();\n\n  // Set a custom header\n  res.setHeader(\"X-Timing-End\", new Date().getTime());\n};\n\n// Middleware that catches errors that occur in remaining middleware\n// and the request\nexport const logErrorsWithACME: NextMiddleware = async (req, res, next) => {\n  try {\n    // Catch any errors that are thrown in remaining\n    // middleware and the API route handler\n    await next();\n  } catch (error) {\n    Acme.captureException(error);\n    res.status(500);\n    res.json({ error: error.message });\n  }\n};\n```\n\n### Composing Middleware Groups\n\n```js\nimport { use } from \"next-api-middleware\";\nimport {\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n} from \"../helpers\";\nimport { connectDatabase, loadUsers } from \"../users\";\n\n// Create a middleware wrapper to be used on API routes\n// that need authentication\nexport const withAuthMiddleware = use(\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n  connectDatabase,\n  loadUsers\n);\n\n// Create a middleware wrapper to be used on API routes\n// that are only used by guests\nexport const withGuestMiddleware = use(\n  isProduction ? [addRequestTiming, logErrorsWithACME] : [],\n  addRequestUUID\n);\n\n// Create a middleware wrapper using arrays of middleware\n// functions; these are flattened and executed in the order\n// in which they are provided\nexport const withXYZMiddleware = use(\n  [addRequestUUID, connectDatabase, loadUsers],\n  [addRequestTiming, logErrorsWithACME]\n);\n```\n\nThe `use` method creates a higher order function that applies middleware to an API route. `use` accepts a list of values that evaluate to middleware functions. It also accepts arrays of middleware functions, which are flattened at runtime (order is preserved).\n\nAnother available method for grouping middleware is `label`. Here's how it works:\n\n```ts\nimport { label } from \"next-api-middleware\";\nimport {\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n} from \"../helpers\";\n\n// Create a middleware wrapper that imports middleware and\n// assigns friendly labels\nconst withMiddleware = label({\n  timing: addRequestTiming,\n  logErrors: logErrorsWithACME,\n  uuids: addRequestUUID,\n  all: [addRequestTiming, logErrorsWithACME, addRequestUUID],\n});\n\nconst apiRouteHandler = async (req, res) => {\n  const { name } = req.locals.user;\n\n  res.status(200);\n  res.send(`Hello, ${name}!`);\n};\n\n// Only invoke `addRequestTiming` and `logErrorsWithACME`, using their\n// friendly labels\nexport default withMiddleware(\"timing\", \"logErrors\")(apiRouteHandler);\n```\n\nUsing `label` creates a middleware group that, by default, doesn't invoke any middleware. Instead, it allows choosing specific middleware by supplying labels as arguments in the API route.\n\n## Advanced\n\n### Middleware Factories\n\nSince `use` accepts values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { use, NextMiddleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): NextMiddleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const withAuthMiddleware = use(\n  httpMethod(\"POST\"),\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID\n);\n```\n\n### Middleware Types\n\n`next-api-middleware` supports two middleware signatures, `NextMiddleware` and `ExpressMiddleware`.\n\n#### `NextMiddleware` (preferred)\n\n`NextMiddleware` is inspired by the asyncronous middleware style popularized by Koa.js. Prefer this style when writing your own middleware.\n\n```ts\ninterface NextMiddleware {\n  (\n    req: NextApiRequest,\n    res: NextApiResponse,\n    next: () => Promise<void>\n  ): Promise<void>;\n}\n```\n\n#### `ExpressMiddleware`\n\n`ExpressMiddleware` roughly matches the signature used by Express/Connect style middleware. This type can be used when importing third-party libraries such as `cors`.\n\n```ts\ninterface ExpressMiddleware<\n  Request = IncomingMessage,\n  Response = ServerResponse\n> {\n  (\n    req: Request,\n    res: Response,\n    next: (error?: any) => void | Promise<void>\n  ): void;\n}\n```\n\nAn example using `cors`:\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nexport const withMiddleware = use(\n  // Asserting express/connect middleware as an `ExpressMiddleware` interface\n  // can resolve type conflicts by libraries that provide their own types.\n  cors() as ExpressMiddleware\n);\n```\n","licenseText":"MIT License\n\nCopyright (c) 2020 Hunter Tunnicliff\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"next-api-middleware@1.0.0-canary.0","dist":{"shasum":"183c3001a0b90ea88b8beeeb6b537a2b67718dd2","integrity":"sha512-jvj1eP4+RZuNlu61C8S8nTm1sqoWwenU80s5ELUk5i0CgC581f3dpfTR1AoiQjusMPraBpgkW+mkzWOX9oQyyw==","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-1.0.0-canary.0.tgz","fileCount":23,"unpackedSize":29409,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg5gjRCRA9TVsSAnZWagAA9icP/RKyDzGkilhKEx4z/LsF\n0jClerKti6kOTmlPiQxM1OXp9JogIjB2oy97AUt/4ru1Oyv8THzbKvn71n13\n2BFgCrrgd+1MGEO8w7bYKgGmpMNQoXrQprpuK0rHoQCRshXT7vAZd/rsXtL9\nsIZMLPq9DhDZBW6NS3GCNtYjbBIJa0M4ylueCHQ9Uqt7scf9Wov88jpGehrL\nnGUhH+gH1uT1PEgpQud+hfWj/ezq9X8urf3Fe4hiiFcn455nXf/CEAN8/6Sn\nnReicrJrsFo8Nv6qUHNcggXi/rZmjB+JKe8C03H6flAWAGXPKrlXbdow1Cu9\nswsTWqff6djKHI0gPeaPHQypZZSJJZagZFPR7fQYDuA711olKd9okFZHYDZW\nP51rf0JEtpML/CykuceLNm0+ploykHIigdvzksocuD477QfNN95kmi8sLekG\nGR5DJluq0lSLkzusWXiZvvy69Kan6x6GMQOvPGxfKro9q+waKN7RcUuDYw46\nR2Oqa2bECgz2V+8W65zBgez7pIOYCLl0DIZCYLg2JyHnMSsLfZUXA7Ad8WSU\nRb8SjLh5ROeLIvMW4XrFaTneKzQcB7VThB528MDaw8/KMFpEoRIiVAI9EeNF\nCJyhDnGLd5A650RSLhEWK4X1Nu1uZobphBOI+kUNzSCbAqG+378FmHHb9kqU\nDwan\r\n=vsg4\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCpzlVA40x8hY8rvi0rmGvo4j8GzZMNk9vIcDW+xm2thwIgPmX1PiMAhOu1z9q4xCka78Gke6cZYk6qkdSDzFlgO5g="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_1.0.0-canary.0_1625688273417_0.14483548205869945"},"_hasShrinkwrap":false},"1.0.0-canary.1":{"name":"next-api-middleware","version":"1.0.0-canary.1","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git@github.com:htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","test":"jest","preversion":"npm test","version":"rm -rf dist && npm run build"},"devDependencies":{"@babel/core":"^7.13.15","@babel/preset-env":"^7.13.15","@babel/preset-typescript":"^7.13.0","@types/cors":"^2.8.10","@types/debug":"^4.1.6","@types/jest":"^26.0.24","@types/node":"^15.0.1","@types/react":"^17.0.3","@types/react-dom":"^17.0.3","babel-jest":"^27.0.2","cors":"^2.8.5","jest":"^27.0.4","jest-extended":"^0.11.5","next":"^10.1.3","ts-node":"^10.0.0","typescript":"^4.2.4"},"dependencies":{"debug":"^4.3.2"},"readmeFilename":"README.md","readme":"# Next.js API Middleware\n\n<p>\n  <a aria-label=\"npm\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm\" src=\"https://img.shields.io/npm/v/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"tests\" src=\"https://img.shields.io/github/workflow/status/htunnicliff/next-api-middleware/Test?style=for-the-badge&labelColor=000000&label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?style=for-the-badge&labelColor=000000&token=XI7G8L08TY\">\n  </a>\n<p>\n\n## Introduction\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares) :thumbsdown:. This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use. :tada:\n\n## Quick Start\n\n```js\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\n// Create a wrapper function that executes middleware\n// before and after an API route request\nconst withMiddleware = use(\n  async (req, res, next) => {\n    console.log(\"Do work before the request\");\n    await next();\n    console.log(\"Clean up\");\n  },\n  async (req, res, next) => {\n    console.log(\"Do more work\");\n    await next();\n    console.log(\"Clean up more\");\n  },\n  cors(), // <---- Invoke Express/Connect middleware like any other middleware\n  async (req, res, next) => {\n    console.log(\"Store user in request\");\n    req.locals = {\n      user: {\n        name: \"Alice\",\n        email: \"alice@example.com\",\n      },\n    };\n\n    await next();\n  }\n);\n\n// Create a normal Next.js API route handler\nconst apiRouteHandler = async (req, res) => {\n  const { name } = req.locals.user;\n\n  res.status(200);\n  res.send(`Hello, ${name}!`);\n};\n\n// Export the route handler wrapped inside the middleware function\nexport default withMiddleware(apiRouteHandler);\n\n/**\n * Console output:\n *\n *   Do work before the request\n *   Do more work\n *   cors\n *   Store user in request\n *   Clean up more\n *   Clean up\n */\n```\n\n## Usage\n\n### Writing Async Middleware\n\n```ts\nimport type { NextMiddleware } from \"next-api-middleware\";\n\n// Middleware that does work before a request\nexport const addRequestUUID: NextMiddleware = async (req, res, next) => {\n  // Set a custom header\n  res.setHeader(\"X-Response-ID\", uuid());\n\n  // Execute the remaining middleware\n  await next();\n};\n\n// Middleware that does work before *and* after a request\nexport const addRequestTiming: NextMiddleware = async (req, res, next) => {\n  // Set a custom header before other middleware and the route handler\n  res.setHeader(\"X-Timing-Start\", new Date().getTime());\n\n  // Execute the remaining middleware\n  await next();\n\n  // Set a custom header\n  res.setHeader(\"X-Timing-End\", new Date().getTime());\n};\n\n// Middleware that catches errors that occur in remaining middleware\n// and the request\nexport const logErrorsWithACME: NextMiddleware = async (req, res, next) => {\n  try {\n    // Catch any errors that are thrown in remaining\n    // middleware and the API route handler\n    await next();\n  } catch (error) {\n    Acme.captureException(error);\n    res.status(500);\n    res.json({ error: error.message });\n  }\n};\n```\n\n### Composing Middleware Groups\n\n```js\nimport { use } from \"next-api-middleware\";\nimport {\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n} from \"../helpers\";\nimport { connectDatabase, loadUsers } from \"../users\";\n\n// Create a middleware wrapper to be used on API routes\n// that need authentication\nexport const withAuthMiddleware = use(\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n  connectDatabase,\n  loadUsers\n);\n\n// Create a middleware wrapper to be used on API routes\n// that are only used by guests\nexport const withGuestMiddleware = use(\n  isProduction ? [addRequestTiming, logErrorsWithACME] : [],\n  addRequestUUID\n);\n\n// Create a middleware wrapper using arrays of middleware\n// functions; these are flattened and executed in the order\n// in which they are provided\nexport const withXYZMiddleware = use(\n  [addRequestUUID, connectDatabase, loadUsers],\n  [addRequestTiming, logErrorsWithACME]\n);\n```\n\nThe `use` method creates a higher order function that applies middleware to an API route. `use` accepts a list of values that evaluate to middleware functions. It also accepts arrays of middleware functions, which are flattened at runtime (order is preserved).\n\nAnother available method for grouping middleware is `label`. Here's how it works:\n\n```ts\nimport { label } from \"next-api-middleware\";\nimport {\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n} from \"../helpers\";\n\n// Create a middleware wrapper that imports middleware and\n// assigns friendly labels\nconst withMiddleware = label({\n  timing: addRequestTiming,\n  logErrors: logErrorsWithACME,\n  uuids: addRequestUUID,\n  all: [addRequestTiming, logErrorsWithACME, addRequestUUID],\n});\n\nconst apiRouteHandler = async (req, res) => {\n  const { name } = req.locals.user;\n\n  res.status(200);\n  res.send(`Hello, ${name}!`);\n};\n\n// Only invoke `addRequestTiming` and `logErrorsWithACME`, using their\n// friendly labels\nexport default withMiddleware(\"timing\", \"logErrors\")(apiRouteHandler);\n```\n\nUsing `label` creates a middleware group that, by default, doesn't invoke any middleware. Instead, it allows choosing specific middleware by supplying labels as arguments in the API route.\n\n## Advanced\n\n### Middleware Factories\n\nSince `use` accepts values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { use, NextMiddleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): NextMiddleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const withAuthMiddleware = use(\n  httpMethod(\"POST\"),\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID\n);\n```\n\n### Middleware Types\n\n`next-api-middleware` supports two middleware signatures, `NextMiddleware` and `ExpressMiddleware`.\n\n#### `NextMiddleware` (preferred)\n\n`NextMiddleware` is inspired by the asyncronous middleware style popularized by Koa.js. Prefer this style when writing your own middleware.\n\n```ts\ninterface NextMiddleware {\n  (\n    req: NextApiRequest,\n    res: NextApiResponse,\n    next: () => Promise<void>\n  ): Promise<void>;\n}\n```\n\n#### `ExpressMiddleware`\n\n`ExpressMiddleware` roughly matches the signature used by Express/Connect style middleware. This type can be used when importing third-party libraries such as `cors`.\n\n```ts\ninterface ExpressMiddleware<\n  Request = IncomingMessage,\n  Response = ServerResponse\n> {\n  (\n    req: Request,\n    res: Response,\n    next: (error?: any) => void | Promise<void>\n  ): void;\n}\n```\n\nAn example using `cors`:\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nexport const withMiddleware = use(\n  // Asserting express/connect middleware as an `ExpressMiddleware` interface\n  // can resolve type conflicts by libraries that provide their own types.\n  cors() as ExpressMiddleware\n);\n```\n","licenseText":"MIT License\n\nCopyright (c) 2020 Hunter Tunnicliff\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"next-api-middleware@1.0.0-canary.1","dist":{"shasum":"6fd4a0ea892e06555c2aab7419a060b7c51c83af","integrity":"sha512-EHBo5LCpnhglVSfXvOc3it98teuhc0eWeoKS57yyoUx3QBY5MMWIryI3OPt5jfCTCLeVhasRi46QHZXZdRIYlA==","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-1.0.0-canary.1.tgz","fileCount":26,"unpackedSize":32408,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg8lnvCRA9TVsSAnZWagAAPcwP/06uZjX96reBPWAicfEm\nRGpESZNfT/zIxyclZ80Xe0TNzR6L2dk3pRTMUBMm5dSb+sjrOUxZi22YN+la\n+tY1L+JZecNMlpG6kOPk5bQigKzMcCGozdNmqBCky+yynlBzwSYP6XLNP03N\nrcv/VryoVgTHe5OXg/XcAiA2qcMwdqGmsHPFmPCaBVQVEXzBn2NTGasCjwss\n04op/br29IsmZN1E2pLuisz6q+5tqicKx0xzzRwEOAHQ6MTzEFyxvYPhJUeD\n9f1gqd5DePUjYB/+F3YUyH+i/bkPWXeVeg36CYasFyOSik41Bi5Kl608HJMO\nAGQEHsIU/Sr5XsrwrHyhA8FZKDA4hXkYg2TWaN3XRL9PRHmyIMVAJp6O+5Ef\nkaMO++MXaqPjAOPfUlrOPeGoLT37BICi+aJJuHX927Q/dyaXTzstRW+mwdL4\nDOHCM0JJExU7XxAKvj6hzmaSGqlzTbZXm9HYwEWaJY1G0bdlNMiW73+oLhVr\n4xRHSNZD/XW48yPwfnuvYNpYDo0gjS/N5pw2lxhSV0/1f8PHWFlu6IbxKGYA\nuBd22qEWLy2+/0T0NwprOGRvnK03/HiR/mSaYxMAMwObFIXwWPW/OFYEEOiM\nyFsEGCm83R+2+3Boxe0USip3JNQn+7MyezzVg+EmhPVImagWJWilWAX8jlMe\n2Wa+\r\n=WTyr\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDkiXYKa0nlQm/MTUXJCaPXmCWNlnju5TaQNbs3caPlAQIhAJ0qEm3Fc46POUZCYC7M5+lDjadXU0wLzYZevIFjTsUw"}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_1.0.0-canary.1_1626495471605_0.5018287338061651"},"_hasShrinkwrap":false},"1.0.0-canary.2":{"name":"next-api-middleware","version":"1.0.0-canary.2","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git@github.com:htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","type-check":"tsc --pretty --noEmit","test":"jest","preversion":"yarn test","version":"rm -rf dist && yarn build"},"devDependencies":{"@babel/core":"^7.13.15","@babel/preset-env":"^7.13.15","@babel/preset-typescript":"^7.13.0","@types/cors":"^2.8.12","@types/debug":"^4.1.6","@types/jest":"^26.0.24","@types/node":"^15.14.2","@types/react":"^17.0.14","@types/react-dom":"^17.0.3","babel-jest":"^27.0.2","cors":"^2.8.5","jest":"^27.0.4","jest-extended":"^0.11.5","next":">=10","ts-node":"^10.0.0","typescript":"^4.2.4"},"dependencies":{"debug":"^4.3.2"},"peerDependencies":{"next":">=10"},"readmeFilename":"README.md","readme":"<p align=\"center\">\n  <h1 align=\"center\">Next.js API Middleware</h1>\n</p>\n\n<p align=\"center\">\n  <a aria-label=\"npm@latest\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm@latest\" src=\"https://img.shields.io/npm/v/next-api-middleware/latest.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"npm@canary\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm@canary\" src=\"https://img.shields.io/npm/v/next-api-middleware/canary.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n</p>\n<p align=\"center\">\n  <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"tests\" src=\"https://img.shields.io/github/workflow/status/htunnicliff/next-api-middleware/Test?style=for-the-badge&labelColor=000000&label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?style=for-the-badge&labelColor=000000&token=XI7G8L08TY\">\n  </a>\n<p>\n\n## Introduction\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares) :thumbsdown:. This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use. :tada:\n\n## Quick Start\n\n```js\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\n// Create a wrapper function that executes middleware\n// before and after an API route request\nconst withMiddleware = use(\n  async (req, res, next) => {\n    console.log(\"Do work before the request\");\n    await next();\n    console.log(\"Clean up\");\n  },\n  async (req, res, next) => {\n    console.log(\"Do more work\");\n    await next();\n    console.log(\"Clean up more\");\n  },\n  cors(), // <---- Invoke Express/Connect middleware like any other middleware\n  async (req, res, next) => {\n    console.log(\"Store user in request\");\n    req.locals = {\n      user: {\n        name: \"Alice\",\n        email: \"alice@example.com\",\n      },\n    };\n\n    await next();\n  }\n);\n\n// Create a normal Next.js API route handler\nconst apiRouteHandler = async (req, res) => {\n  const { name } = req.locals.user;\n\n  res.status(200);\n  res.send(`Hello, ${name}!`);\n};\n\n// Export the route handler wrapped inside the middleware function\nexport default withMiddleware(apiRouteHandler);\n\n/**\n * Console output:\n *\n *   Do work before the request\n *   Do more work\n *   cors\n *   Store user in request\n *   Clean up more\n *   Clean up\n */\n```\n\n## Usage\n\n### Writing Async Middleware\n\n```ts\nimport type { NextMiddleware } from \"next-api-middleware\";\n\n// Middleware that does work before a request\nexport const addRequestUUID: NextMiddleware = async (req, res, next) => {\n  // Set a custom header\n  res.setHeader(\"X-Response-ID\", uuid());\n\n  // Execute the remaining middleware\n  await next();\n};\n\n// Middleware that does work before *and* after a request\nexport const addRequestTiming: NextMiddleware = async (req, res, next) => {\n  // Set a custom header before other middleware and the route handler\n  res.setHeader(\"X-Timing-Start\", new Date().getTime());\n\n  // Execute the remaining middleware\n  await next();\n\n  // Set a custom header\n  res.setHeader(\"X-Timing-End\", new Date().getTime());\n};\n\n// Middleware that catches errors that occur in remaining middleware\n// and the request\nexport const logErrorsWithACME: NextMiddleware = async (req, res, next) => {\n  try {\n    // Catch any errors that are thrown in remaining\n    // middleware and the API route handler\n    await next();\n  } catch (error) {\n    Acme.captureException(error);\n    res.status(500);\n    res.json({ error: error.message });\n  }\n};\n```\n\n### Composing Middleware Groups\n\n```js\nimport { use } from \"next-api-middleware\";\nimport {\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n} from \"../helpers\";\nimport { connectDatabase, loadUsers } from \"../users\";\n\n// Create a middleware wrapper to be used on API routes\n// that need authentication\nexport const withAuthMiddleware = use(\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n  connectDatabase,\n  loadUsers\n);\n\n// Create a middleware wrapper to be used on API routes\n// that are only used by guests\nexport const withGuestMiddleware = use(\n  isProduction ? [addRequestTiming, logErrorsWithACME] : [],\n  addRequestUUID\n);\n\n// Create a middleware wrapper using arrays of middleware\n// functions; these are flattened and executed in the order\n// in which they are provided\nexport const withXYZMiddleware = use(\n  [addRequestUUID, connectDatabase, loadUsers],\n  [addRequestTiming, logErrorsWithACME]\n);\n```\n\nThe `use` method creates a higher order function that applies middleware to an API route. `use` accepts a list of values that evaluate to middleware functions. It also accepts arrays of middleware functions, which are flattened at runtime (order is preserved).\n\nAnother available method for grouping middleware is `label`. Here's how it works:\n\n```ts\nimport { label } from \"next-api-middleware\";\nimport {\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID,\n} from \"../helpers\";\n\n// Create a middleware wrapper that imports middleware and\n// assigns friendly labels\nconst withMiddleware = label({\n  timing: addRequestTiming,\n  logErrors: logErrorsWithACME,\n  uuids: addRequestUUID,\n  all: [addRequestTiming, logErrorsWithACME, addRequestUUID],\n});\n\nconst apiRouteHandler = async (req, res) => {\n  const { name } = req.locals.user;\n\n  res.status(200);\n  res.send(`Hello, ${name}!`);\n};\n\n// Only invoke `addRequestTiming` and `logErrorsWithACME`, using their\n// friendly labels\nexport default withMiddleware(\"timing\", \"logErrors\")(apiRouteHandler);\n```\n\nUsing `label` creates a middleware group that, by default, doesn't invoke any middleware. Instead, it allows choosing specific middleware by supplying labels as arguments in the API route.\n\n## Advanced\n\n### Middleware Factories\n\nSince `use` accepts values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { use, NextMiddleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): NextMiddleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const withAuthMiddleware = use(\n  httpMethod(\"POST\"),\n  addRequestTiming,\n  logErrorsWithACME,\n  addRequestUUID\n);\n```\n\n### Middleware Types\n\n`next-api-middleware` supports two middleware signatures, `NextMiddleware` and `ExpressMiddleware`.\n\n#### `NextMiddleware` (preferred)\n\n`NextMiddleware` is inspired by the asyncronous middleware style popularized by Koa.js. Prefer this style when writing your own middleware.\n\n```ts\ninterface NextMiddleware {\n  (\n    req: NextApiRequest,\n    res: NextApiResponse,\n    next: () => Promise<void>\n  ): Promise<void>;\n}\n```\n\n#### `ExpressMiddleware`\n\n`ExpressMiddleware` roughly matches the signature used by Express/Connect style middleware. This type can be used when importing third-party libraries such as `cors`.\n\n```ts\ninterface ExpressMiddleware<\n  Request = IncomingMessage,\n  Response = ServerResponse\n> {\n  (\n    req: Request,\n    res: Response,\n    next: (error?: any) => void | Promise<void>\n  ): void;\n}\n```\n\nAn example using `cors`:\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nexport const withMiddleware = use(\n  // Asserting express/connect middleware as an `ExpressMiddleware` interface\n  // can resolve type conflicts by libraries that provide their own types.\n  cors() as ExpressMiddleware\n);\n```\n","licenseText":"MIT License\n\nCopyright (c) 2020 Hunter Tunnicliff\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"next-api-middleware@1.0.0-canary.2","dist":{"shasum":"883069b8735eeec85e79c2eaa13f28cbb0f3a9dc","integrity":"sha512-MJzFEI8enxUZOU1l6bGP7aAtTlob1ahZokI6da+yMgzimZKL0Sj+RiWjzHvMW4FHTeRLDrecwxom6BqfDbTymw==","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-1.0.0-canary.2.tgz","fileCount":4,"unpackedSize":10901,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg8xgPCRA9TVsSAnZWagAAs7cP/R3NSljEHhfyIxTNhnHs\n9apM0EWJD/tasVegkm+2WX0294UQJ+XEZS8xmBmuq0lJ/37RmGVWzTr9s6I2\nwqx2w2ER2c2XYXbJdYiIVQhu69GSLFEwF7OJVtQZjIgxwfTv6pCOp2BLrmye\nG+2sgqhVAfoH/FGy4LeI+IJoYGgaXAJk85QWwLjaZm3IBVbYYBwDOBo/Abq2\nug6ATTkJf3+cI5GRrgbAf6xgjLeGLWs8++ndQDj1S75oZIXEwUTWGA1v8gkE\nFcPPjtXQ0Ku3j8by7UlibgRAttf6I9BbxDMOAoKmSyHsOUuVGw5sk6+/Zq8D\nXD9aa6EM3juAyP2kRUawbBTbtDG42eNt1RQ1nHSOTdwocBaLKU7k63qjnhjV\nG53eHK03aH3LnU/Haip6QPJtFE7mNfH6h10q8HnNTdo7wABX0fzFQfB0S4Pi\nFgUqz8gfGJDmWjGff+BFJEBDvEbr9qXazTcnDYO0GKEBzrYeWilAHYorDOsK\niRb47RYSRFr8vCiidL1UrXSCkyY5uemg1i5Y2G4KIsZ4Qmhfvf3BlfbhNeD5\nO5GZYGjjT+9didSmC+IrbJdyTPdKt/ZMcSgsFsTWdmwzoXwNHldCZOtggJOM\neauSTmQTYOP2bMAwyP54DnX6AdezxLAziXZ9DsQSEwacK/eegDqhFsVWxYsE\nPvU4\r\n=koa+\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBKIMOMXaAaWE/YRD+Gu0xjC9N2pOApvoo2/MUxnt1K8AiB/tK6Fdz7UmaL5GGeIEseYJqImB8FAN28iFMD9eIHluA=="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_1.0.0-canary.2_1626544143610_0.6959661352028552"},"_hasShrinkwrap":false},"1.0.0-beta.0":{"name":"next-api-middleware","version":"1.0.0-beta.0","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git@github.com:htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","type-check":"tsc --pretty --noEmit","test":"jest","preversion":"yarn test","version":"rm -rf dist && yarn build"},"devDependencies":{"@babel/core":"^7.14.8","@babel/preset-env":"^7.14.8","@babel/preset-typescript":"^7.13.0","@types/cors":"^2.8.12","@types/debug":"^4.1.6","@types/jest":"^26.0.24","@types/node":"^15.14.2","@types/react":"^17.0.14","@types/react-dom":"^17.0.9","babel-jest":"^27.0.2","cors":"^2.8.5","jest":"^27.0.4","jest-extended":"^0.11.5","next":">=10","ts-node":"^10.1.0","typescript":"^4.2.4"},"dependencies":{"debug":"^4.3.2"},"peerDependencies":{"next":">=10"},"readmeFilename":"README.md","readme":"<p align=\"center\">\n  <h1 align=\"center\">Next.js API Middleware</h1>\n</p>\n\n<p align=\"center\">\n  <a aria-label=\"npm@latest\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm@latest\" src=\"https://img.shields.io/npm/v/next-api-middleware/latest.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"npm@canary\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm@canary\" src=\"https://img.shields.io/npm/v/next-api-middleware/canary.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n</p>\n<p align=\"center\">\n  <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"tests\" src=\"https://img.shields.io/github/workflow/status/htunnicliff/next-api-middleware/Test?style=for-the-badge&labelColor=000000&label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?style=for-the-badge&labelColor=000000&token=XI7G8L08TY\">\n  </a>\n<p>\n\n## Introduction\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares) :thumbsdown:. This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use. :tada:\n\n## Table of Contents\n\n- [Quick Start](#quick-start)\n- [How It Works](#quick-start)\n- [APIs](#apis)\n  - [`label`](#label)\n  - [`use`](#use)\n- [Usage Guide](#usage-guide)\n- [Advanced](#advanced)\n  - [Debugging](#debugging)\n  - [Middleware Factories](#middleware-factories)\n  - [Middleware Types](#middleware-types)\n    - [`NextMiddleware`](#nextmiddleware-preferred)\n    - [`ExpressMiddleware`](#expressmiddleware-compatibility)\n\n## Quick Start\n\n```ts\nimport { label, NextMiddleware } from \"next-api-middleware\";\nimport * as Sentry from \"@sentry/nextjs\";\nimport nanoid from \"nanoid\";\n\n// 1 – Create middleware functions\n\nconst captureErrors: NextMiddleware = async (req, res, next) => {\n  try {\n    // Catch any errors that are thrown in remaining\n    // middleware and the API route handler\n    await next();\n  } catch (err) {\n    const eventId = Sentry.captureException(err);\n\n    res.status(500);\n    res.json({ error: err });\n  }\n};\n\nconst addRequestId: NextMiddleware = async (req, res, next) => {\n  // Let remaining middleware and API route execute\n  await next();\n\n  // Apply header\n  res.setHeader(\"X-Response-ID\", nanoid());\n};\n\n// 2 – Use `label` to assemble all middleware\n\nconst withMiddleware = label(\n  {\n    addRequestId,\n    sentry: captureErrors, // <-- Optionally alias middleware\n  },\n  [\"sentry\"] // <-- Provide a list of middleware to call automatically\n);\n\n// 3 – Define your API route handler\n\nconst apiRouteHandler = async (req, res) => {\n  res.status(200);\n  res.send(\"Hello world!\");\n};\n\n// 4 – Choose middleware to invoke for this API route\n\nexport default withMiddleware(\"addRequestId\")(apiRouteHandler);\n```\n\n## How It Works\n\nMy mental model for how this library handles middleware functions is that of a \"winding and unwinding stack.\"\n\nLet's imagine you've used `label` to add two middleware functions to an API route.\n\nWhen a request comes in, this is a rough impression of how that request makes its way through all middleware functions, the API route handler itself, and then back up through the middleware.\n\n```\n              |-----------------|-----------------|--------------------|\n              |  Middleware #1  |  Middleware #2  | API Route Handler  |\n              |-----------------|-----------------|--------------------|\n              |                 |                 |                    |\nRequest ------|----> Setup -----|----> Setup -----|-->------|          |\n              |                 |                 |         |          |\n              |-----------------|-----------------|         V          |\n              |                 |                 |                    |\n              |   await next()  |   await next()  |     API stuff      |\n              |                 |                 |                    |\n              |-----------------|-----------------|         |          |\n              |                 |                 |         |          |\nResponse <----|--- Teardown <---|--- Teardown <---|---------|          |\n              |                 |                 |                    |\n              |-----------------|-----------------|--------------------|\n```\n\nWhile this is a crummy ASCII diagram, I think it gives the right impression. The request winds its way though each middleware function in succession, hits the API route handler, and then proceeds to \"unwind\" its way through the stack.\n\nEvery middleware function has the opportunity to go through three phases:\n\n1. Setup\n2. Waiting\n3. Teardown\n\nThe \"Setup\" phase covers everything that happens before `await next()`. The \"Waiting\" phase is really just `await next()`. The \"Teardown\" phase is the remaining code within a middleware function after `await next()`.\n\nIt is worth noting that although these phases are available to all middleware functions, you don't need to take advantage of them all.\n\nFor example, in error catching middleware you might simply wrap `await next()` in a `try / catch` block. On the other hand, you might have request timing middleware that captures a start time during the setup phase, waits, and then captures a finish time in the teardown phase.\n\n## APIs\n\n### `label`\n\nThis is the primary utility for creating reusuable collections of middleware for use throughout many Next.js API routes.\n\n```ts\nconst withMiddleware = label(middleware, defaults);\n```\n\n#### Parameters\n\n- `middleware`: an object containing middleware functions or arrays of middleware\n- `defaults`: (optional) an array of `middleware` keys that will be invoked automatically\n\n#### Return Value\n\n`label` returns a function (conventionally referred to as `withMiddleware`) that uses currying to accept a list of middleware names to be invoked, followed by a Next.js API handler function.\n\nTypically, `withMiddleware` will be imported in API route files and used at the default export statement:\n\n```ts\nimport { withMiddleware } from \"../helpers/my-middleware\";\n\nconst apiRouteHandler = async (req, res) => {\n  ...\n}\n\nexport default withMiddleware(\"foo\", \"bar\", \"baz\")(apiRouteHandler);\n```\n\nThough `label` could contain many middleware functions, the actual middleware invoked by an API route is determined by the names passed in to `withMiddleware`.\n\n#### Examples\n\n##### Basic Use\n\n```ts\nconst logErrors = async (req, res, next) => {\n  try {\n    await next();\n  } catch (error) {\n    console.error(error);\n    res.status(500);\n    res.json({ error });\n  }\n};\n\nconst withMiddleware = label({\n  logErrors,\n});\n\n// export default withMiddleware(\"logErrors\")(apiRouteHandler);\n```\n\n##### Aliases\n\n```ts\nconst withMiddleware = label({\n  error: logErrors,\n});\n\n// export default withMiddleware(\"error\")(apiRouteHandler);\n```\n\n##### Groups\n\n```ts\nimport { foo, bar, baz } from \"./my-middleware\";\n\nconst withMiddleware = label({\n  error: logErrors,\n  myGroup: [foo, bar, baz],\n});\n\n// export default withMiddleware(\"error\", \"myGroup\")(apiRouteHandler);\n```\n\n##### Defaults\n\n```ts\nconst withMiddleware = label(\n  {\n    error: logErrors,\n    myGroup: [foo, bar, baz],\n  },\n  [\"error\"]\n);\n\n// export default withMiddleware(\"myGroup\")(apiRouteHandler);\n```\n\n### `use`\n\nThis utility accepts middleware functions directly and executes them all in order. It is a simpler alternative to `label` that can be useful for handling one-off middleware functions.\n\n```ts\nconst withInlineMiddleware = use(...middleware);\n```\n\n#### Parameters\n\n- `middleware`: a list of middleware functions and/or arrays of middleware functions\n\n#### Return Value\n\n`use` returns a function that accepts a Next.js API route handler.\n\n#### Examples\n\n##### CORS\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nconst apiRouteThatOnlyNeedsCORS = async (req, res) => {\n  ...\n}\n\nexport default use(cors())(apiRouteThatOnlyNeedsCORS);\n```\n\n## Usage Guide\n\nSee [EXAMPLES.md](./EXAMPLES.md) for more detailed examples of `label` and `use`.\n\n## Advanced\n\n### Debugging\n\n`next-api-middleware` uses [`debug`](https://npmjs.com/package/debug) to provide different levels of debug logging.\n\nLogs for this library are available under the `middleware` namespace.\n\n> These example commands assume the presence of a script called `dev` in your project's `package.json` that runs `next dev`.\n\n```sh\n# Show all logs\nDEBUG=middleware:* npm run dev\n\n# Show all logs for the `/api/foo` route\nDEBUG=middleware:/api/foo npm run dev\n\n# Show all logs for the 3rd middleware function in `/api/foo`\nDEBUG=middleware:/api/foo:fn-3 npm run dev\n```\n\nYou can also wrap the value of `DEBUG` in quotes if desired:\n\n```sh\nDEBUG='middleware:/api/foo' npm run dev\n```\n\n### Middleware Factories\n\nSince `use` and `label` accept values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { NextMiddleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): NextMiddleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const postRequestsOnlyMiddleware = httpMethod(\"POST\");\n```\n\n### Middleware Types\n\n`next-api-middleware` supports two middleware signatures, `NextMiddleware` and `ExpressMiddleware`.\n\n#### `NextMiddleware`\n\n`NextMiddleware` is inspired by the asyncronous middleware style popularized by Koa.js. Prefer this style when writing your own middleware.\n\n```ts\ninterface NextMiddleware {\n  (\n    req: NextApiRequest,\n    res: NextApiResponse,\n    next: () => Promise<void>\n  ): Promise<void>;\n}\n```\n\n#### `ExpressMiddleware` (compatibility)\n\n`ExpressMiddleware` roughly matches the signature used by Express/Connect style middleware. This type can be used when importing third-party libraries such as `cors`.\n\n```ts\ninterface ExpressMiddleware<\n  Request = IncomingMessage,\n  Response = ServerResponse\n> {\n  (\n    req: Request,\n    res: Response,\n    next: (error?: any) => void | Promise<void>\n  ): void;\n}\n```\n\nAn example using `cors`:\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nexport const withMiddleware = use(\n  // Asserting express/connect middleware as an `ExpressMiddleware` interface\n  // can resolve type conflicts by libraries that provide their own types.\n  cors() as ExpressMiddleware\n);\n```\n","licenseText":"MIT License\n\nCopyright (c) 2020 Hunter Tunnicliff\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"next-api-middleware@1.0.0-beta.0","dist":{"shasum":"5f5b710890d16c5df9d0c8d4f2565399d692174c","integrity":"sha512-npbzuztPKqmsn5Ys2qFfigeimC2N2agPtEyyGRmoeGTBhQDxSxysclXOGrX/JqG1keo6Bq1f7YGdFgA0e5DO5g==","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-1.0.0-beta.0.tgz","fileCount":4,"unpackedSize":13846,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg+vpzCRA9TVsSAnZWagAAS1QQAJ1ZAhTu5koJ33AxtZJF\nSwuyWoOgx5/vk6dmhTth7QsN2BMj50isZWkBu3utKA4G5EvxZw6X2ZFCCzko\nd0WfQ+9AXqQziTAncxKw/ghnSFRzmJNUIKOX7qJGh/+E+lAvsH8dls5Ja8SW\n1WauWhh2P+uAhP/uHfLNj9XlcgYFIBTeeJ0h2LjoXbjFgpr5uLFI+hxTdr2b\n7j0QMkDPn47XItpd2h89LgouHnqF8bYONfAKZ9+Vcpv2aBxXJ1ImXco6XRw4\nxFetmgmm+vpdRfm0poOjkMk/x/JE0BdOzq/wMVb3HXelX0o+LFiVinauTclo\ngkkOpyHXLTJaMF+HFVJjrSpxD9sP5ap7OBjqQnZxWsxV9jfLGI5cvWjRYO8V\nz0T5OQQheNPGbLtr0Dk2uUEUW0lP4B6Hqd4NnZEYCTOc6w/kymgvj8mMJy48\nJcWEMpUS7dnQBUFS0yAWG/aVZ4jh/1szs1ufaIG6+uzJt/OYZyKOlJKvRMhE\nJjBF2g9Duu08bXTWMClOCt8mYg6FQXaIHuRnGht5EeRkrot4G7JDPkLEO9r9\nx6pKE86xSFJfvBM9PVuMhcVe1jC9P7Wfvauo2mMwYInsjhuj73jwT6ZDFLBD\nZtRR2A+Fhp+uBr5VMH8TYrNb7EI3ohB4jGYFaLXGw8Q6O/k02flc3r2YYjW3\nOPIW\r\n=UFCp\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDw+oPgjY8LWGT5pwhjJ9CZ268MXz8WduZtM1a/+GlzXwIgW2zMh8Lj6eYYNGdhSa6Eq7ihlLtv/WWJrFQAYzPrHtk="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_1.0.0-beta.0_1627060851536_0.09889324197441174"},"_hasShrinkwrap":false},"1.0.0-beta.2":{"name":"next-api-middleware","version":"1.0.0-beta.2","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git@github.com:htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","type-check":"tsc --pretty --noEmit","test":"jest","preversion":"yarn test","version":"rm -rf dist && yarn build"},"devDependencies":{"@babel/core":"^7.14.8","@babel/preset-env":"^7.14.8","@babel/preset-typescript":"^7.13.0","@types/cors":"^2.8.12","@types/debug":"^4.1.6","@types/jest":"^26.0.24","@types/node":"^15.14.2","@types/react":"^17.0.14","@types/react-dom":"^17.0.9","babel-jest":"^27.0.2","cors":"^2.8.5","jest":"^27.0.4","jest-extended":"^0.11.5","next":">=10","ts-node":"^10.1.0","typescript":"^4.2.4"},"dependencies":{"debug":"^4.3.2"},"peerDependencies":{"next":">=10"},"readmeFilename":"README.md","readme":"<p align=\"center\">\n  <h1 align=\"center\">Next.js API Middleware</h1>\n</p>\n\n<p align=\"center\">\n  <a aria-label=\"npm@latest\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm@latest\" src=\"https://img.shields.io/npm/v/next-api-middleware/latest.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"npm@canary\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm@canary\" src=\"https://img.shields.io/npm/v/next-api-middleware/canary.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n</p>\n<p align=\"center\">\n  <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"tests\" src=\"https://img.shields.io/github/workflow/status/htunnicliff/next-api-middleware/Test?style=for-the-badge&labelColor=000000&label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?style=for-the-badge&labelColor=000000&token=XI7G8L08TY\">\n  </a>\n<p>\n\n## Introduction\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares) :thumbsdown:. This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use. :tada:\n\n## Table of Contents\n\n- [Quick Start](#quick-start)\n- [How It Works](#quick-start)\n- [APIs](#apis)\n  - [`label`](#label)\n  - [`use`](#use)\n- [Usage Guide](#usage-guide)\n- [Advanced](#advanced)\n  - [Debugging](#debugging)\n  - [Middleware Factories](#middleware-factories)\n  - [Middleware Types](#middleware-types)\n    - [`NextMiddleware`](#nextmiddleware-preferred)\n    - [`ExpressMiddleware`](#expressmiddleware-compatibility)\n\n## Quick Start\n\n```ts\nimport { label, NextMiddleware } from \"next-api-middleware\";\nimport * as Sentry from \"@sentry/nextjs\";\nimport nanoid from \"nanoid\";\n\n// 1 – Create middleware functions\n\nconst captureErrors: NextMiddleware = async (req, res, next) => {\n  try {\n    // Catch any errors that are thrown in remaining\n    // middleware and the API route handler\n    await next();\n  } catch (err) {\n    const eventId = Sentry.captureException(err);\n\n    res.status(500);\n    res.json({ error: err });\n  }\n};\n\nconst addRequestId: NextMiddleware = async (req, res, next) => {\n  // Let remaining middleware and API route execute\n  await next();\n\n  // Apply header\n  res.setHeader(\"X-Response-ID\", nanoid());\n};\n\n// 2 – Use `label` to assemble all middleware\n\nconst withMiddleware = label(\n  {\n    addRequestId,\n    sentry: captureErrors, // <-- Optionally alias middleware\n  },\n  [\"sentry\"] // <-- Provide a list of middleware to call automatically\n);\n\n// 3 – Define your API route handler\n\nconst apiRouteHandler = async (req, res) => {\n  res.status(200);\n  res.send(\"Hello world!\");\n};\n\n// 4 – Choose middleware to invoke for this API route\n\nexport default withMiddleware(\"addRequestId\")(apiRouteHandler);\n```\n\n## How It Works\n\nMy mental model for how this library handles middleware functions is that of a \"winding and unwinding stack.\"\n\nLet's imagine you've used `label` to add two middleware functions to an API route.\n\nWhen a request comes in, this is a rough impression of how that request makes its way through all middleware functions, the API route handler itself, and then back up through the middleware.\n\n```\n              |-----------------|-----------------|--------------------|\n              |  Middleware #1  |  Middleware #2  | API Route Handler  |\n              |-----------------|-----------------|--------------------|\n              |                 |                 |                    |\nRequest ------|----> Setup -----|----> Setup -----|-->------|          |\n              |                 |                 |         |          |\n              |-----------------|-----------------|         V          |\n              |                 |                 |                    |\n              |   await next()  |   await next()  |     API stuff      |\n              |                 |                 |                    |\n              |-----------------|-----------------|         |          |\n              |                 |                 |         |          |\nResponse <----|--- Teardown <---|--- Teardown <---|---------|          |\n              |                 |                 |                    |\n              |-----------------|-----------------|--------------------|\n```\n\nWhile this is a crummy ASCII diagram, I think it gives the right impression. The request winds its way though each middleware function in succession, hits the API route handler, and then proceeds to \"unwind\" its way through the stack.\n\nEvery middleware function has the opportunity to go through three phases:\n\n1. Setup\n2. Waiting\n3. Teardown\n\nThe \"Setup\" phase covers everything that happens before `await next()`. The \"Waiting\" phase is really just `await next()`. The \"Teardown\" phase is the remaining code within a middleware function after `await next()`.\n\nIt is worth noting that although these phases are available to all middleware functions, you don't need to take advantage of them all.\n\nFor example, in error catching middleware you might simply wrap `await next()` in a `try / catch` block. On the other hand, you might have request timing middleware that captures a start time during the setup phase, waits, and then captures a finish time in the teardown phase.\n\n## APIs\n\n### `label`\n\nThis is the primary utility for creating reusuable collections of middleware for use throughout many Next.js API routes.\n\n```ts\nconst withMiddleware = label(middleware, defaults);\n```\n\n#### Parameters\n\n- `middleware`: an object containing middleware functions or arrays of middleware\n- `defaults`: (optional) an array of `middleware` keys that will be invoked automatically\n\n#### Return Value\n\n`label` returns a function (conventionally referred to as `withMiddleware`) that uses currying to accept a list of middleware names to be invoked, followed by a Next.js API handler function.\n\nTypically, `withMiddleware` will be imported in API route files and used at the default export statement:\n\n```ts\nimport { withMiddleware } from \"../helpers/my-middleware\";\n\nconst apiRouteHandler = async (req, res) => {\n  ...\n}\n\nexport default withMiddleware(\"foo\", \"bar\", \"baz\")(apiRouteHandler);\n```\n\nThough `label` could contain many middleware functions, the actual middleware invoked by an API route is determined by the names passed in to `withMiddleware`.\n\n#### Examples\n\n##### Basic Use\n\n```ts\nconst logErrors = async (req, res, next) => {\n  try {\n    await next();\n  } catch (error) {\n    console.error(error);\n    res.status(500);\n    res.json({ error });\n  }\n};\n\nconst withMiddleware = label({\n  logErrors,\n});\n\n// export default withMiddleware(\"logErrors\")(apiRouteHandler);\n```\n\n##### Aliases\n\n```ts\nconst withMiddleware = label({\n  error: logErrors,\n});\n\n// export default withMiddleware(\"error\")(apiRouteHandler);\n```\n\n##### Groups\n\n```ts\nimport { foo, bar, baz } from \"./my-middleware\";\n\nconst withMiddleware = label({\n  error: logErrors,\n  myGroup: [foo, bar, baz],\n});\n\n// export default withMiddleware(\"error\", \"myGroup\")(apiRouteHandler);\n```\n\n##### Defaults\n\n```ts\nconst withMiddleware = label(\n  {\n    error: logErrors,\n    myGroup: [foo, bar, baz],\n  },\n  [\"error\"]\n);\n\n// export default withMiddleware(\"myGroup\")(apiRouteHandler);\n```\n\n### `use`\n\nThis utility accepts middleware functions directly and executes them all in order. It is a simpler alternative to `label` that can be useful for handling one-off middleware functions.\n\n```ts\nconst withInlineMiddleware = use(...middleware);\n```\n\n#### Parameters\n\n- `middleware`: a list of middleware functions and/or arrays of middleware functions\n\n#### Return Value\n\n`use` returns a function that accepts a Next.js API route handler.\n\n#### Examples\n\n##### CORS\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nconst apiRouteThatOnlyNeedsCORS = async (req, res) => {\n  ...\n}\n\nexport default use(cors())(apiRouteThatOnlyNeedsCORS);\n```\n\n## Usage Guide\n\nSee [EXAMPLES.md](./EXAMPLES.md) for more detailed examples of `label` and `use`.\n\n## Advanced\n\n### Debugging\n\n`next-api-middleware` uses [`debug`](https://npmjs.com/package/debug) to provide different levels of debug logging.\n\nLogs for this library are available under the `middleware` namespace.\n\n> These example commands assume the presence of a script called `dev` in your project's `package.json` that runs `next dev`.\n\n```sh\n# Show all logs\nDEBUG=middleware:* npm run dev\n\n# Show all logs for the `/api/foo` route\nDEBUG=middleware:/api/foo npm run dev\n\n# Show all logs for the 3rd middleware function in `/api/foo`\nDEBUG=middleware:/api/foo:fn-3 npm run dev\n```\n\nYou can also wrap the value of `DEBUG` in quotes if desired:\n\n```sh\nDEBUG='middleware:/api/foo' npm run dev\n```\n\n### Middleware Factories\n\nSince `use` and `label` accept values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { NextMiddleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): NextMiddleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const postRequestsOnlyMiddleware = httpMethod(\"POST\");\n```\n\n### Middleware Types\n\n`next-api-middleware` supports two middleware signatures, `NextMiddleware` and `ExpressMiddleware`.\n\n#### `NextMiddleware`\n\n`NextMiddleware` is inspired by the asyncronous middleware style popularized by Koa.js. Prefer this style when writing your own middleware.\n\n```ts\ninterface NextMiddleware {\n  (\n    req: NextApiRequest,\n    res: NextApiResponse,\n    next: () => Promise<void>\n  ): Promise<void>;\n}\n```\n\n#### `ExpressMiddleware` (compatibility)\n\n`ExpressMiddleware` roughly matches the signature used by Express/Connect style middleware. This type can be used when importing third-party libraries such as `cors`.\n\n```ts\ninterface ExpressMiddleware<\n  Request = IncomingMessage,\n  Response = ServerResponse\n> {\n  (\n    req: Request,\n    res: Response,\n    next: (error?: any) => void | Promise<void>\n  ): void;\n}\n```\n\nAn example using `cors`:\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nexport const withMiddleware = use(\n  // Asserting express/connect middleware as an `ExpressMiddleware` interface\n  // can resolve type conflicts by libraries that provide their own types.\n  cors() as ExpressMiddleware\n);\n```\n","licenseText":"MIT License\n\nCopyright (c) 2020 Hunter Tunnicliff\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"next-api-middleware@1.0.0-beta.2","dist":{"shasum":"048519152840db32bd1b1a2e956584fcfddea77c","integrity":"sha512-lTvhBrFuCfINKPxfldKfY+JDPPCOFUMSGsh/r0QQye7VCQS+iJvkG+MhPZECqBhxPIzHVhqYQKpkm0CGOIxlQw==","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-1.0.0-beta.2.tgz","fileCount":26,"unpackedSize":36663,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg+v2WCRA9TVsSAnZWagAAEqoQAJDcdvzzzrc1PxWiCUig\n+xua/kpcb8QXPwsBcWEQRTurregR5gfvcB9GcnGgMsNXzzob1jj01B156kYe\nbT7lYzAOdtfS2BIIMFwWn0KL8ESstrejpjowiyQ2ZwYkpDf/du50c6/TvgeB\nZC1PsoXKEQ4DDo0YS0jgzX1cjz0+rCIe4ptAA5xqNEepyA7zEwF+GbduxSEa\ntnIKCefekGFrxR8QoKzs0vAUDsf16zzEfIR5hlTebfo3tDGX5aF165ftRJev\n0k2sqWIq4duScONlVIXR65djUQYYMNqp38tiC4lqJkJvnCigo2Jhz+m3kr5u\n083nKdr7diFUsqKV8bbJnixbFV5XJwLpd/ehihyftssuT01w5pfiYH3IccB9\nC3JAbw3OqddPcoShZ+SmIUZWcavvW4+PCSD70Z+U4IHWSMmFgjEh2GZ8oX3W\ng4GrMo28pYSu9sqyJd0Iu4YYqX3hJJ+g1pkLAfzcePfwwMMKollS6k3/ysBg\nyeKdNik2lcaVwnLYWhxbpN6dhh/2iEnx8qpKtV9g0cFKYiSu0xGmz7H2H7Pw\ni2vx/TU2UV6WVXfi0zAcRcKPnmQKCRG6jWpzb52J2uhCiUSzrz1XB3Hw5P43\nTrPIt/gf9S9HBJ7cd3zUFPU9vD6wCQON5FXvTZRcN+DFPtqORKcvZzIGOf0T\nL93m\r\n=XUbK\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDJ9lx+zvv2P4C1fBhNTERy0TvW8Soq+4ilioD4glMTQwIgDz2j6GgHSLgHJsNVKmgno0Pjexr8qDa12DL1GAYyg+w="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_1.0.0-beta.2_1627061654087_0.12133060376217064"},"_hasShrinkwrap":false},"1.0.0":{"name":"next-api-middleware","version":"1.0.0","description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git@github.com:htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","type-check":"tsc --pretty --noEmit","test":"jest","preversion":"yarn test","version":"rm -rf dist && yarn build"},"devDependencies":{"@babel/core":"^7.14.8","@babel/preset-env":"^7.14.8","@babel/preset-typescript":"^7.13.0","@types/cors":"^2.8.12","@types/debug":"^4.1.6","@types/jest":"^26.0.24","@types/node":"^15.14.2","@types/react":"^17.0.14","@types/react-dom":"^17.0.9","babel-jest":"^27.0.2","cors":"^2.8.5","jest":"^27.0.4","jest-extended":"^0.11.5","next":">=10","ts-node":"^10.1.0","typescript":"^4.2.4"},"dependencies":{"debug":"^4.3.2"},"peerDependencies":{"next":">=10"},"licenseText":"MIT License\n\nCopyright (c) 2020 Hunter Tunnicliff\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"next-api-middleware@1.0.0","dist":{"shasum":"96f2f654025af0811cea14ba6e347ea7c44d9ea9","integrity":"sha512-XfWwOtUmAF6DSdH5DoXBPjlnCGa4fWE2INHClRwT2gw0Xfv7ujxCUbvCz6vc/xS4IlDhQYUWng0c3/NLUntxYw==","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-1.0.0.tgz","fileCount":26,"unpackedSize":36656,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg+xlICRA9TVsSAnZWagAATkIP/RUR3lVEsNgeknk86D8Z\n8tkEQ3/hF+uQ9fYv0CPGblLzh2QlCd4KBB+E/Vx83IvsCEqGn1XPVXAiBWBR\nLcdHz6W4xzV8au4Atp8nxr7CQ8b8VaYK6/NCuEsb3SHiyek9ZRLRDVfSChRP\nCWoZm+RykbPaYer9ht5GeiDj2L4+4JoeETApaIjoLizceNM58gk1jV2370/Q\nkYIREYsvKZ/IuxQLUk2f+Tg0HMIabLzJaB3mp8wCR73rzWZgXabS1Y080BTL\nSAKgEUC92pZ/GVnuJLMtUX6umUwwCAHLb3CQfHbmeJIVuXZGZpBo6Ywwg6dA\nLopye2xMVbBbYbYUhG+eSF99aV1gwtilEI7QyBkrKyqtRQ6rqOuB2jep1O/Q\nSN5qKR+eusRAqVhglloLcwcIF3dmSlox40gkJL+ZCZeCoZL+GEhH70yFzOKa\neZvBABbY/x+JsoGywsbmJMAofoJCCejZJrf4CZOVCCS5x1JUhyxjI8tdInhN\nFn0jo1bQNYFcaTgAcLMlfCsh62rJ3sVdjMMSttEIrZipvkRotQeSrs5XULXa\nYLgy7/iTxsFxddkhnjH447b/BbgcTdvDlGtE8fk8cfa+OypaosLvtqe9JDPF\n3IvngbfKB0b+gV/ZON4/iIg/Lz5yQDL0ryDIBOBzhbPtGVUaEQ9zfOqnU4vm\nvURY\r\n=HCBx\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIC6D91lKZutjd3CYJAs7FaNsSSzpAlHKmf5kQ9fPobhpAiEA3eUaxxRlXpZnZHhd7Qmwldnv9R/TL65jWBt0CAj5ZEw="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_1.0.0_1627068744148_0.9817730273480891"},"_hasShrinkwrap":false},"1.0.1":{"name":"next-api-middleware","version":"1.0.1","description":"Middleware solution for Next.js API routes","author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"repository":{"type":"git","url":"https://github.com/htunnicliff/next-api-middleware.git"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">= 12.20"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"tsc","type-check":"tsc --pretty --noEmit","test":"jest","preversion":"yarn test","version":"rm -rf dist && yarn build"},"devDependencies":{"@babel/core":"^7.14.8","@babel/preset-env":"^7.14.8","@babel/preset-typescript":"^7.13.0","@types/cors":"^2.8.12","@types/debug":"^4.1.6","@types/jest":"^26.0.24","@types/node":"^15.14.2","@types/react":"^17.0.14","@types/react-dom":"^17.0.9","babel-jest":"^27.0.2","cors":"^2.8.5","jest":"^27.0.4","jest-extended":"^0.11.5","next":">=10","ts-node":"^10.1.0","typescript":"^4.2.4"},"dependencies":{"debug":"^4.3.2"},"peerDependencies":{"next":">=10"},"licenseText":"MIT License\n\nCopyright (c) 2020 Hunter Tunnicliff\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"next-api-middleware@1.0.1","dist":{"shasum":"8dd76abeace9e10c6be29c9c9486a84cb649cc12","integrity":"sha512-t8UbZ9UUPFB7nklrHdAusn7MfoVSHtnWlRV1R0hirvLRHPDnzidTnw1Mu99Kqvc1bVC0rQSUX5kf0j/4nmEtfA==","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-1.0.1.tgz","fileCount":26,"unpackedSize":36637,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg+xqcCRA9TVsSAnZWagAAtcgP/0FObW5RlGqitK9T0LTK\nYfzcQyyTOh20zutYSZ2uUO6Z5KKQJsKPKsGl8zzdYhRIs4HIACRjCgGGCC/Y\n3VqF4p3quKsOEhL0K/E5PIoL8M8sDGYT9lAg8noInKX9uddnX1uhnVgr9yWp\n0kXI9NjOmXRbh2iiERYfrv4VAOsjLjJEq/U1WkIbyGMWvdW1gIt2dQvAgPSX\nLubmcdTbLuQ3exhqarxak38duMzjRNz9pfk+LdqvVQ9TduxWl4beMfxCJHEw\nyWmBK1s+jiSmj6cC+BnJ8pPnU8szOsCBzTH4J2H8oYcd8vxEhz6PkC+O3+1R\nTolm6GtRr7YowVaZHNG5XMLcR3Rm07NE2jzG/nrnsnJiyDDclfqlwAPN3+3b\nnMSSR43dCWXXtDU8c9HW/O3vUGYiXCvcTFfIoqWv1Eej3ubcKZ6GtOd6Lmhk\nsQiW7s+Ley7n9TfbMu6KOvJMNKcWjSV4ux8Zy1Hfxos0q9bSc0Pvel4ipIpr\nJE8UB7ukpVPw5WBuLy1H4bIluOJs7JVSyXg8Wcn9Oh37hsbC3/WbPgNrmkFo\n77KMsI5Vh3z+Jf9sifIZiWLWOdeNNBspRQCKrk1VvxorT+qVuvWyV0420ziG\nwojCIAclFXWIMSIOyDqLOoNMYFBx7WokP1wvvw3omr5xyqg6D05rzuTQnCWR\naPUy\r\n=/jyw\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC82EIin7JfYVvg5LEAeuXX6/0soMMxAeM8/+fb5rB4eQIgKXrOp3i8Dce8nzijLm7gCb8tmBr5hLcF+f05dEEVPI4="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_1.0.1_1627069084647_0.4835269047091042"},"_hasShrinkwrap":false},"2.0.0-canary.0":{"name":"next-api-middleware","version":"2.0.0-canary.0","description":"Middleware solution for Next.js API routes","author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"repository":{"type":"git","url":"https://github.com/htunnicliff/next-api-middleware.git"},"license":"MIT","type":"module","exports":{".":"./dist/index.js","./label.js":"./dist/label.js","./label":"./dist/label.js","./use.js":"./dist/use.js","./use":"./dist/use.js","./validation.js":"./dist/validation.js","./validation":"./dist/validation.js"},"types":"./dist/index.d.ts","engines":{"node":"^12.20.0 || ^14.13.1 || >=16.0.0"},"keywords":["next","next.js","middleware","API routes","express"],"scripts":{"build":"rimraf dist && tsc","type-check":"tsc --pretty --noEmit","test":"jest","preversion":"yarn test","version":"yarn build"},"devDependencies":{"@babel/core":"^7.17.0","@babel/preset-env":"^7.16.11","@babel/preset-typescript":"^7.16.7","@types/cors":"^2.8.12","@types/debug":"^4.1.7","@types/jest":"^26.0.24","@types/node":"^16.11.22","@types/react":"^17.0.38","@types/react-dom":"^17.0.11","babel-jest":"^27.4.6","cors":"^2.8.5","jest":"^27.4.7","jest-extended":"^0.11.5","jest-ts-webcompat-resolver":"^1.0.0","next":">=10","rimraf":"^3.0.2","ts-node":"^10.4.0","typescript":"^4.5.5"},"dependencies":{"debug":"^4.3.3","release":"^6.3.0"},"peerDependencies":{"next":">=10"},"volta":{"node":"16.13.2","yarn":"1.22.17"},"readmeFilename":"README.md","readme":"<p align=\"center\">\n  <h1 align=\"center\">Next.js API Middleware</h1>\n</p>\n\n<p align=\"center\">\n  <a aria-label=\"npm\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm\" src=\"https://img.shields.io/npm/v/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n   <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"tests\" src=\"https://img.shields.io/github/workflow/status/htunnicliff/next-api-middleware/Test?style=for-the-badge&labelColor=000000&label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?style=for-the-badge&labelColor=000000&token=XI7G8L08TY\">\n  </a>\n</p>\n\n## Introduction\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares) :thumbsdown:. This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use. :tada:\n\n## Table of Contents\n\n- [Quick Start](#quick-start)\n- [How It Works](#quick-start)\n- [APIs](#apis)\n  - [`label`](#label)\n  - [`use`](#use)\n- [Usage Guide](#usage-guide)\n- [Advanced](#advanced)\n  - [Debugging](#debugging)\n  - [Middleware Factories](#middleware-factories)\n  - [Middleware Types](#middleware-types)\n    - [`NextMiddleware`](#nextmiddleware-preferred)\n    - [`ExpressMiddleware`](#expressmiddleware-compatibility)\n\n## Quick Start\n\n```ts\nimport { label, NextMiddleware } from \"next-api-middleware\";\nimport * as Sentry from \"@sentry/nextjs\";\nimport nanoid from \"nanoid\";\n\n// 1 – Create middleware functions\n\nconst captureErrors: NextMiddleware = async (req, res, next) => {\n  try {\n    // Catch any errors that are thrown in remaining\n    // middleware and the API route handler\n    await next();\n  } catch (err) {\n    const eventId = Sentry.captureException(err);\n\n    res.status(500);\n    res.json({ error: err });\n  }\n};\n\nconst addRequestId: NextMiddleware = async (req, res, next) => {\n  // Let remaining middleware and API route execute\n  await next();\n\n  // Apply header\n  res.setHeader(\"X-Response-ID\", nanoid());\n};\n\n// 2 – Use `label` to assemble all middleware\n\nconst withMiddleware = label(\n  {\n    addRequestId,\n    sentry: captureErrors, // <-- Optionally alias middleware\n  },\n  [\"sentry\"] // <-- Provide a list of middleware to call automatically\n);\n\n// 3 – Define your API route handler\n\nconst apiRouteHandler = async (req, res) => {\n  res.status(200);\n  res.send(\"Hello world!\");\n};\n\n// 4 – Choose middleware to invoke for this API route\n\nexport default withMiddleware(\"addRequestId\")(apiRouteHandler);\n```\n\n## How It Works\n\nMy mental model for how this library handles middleware functions is that of a \"winding and unwinding stack.\"\n\nLet's imagine you've used `label` to add two middleware functions to an API route.\n\nWhen a request comes in, this is a rough impression of how that request makes its way through all middleware functions, the API route handler itself, and then back up through the middleware.\n\n```\n              |-----------------|-----------------|--------------------|\n              |  Middleware #1  |  Middleware #2  | API Route Handler  |\n              |-----------------|-----------------|--------------------|\n              |                 |                 |                    |\nRequest ------|----> Setup -----|----> Setup -----|-->------|          |\n              |                 |                 |         |          |\n              |-----------------|-----------------|         V          |\n              |                 |                 |                    |\n              |   await next()  |   await next()  |     API stuff      |\n              |                 |                 |                    |\n              |-----------------|-----------------|         |          |\n              |                 |                 |         |          |\nResponse <----|--- Teardown <---|--- Teardown <---|---------|          |\n              |                 |                 |                    |\n              |-----------------|-----------------|--------------------|\n```\n\nWhile this is a crummy ASCII diagram, I think it gives the right impression. The request winds its way though each middleware function in succession, hits the API route handler, and then proceeds to \"unwind\" its way through the stack.\n\nEvery middleware function has the opportunity to go through three phases:\n\n1. Setup\n2. Waiting\n3. Teardown\n\nThe \"Setup\" phase covers everything that happens before `await next()`. The \"Waiting\" phase is really just `await next()`. The \"Teardown\" phase is the remaining code within a middleware function after `await next()`.\n\nIt is worth noting that although these phases are available to all middleware functions, you don't need to take advantage of them all.\n\nFor example, in error catching middleware you might simply wrap `await next()` in a `try / catch` block. On the other hand, you might have request timing middleware that captures a start time during the setup phase, waits, and then captures a finish time in the teardown phase.\n\n## APIs\n\n### `label`\n\nThis is the primary utility for creating reusuable collections of middleware for use throughout many Next.js API routes.\n\n```ts\nconst withMiddleware = label(middleware, defaults);\n```\n\n#### Parameters\n\n- `middleware`: an object containing middleware functions or arrays of middleware\n- `defaults`: (optional) an array of `middleware` keys that will be invoked automatically\n\n#### Return Value\n\n`label` returns a function (conventionally referred to as `withMiddleware`) that uses currying to accept a list of middleware names to be invoked, followed by a Next.js API handler function.\n\nTypically, `withMiddleware` will be imported in API route files and used at the default export statement:\n\n```ts\nimport { withMiddleware } from \"../helpers/my-middleware\";\n\nconst apiRouteHandler = async (req, res) => {\n  ...\n}\n\nexport default withMiddleware(\"foo\", \"bar\", \"baz\")(apiRouteHandler);\n```\n\nThough `label` could contain many middleware functions, the actual middleware invoked by an API route is determined by the names passed in to `withMiddleware`.\n\n#### Examples\n\n##### Basic Use\n\n```ts\nconst logErrors = async (req, res, next) => {\n  try {\n    await next();\n  } catch (error) {\n    console.error(error);\n    res.status(500);\n    res.json({ error });\n  }\n};\n\nconst withMiddleware = label({\n  logErrors,\n});\n\n// export default withMiddleware(\"logErrors\")(apiRouteHandler);\n```\n\n##### Aliases\n\n```ts\nconst withMiddleware = label({\n  error: logErrors,\n});\n\n// export default withMiddleware(\"error\")(apiRouteHandler);\n```\n\n##### Groups\n\n```ts\nimport { foo, bar, baz } from \"./my-middleware\";\n\nconst withMiddleware = label({\n  error: logErrors,\n  myGroup: [foo, bar, baz],\n});\n\n// export default withMiddleware(\"error\", \"myGroup\")(apiRouteHandler);\n```\n\n##### Defaults\n\n```ts\nconst withMiddleware = label(\n  {\n    error: logErrors,\n    myGroup: [foo, bar, baz],\n  },\n  [\"error\"]\n);\n\n// export default withMiddleware(\"myGroup\")(apiRouteHandler);\n```\n\n### `use`\n\nThis utility accepts middleware functions directly and executes them all in order. It is a simpler alternative to `label` that can be useful for handling one-off middleware functions.\n\n```ts\nconst withInlineMiddleware = use(...middleware);\n```\n\n#### Parameters\n\n- `middleware`: a list of middleware functions and/or arrays of middleware functions\n\n#### Return Value\n\n`use` returns a function that accepts a Next.js API route handler.\n\n#### Examples\n\n##### CORS\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nconst apiRouteThatOnlyNeedsCORS = async (req, res) => {\n  ...\n}\n\nexport default use(cors())(apiRouteThatOnlyNeedsCORS);\n```\n\n## Usage Guide\n\nSee [EXAMPLES.md](./EXAMPLES.md) for more detailed examples of `label` and `use`.\n\n## Advanced\n\n### Debugging\n\n`next-api-middleware` uses [`debug`](https://npmjs.com/package/debug) to provide different levels of debug logging.\n\nLogs for this library are available under the `middleware` namespace.\n\n> These example commands assume the presence of a script called `dev` in your project's `package.json` that runs `next dev`.\n\n```sh\n# Show all logs\nDEBUG=middleware:* npm run dev\n\n# Show all logs for the `/api/foo` route\nDEBUG=middleware:/api/foo npm run dev\n\n# Show all logs for the 3rd middleware function in `/api/foo`\nDEBUG=middleware:/api/foo:fn-3 npm run dev\n```\n\nYou can also wrap the value of `DEBUG` in quotes if desired:\n\n```sh\nDEBUG='middleware:/api/foo' npm run dev\n```\n\n### Middleware Factories\n\nSince `use` and `label` accept values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { NextMiddleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): NextMiddleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const postRequestsOnlyMiddleware = httpMethod(\"POST\");\n```\n\n### Middleware Types\n\n`next-api-middleware` supports two middleware signatures, `NextMiddleware` and `ExpressMiddleware`.\n\n#### `NextMiddleware`\n\n`NextMiddleware` is inspired by the asyncronous middleware style popularized by Koa.js. Prefer this style when writing your own middleware.\n\n```ts\ninterface NextMiddleware {\n  (\n    req: NextApiRequest,\n    res: NextApiResponse,\n    next: () => Promise<void>\n  ): Promise<void>;\n}\n```\n\n#### `ExpressMiddleware` (compatibility)\n\n`ExpressMiddleware` roughly matches the signature used by Express/Connect style middleware. This type can be used when importing third-party libraries such as `cors`.\n\n```ts\ninterface ExpressMiddleware<\n  Request = IncomingMessage,\n  Response = ServerResponse\n> {\n  (\n    req: Request,\n    res: Response,\n    next: (error?: any) => void | Promise<void>\n  ): void;\n}\n```\n\nAn example using `cors`:\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nexport const withMiddleware = use(\n  // Asserting express/connect middleware as an `ExpressMiddleware` interface\n  // can resolve type conflicts by libraries that provide their own types.\n  cors() as ExpressMiddleware\n);\n```\n","licenseText":"MIT License\n\nCopyright (c) 2020 Hunter Tunnicliff\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"next-api-middleware@2.0.0-canary.0","dist":{"shasum":"a3bd90adebc500f9eebb078a69024c9ba309a639","integrity":"sha512-Pns8GGfefjdubOBOJW/olXRnEOygNIzsDGIEshCtFkC/z1qpm/tL4EknRWpJOfqMEq6FO/Su59Pe8+voKuHW0Q==","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-2.0.0-canary.0.tgz","fileCount":26,"unpackedSize":35343,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJiADzoCRA9TVsSAnZWagAAMVgQAJhMq9TdQoG6E+vmrE76\nydLSOTEp2lN++xler7p8mVINGRczYPIn2AzzV1CMVOEJdMDtqNEDPF4xt0lc\n+zKrHd8sv8k++NwPM2uCUEsEfdioIP/I/jN03o+9f4KNa95l2Wl+4TVe4hny\n7tc1piNBrBjrcPL9fvXivGeYWV6mrea1Lfb4TYH0s2t2xGsIo1Dk5CdF2pQF\nCtj9dPfg4bda2J3g6LmnpJTPjNv823Fv3IueDaC77iCW7PzkhlaL2LH4zTgP\nYTKkECIS5rROqBjsm6l99MbFzJ3hdY0rVD8eHJOE0T2cCDRtqgFqIeaZrMRt\nVWl7Jq7IsE0WdE2zYCRt+5a9D6bNK21ychw+crIBfWwEzpdSh/ZD3ILzto60\nuBIB6FTpRiQ1JBrdgBkYqxiNjKFHQ8/qbEYnm8oJI90SKFtKb0SN9njDvpZd\ngza5ZXPSDgyF/zKI6YlWCNRHVyfFiiddIv7pSicEwg2s3ZLrz0l6LpK1PRO/\nXWBZSAmtx3akbXbbEsDfJIkUduD4Iv9/w7k/gBXG7rsWW93Kw4xuPmvHxSSC\n9Buh+exaronNfUgDNlAT0z36UWv+8VsW6FzyNRAxxB/24p9NLHwqrAD9nmNS\noCOOl/+hKyd9WqXUi6vuJXWEjP+tGE2ktTLmc0JfEViMEIB6O1LevleuExDG\nOi86\r\n=FafM\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBQJjwdJHU+HiEryuw9dCPN8BQo231ewVnVodCQkqjxyAiEAqIZUhtUrNtMEX23hYncrLavDAcOsq3OMeMP9Ec7RxhM="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_2.0.0-canary.0_1644182760465_0.6189799778785303"},"_hasShrinkwrap":false},"2.0.0-canary.1":{"name":"next-api-middleware","version":"2.0.0-canary.1","description":"Middleware solution for Next.js API routes","author":"Hunter Tunnicliff <htunnicliff@hey.com>","repository":"htunnicliff/next-api-middleware","license":"MIT","type":"module","exports":{"require":"./dist/index.cjs","import":"./dist/index.js"},"types":"./dist/index.d.ts","engines":{"node":"^12.20.0 || ^14.13.1 || >=16.0.0"},"keywords":["next","next.js","middleware","API routes","express"],"devDependencies":{"@swc/core":"^1.2.148","@swc/jest":"^0.2.20","@types/debug":"^4.1.7","@types/jest":"^26.0.24","@types/node":"^16.11.26","jest":"^27.5.1","jest-extended":"^0.11.5","jest-ts-webcompat-resolver":"^1.0.0","next":"^12.1.0","tsup":"^6.1.3","typescript":"^4.6.2"},"peerDependencies":{"next":">=10"},"scripts":{"build":"tsup","type-check":"tsc --pretty --noEmit","test":"jest","version":"pnpm test && pnpm build"},"_id":"next-api-middleware@2.0.0-canary.1","_integrity":"sha512-3WV7XJVfndapOb1GifQgBx8vWY9bDM3P9GCWFdJ0Kh9EqypYyOcSCrdVseoKbcHDN49EAMaNV34K/dJlkLEKuA==","_resolved":"/private/var/folders/tl/dg1sjkw12m94j38m7pps6clc0000gn/T/e9e7694739795c81caa9be9b3f95646c/next-api-middleware-2.0.0-canary.1.tgz","_from":"file:next-api-middleware-2.0.0-canary.1.tgz","_nodeVersion":"16.13.1","_npmVersion":"8.1.2","dist":{"integrity":"sha512-3WV7XJVfndapOb1GifQgBx8vWY9bDM3P9GCWFdJ0Kh9EqypYyOcSCrdVseoKbcHDN49EAMaNV34K/dJlkLEKuA==","shasum":"3cf69294b1cc530edb0f1348a6d8f04b5d4bc77d","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-2.0.0-canary.1.tgz","fileCount":6,"unpackedSize":23559,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCMYtIWLP5rIE+9WPOknVFDEAgNzaSyE+u/zvV0RLlIgwIhAN+zqP8wvSrwv9zl3olBEqQPZtKqvGKz5JesAqjBODKR"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJi0tk7ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrkvA//ZLnIvkNW55qTfiOMIB6s1hifUtdF4crLY8qSDKE5OrXsSS9T\r\nU1CAfvMOioXHNSicMtbiBwHoaznhlQuIu90NGzq85N/Y3QgvNu46Y06K6iCu\r\nEtQY1Yt3Y/1XbJlFfo47PU+v941sN+yeo1Cg4knOedD79wLbSAjcLGhG91M+\r\nDdtQgLhhpnOSWG1lFwDwBr5s8qvRGYbeFwRIWHtMx94ckrhAE5pKNKNWyv/9\r\nkC5EJ08E8f9uBjH85BE1NlDesfOGJu4NEo4aQqYgX28fvnL5hCYiZ8l7P2xC\r\nFy0ZfOzoFiJqwzFWEtJEi+OqJLZ+wHAggwtPCPSWeJfmwm6GP/fc//l1Udc2\r\nUQUUjbgcMP2S32uxwBYYO3S40f70ucZpLMVwqHERkMVCI4uWknL7YIZHdgaO\r\nuGTVjsjsqyz8cFDWVqTkxjYaFZXk86tlpoSJ1Ln8voJ4zTYOz6wIa7jGMfef\r\nmLMZIP/Ppq6iSuE+whbEFFCXEOepmbwmjAy8DPqIeRBJdfOuZJ0DnrrDGh3l\r\naq9cmnLn9aUcYCepIWe3hFnt0j7ffLRyjsFbuJ054SziTJqXKrTDxvYKiM/1\r\nZyimCsRv9m/kLXfvYPZMs/8H4cVcQ4cYlTSXSjr2NWuq8Nd46lQURdmCMcgO\r\nqn7FF44syx84PuF4wlvz3UA2TIYMuSJzR/4=\r\n=jkH3\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_2.0.0-canary.1_1657985339190_0.6920647874438264"},"_hasShrinkwrap":false},"2.0.0":{"name":"next-api-middleware","version":"2.0.0","description":"Middleware solution for Next.js API routes","author":"Hunter Tunnicliff <htunnicliff@hey.com>","repository":"htunnicliff/next-api-middleware","license":"MIT","type":"module","exports":{"require":"./dist/index.cjs","import":"./dist/index.js"},"types":"./dist/index.d.ts","engines":{"node":"^12.20.0 || ^14.13.1 || >=16.0.0"},"keywords":["next","next.js","middleware","API routes","express"],"devDependencies":{"@swc/core":"^1.2.148","@swc/jest":"^0.2.20","@types/debug":"^4.1.7","@types/jest":"^26.0.24","@types/node":"^16.11.26","jest":"^27.5.1","jest-extended":"^0.11.5","jest-ts-webcompat-resolver":"^1.0.0","next":"^12.1.0","tsup":"^6.1.3","typescript":"^4.6.2"},"peerDependencies":{"next":">=10"},"scripts":{"build":"tsup","type-check":"tsc --pretty --noEmit","test":"jest","version":"pnpm test && pnpm build"},"_id":"next-api-middleware@2.0.0","_integrity":"sha512-pBjFIo+Q+90F7yodATTgYJSZCRc8XteXZ9ZswbNcT6zYGsMSlNniUmoM6hXl7XZN6UiJOyNXVlS1EkXR+99XRw==","_resolved":"/private/var/folders/tl/dg1sjkw12m94j38m7pps6clc0000gn/T/9061085731e7a23e5b594d0b222bcf2e/next-api-middleware-2.0.0.tgz","_from":"file:next-api-middleware-2.0.0.tgz","_nodeVersion":"16.13.1","_npmVersion":"8.1.2","dist":{"integrity":"sha512-pBjFIo+Q+90F7yodATTgYJSZCRc8XteXZ9ZswbNcT6zYGsMSlNniUmoM6hXl7XZN6UiJOyNXVlS1EkXR+99XRw==","shasum":"190180f9cd8222783b75aaa37fe55fda5372dc8c","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-2.0.0.tgz","fileCount":6,"unpackedSize":22417,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCTVWZwOIDsEo3wKOZJljhn87arIL05ljLU15xUEiI4kQIgHXdEEDaowz6XBWc4K5wCR5KaMtAZ3rrsn5bWeAMmIrw="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJi0tvaACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmpqhw/8DoQrl2CGAHzp5pSt3qgSsDPKKDTJbxpq58wqL5plNrUO1cP5\r\nT9j+1o+Zf9CNy+4/yv634OmK4TQejFTATMJjo8Wp8YNPjyikiPJ0ncu7OzTM\r\nPzxc9yG5erFfw8nM8RUFMpeEF2ZN8YwALylA/4pYUjcPmBJMzzvhrm6eW4By\r\n9ObeExA3+t+7+Ku2WQTRubK9gWxUM9KTq2+ZBeXiubjlq/ZWQYVRQiqZZANu\r\n5kQxJ8pO2mtIfe6jiIDkECyAEq6A51sp6fEla6Iaoaa1pNHv8+KOXkk/8TPB\r\nO7uC1l5qetluEfZ2jjSeAagA7YTU4izLAWEQX1HF1HXOV18tpzA+QOnQrmt7\r\n/vC3WHtxYRUOcEelMLmrY7rxp0YSqP7+q5EDmoTzKLMLuXrrjGsdkf6/7gKE\r\nqLDERN7gEqT8ja66KZflaCwVqEZAVhDgv3rHSOKkV7Njqftyj6ZL+N0Uoqgj\r\nlNxUcbHAgqzt3qKBXrK/nbSM1Awmnt38ID1WG0OdzazIECGqjtD3cXt0mcFn\r\n+lpVNrIGQh6WY6GFpOUvwhmh7O+RGqLkSNXgStQOGVWu+M/NEgwc3IPq+Ugk\r\nWejD0kVZ0a+sWCF1ltzTluHnEJIQcdg4tqXOCun37ufNf25UMvqUO9O1H+lR\r\n2OWsQygLnyvxl2LktNGUylh1NrHLAXFHFHw=\r\n=0NYZ\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_2.0.0_1657986009944_0.7133300222256891"},"_hasShrinkwrap":false},"2.0.1":{"name":"next-api-middleware","version":"2.0.1","description":"Middleware solution for Next.js API routes","author":"Hunter Tunnicliff <htunnicliff@hey.com>","repository":"htunnicliff/next-api-middleware","license":"MIT","type":"module","exports":{"require":"./dist/index.cjs","import":"./dist/index.js"},"types":"./dist/index.d.ts","engines":{"node":"^12.20.0 || ^14.13.1 || >=16.0.0"},"keywords":["next","next.js","middleware","API routes","express"],"devDependencies":{"@swc/core":"^1.2.148","@swc/jest":"^0.2.20","@types/jest":"^26.0.24","@types/node":"^16.11.26","jest":"^27.5.1","jest-extended":"^0.11.5","jest-ts-webcompat-resolver":"^1.0.0","next":"^12.1.0","tsup":"^6.1.3","typescript":"^4.6.2"},"peerDependencies":{"next":">=10"},"scripts":{"build":"tsup","type-check":"tsc --pretty --noEmit","test":"jest","version":"pnpm test && pnpm build"},"_id":"next-api-middleware@2.0.1","_integrity":"sha512-yqOQjrOXkqZ7f1FybmTRWE1F4ypfcNWMC5FnmLresfu4ljY0W7iNwvOsaxSvCnoLYF5dnqxq+XeVeac7Gx9s9A==","_resolved":"/private/var/folders/tl/dg1sjkw12m94j38m7pps6clc0000gn/T/d0fe514695985c3668c34dfb4b032b6e/next-api-middleware-2.0.1.tgz","_from":"file:next-api-middleware-2.0.1.tgz","_nodeVersion":"16.13.1","_npmVersion":"8.1.2","dist":{"integrity":"sha512-yqOQjrOXkqZ7f1FybmTRWE1F4ypfcNWMC5FnmLresfu4ljY0W7iNwvOsaxSvCnoLYF5dnqxq+XeVeac7Gx9s9A==","shasum":"e794da5dc7bb41b3226ee50eb70313bdedeb324b","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-2.0.1.tgz","fileCount":6,"unpackedSize":21673,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCHdLUS+Nv+AkgdD/FtpKQVrHX3aygwTPR+QZndA2A9rQIgQeUnXkU/mh+UR+NAtk7JutTaLmX+MdjKkHeR231pDAE="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJi0uJ/ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmomYw/+Kkf/oXHIPrBkQwL6WSTvqXrP0lONYMYzlLxTdbj2TsO1jfIH\r\n1rWee2bIFHP1paVV6CitH/aCXTSMqupuNl4DYTTY0o1JGImaLHJzwOnuRpbE\r\nF8hvdB0j1sX5pogbDM1ODiPzB0tb4cNNUvNw73I+YPs1IB54jlQNaYP1oC8Z\r\nedFoKg/u8eaCYie+YIn7gJJpVpNorQILlWkn66bXnKjWZ6XU3C+4B5D+9IL0\r\n0BXuCzIb5t9NvuKljG4pero3BgXhkfhVOTTV89CKdxFAojtkjhWpYERdwECx\r\n81855oU7PXVu2vKmR+4blAsGw09qLNfTb731Bn8BZLXnvelKjXpGw4Wk8Jol\r\nof6R3MoUY4ZpUHYQIx5m6+bpdHxfhZpsznLwLYlgn3kE6TnIolEKw098LWIv\r\nvOb521qhhc0Ec5dB0DWywxYs3W7nDWRtw0PMdrdVxhFl9KU0VggBgwEMjIO6\r\nNoMZeKblcpFH5+gecdswgLf551SJALxhp+M9VfmbuDmPVTjeEguyok/clWwL\r\nZnFe2TczKjezwJxzfTTuLxANPB5pfdJs5iP0Cevz/8pTIhY7BgYXq8xKp6R9\r\nkPVPl6bXhuVfQloj1L+sw6RfcIVdYv41I4+J7Y8dMUeKrf4EH4hsISijSJR+\r\nQEFxDUdEdGgrMeCYuj2ZGXqv5vOsP6H+2BI=\r\n=NY1l\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_2.0.1_1657987711345_0.2501134682923971"},"_hasShrinkwrap":false},"2.1.0-beta.0":{"name":"next-api-middleware","version":"2.1.0-beta.0","description":"Middleware solution for Next.js API routes","author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"repository":{"type":"git","url":"git+https://github.com/htunnicliff/next-api-middleware.git"},"license":"MIT","type":"module","exports":{"require":"./dist/index.cjs","import":"./dist/index.js"},"types":"./dist/index.d.ts","engines":{"node":"^12.20.0 || ^14.13.1 || >=16.0.0"},"keywords":["next","next.js","middleware","API routes","express"],"devDependencies":{"@swc/core":"^1.3.24","@swc/jest":"^0.2.24","@types/jest":"^26.0.24","@types/node":"^16.18.10","jest":"^27.5.1","jest-extended":"^0.11.5","jest-ts-webcompat-resolver":"^1.0.0","next":"^12.3.4","tsup":"^6.5.0","typescript":"^4.9.4"},"peerDependencies":{"next":">=10"},"scripts":{"build":"tsup","type-check":"tsc --pretty --noEmit","test":"jest","version":"pnpm test && pnpm build"},"readme":"<p align=\"center\">\n  <h1 align=\"center\">Next.js API Middleware</h1>\n</p>\n\n<p align=\"center\">\n  <a aria-label=\"npm\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm\" src=\"https://img.shields.io/npm/v/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg?style=for-the-badge&labelColor=000000\">\n  </a>\n   <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"tests\" src=\"https://img.shields.io/github/workflow/status/htunnicliff/next-api-middleware/Test?style=for-the-badge&labelColor=000000&label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?style=for-the-badge&labelColor=000000&token=XI7G8L08TY\">\n  </a>\n</p>\n\n## Introduction\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares) :thumbsdown:. This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use. :tada:\n\n## Table of Contents\n\n- [Quick Start](#quick-start)\n- [How It Works](#quick-start)\n- [APIs](#apis)\n  - [`label`](#label)\n  - [`use`](#use)\n- [Usage Guide](#usage-guide)\n- [Advanced](#advanced)\n  - [Middleware Factories](#middleware-factories)\n  - [Middleware Signature](#middleware-types)\n\n## Quick Start\n\n```ts\nimport { label, Middleware } from \"next-api-middleware\";\nimport * as Sentry from \"@sentry/nextjs\";\nimport nanoid from \"nanoid\";\n\n// 1 – Create middleware functions\n\nconst captureErrors: Middleware = async (req, res, next) => {\n  try {\n    // Catch any errors that are thrown in remaining\n    // middleware and the API route handler\n    await next();\n  } catch (err) {\n    const eventId = Sentry.captureException(err);\n\n    res.status(500);\n    res.json({ error: err });\n  }\n};\n\nconst addRequestId: Middleware = async (req, res, next) => {\n  // Let remaining middleware and API route execute\n  await next();\n\n  // Apply header\n  res.setHeader(\"X-Response-ID\", nanoid());\n};\n\n// 2 – Use `label` to assemble all middleware\n\nconst withMiddleware = label(\n  {\n    addRequestId,\n    sentry: captureErrors, // <-- Optionally alias middleware\n  },\n  [\"sentry\"] // <-- Provide a list of middleware to call automatically\n);\n\n// 3 – Define your API route handler\n\nconst apiRouteHandler = async (req, res) => {\n  res.status(200);\n  res.send(\"Hello world!\");\n};\n\n// 4 – Choose middleware to invoke for this API route\n\nexport default withMiddleware(\"addRequestId\")(apiRouteHandler);\n```\n\n## How It Works\n\nMy mental model for how this library handles middleware functions is that of a \"winding and unwinding stack.\"\n\nLet's imagine you've used `label` to add two middleware functions to an API route.\n\nWhen a request comes in, this is a rough impression of how that request makes its way through all middleware functions, the API route handler itself, and then back up through the middleware.\n\n```\n              |-----------------|-----------------|--------------------|\n              |  Middleware #1  |  Middleware #2  | API Route Handler  |\n              |-----------------|-----------------|--------------------|\n              |                 |                 |                    |\nRequest ------|----> Setup -----|----> Setup -----|-->------|          |\n              |                 |                 |         |          |\n              |-----------------|-----------------|         V          |\n              |                 |                 |                    |\n              |   await next()  |   await next()  |     API stuff      |\n              |                 |                 |                    |\n              |-----------------|-----------------|         |          |\n              |                 |                 |         |          |\nResponse <----|--- Teardown <---|--- Teardown <---|---------|          |\n              |                 |                 |                    |\n              |-----------------|-----------------|--------------------|\n```\n\nWhile this is a crummy ASCII diagram, I think it gives the right impression. The request winds its way though each middleware function in succession, hits the API route handler, and then proceeds to \"unwind\" its way through the stack.\n\nEvery middleware function has the opportunity to go through three phases:\n\n1. Setup\n2. Waiting\n3. Teardown\n\nThe \"Setup\" phase covers everything that happens before `await next()`. The \"Waiting\" phase is really just `await next()`. The \"Teardown\" phase is the remaining code within a middleware function after `await next()`.\n\nIt is worth noting that although these phases are available to all middleware functions, you don't need to take advantage of them all.\n\nFor example, in error catching middleware you might simply wrap `await next()` in a `try / catch` block. On the other hand, you might have request timing middleware that captures a start time during the setup phase, waits, and then captures a finish time in the teardown phase.\n\nYou can also optionally abort the chain by returning a resolved value from your middleware rather than calling `await next()`. If a returned value is detected, `next-api-middleware` will not call the next middleware, nor their API Route Handler. This is useful if you want your middleware to handle the response for your API route.\n\n## APIs\n\n### `label`\n\nThis is the primary utility for creating reusuable collections of middleware for use throughout many Next.js API routes.\n\n```ts\nconst withMiddleware = label(middleware, defaults);\n```\n\n#### Parameters\n\n- `middleware`: an object containing middleware functions or arrays of middleware\n- `defaults`: (optional) an array of `middleware` keys that will be invoked automatically\n\n#### Return Value\n\n`label` returns a function (conventionally referred to as `withMiddleware`) that uses currying to accept a list of middleware names to be invoked, followed by a Next.js API handler function.\n\nTypically, `withMiddleware` will be imported in API route files and used at the default export statement:\n\n```ts\nimport { withMiddleware } from \"../helpers/my-middleware\";\n\nconst apiRouteHandler = async (req, res) => {\n  ...\n}\n\nexport default withMiddleware(\"foo\", \"bar\", \"baz\")(apiRouteHandler);\n```\n\nThough `label` could contain many middleware functions, the actual middleware invoked by an API route is determined by the names passed in to `withMiddleware`.\n\n#### Examples\n\n##### Basic Use\n\n```ts\nconst logErrors = async (req, res, next) => {\n  try {\n    await next();\n  } catch (error) {\n    console.error(error);\n    res.status(500);\n    res.json({ error });\n  }\n};\n\nconst withMiddleware = label({\n  logErrors,\n});\n\n// export default withMiddleware(\"logErrors\")(apiRouteHandler);\n```\n\n##### Aliases\n\n```ts\nconst withMiddleware = label({\n  error: logErrors,\n});\n\n// export default withMiddleware(\"error\")(apiRouteHandler);\n```\n\n##### Groups\n\n```ts\nimport { foo, bar, baz } from \"./my-middleware\";\n\nconst withMiddleware = label({\n  error: logErrors,\n  myGroup: [foo, bar, baz],\n});\n\n// export default withMiddleware(\"error\", \"myGroup\")(apiRouteHandler);\n```\n\n##### Defaults\n\n```ts\nconst withMiddleware = label(\n  {\n    error: logErrors,\n    myGroup: [foo, bar, baz],\n  },\n  [\"error\"]\n);\n\n// export default withMiddleware(\"myGroup\")(apiRouteHandler);\n```\n\n### `use`\n\nThis utility accepts middleware functions directly and executes them all in order. It is a simpler alternative to `label` that can be useful for handling one-off middleware functions.\n\n```ts\nconst withInlineMiddleware = use(...middleware);\n```\n\n#### Parameters\n\n- `middleware`: a list of middleware functions and/or arrays of middleware functions\n\n#### Return Value\n\n`use` returns a function that accepts a Next.js API route handler.\n\n#### Examples\n\n##### CORS\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nconst apiRouteThatOnlyNeedsCORS = async (req, res) => {\n  ...\n}\n\nexport default use(cors())(apiRouteThatOnlyNeedsCORS);\n```\n\n## Usage Guide\n\nSee [EXAMPLES.md](./EXAMPLES.md) for more detailed examples of `label` and `use`.\n\n## Advanced\n\n### Middleware Factories\n\nSince `use` and `label` accept values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { Middleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): Middleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const postRequestsOnlyMiddleware = httpMethod(\"POST\");\n```\n\n### Middleware Signature\n\n`Middleware` is inspired by the asyncronous middleware style popularized by Koa.js.\n\n```ts\ntype Middleware<Request = NextApiRequest, Response = NextApiResponse> = (\n  req: Request,\n  res: Response,\n  next: () => Promise<void>\n) => Promise<void>;\n```\n","readmeFilename":"README.md","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_id":"next-api-middleware@2.1.0-beta.0","_integrity":"sha512-MV9XpwYk6lTyPb0/d3xd9IeYx8m29OhTSh8BbDt9g0wCCPRQhrejCMS7iH0aQltBvS1UMQzD3CGufiSIHnXu8g==","_resolved":"/private/var/folders/bg/z76vdpx154lgp0blvqrrjkfh0000gp/T/64a39c42c63ed55164a8c87c7dade786/next-api-middleware-2.1.0-beta.0.tgz","_from":"file:next-api-middleware-2.1.0-beta.0.tgz","_nodeVersion":"18.12.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-MV9XpwYk6lTyPb0/d3xd9IeYx8m29OhTSh8BbDt9g0wCCPRQhrejCMS7iH0aQltBvS1UMQzD3CGufiSIHnXu8g==","shasum":"282d679f51107ca3b0ed191e669efe91b5413f08","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-2.1.0-beta.0.tgz","fileCount":6,"unpackedSize":22718,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFrPYphgqzXSoDjhClHdFqIdpGs+TReeK5ctAREJ9RjOAiAgqEU7eYZA++PNVLa6lmRX0fvRr4INfzilZ5UN6fhTtg=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjo1FdACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmp7QQ/8CCJEozhrNZoIOC6amBXDmAqOCwbBEXLg3kDHuhpkjsvrTNGg\r\npRUGHAsyWZ1jABxirzfXv/Wcz+r5scuoESzrxGTD5hpfflCVEPgGo1aVlR+T\r\n4RfIed3lqvavDXYsLBdp6xqWmysFz1x0w52H5SPB1tLuaI9Gn+t/HZYl6laU\r\nm4HStGJEks71t6aYgynBrYceO3sVn5bZ27auEIlzfsP3GxOThF2Xn/B0jDgS\r\nx4WhzKg47sE9UXH3SptLqXGs/Zmqqcmq+BbBboGkFuV3PLooy0Hy4Lj2R1Xw\r\nv+1/hfT2+pweSGwrkiUwS/pM/Ot0LvVB2cJ00BCuR7QXHrDrSDDqc6gYW294\r\np2+IPJCjtDiTMHaivOTSDc/ExDdvQdnlVgOks0Zt5PfaCckJt68bAvA0Ydju\r\npLFZGt0xlRw8h2IZV2cYrTzRLlnWncAU6rxVupnpncWIm8WDwxTiqWrdqCqS\r\nw2GEYhO0jT+8HOgAxDkePsS22IqU98zroVt7ya87hRVcbx7PmBJdap9noOpI\r\n846rpMYGzfYqINlWP64jIAyWr57x+F62NzxwxxIuWJPeaoWAwG4GY8oQn4hJ\r\nTLBFyGSxVd/xqPdlDR6FNsX6GxPTmzER4Kg3mQxVvdlHleuorcDR2cxLopEx\r\nJFgVjovZWKuYJ4oOtPsXRxyoDsYv7eE9UoE=\r\n=0iKf\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_2.1.0-beta.0_1671647581696_0.9598299043185468"},"_hasShrinkwrap":false},"3.0.0":{"name":"next-api-middleware","version":"3.0.0","description":"Middleware solution for Next.js API routes","author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"repository":{"type":"git","url":"git+https://github.com/htunnicliff/next-api-middleware.git"},"license":"MIT","type":"module","exports":{"require":"./dist/index.cjs","import":"./dist/index.js"},"types":"./dist/index.d.ts","engines":{"node":">=16.0.0"},"keywords":["next","next.js","middleware","API routes"],"scripts":{"build":"tsup","type-check":"tsc --pretty --noEmit","test":"jest","version":"pnpm test && pnpm build"},"devDependencies":{"@swc/core":"^1.3.24","@swc/jest":"^0.2.24","@types/jest":"^26.0.24","@types/node":"^16.18.10","jest":"^27.5.1","jest-extended":"^0.11.5","jest-ts-webcompat-resolver":"^1.0.0","next":"^12.3.4","tsup":"^6.5.0","typescript":"^4.9.4"},"peerDependencies":{"next":">=10"},"_id":"next-api-middleware@3.0.0","gitHead":"0ae518641e6cffd4c2a17c936347c797670005f1","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"},"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","_nodeVersion":"20.9.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-SGr//xLWvNrbD2eCsTcJMCvtbtppUmZp7sH5a+kq7PW649K+ZFHY+slfkbgmBl4vCEqwQFo/riqpYubemmUXVA==","shasum":"0fc813ada350e173dadc8b0e1214fd312a2132b6","tarball":"https://registry.npmjs.org/next-api-middleware/-/next-api-middleware-3.0.0.tgz","fileCount":6,"unpackedSize":22168,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAvRZBHuYxsoGBm6Ux5/JLd+eCObJ2BSnTj4qDUhV51dAiB+CtnESzEhF6yPLm8I7SNuo7DIGM68nvlPnVqkErGDzw=="}]},"_npmUser":{"name":"htunnicliff","email":"me@huntertunnicliff.com"},"directories":{},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-api-middleware_3.0.0_1706222916973_0.668942247873336"},"_hasShrinkwrap":false}},"time":{"created":"2020-11-29T23:12:39.247Z","0.1.0":"2020-11-29T23:12:39.347Z","modified":"2024-01-25T22:48:37.310Z","0.2.0":"2020-11-30T19:50:33.486Z","0.2.1":"2020-11-30T19:53:43.066Z","0.2.2":"2020-11-30T19:58:33.727Z","0.2.3":"2020-11-30T20:04:52.117Z","0.2.4":"2020-11-30T20:07:52.571Z","0.3.0":"2020-11-30T20:25:55.847Z","0.3.1":"2020-11-30T21:26:24.452Z","0.3.2":"2020-11-30T21:31:56.278Z","0.3.3":"2020-11-30T23:29:21.458Z","0.3.4":"2020-12-01T00:06:18.518Z","0.4.0":"2020-12-01T18:30:35.618Z","0.5.0":"2020-12-01T19:41:07.849Z","0.5.1":"2020-12-02T18:00:45.589Z","0.5.2":"2020-12-02T20:49:06.633Z","0.6.0":"2020-12-02T22:06:30.004Z","0.7.0-canary.0":"2021-02-12T19:37:18.096Z","0.7.0-canary.1":"2021-02-19T18:27:47.797Z","0.6.1":"2021-02-19T18:30:40.308Z","0.7.0":"2021-02-24T23:04:41.866Z","0.7.1":"2021-04-12T23:01:57.697Z","1.0.0-canary.0":"2021-07-07T20:04:33.551Z","1.0.0-canary.1":"2021-07-17T04:17:51.778Z","1.0.0-canary.2":"2021-07-17T17:49:03.746Z","1.0.0-beta.0":"2021-07-23T17:20:51.717Z","1.0.0-beta.2":"2021-07-23T17:34:14.222Z","1.0.0":"2021-07-23T19:32:24.273Z","1.0.1":"2021-07-23T19:38:04.779Z","2.0.0-canary.0":"2022-02-06T21:26:00.663Z","2.0.0-canary.1":"2022-07-16T15:28:59.318Z","2.0.0":"2022-07-16T15:40:10.127Z","2.0.1":"2022-07-16T16:08:31.495Z","2.1.0-beta.0":"2022-12-21T18:33:01.861Z","3.0.0":"2024-01-25T22:48:37.119Z"},"maintainers":[{"name":"htunnicliff","email":"me@huntertunnicliff.com"}],"description":"Middleware solution for Next.js API routes","repository":{"type":"git","url":"git+https://github.com/htunnicliff/next-api-middleware.git"},"author":{"name":"Hunter Tunnicliff","email":"htunnicliff@hey.com"},"license":"MIT","readme":"<p align=\"center\">\n  <h1 align=\"center\">Next.js API Middleware</h1>\n</p>\n\n<p align=\"center\">\n  <a aria-label=\"npm\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm\" src=\"https://img.shields.io/npm/v/next-api-middleware.svg\">\n  </a>\n  <a aria-label=\"npm downloads\" href=\"https://www.npmjs.com/package/next-api-middleware\">\n    <img alt=\"npm\" src=\"https://img.shields.io/npm/dm/next-api-middleware\">\n  </a>\n  <a aria-label=\"license\" href=\"https://github.com/htunnicliff/next-api-middleware/blob/master/LICENSE\">\n    <img alt=\"license\" src=\"https://img.shields.io/github/license/htunnicliff/next-api-middleware.svg\">\n  </a>\n   <a aria-label=\"tests\" href=\"https://github.com/htunnicliff/next-api-middleware/actions?query=workflow%3ATest\">\n    <img alt=\"GitHub Workflow Status\" src=\"https://img.shields.io/github/actions/workflow/status/htunnicliff/next-api-middleware/main.yaml?label=Tests\">\n  </a>\n  <a aria-label=\"coverage\" href=\"https://codecov.io/gh/htunnicliff/next-api-middleware/\">\n    <img alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/htunnicliff/next-api-middleware?&&token=XI7G8L08TY\">\n  </a>\n</p>\n\n## Introduction\n\n> ⚠️ **This library was written to support API routes that use the Next.js [Pages Router](https://nextjs.org/docs/pages). It has not been tested with the [App Router](https://nextjs.org/docs/app).**\n\n[Next.js API routes](https://nextjs.org/docs/api-routes/introduction) are a ridiculously fun and simple way to add backend functionality to a React app. However, when it comes time to add middleware, there is no easy way to implement it.\n\nThe official Next.js docs recommend writing functions [inside your API route handler](https://nextjs.org/docs/api-routes/api-middlewares). This is a huge step backward compared to the clean APIs provided by Express.js or Koa.js.\n\nThis library attempts to provide minimal, clean, composable middleware patterns that are both productive and pleasant to use.\n\n## Table of Contents\n\n- [Quick Start](#quick-start)\n- [How It Works](#quick-start)\n- [APIs](#apis)\n  - [`label`](#label)\n  - [`use`](#use)\n- [Usage Guide](#usage-guide)\n- [Advanced](#advanced)\n  - [Middleware Factories](#middleware-factories)\n  - [Middleware Signature](#middleware-types)\n- [Alternatives](#alternatives)\n\n## Quick Start\n\n```ts\nimport { label, Middleware } from \"next-api-middleware\";\nimport * as Sentry from \"@sentry/nextjs\";\nimport nanoid from \"nanoid\";\n\n// 1 – Create middleware functions\n\nconst captureErrors: Middleware = async (req, res, next) => {\n  try {\n    // Catch any errors that are thrown in remaining\n    // middleware and the API route handler\n    await next();\n  } catch (err) {\n    const eventId = Sentry.captureException(err);\n\n    res.status(500);\n    res.json({ error: err });\n  }\n};\n\nconst addRequestId: Middleware = async (req, res, next) => {\n  // Let remaining middleware and API route execute\n  await next();\n\n  // Apply header\n  res.setHeader(\"X-Response-ID\", nanoid());\n};\n\n// 2 – Use `label` to assemble all middleware\n\nconst withMiddleware = label(\n  {\n    addRequestId,\n    sentry: captureErrors, // <-- Optionally alias middleware\n  },\n  [\"sentry\"] // <-- Provide a list of middleware to call automatically\n);\n\n// 3 – Define your API route handler\n\nconst apiRouteHandler = async (req, res) => {\n  res.status(200);\n  res.send(\"Hello world!\");\n};\n\n// 4 – Choose middleware to invoke for this API route\n\nexport default withMiddleware(\"addRequestId\")(apiRouteHandler);\n```\n\n## How It Works\n\nMy mental model for how this library handles middleware functions is that of a \"winding and unwinding stack.\"\n\nLet's imagine you've used `label` to add two middleware functions to an API route.\n\nWhen a request comes in, this is a rough impression of how that request makes its way through all middleware functions, the API route handler itself, and then back up through the middleware.\n\n```\n              |-----------------|-----------------|--------------------|\n              |  Middleware #1  |  Middleware #2  | API Route Handler  |\n              |-----------------|-----------------|--------------------|\n              |                 |                 |                    |\nRequest ------|----> Setup -----|----> Setup -----|-->------|          |\n              |                 |                 |         |          |\n              |-----------------|-----------------|         V          |\n              |                 |                 |                    |\n              |   await next()  |   await next()  |     API stuff      |\n              |                 |                 |                    |\n              |-----------------|-----------------|         |          |\n              |                 |                 |         |          |\nResponse <----|--- Teardown <---|--- Teardown <---|---------|          |\n              |                 |                 |                    |\n              |-----------------|-----------------|--------------------|\n```\n\nWhile this is a crummy ASCII diagram, I think it gives the right impression. The request winds its way though each middleware function in succession, hits the API route handler, and then proceeds to \"unwind\" its way through the stack.\n\nEvery middleware function has the opportunity to go through three phases:\n\n1. Setup\n2. Waiting\n3. Teardown\n\nThe \"Setup\" phase covers everything that happens before `await next()`. The \"Waiting\" phase is really just `await next()`. The \"Teardown\" phase is the remaining code within a middleware function after `await next()`.\n\nIt is worth noting that although these phases are available to all middleware functions, you don't need to take advantage of them all.\n\nFor example, in error catching middleware you might simply wrap `await next()` in a `try / catch` block. On the other hand, you might have request timing middleware that captures a start time during the setup phase, waits, and then captures a finish time in the teardown phase.\n\n## APIs\n\n### `label`\n\nThis is the primary utility for creating reusuable collections of middleware for use throughout many Next.js API routes.\n\n```ts\nconst withMiddleware = label(middleware, defaults);\n```\n\n#### Parameters\n\n- `middleware`: an object containing middleware functions or arrays of middleware\n- `defaults`: (optional) an array of `middleware` keys that will be invoked automatically\n\n#### Return Value\n\n`label` returns a function (conventionally referred to as `withMiddleware`) that uses currying to accept a list of middleware names to be invoked, followed by a Next.js API handler function.\n\nTypically, `withMiddleware` will be imported in API route files and used at the default export statement:\n\n```ts\nimport { withMiddleware } from \"../helpers/my-middleware\";\n\nconst apiRouteHandler = async (req, res) => {\n  ...\n}\n\nexport default withMiddleware(\"foo\", \"bar\", \"baz\")(apiRouteHandler);\n```\n\nThough `label` could contain many middleware functions, the actual middleware invoked by an API route is determined by the names passed in to `withMiddleware`.\n\n#### Examples\n\n##### Basic Use\n\n```ts\nconst logErrors = async (req, res, next) => {\n  try {\n    await next();\n  } catch (error) {\n    console.error(error);\n    res.status(500);\n    res.json({ error });\n  }\n};\n\nconst withMiddleware = label({\n  logErrors,\n});\n\n// export default withMiddleware(\"logErrors\")(apiRouteHandler);\n```\n\n##### Aliases\n\n```ts\nconst withMiddleware = label({\n  error: logErrors,\n});\n\n// export default withMiddleware(\"error\")(apiRouteHandler);\n```\n\n##### Groups\n\n```ts\nimport { foo, bar, baz } from \"./my-middleware\";\n\nconst withMiddleware = label({\n  error: logErrors,\n  myGroup: [foo, bar, baz],\n});\n\n// export default withMiddleware(\"error\", \"myGroup\")(apiRouteHandler);\n```\n\n##### Defaults\n\n```ts\nconst withMiddleware = label(\n  {\n    error: logErrors,\n    myGroup: [foo, bar, baz],\n  },\n  [\"error\"]\n);\n\n// export default withMiddleware(\"myGroup\")(apiRouteHandler);\n```\n\n### `use`\n\nThis utility accepts middleware functions directly and executes them all in order. It is a simpler alternative to `label` that can be useful for handling one-off middleware functions.\n\n```ts\nconst withInlineMiddleware = use(...middleware);\n```\n\n#### Parameters\n\n- `middleware`: a list of middleware functions and/or arrays of middleware functions\n\n#### Return Value\n\n`use` returns a function that accepts a Next.js API route handler.\n\n#### Examples\n\n##### CORS\n\n```ts\nimport { use } from \"next-api-middleware\";\nimport cors from \"cors\";\n\nconst apiRouteThatOnlyNeedsCORS = async (req, res) => {\n  ...\n}\n\nexport default use(cors())(apiRouteThatOnlyNeedsCORS);\n```\n\n## Usage Guide\n\nSee [EXAMPLES.md](./EXAMPLES.md) for more detailed examples of `label` and `use`.\n\n## Advanced\n\n### Middleware Factories\n\nSince `use` and `label` accept values that evaluate to middleware functions, this provides the opportunity to create custom middleware factories.\n\nHere's an example of a factory that generates a middleware function to only allow requests with a given HTTP method:\n\n```ts\nimport { Middleware } from \"next-api-middleware\";\n\nconst httpMethod = (\n  allowedHttpMethod: \"GET\" | \"POST\" | \"PATCH\"\n): Middleware => {\n  return async function (req, res, next) {\n    if (req.method === allowedHttpMethod || req.method == \"OPTIONS\") {\n      await next();\n    } else {\n      res.status(404);\n      res.end();\n    }\n  };\n};\n\nexport const postRequestsOnlyMiddleware = httpMethod(\"POST\");\n```\n\n### Middleware Signature\n\n`Middleware` is inspired by the asyncronous middleware style popularized by Koa.js.\n\n```ts\ntype Middleware<Request = NextApiRequest, Response = NextApiResponse> = (\n  req: Request,\n  res: Response,\n  next: () => Promise<void>\n) => Promise<void>;\n```\n\n## Alternatives\n\n- [next-connect](https://github.com/hoangvvo/next-connect)\n","readmeFilename":"README.md","keywords":["next","next.js","middleware","API routes"],"homepage":"https://github.com/htunnicliff/next-api-middleware#readme","bugs":{"url":"https://github.com/htunnicliff/next-api-middleware/issues"}}