{"id":17336330,"url":"https://github.com/juanjodiaz/serverless-middleware","last_synced_at":"2025-06-30T11:35:07.607Z","repository":{"id":34245850,"uuid":"173507030","full_name":"juanjoDiaz/serverless-middleware","owner":"juanjoDiaz","description":"Serverless plugin to allow middleware handlers configured directly in serverless.yaml","archived":false,"fork":false,"pushed_at":"2025-01-26T18:30:44.000Z","size":627,"stargazers_count":18,"open_issues_count":4,"forks_count":11,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-09T18:18:11.626Z","etag":null,"topics":["aws","aws-lambda","aws-plugin","lambda","serverless"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/juanjoDiaz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-02T22:44:21.000Z","updated_at":"2025-01-26T18:30:19.000Z","dependencies_parsed_at":"2024-01-17T21:24:55.439Z","dependency_job_id":"7db736ec-b187-41cf-9d62-1fd82a6a2cab","html_url":"https://github.com/juanjoDiaz/serverless-middleware","commit_stats":{"total_commits":84,"total_committers":2,"mean_commits":42.0,"dds":"0.011904761904761862","last_synced_commit":"39d10fb0b85a6326fc6e83d34419dbb06ea65c75"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juanjoDiaz%2Fserverless-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juanjoDiaz%2Fserverless-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juanjoDiaz%2Fserverless-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juanjoDiaz%2Fserverless-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juanjoDiaz","download_url":"https://codeload.github.com/juanjoDiaz/serverless-middleware/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085326,"owners_count":21045139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-lambda","aws-plugin","lambda","serverless"],"created_at":"2024-10-15T15:29:32.835Z","updated_at":"2025-04-09T18:18:17.003Z","avatar_url":"https://github.com/juanjoDiaz.png","language":"JavaScript","readme":"Serverless Middleware\n=====================\n[![Serverless][serverless-badge]](serverless-badge-url)\n[![npm version][npm-version-badge]][npm-version-badge-url]\n[![npm monthly downloads][npm-downloads-badge]][npm-version-badge-url]\n[![Node.js CI](https://github.com/juanjoDiaz/serverless-middleware/actions/workflows/on-push.yaml/badge.svg)](https://github.com/juanjoDiaz/serverless-middleware/actions/workflows/on-push.yaml)\n[![Coverage Status][coveralls-badge]][coveralls-badge-url]\n[![license](https://img.shields.io/npm/l/serverless-middleware.svg)](https://raw.githubusercontent.com/juanjoDiaz/serverless-middleware/master/LICENSE)\n\nServerless plugin to allow middleware handlers configured directly in serverless.yaml\n\n## Requirements:\n* Serverless v3\n* AWS provider\n* Node.js 14+\n\n### Supported runtimes\n\n- [x] nodejs12.x (both Javascript and Typescript)\n- [x] nodejs14.x (both Javascript and Typescript)\n- [x] nodejs16.x (both Javascript and Typescript)\n- [x] nodejs18.x (both Javascript and Typescript)\n- [x] nodejs20.x (both Javascript and Typescript)\n- [x] nodejs22.x (both Javascript and Typescript)\n- [ ] dotnetcore2.1\n- [ ] java8\n- [ ] java11\n- [ ] go1.x\n- [ ] python2.7\n- [ ] python3.7\n- [ ] ruby2.5\n- [ ] provided \n\n## Installation\n\nInstall via npm in the root of your Serverless service:\n\n```sh\nnpm install serverless-middleware --save-dev\n```\n\nAdd the plugin to the `plugins` array in your Serverless `serverless.yaml`:\n\n```yaml\nplugins:\n  - serverless-middleware\n```\n\n## How it works\n\nMiddleware allows you to set up multiple handlers to be executed sequentially including error handlers that will capture any exception in the chain.\n\nMiddlewares are just standard AWS lambda handlers that return a promise (or are async).\nHandlers using `callback` will NOT work.\n```js\nconst myMiddleware = async (event, context) =\u003e { ... };\n```\n\nOnce `serverless-middleware` is installed you can set the `function.middleware` property to an array and skip the `function.handler` property.\nEach middleware handler can be a string (like a standard handler would be) or an object containing the properties `then` and/or `catch`.\n\nFor example:\n\n```yaml\nprovider:\n  name: aws\n  runtime: nodejs22.x\n  \nfunctions:\n  myFunction:\n    middleware:\n      - auth.authenticate\n      - auth.authorize\n      - then: myFunction.handler # `then:` is unnecessary here.\n      - catch: utils.handlerError\n      - # or both can be combined\n        then: logger.log\n        catch: utils.handlerLoggerError\n```\n\nwill result in an execution like:\n\n```js\nPromise.resolve()\n  .then(require('./auth').authenticate)\n  .then(require('./auth').authorize)\n  .then(require('./myFunction').handler)\n  .catch(require('./utils').handlerError)\n  .then(require('./logger').log)\n  .catch(require('./utils').handlerLoggerError);\n```\n\nAs with standard promises, catch handlers are only executed when there are exceptions.\nThe resulting lambda will return the result returned by the last middleware handler executed.\n\nThe `event` and `context` objects are passed from handler to handler so you can attach new properties to be accessed by subsequent handlers.\n`context` always contains the result of the previous handler in the `prev` property.\nThe user can also stop at any point in the chain by calling the `end` method in the `context` argument. After `context.end()` is called, no more handlers will be executed.\n\nFor example:\n\n```js\nconst myMiddleware = async (event, context) =\u003e {\n  if (context.prev === undefined) {\n    // Previous middleware handler didn't return. End execution.\n    context.end();\n    return {\n      statusCode: 200,\n      body: 'No results',\n    };\n  }\n\n  ...\n};\n```\n\nYou can also add pre/pos- middleware handlers and maintain the `function.handler`. These middleware are just prepended/appended to the main handler.\n\nFor example:\n\n```yaml\n\nprovider:\n  name: aws\n  runtime: nodejs22.x\n  \nfunctions:\n  myFunction:\n    events:\n      - http:\n          path: my-function\n          method: get\n    handler: myFunction.handler\n    middleware:\n      pre:\n        - auth.authenticate\n        - auth.authorize\n      pos:\n        - catch: utils.handlerError\n```\n\nYou can also add pre/pos- middleware handlers at the package level using the `custom.middleware` section of `serverless.yaml`. These middleware are just prepended/appended to all the function middleware handlers chain.\n\nFor example:\n\n```yaml\n\nprovider:\n  name: aws\n  runtime: nodejs22.x\n\ncustom:\n  middleware:\n    pre:\n      - auth.authenticate\n    pos:\n      - catch: utils.handlerError\n\nfunctions:\n  myAnonymousFunction:\n    events:\n      - http:\n          path: my-anonymous-function\n          method: get\n    handler: myAnonymousFunction.handler\n  myFunction:\n    events:\n      - http:\n          path: my-function\n          method: get\n    handler: myFunction.handler\n    middleware:\n      pre:\n        - auth.authorize\n```\n\nwill result in a similar promise chain as above.\n\n## Packaging\n\nIn most cases, you shouldn't need to change the default packaging configuration.\nFor edge cases, Middleware can be configured to use a specific intermediary folder and to not clear it after creating the serverless package.\n\nThese settings are also set in the `custom.middleware` section of `serverless.yaml`\n\n```yaml\ncustom:\n  middleware:\n    folderName: my_custom_folder  # defaults to '.middleware'\n    cleanFolder: false            # defaults to 'true'\n```\n\nThis might be useful if you are using `sls package` and building your own artifacts.\n\n## Migrations\n\n### v1.0.0 to 2.0.0\n\n#### Use function.middleware instead fo function.custom.middleware\n\n### v0.0.14 to v0.0.15\n\n#### Use function.custom.middleware instead fo function.handler\nPassing an array to the handler property is not allowed anymore since Serverless is getting stricter with it's types and it also causes issues with Typescript.\n\nSo\n```js\nfunctions:\n  myFunction:\n    handler:\n      - auth.authenticate\n      - auth.authorize\n      - then: myFunction.handler # `then:` is unnecessary here.\n      - catch: utils.handlerError\n      - # or both can be combined\n        then: logger.log\n        catch: utils.handlerLoggerError\n```\nbecomes\n```js\nfunctions:\n  myFunction:\n    custom:\n      middleware:\n        - auth.authenticate\n        - auth.authorize\n        - then: myFunction.handler # `then:` is unnecessary here.\n        - catch: utils.handlerError\n        - # or both can be combined\n          then: logger.log\n          catch: utils.handlerLoggerError\n```\n\n## Contribute\n\nHelp us to make this plugin better.\n\n* Clone the code\n* Install the dependencies with `npm install`\n* Create a feature branch `git checkout -b new_feature`\n* Add your code and add tests if you implement a new feature\n* Validate your changes `npm run lint` and `npm test` (or `npm run test-with-coverage`)\n\n## License\n\nThis software is released under the MIT license. See [the license file](LICENSE) for more details.\n\n[serverless-badge]: http://public.serverless.com/badges/v3.svg\n[serverless-badge-url]: http://www.serverless.com\n[npm-version-badge]: https://badge.fury.io/js/serverless-middleware.svg\n[npm-version-badge-url]: https://www.npmjs.com/package/serverless-middleware\n[npm-downloads-badge]: https://img.shields.io/npm/dm/serverless-middleware.svg\n[coveralls-badge]: https://coveralls.io/repos/juanjoDiaz/serverless-middleware/badge.svg?branch=master\n[coveralls-badge-url]: https://coveralls.io/r/juanjoDiaz/serverless-middleware?branch=master","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuanjodiaz%2Fserverless-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuanjodiaz%2Fserverless-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuanjodiaz%2Fserverless-middleware/lists"}