correlation-id-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Correlation ID (with types) for nodejs

Install

npm i correlation-id-ts --save

Compatibility

Requires node >=12.17.0.

API

withId([id,] work)

Executes function work and returns result returned from work. Inside work and any other nested functions (sync or async) executions getId() will return the same id. The id for the context may be set explicitly with the optional correlationId parameter, otherwise it will be a v4 uuid. Calls to withId() may be nested.

correlator.withId({
  correlationId: "meow",
  fn: () => {
    console.log(correlator.getId()); //meow
  },
});
correlator.withId({
  fn: () => {
    console.log(correlator.getId()); // some `some uuid`
  },
});

bindId([id,] work)

Returns function work bound with a correlation scope. When work is executed all calls to getId() will return the same id. The id for the context may be set explicitly with the optional correlationId parameter, otherwise it will be a v4 uuid. Arguments passed to the bound function will be applied to work.

const boundFn = correlator.bindId({
  fn: (name: string) => `meow-meow, ${name}, ${correlator.getId()}`,
});
boundFn("John"); // meow-meow, John, `some uuid`

const boundFn2 = correlator.bindId({
  correlationId: 'bark'
  fn: (name: string) => `meow-meow, ${name}, ${correlator.getId()}`,
});
boundFn2("John"); // meow-meow, John, `bark`

License

MIT

Dependents (0)

Package Sidebar

Install

npm i correlation-id-ts

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

7.67 kB

Total Files

6

Last publish

Collaborators

  • 4thwithme