{"id":20330071,"url":"https://github.com/redapesolutions/koalambda","last_synced_at":"2025-08-20T16:23:01.870Z","repository":{"id":75292933,"uuid":"119002799","full_name":"redapesolutions/koalambda","owner":"redapesolutions","description":"A Koa-like middleware concept to quickly build Lambda functions","archived":false,"fork":false,"pushed_at":"2018-09-19T07:16:15.000Z","size":578,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-14T18:11:16.430Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/redapesolutions.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":"2018-01-26T03:49:22.000Z","updated_at":"2018-09-19T07:16:10.000Z","dependencies_parsed_at":"2023-06-18T04:31:21.907Z","dependency_job_id":null,"html_url":"https://github.com/redapesolutions/koalambda","commit_stats":{"total_commits":65,"total_committers":3,"mean_commits":"21.666666666666668","dds":0.4307692307692308,"last_synced_commit":"7555c10181488701db40b4ee625ec9bf246d1caf"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redapesolutions%2Fkoalambda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redapesolutions%2Fkoalambda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redapesolutions%2Fkoalambda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redapesolutions%2Fkoalambda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redapesolutions","download_url":"https://codeload.github.com/redapesolutions/koalambda/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241844856,"owners_count":20029737,"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":[],"created_at":"2024-11-14T20:14:51.268Z","updated_at":"2025-03-04T12:20:44.349Z","avatar_url":"https://github.com/redapesolutions.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Koala-mbda or Koa-Lambda [![Build Status](https://travis-ci.org/redapesolutions/koalambda.svg?branch=master)](https://travis-ci.org/redapesolutions/koalambda) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![Coverage Status](https://coveralls.io/repos/github/redapesolutions/koalambda/badge.svg?branch=master)](https://coveralls.io/github/redapesolutions/koalambda?branch=master)\n\n\nKoa-style middleware concept to quickly build AWS Lambda functions\n\nMiddlewares\n===========\n\nUse Koa style middlewares and the `kompose` method. Examples can be found in handlers e.g. `user/addToHistory.ts`  \nA handler is therefore simply equal to the outcome of `kompose` e.g.\n\n```js\nexport const handler = kompose(\n    callbackBased,\n    ...,\n    async (ctx, next) =\u003e {\n        ... // \u003c- this is called while going down the middleware chain\n        await next()\n        ... // \u003c- this is called once the bottom of the chain has been reached and we are going back up the chain\n    },\n    ...\n)\n```\n\n## Rules\n\nA middleware follows the following rules:\n\n- arguments are `ctx:EventContext` and `next` (optional, see Utility middlewares below)\n- `ctx` contains three properties: `event:AWSEvent`, `context:AWSContext` and `state`. The event type varies depending on the trigger of the lambda function.\n- All middlewares should be `async` functions; they should always call `await next()` (or `next \u0026\u0026 await next()` in the case of utility)\n- Code reached _before_ the `await next()` call is processed _down_ the middleware chain\n- Code reached _after_ the `await next()` call is processed _up_ the middleware chain\n- In case of errors, the middleware should `throw` an error, not return a rejected promise\n\n## Convention\n\n- All properties to be added or modified along the middleware chain should be added/modified on the `ctx.state` object\n- For HTTP based calls, the top chain will expect a `ctx.state.response` property to be populated\n- Errors thrown follow the pattern: `{message: '...', code: 42}`\n\n## Utility middlewares\n\nIn case of utility middlewares, that are expected to be used both as normal functions and as part of a middleware chain:\n\n- the `next` parameter should be made optional (`next?`)\n- the call to `next` should be made optional with the statement `next \u0026\u0026 await next()`\n\nE.g. `withUserId` can be used as a part of a chain, when user isn't needed (only id) but it is also used within `withUser` as normal function\n\n## Base middlewares\n\n↓ means acts _down_ the chain; ↑ _up_ the chain\n\n### callbackBased \u0026 contextBased ↑\n\nDecide how the outcome of the Lambda function is triggered. Http calls use callback, whereas Cognito triggers use context for example. As of this writing, one of the two **must** be used as the top middleware for any *handler* function\n\n### httpResponse \u0026 standardHttpResponse ↑\n\nHandles Http responses, both success and error. `standardHttpResponse` simply uses a default code 200 for success, as well as default CORS enabled and JSON content.\n\n### withUserId ↓\n\nReads the user Id from the request and adds it as `ctx.state.userId`\n\n### withUser ↓\n\nReads the user Id and loads the corresponding user object. Adds the user as `ctx.state.user`  \n*Note:* Does not require `withUserId` to be added to the chain as it is already part of the code of withUser.\n\n### jsonBody ↓\n\nJSON-Parses the body of a POST. Sets the parsed object back onto `ctx.state.body` (overwrites original)\n\n### putInState ↓\n\nTakes value with specified name and puts it in the state\n\n### filter ↓\n\nfilter the specified argument by specified function\n## Logical switches\n\n### when\n\nTakes a \"check\" callback which receives the context, and a middleware. If check passes (either as a boolean or a Promise which resolves with a boolean), middleware is called.\n\n*Note:* Check promise rejection is not currently considered a fail of the check\n\n### whenAttributeExists\n\nSimilar to `when` but checks whether a given attribute exists on the state. Uses `lodash.get` for shortcut (dot notation)\n\n### whenAttributeEquals\n\nSimilar to `whenAttributeExists` but checks whether the given attribute equals the given value.\n\n## Changelog\n\n### v0.0.2:\n\n- move all properties to state\n- move `koa-compose` initialisation higher to avoid checking functions twice (thank you @yujilim)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredapesolutions%2Fkoalambda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredapesolutions%2Fkoalambda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredapesolutions%2Fkoalambda/lists"}