0.3.0 • Published 15 days ago

@getlarge/nestjs-tools-async-local-storage v0.3.0

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
15 days ago

Async Local Storage`

npm

Installation

$ npm install --save @getlarge/nestjs-tools-async-local-storage

Usage

import assert from 'assert';
import { AsyncLocalStorage } from 'async_hooks';

// to provide type safety for the store map
declare module '@getlarge/nestjs-tools-async-local-storage' {
  interface RequestContext {
    type?: string;
  }
  interface ContextStoreProperties {
    id: number;
    username: string;
    profilePicture: string;
    name: string;
  }
}

import { AsyncLocalStorageService } from '@getlarge/nestjs-tools-async-local-storage';

const service = new AsyncLocalStorageService(new AsyncLocalStorage());

assert.throws(() => service.store.set('id', 1), new TypeError("Cannot read properties of undefined (reading 'set')"));
assert.throws(() => service.set('id', 1), new Error("Store is not initialized. Call 'enterWith' or 'run' first."));

service.enterWith(new Map());

// access store map directly
service.store.set('id', 1);
const id = service.store.get('id');
assert(typeof id === 'number');

// access store map via service which extends Map
service.set('username', 'john');
assert(typeof service.get('username') === 'string');

service.requestContext = { type: 'http' };
const requestContext = service.requestContext;
assert(typeof requestContext.type === 'string');

service.delete('username');
0.3.0

15 days ago