{"id":23131275,"url":"https://github.com/emartech/cls-adapter","last_synced_at":"2026-03-16T16:02:08.977Z","repository":{"id":57111315,"uuid":"120508981","full_name":"emartech/cls-adapter","owner":"emartech","description":"Continuation Local Storage middleware for easier storage access inside function calls","archived":false,"fork":false,"pushed_at":"2024-01-16T18:20:20.000Z","size":281,"stargazers_count":12,"open_issues_count":8,"forks_count":3,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-06-04T13:44:22.626Z","etag":null,"topics":[],"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/emartech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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-02-06T19:01:57.000Z","updated_at":"2024-06-17T15:25:29.000Z","dependencies_parsed_at":"2024-01-16T21:58:52.013Z","dependency_job_id":"852e57aa-6d38-4d3a-9aeb-3fcded087fd7","html_url":"https://github.com/emartech/cls-adapter","commit_stats":{"total_commits":14,"total_committers":3,"mean_commits":4.666666666666667,"dds":0.2142857142857143,"last_synced_commit":"55343157d0c994d89193fd3dad463b092a9b1bd6"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/emartech/cls-adapter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emartech%2Fcls-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emartech%2Fcls-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emartech%2Fcls-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emartech%2Fcls-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emartech","download_url":"https://codeload.github.com/emartech/cls-adapter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emartech%2Fcls-adapter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260153350,"owners_count":22966901,"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-12-17T11:12:30.575Z","updated_at":"2026-03-16T16:02:08.892Z","avatar_url":"https://github.com/emartech.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @emartech/cls-adapter\n\nA wrapper around the Continuation Local Storage library [cls-hooked](https://github.com/Jeff-Lewis/cls-hooked).\nMakes storing and retrieving of context dependent information easier.\nIt acts as a thread aware storage.\n\nWhen a value is set on the storage with a given key,\nthat value will be available inside functions calls from the parent function.\n\n### Installation\n\n```bash\nnpm install @emartech/cls-adapter\n```\n\n### Usage\n\n```javascript\nconst Koa = require('koa');\nconst ClsAdapter = require('@emartech/cls-adapter');\n\nconst logWithStorage = (message) =\u003e {\n  console.log(Object.assign({ message }, ClsAdapter.getContextStorage()));\n};\nconst calculationResult = () =\u003e {\n  logWithStorage(100);\n};\n\nconst app = new Koa();\napp.use(ClsAdapter.getKoaMiddleware());\n\napp.use(async (ctx) =\u003e {\n  ClsAdapter.setOnContext('customer_id', 1000);\n\n  logWithStorage('works');\n  // { message: 'works', request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }\n  \n  calculationResult();\n  // { message: 100, request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }\n\n  ctx.body = 'It works';\n});\n\napp.listen(3000);\n\n```\n\n### API\n\n#### ClsAdapter.getKoaMiddleware()\n\nReturns a middleware function compatible with Koa that stores (or generates if missing) \nthe request identifier from the header (X-Request-Id) and sets it on the storage as `request_id`.\n\n```javascript\nconst app = new Koa();\napp.use(ClsAdapter.getKoaMiddleware());\n\napp.use(async () =\u003e {\n  ClsAdapter.getContextStorage();\n  // { request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }\n});\n```\n\n#### ClsAdapter.getExpressMiddleware()\n\nReturns a middleware function compatible with Express that stores (or generates if missing) \nthe request identifier from the header (X-Request-Id) and sets it on the storage as `request_id`.\n\n```javascript\nconst app = express();\napp.use(ClsAdapter.getExpressMiddleware());\n\napp.use(() =\u003e {\n  ClsAdapter.getContextStorage();\n  // { request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }\n});\n```\n\n#### ClsAdapter.getContextStorage()\n\nReturns the all the values set on the storage.\n\n#### ClsAdapter.setOnContext(key, value)\n\nSets a key with a given value on the storage.\n\n```javascript\nClsAdapter.setOnContext('customer_id', 1);\nClsAdapter.setOnContext('application.customer.id', 11);\n\nClsAdapter.getContextStorage();\n// { customer_id: 1, application: { customer: { id: 11 } } }\n```\n\n#### ClsAdapter.getRequestId()\n\nReturns the the request identifier set on the storage. The identifiers key is `request_id`.\n\n```javascript\nClsAdapter.setOnContext('request_id', 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a');\nClsAdapter.getRequestId();\n// 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a'\n```\n\n#### ClsAdapter.addContextStorageToInput()\n\nReturns a function that extends the given object with the current storage.\n\n```javascript\nClsAdapter.setOnContext('customer_id', 1);\n\nClsAdapter.addContextStorageToInput()({ debug: true });\n// { debug: true, customer_id: 1 }\n```\n\n#### ClsAdapter.addRequestIdToInput()\n\nReturns a function that extends the given object with the request identifier set on the current storage.\n\n```javascript\nClsAdapter.setOnContext('request_id', 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a');\n\nClsAdapter.addRequestIdToInput()({ debug: true });\n// { debug: true, request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femartech%2Fcls-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femartech%2Fcls-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femartech%2Fcls-adapter/lists"}