Minimal request-scoped context propagation for Node.js
npm install @nagaraj6618/request-context@nagaraj6618/request-context โ v1.1.0)You only need to update these parts if you already pasted the previous README.
---
``bash`
npm install @nagaraj6618/request-context
---
`ts
import { context } from "@nagaraj6618/request-context";
context.run({ requestId: "req-123" }, async () => {
await doSomething();
console.log(context.get("requestId")); // req-123
});
`
---
`ts`
context.run({ requestId: "req-1" }, async () => {
// async-safe context
});
---
`ts`
context.get("requestId"); // string | undefined
---
`ts`
context.set("userId", "u-42");
---
`ts`
context.has("userId"); // true / false
---
`ts`
context.update({
userId: "u-1",
role: "ADMIN"
});
---
`ts`
context.getAll();
// { requestId, userId, role }
---
`ts
import crypto from "crypto";
import { context } from "@nagaraj6618/request-context";
app.use((req, res, next) => {
context.run(
{ requestId: crypto.randomUUID() },
next
);
});
app.get("/orders", async (req, res) => {
console.log(context.get("requestId"));
res.send("ok");
});
`
---
* v1.0.0 โ core API
* v1.1.0 โ added has() and update()` (non-breaking)
---