{"id":23174213,"url":"https://github.com/coursedesign/koa-middleware-adapter","last_synced_at":"2026-05-08T13:34:57.170Z","repository":{"id":37070903,"uuid":"234123612","full_name":"CourseDesign/koa-middleware-adapter","owner":"CourseDesign","description":"Functions and promises can be used as middleware in koa","archived":false,"fork":false,"pushed_at":"2023-01-16T21:03:37.000Z","size":645,"stargazers_count":1,"open_issues_count":13,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-20T23:42:39.980Z","etag":null,"topics":["adapter","koa","middleware","npm","npm-package"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/koa-middleware-adapter","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/CourseDesign.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}},"created_at":"2020-01-15T16:29:14.000Z","updated_at":"2021-11-03T00:54:07.000Z","dependencies_parsed_at":"2023-02-10T06:15:19.881Z","dependency_job_id":null,"html_url":"https://github.com/CourseDesign/koa-middleware-adapter","commit_stats":null,"previous_names":["kdpark0723/koa-middleware-adapter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CourseDesign%2Fkoa-middleware-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CourseDesign%2Fkoa-middleware-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CourseDesign%2Fkoa-middleware-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CourseDesign%2Fkoa-middleware-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CourseDesign","download_url":"https://codeload.github.com/CourseDesign/koa-middleware-adapter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247268493,"owners_count":20911146,"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":["adapter","koa","middleware","npm","npm-package"],"created_at":"2024-12-18T05:19:37.011Z","updated_at":"2026-05-08T13:34:57.118Z","avatar_url":"https://github.com/CourseDesign.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Koa Middleware Adapter\n\n![](https://img.shields.io/npm/dm/koa-middleware-adapter.png?style=flat-square)\n\n**Functions and promises can be used as middleware in koa.**\n\n​    \n\n```js\nconst Koa = require('koa');\nconst adapter = require('koa-middleware-adapter');\n\nconst app = new Koa();\n\napp.use(adapter.create(() =\u003e 'Hello, World!'));\n\nconst port = 4000;\napp.listen(port, () =\u003e {\n  console.log(`server is listening to port ${port}`);\n});\n```\n\n​    \n\n## Document\n\n- [example](https://github.com/kdPark0723/koa-middleware-adapter/tree/master/example)\n- [source](https://github.com/kdPark0723/koa-middleware-adapter)\n\n​    \n\n\n## Install\n\n```shell\n$ npm i koa-middleware-adapter\n```\n\n​    \n\n## Usage\n\n```js\nconst Koa = require('koa');\nconst Router = require('koa-router');\nconst bodyParser = require('koa-bodyparser');\nconst adapter = require('koa-middleware-adapter');\n\nconst UserDao = require('./userDao');\n\nconst userDao = new UserDao();\n\nasync function findUserInfo(userId, userDao) {\n  const user = await userDao.findById(Number(userId));\n  if (!user) throw new adapter.NotFound('User Not Found');\n\n  return user;\n}\n\nasync function createUserInfo(user, userDao) {\n  await userDao.insert(user);\n  return user;\n}\n\nconst findUserMiddleware = adapter.create(findUserInfo, {\n  status: 200,\n  parameters: [\n    new adapter.parameter.Parameter(adapter.parameter.where.params, { name: 'id', index: 0 }),\n    new adapter.parameter.Parameter(userDao, { index: 1 }),\n  ],\n});\n\nconst findUserInContextMiddleware = adapter.create(findUserInfo, {\n  status: 200,\n  parameters: [\n    new adapter.parameter.Parameter(new adapter.parameter.where.Where('user', true), { name: 'id', index: 0 }),\n    new adapter.parameter.Parameter(userDao, { index: 1 }),\n  ],\n});\n\nconst createUserMiddleware = adapter.create(createUserInfo, {\n  status: null,\n  parameters: [\n    new adapter.parameter.Parameter(adapter.parameter.where.body, { index: 0 }),\n    new adapter.parameter.Parameter(userDao, { index: 1 }),\n  ],\n  response: new adapter.response.Response(adapter.response.where.context, { name: 'user' }),\n});\n\nconst router = new Router();\n\nrouter.get('/users/:id', findUserMiddleware);\nrouter.post('/user', createUserMiddleware, findUserInContextMiddleware);\n\nconst app = new Koa();\n\napp.use(bodyParser());\n\napp.use(router.routes());\napp.use(router.allowedMethods());\n\nconst port = 4000;\napp.listen(port, () =\u003e {\n  console.log(`server is listening to port ${port}`);\n});\n```\n\n### Request\n\n- *POST* http://localhost:4000/user\n\n  ```json\n  {\n  \t\"username\": \"test\",\n  \t\"password\": \"test\"\n  }\n  ```\n\n### Response\n\n- status: 200\n\n  ```json\n  {\n      \"username\": \"test\",\n      \"password\": \"test\",\n      \"id\": 0\n  }\n  ```\n\n​    \n\n### Request\n\n- *GET* http://localhost:4000/users/0\n\n### Response\n\n- status: 200\n\n  ```json\n  {\n      \"username\": \"test\",\n      \"password\": \"test\",\n      \"id\": 0\n  }\n  ```\n\n​    \n\n## Spec\n\n### Adapt\n\n```js\nadapter.create(listener, { status, type, parameters, response, handlers, thisArg });\n```\n\n​    \n\n### Parameters\n\n```js\nfunction Parameter(\n  where = new Where(null, true, true, true),\n  options = {},\n) {\n  this.where = where;\n  this.name = options.name || null;\n  this.as = options.as || null;\n  this.index = options.index || 0;\n  this.combineLevel = options.combineLevel || 0;\n}\n```\n\n- Parameters defines the information of the parameters to pass.\n  - `where` defines where to find the parameter.\n    - If `where` is not an instance of `Where`, the parameter is `where` .\n    - If `where` is an instance of `Where`, the parameter is found in `ctx` with information from `where`.\n  - `name` is the name of the parameter.\n    - If `name` exists, the same name is taken from the parameter's location.\n  - `combineLevel` is the level at which the imported arguments are to be combined.\n    - `0` means the imported parameter is the parameter to pass.\n    - `1` means the imported parameter is a child of the parameter to pass.\n  - `as` specifies a name when passing a parameter.\n  - `index` is index of the parameter to pass.\n\n- The default value is `params`, `query`, `header`, `body`, `cookies` defined.\n\n​    \n\n### Response\n\n```js\nfunction Response(\n  where = new Where(null, true, true, true),\n  options = {},\n) {\n  this.where = where;\n  this.name = options.name || null;\n  this.type = options.type;\n}\n```\n\n- The response determines how to handle the lister's response\n  - `where` defines where to bind the response.\n    - If `where` is not an instance of `Where`, the response is injected in `where` .\n    - If `where` is an instance of `Where`, the position is found in `ctx` with information from `where` and inject the response.\n  - `name` is the name of the response.\n    - If `name` exists, the same name is defined in the position to inject the response.\n- The default value is `body`, which binds the request value to the body of the response.\n\n​    \n\n### Where\n\n```js\nfunction Where(name, context, koa, node, setterAndGetter) {\n  this.name = name;\n  this.context = context;\n  this.koa = koa;\n  this.node = node;\n  this.setterAndGetter = setterAndGetter;\n}\n```\n\n- Where defines where to find the parameter.\n  - `name` is the name of the location from which to retrieve the parameter.\n  - `context` means to find the location of a parameter in `ctx`.\n  - `koa` means to find the location of a parameter in `ctx.request`.\n  - `node` means to find the location of a parameter in `ctx.req`.\n- `setterAndGetter` means to use `set` or `get` method to extract or inject object, if `set` or `get` method is exist.\n\n​    \n\n### Handlers\n\n```js\nconst handlers = { extractParameterHandler, injectResponseHandler, errorHandler };\n```\n\n#### Extract Parameter Handler\n\n```js\nfunction extractParameterHandler(ctx, parameters) {}\n```\n\n#### Inject Response Handler\n\n```js\nfunction injectResponseHandler(ctx, result, options = { response, status, type }) {}\n```\n\n#### Error Handler\n\n```js\nfunction errorHandler(ctx, error) {}\n```\n\n​    \n\n### thisArg\n\n- If this is exist, listener call by thisArg.\n- If this type is function, listener call by result of thisArg.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoursedesign%2Fkoa-middleware-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoursedesign%2Fkoa-middleware-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoursedesign%2Fkoa-middleware-adapter/lists"}