conode
TypeScript icon, indicating that this package has built-in type declarations

0.2.17 • Public • Published

CoNode

0-deps async context management for Node.js

Build Status NPM version Downloads Coverage Status

Table of Contents

Features

  • Zero dependencies
  • Lightweight wrappers around AsyncLocalStorage
  • Full TypeScript support
  • Works in CommonJS and ES Module environments

Installing

Using yarn:

yarn add conode

Using npm:

yarn add conode

Examples

Quick start

import { createContext } from 'conode';

interface RequestContext { userId: string }

const reqCtx = createContext<RequestContext>();

async function handleRequest() {
  console.log(reqCtx.getContext()?.userId);
}

await reqCtx.contextualize({ userId: 'abc123' }, handleRequest);
// Logs: 'abc123'

Functional approach

import { createContext } from '../index';
const auth = createContext();

const action = () => {
  const jwt = auth.getContext();
  console.log(jwt);
};

auth.contextualize(jwt, action);

Class approach with decorator

import { contextualize, Contextable } from '../index';

const action = () => {
  const service = Service.getContext();
  console.log(service.getSomething());
};

class Service extends Contextable() {
  @contextualize
  async run() {
    await action();
  }
  getSomething() {
    return 'something';
  }
}

const service = new Service();

service.run();

License

License Apache-2.0 Copyright (c) 2022-present Ivan Zakharchanka

Package Sidebar

Install

npm i conode

Weekly Downloads

3,028

Version

0.2.17

License

Apache-2.0

Unpacked Size

14.6 kB

Total Files

9

Last publish

Collaborators

  • 3axap4ehko