{"_id":"@aslina/app-context","_rev":"4-0800496538a1ce571e17231f742542c6","name":"@aslina/app-context","dist-tags":{"latest":"1.1.9"},"versions":{"1.1.6":{"name":"@aslina/app-context","version":"1.1.6","author":{"name":"echaoeoen","email":"echaoeoen@gmail.com"},"license":"MIT","_id":"@aslina/app-context@1.1.6","maintainers":[{"name":"echaoeoen","email":"andyepso@gmail.com"}],"homepage":"https://github.com/echaoeoen/app-context#readme","bugs":{"url":"https://github.com/echaoeoen/app-context/issues"},"dist":{"shasum":"ee8ebc8066f7438551955aa335d9c361b33ff91f","tarball":"https://registry.npmjs.org/@aslina/app-context/-/app-context-1.1.6.tgz","fileCount":15,"integrity":"sha512-FGTeZ/K3kJBZxNpdKFstp6fVXkfycc2RpTQEzXbyfccTJTlHX8A2k9xuJyE9pjn937rq6XgngLbCJppoLXf/Wg==","signatures":[{"sig":"MEUCIDJjsEyAvU8TLqfBQV+AeOZu1eDuot8NtmRyYI4MBLAxAiEAt5DtEQxXVGzNrn7xiyHf6+VVdDKNfrRy+Oq35p7Qht4=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":61242},"main":"./dist/index.js","types":"./dist/index.d.ts","gitHead":"012f4911eb28f3316a10537186d905e0020a04b7","scripts":{"lint":"eslint . --ext .ts","test":"jest","build":"tsc","clean":"rimraf dist","semantic-release":"semantic-release","semantic-release-dry-run":"semantic-release --dry-run"},"_npmUser":{"name":"echaoeoen","email":"andyepso@gmail.com"},"repository":{"url":"git+https://github.com/echaoeoen/app-context.git","type":"git"},"_npmVersion":"10.8.3","description":"This library simplifies the way you manage and pass data across deeply nested function calls by introducing a shared execution context. With this library, you no longer need to manually pass parameters between functions. Instead, you set context data at t","directories":{},"_nodeVersion":"20.18.0","dependencies":{"uuid":"^10.0.0","middie":"^7.1.0","express":"^4.19.2","fastify":"^4.28.1","@fastify/middie":"^8.3.1","semantic-release":"^24.1.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsc":"^2.0.4","jest":"^29.7.0","eslint":"^9.9.1","rimraf":"^6.0.1","ts-jest":"^29.2.5","ts-node":"^10.9.2","typescript":"^5.5.4","@types/jest":"^29.5.12","@types/node":"^22.5.3","@jest/globals":"^29.7.0","@types/express":"^4.17.21","@semantic-release/git":"^10.0.1","@semantic-release/npm":"^12.0.1","@semantic-release/github":"^10.3.2","@semantic-release/changelog":"^6.0.3","@semantic-release/commit-analyzer":"^13.0.0","@semantic-release/release-notes-generator":"^14.0.1"},"_npmOperationalInternal":{"tmp":"tmp/app-context_1.1.6_1739292582530_0.8901616635744849","host":"s3://npm-registry-packages-npm-production"}},"1.1.7":{"name":"@aslina/app-context","version":"1.1.7","author":{"name":"echaoeoen","email":"echaoeoen@gmail.com"},"license":"MIT","_id":"@aslina/app-context@1.1.7","maintainers":[{"name":"echaoeoen","email":"andyepso@gmail.com"}],"homepage":"https://github.com/echaoeoen/app-context#readme","bugs":{"url":"https://github.com/echaoeoen/app-context/issues"},"dist":{"shasum":"f32aa2e4951eeaa6e43043ed761b62424b4be3f2","tarball":"https://registry.npmjs.org/@aslina/app-context/-/app-context-1.1.7.tgz","fileCount":15,"integrity":"sha512-DYIfa0uQydy8/wjvQElE73D/9VeeiOx6RHrZ99bbhZxz3K+Yv6LLlie4GldMZNmkki0HB70MmXzXJRmvyMInZg==","signatures":[{"sig":"MEQCIFx4ZkkLrpNBD5K/C/8hlfiE5iZbWOswkKq19zxn2VUWAiA448hgFbUZtlNHK7+cMuHqTxhLo9vcUDwaoBiNPJVUFg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":61217},"main":"./dist/index.js","types":"./dist/index.d.ts","gitHead":"68f4559194d6e81bfee046fab11df7a31a6f5ba8","scripts":{"lint":"eslint . --ext .ts","test":"jest","build":"tsc","clean":"rimraf dist","semantic-release":"semantic-release","semantic-release-dry-run":"semantic-release --dry-run"},"_npmUser":{"name":"echaoeoen","email":"andyepso@gmail.com"},"repository":{"url":"git+https://github.com/echaoeoen/app-context.git","type":"git"},"_npmVersion":"10.8.3","description":"This library simplifies the way you manage and pass data across deeply nested function calls by introducing a shared execution context. With this library, you no longer need to manually pass parameters between functions. Instead, you set context data at t","directories":{},"_nodeVersion":"20.18.0","dependencies":{"uuid":"^10.0.0","middie":"^7.1.0","express":"^4.19.2","fastify":"^4.28.1","@fastify/middie":"^8.3.1","semantic-release":"^24.1.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsc":"^2.0.4","jest":"^29.7.0","eslint":"^9.9.1","rimraf":"^6.0.1","ts-jest":"^29.2.5","ts-node":"^10.9.2","typescript":"^5.5.4","@types/jest":"^29.5.12","@types/node":"^22.5.3","@jest/globals":"^29.7.0","@types/express":"^4.17.21","@semantic-release/git":"^10.0.1","@semantic-release/npm":"^12.0.1","@semantic-release/github":"^10.3.2","@semantic-release/changelog":"^6.0.3","@semantic-release/commit-analyzer":"^13.0.0","@semantic-release/release-notes-generator":"^14.0.1"},"_npmOperationalInternal":{"tmp":"tmp/app-context_1.1.7_1739292666056_0.6112544419276305","host":"s3://npm-registry-packages-npm-production"}},"1.1.8":{"name":"@aslina/app-context","version":"1.1.8","author":{"name":"echaoeoen","email":"echaoeoen@gmail.com"},"license":"MIT","_id":"@aslina/app-context@1.1.8","maintainers":[{"name":"echaoeoen","email":"andyepso@gmail.com"}],"homepage":"https://github.com/echaoeoen/app-context#readme","bugs":{"url":"https://github.com/echaoeoen/app-context/issues"},"dist":{"shasum":"025fc42bbf2af762d43bd4dac02b7104fcea0117","tarball":"https://registry.npmjs.org/@aslina/app-context/-/app-context-1.1.8.tgz","fileCount":21,"integrity":"sha512-CUzwRoYcI2CPb0GA03zmHVPj+1IHZiGzdAUuKnbuqsOjsjXg+qTw+X2C+Kbv/jss3bmmB4NU2Rcisqx2BCbjog==","signatures":[{"sig":"MEUCIQDrtxtfnhdP2FBbh3meP1ppnLfrS3zgb/Yp0hmuGS21hAIgDN4qMnb9aJ+a+VVXPxY87HItj1dG+FMLrrK85UfBQmI=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":64465},"main":"./dist/index.js","types":"./dist/index.d.ts","gitHead":"0a6a4b783020646e617981fb6ffce7bf81aa8a0b","scripts":{"lint":"eslint . --ext .ts","test":"jest","build":"tsc","clean":"rimraf dist","semantic-release":"semantic-release","semantic-release-dry-run":"semantic-release --dry-run"},"_npmUser":{"name":"echaoeoen","email":"andyepso@gmail.com"},"repository":{"url":"git+https://github.com/echaoeoen/app-context.git","type":"git"},"_npmVersion":"10.8.3","description":"This library simplifies the way you manage and pass data across deeply nested function calls by introducing a shared execution context. With this library, you no longer need to manually pass parameters between functions. Instead, you set context data at t","directories":{},"_nodeVersion":"20.19.1","dependencies":{"uuid":"^10.0.0","middie":"^7.1.0","express":"^4.19.2","fastify":"^4.28.1","@fastify/middie":"^8.3.1","semantic-release":"^24.1.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsc":"^2.0.4","jest":"^29.7.0","eslint":"^9.9.1","rimraf":"^6.0.1","ts-jest":"^29.2.5","ts-node":"^10.9.2","typescript":"^5.5.4","@types/jest":"^29.5.12","@types/node":"^22.5.3","@jest/globals":"^29.7.0","@types/express":"^4.17.21","@semantic-release/git":"^10.0.1","@semantic-release/npm":"^12.0.1","@semantic-release/github":"^10.3.2","@semantic-release/changelog":"^6.0.3","@semantic-release/commit-analyzer":"^13.0.0","@semantic-release/release-notes-generator":"^14.0.1"},"_npmOperationalInternal":{"tmp":"tmp/app-context_1.1.8_1750106647337_0.5365109298727433","host":"s3://npm-registry-packages-npm-production"}},"1.1.9":{"name":"@aslina/app-context","version":"1.1.9","description":"This library simplifies the way you manage and pass data across deeply nested function calls by introducing a shared execution context. With this library, you no longer need to manually pass parameters between functions. Instead, you set context data at t","main":"./dist/index.js","types":"./dist/index.d.ts","publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+https://github.com/echaoeoen/app-context.git"},"author":{"name":"echaoeoen","email":"echaoeoen@gmail.com"},"license":"MIT","dependencies":{"@fastify/middie":"^8.3.1","express":"^4.19.2","fastify":"^4.28.1","middie":"^7.1.0","semantic-release":"^24.1.0","uuid":"^10.0.0"},"devDependencies":{"@jest/globals":"^29.7.0","@semantic-release/changelog":"^6.0.3","@semantic-release/commit-analyzer":"^13.0.0","@semantic-release/git":"^10.0.1","@semantic-release/github":"^10.3.2","@semantic-release/npm":"^12.0.1","@semantic-release/release-notes-generator":"^14.0.1","@types/express":"^4.17.21","@types/jest":"^29.5.12","@types/node":"^22.5.3","eslint":"^9.9.1","jest":"^29.7.0","rimraf":"^6.0.1","ts-jest":"^29.2.5","ts-node":"^10.9.2","tsc":"^2.0.4","typescript":"^5.5.4"},"scripts":{"build":"tsc","clean":"rimraf dist","test":"jest","lint":"eslint . --ext .ts","semantic-release":"semantic-release","semantic-release-dry-run":"semantic-release --dry-run"},"_id":"@aslina/app-context@1.1.9","gitHead":"f77d508048dd23c73eccf56efe4bb21e402cbe68","bugs":{"url":"https://github.com/echaoeoen/app-context/issues"},"homepage":"https://github.com/echaoeoen/app-context#readme","_nodeVersion":"20.19.1","_npmVersion":"10.8.3","dist":{"integrity":"sha512-KZQvR74KiAKp2SEofD5KdVq8CW8p3GZe04c6KM1m/B0XNmgyScFwFNX09LyTrwz1bf5OcpBQL9bLErp0UgKIpQ==","shasum":"75373898f7a5af4b920f43b20325ce8a4ecb81cc","tarball":"https://registry.npmjs.org/@aslina/app-context/-/app-context-1.1.9.tgz","fileCount":15,"unpackedSize":61433,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQC3qr5sLdEtmdqCremZctCwA8eASTLg0+de3df2RKs9qQIgKtohcVTdRFqskuYYuJQqF38rSYrrGSe1jDjPQpLKYJA="}]},"_npmUser":{"name":"echaoeoen","email":"andyepso@gmail.com"},"directories":{},"maintainers":[{"name":"echaoeoen","email":"andyepso@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/app-context_1.1.9_1750112578061_0.5000818292687428"},"_hasShrinkwrap":false}},"time":{"created":"2025-02-11T16:49:42.442Z","modified":"2025-06-16T22:22:58.409Z","1.1.6":"2025-02-11T16:49:42.767Z","1.1.7":"2025-02-11T16:51:06.237Z","1.1.8":"2025-06-16T20:44:07.579Z","1.1.9":"2025-06-16T22:22:58.225Z"},"bugs":{"url":"https://github.com/echaoeoen/app-context/issues"},"author":{"name":"echaoeoen","email":"echaoeoen@gmail.com"},"license":"MIT","homepage":"https://github.com/echaoeoen/app-context#readme","repository":{"type":"git","url":"git+https://github.com/echaoeoen/app-context.git"},"description":"This library simplifies the way you manage and pass data across deeply nested function calls by introducing a shared execution context. With this library, you no longer need to manually pass parameters between functions. Instead, you set context data at t","maintainers":[{"name":"echaoeoen","email":"andyepso@gmail.com"}],"readme":"\n# AppContext Library\n\nThe `AppContext` library simplifies managing execution context across asynchronous function calls in Node.js applications. This eliminates the need to pass parameters through deeply nested function calls by using the `AsyncLocalStorage` API. With `AppContext`, you can store and access context-specific data (e.g., user authentication details, request IDs) throughout your application logic without passing them explicitly.\n\n## Installation\n\nInstall the library via npm:\n\n```bash\nnpm install @aslina/app-context\n```\n\nYou'll also need to install the `uuid` package as a peer dependency:\n\n```bash\nnpm install uuid\n```\n\n## Usage\n\n### Basic Setup\n\nTo use the `AppContext` library, define your context data type, initialize the context, and start using it within asynchronous functions.\n\n```typescript\nimport AppContext from '@aslina/app-context';\n\n// Define your context data structure\ntype MyContextType = {\n  userId: string;\n  requestId: string;\n};\n\n// Initialize the context\nconst appContext = AppContext.context<MyContextType>();\n\n// Start a new context and run a function within it\nappContext.startContext(async () => {\n  // Set context data\n  appContext.set({ userId: '12345', requestId: 'req-67890' });\n\n  // Retrieve context data later in the same execution context\n  const contextData = appContext.get();\n  console.log(contextData); // { userId: '12345', requestId: 'req-67890' }\n});\n```\n\n### API Reference\n\n#### `startContext<PromiseType>(fn: () => void | Promise<PromiseType>)`\n\nStarts a new execution context, running the provided function within this context. The function can be synchronous or asynchronous.\n\n- **Returns:** `Promise<PromiseType>`\n\n```typescript\nappContext.startContext<Promise<void>>(() => {\n  // Set and use context data\n  appContext.set({ userId: '12345' });\n});\n```\n\n#### `get(): Partial<ContextDataType>`\n\nRetrieves the context data associated with the current execution context. Throws an error if no context is found.\n\n- **Returns:** `Partial<ContextDataType>`\n\n```typescript\nconst contextData = appContext.get();\n```\n\n#### `set(contextData: Partial<ContextDataType>)`\n\nSets or updates the context data for the current execution context.\n\n```typescript\nappContext.set({ userId: '67890' });\n```\n\n## Example Use Case\n\n### Vanila nodeJS\nThe `AppContext` library is particularly useful in scenarios where you need to manage shared context in applications with complex asynchronous workflows, such as:\n\n- Request handling in an API server\n- Managing user sessions or transaction data across multiple layers\n- Storing request-specific data like tracking IDs or logging information\n\n```typescript\nimport AppContext from '@aslina/app-context';\n\ntype RequestContext = {\n  requestId: string;\n  userId: string;\n};\n\nconst requestContext = AppContext.context<RequestContext>();\n\nasync function handleRequest() {\n  await requestContext.startContext(async () => {\n    requestContext.set({ requestId: 'abc123', userId: 'user456' });\n    \n    const context = requestContext.get();\n    console.log(`Request ID: ${context.requestId}, User ID: ${context.userId}`);\n  });\n}\n\nhandleRequest();\n```\n\n### Express\n\n```typescript\nimport express, { Request, Response } from 'express';\nimport AppContext, { expressAppContext, getExpressContext, setExpressContext, ExpressRequestContext } from '@aslina/app-context';\n\nconst app = express();\ninterface ContextData {\n  user: UserData\n}\n// Pre-request function to be executed before each request is handled\nconst preRequestFn = async (req: Request, res?: Response) => {\n  console.log('Pre-request function is called!');\n  const userData = await someRepo.getUser(req.session.userId);\n  setExpressContext({user: userData});\n};\n\n// Apply the AppContext middleware to handle context for each request\napp.use(expressAppContext<ContextData>(preRequestFn));\n\n// Example route\napp.get('/user', (req: Request, res: Response) => {\n  // Retrieve the current context, including request-specific data\n  const context = AppContext.context<ExpressRequestContext<ContextData>>();\n\n  // Respond with the user ID from the context\n  res.json({\n    message: 'Hello, User!',\n    userId: context.user,\n  });\n});\n\n// Start the server\napp.listen(3000, () => {\n  console.log('Server is running on http://localhost:3000');\n});\n```\n### fastify\n\n```typescript\nimport Fastify from 'fastify';\nimport { registerFastifyAppContext, getFastifyContext, setFastifyContext } from '@aslina/app-context';\n\nconst fastify = Fastify();\n\n// Pre-request function to be executed before each request\nconst preRequestFn = (req: IncomingMessage) => {\n  console.log('Pre-request function called for Fastify!');\n};\n\n// Register the AppContext middleware\nregisterFastifyAppContext(fastify, preRequestFn);\n\nfastify.get('/user', async (request, reply) => {\n  // Get the current Fastify context\n  const context = getFastifyContext<{ userId: string }>();\n\n  // Set some data in the context\n  setFastifyContext({ userId: 'user123' });\n\n  // Send a response with the context data\n  return {\n    message: 'Hello from Fastify!',\n    userId: context.userId,\n  };\n});\n\n// Start the server\nfastify.listen({ port: 3000 }, (err, address) => {\n  if (err) throw err;\n  console.log(`Server running at ${address}`);\n});\n```\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","readmeFilename":"readme.md"}