npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nest-kr/request-storage

v0.0.10

Published

```cli npm i @nest-kr/request-storage ```

Downloads

15

Readme

Nest-kr/request-storage

This library for handling data per request. you can set and get data from storage which is maintained during the request scope. It uses cls-hooked internally and compatible with nest.js.

Installation

$ npm install --save @nest-kr/request-storage

How to use

Configure module

const app = await NestFactory.create(AppModule);
const requestStorage = app.get(RequestStorage);

app.use(RequestStorageMiddleware(requestStorage));

If you don't pass RequestStorage, RequestStorageMiddleware will create a new RequestStorage;

const app = await NestFactory.create(AppModule);

app.use(RequestStorageMiddleware());

Use RequestStorage

You can save a data and get the data from everywhere. data will be maintained during the request scope.

import { RequestStorage } from '@nest-kr/request-storage';

@Controller('users')
export class UserController {
  constructor(private requestStorage: RequestStorage) {}

  @Post('/')
  async create() {
    this.requestStorage.set('key', 'value');
    ...
  }
}

...


import {Injectable} from '@nestjs/common';
import { Transaction } from '@nest-kr/transaction';

@Injectable()
export class UserService {
    constructor(
        private requestStorage: RequestStorage,
    ){}

    async create(event: IntegrationEvent): Promise<void> {
        ...
        const data = this.requestStorage.get('key');
        ...
    }
}

Use RequestStorage with RequestInterceptor

Create RequestInterceptor

import { RequestInterceptor, RequestStorage } from '@nest-kr/request-storage';

export const AUTH_INFORMATION_KEY = 'AUTH_INFORMATION_KEY';

export class AuthInterceptor implements RequestInterceptor {
  intercept(req: any, requestStorage: RequestStorage): void {
    const headers = req.headers;
    const userId: string = headers['user-id'] || '';
    const userAgent: string = headers['user-agent'] || '';

    requestStorage.set(AUTH_INFORMATION_KEY, {
      userId,
      userAgent
    });
  }
}

Add RequestInterceptor

const app = async NestFactory.create(AppModule);
const requestStorage = app.get(RequestStorage);
const authInterceptor = app.get(AuthInterceptor); // or new AuthInterceptor();

app.use(RequestStorageMiddleware(requestStorage, [authInterceptor]));

Use RequestStorage


@Injectable()
export class UserService {
    constructor(
        private requestStorage: RequestStorage,
    ){}

    async handle(event: UserCreatedEvent): Promise<void> {
        ...
        const {userID, userAgent} = this.requestStorage.get(AUTH_INFORMATION_KEY');
        ...

    }
}