{"id":31230843,"url":"https://github.com/aragaoi/context-storage","last_synced_at":"2026-01-20T17:35:04.066Z","repository":{"id":311672498,"uuid":"1044533061","full_name":"aragaoi/context-storage","owner":"aragaoi","description":"Generic AsyncLocalStorage wrapper for context management","archived":false,"fork":false,"pushed_at":"2025-09-02T20:05:58.000Z","size":194,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-22T10:26:22.754Z","etag":null,"topics":["async-local-storage","context-manager","generic-tool","helper-tools","npm-package","request-context","shared-context","utility"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/aragaoi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-08-25T20:39:17.000Z","updated_at":"2025-09-02T20:06:04.000Z","dependencies_parsed_at":"2025-08-25T22:58:33.115Z","dependency_job_id":"c63524ae-db04-4247-a4c5-b7fd5daa6851","html_url":"https://github.com/aragaoi/context-storage","commit_stats":null,"previous_names":["aragaoi/context-storage"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/aragaoi/context-storage","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aragaoi%2Fcontext-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aragaoi%2Fcontext-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aragaoi%2Fcontext-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aragaoi%2Fcontext-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aragaoi","download_url":"https://codeload.github.com/aragaoi/context-storage/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aragaoi%2Fcontext-storage/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281731373,"owners_count":26551804,"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","status":"online","status_checked_at":"2025-10-29T02:00:06.901Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["async-local-storage","context-manager","generic-tool","helper-tools","npm-package","request-context","shared-context","utility"],"created_at":"2025-09-22T10:13:29.012Z","updated_at":"2025-10-30T01:40:02.949Z","avatar_url":"https://github.com/aragaoi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @aragaoi/context-storage\n\n[![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](https://github.com/seachegue/context-storage) [![Version](https://img.shields.io/npm/v/@aragaoi/context-storage.svg)](https://www.npmjs.com/package/@aragaoi/context-storage)\n\n\u003e **Documentation**: This package is a wrapper around Node.js [AsyncLocalStorage](https://nodejs.org/download/release/v18.17.1/docs/api/async_context.html). For a detailed explanation of how AsyncLocalStorage works, see our [AsyncLocalStorage Guide](./ASYNC_STORAGE_GUIDE.md).\n- 🔒 **Type-safe**: Full TypeScript support with generics\n- 🚀 **Zero dependencies**: Only uses Node.js built-ins\n- 🧹 **Automatic cleanup**: Context is automatically cleaned up after async operations\n- 🎯 **Simple API**: Easy to use with minimal boilerplate\n- 🔄 **Async support**: Works seamlessly with async/await\n\n## Installation\n\n```bash\nnpm install context-storage\n```\n\n## Quick Start\n\n### Basic Usage\n\n```typescript\nimport { ContextStorage } from \"context-storage\";\n\n// Create a context storage for user data\nconst userContext = new ContextStorage(() =\u003e ({\n  userId: \"\",\n  role: \"guest\"\n}));\n\n// Use context in your application\nuserContext.runWithContext(() =\u003e {\n  // Set user data\n  userContext.updateContext({ userId: \"user-123\", role: \"admin\" });\n  \n  // Access context anywhere in this scope\n  const user = userContext.getContext();\n  console.log(user); // { userId: \"user-123\", role: \"admin\" }\n});\n```\n\n### Request Context (Pre-configured)\n\n```typescript\nimport { requestContextStorage } from \"context-storage\";\n\n// Use the pre-configured request context\nrequestContextStorage.runWithContext(() =\u003e {\n  // Add user info to context\n  requestContextStorage.updateContext({\n    userId: \"user-123\",\n    tenantId: \"tenant-456\"\n  });\n  \n  // Access request context anywhere\n  const context = requestContextStorage.getContext();\n  console.log(context.requestId); // Unique request ID\n  console.log(context.userId); // \"user-123\"\n});\n```\n\n## API Reference\n\n### ContextStorage\u003cT\u003e\n\n#### Constructor\n```typescript\nnew ContextStorage\u003cT\u003e(contextFactory: () =\u003e T, contextName?: string)\n```\n\n#### Methods\n\n- `runWithContext\u003cT\u003e(callback: () =\u003e T): T` - Run code within a context\n- `getContext(): T | null` - Get current context (nullable)\n- `getContextOrThrow(): T` - Get current context (throws if missing)\n- `updateContext(partial: Partial\u003cT\u003e): void` - Update current context\n\n### RequestContext\n\nPre-configured context type for HTTP requests:\n\n```typescript\ntype RequestContext = {\n  requestId: string;\n  userId?: string;\n  tenantId?: string;\n  startedAt: string;\n  startedAtTimestamp: number;\n  token?: string;\n};\n```\n\n## Examples\n\n### Express.js Middleware\n\n```typescript\nimport { requestContextStorage } from \"context-storage\";\n\napp.use((req, res, next) =\u003e {\n  requestContextStorage.runWithContext(() =\u003e {\n    // Add request data to context\n    requestContextStorage.updateContext({\n      userId: req.user?.id,\n      token: req.headers.authorization\n    });\n    \n    next();\n  });\n});\n\n// In your route handlers\napp.get(\"/api/data\", (req, res) =\u003e {\n  const context = requestContextStorage.getContextOrThrow();\n  console.log(`Request ${context.requestId} from user ${context.userId}`);\n  // ... handle request\n});\n```\n\n### Database Operations\n\n```typescript\nimport { ContextStorage } from \"context-storage\";\n\nconst dbContext = new ContextStorage(() =\u003e ({\n  transactionId: \"\",\n  userId: \"\"\n}));\n\nasync function createUser(userData: any) {\n  return dbContext.runWithContext(async () =\u003e {\n    dbContext.updateContext({\n      transactionId: generateId(),\n      userId: userData.id\n    });\n    \n    // All database operations have access to context\n    await db.beginTransaction();\n    await db.insertUser(userData);\n    await db.logActivity(\"user_created\");\n    await db.commit();\n  });\n}\n```\n\n## Requirements\n\n- Node.js 16.0.0 or higher\n- TypeScript (recommended)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faragaoi%2Fcontext-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faragaoi%2Fcontext-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faragaoi%2Fcontext-storage/lists"}