open-index/open-npm huggingface dataset:
npm package: @quicksend/nestjs-request-context
Description: Keep track of request-level data in NestJS using AsyncLocalStorage
Latest version: 1.3.0
Deprecated: Package has been moved. Use @alexy4744/nestjs-request-context

# NestJS Request Context

Keep track of request-level data in NestJS using AsyncLocalStorage

## Prerequisites
 - [Node.js](https://nodejs.org/en/) >= 16.0.0

## Installation

```bash
$ npm install @quicksend/nestjs-request-context
```

## Usage

### RequestContext

First, create a class that extends `RequestContext`. This class will hold your request-level data.

```ts
import { RequestContext as BaseRequestContext } from "@quicksend/nestjs-request-context";

export class RequestContext extends BaseRequestContext<RequestContext>() {
  data?: string;
}
```

### RequestContextMiddleware

Next, apply `RequestContextMiddleware` as a global middleware and pass in our request context as a parameter:

```ts
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from "@nestjs/common";

import { RequestContextMiddleware } from "@quicksend/nestjs-request-context";

import { RequestContext } from "./request.context";

@Module()
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer): void {
    consumer.apply(RequestContextMiddleware(RequestContext)).forRoutes({
      method: RequestMethod.ALL,
      path: "*"
    });
  }
}
```

You can now access the request context from anywhere in your application.

```ts
import { Controller, Get } from "@nestjs/common";

import { RequestContext } from "./request.context";

@Controller()
export class AppController {
  @Get()
  get(): RequestContext {
    const store = RequestContext.getStore();

    store.data = "test";

    const data = RequestContext.getItem("data");
    
    console.log(item); // "test"

    return store;
  }
}
```

### RequestContextInterceptor

For use cases where a middleware is not appropriate (i.e. NestJS microservices), you can use the `RequestContextInterceptor`. It can be applied normally like any other interceptor. Note that interceptors run after guards, refer to the request lifecycle [here](https://docs.nestjs.com/faq/request-lifecycle#summary).

```ts
import { Controller, Post, Query, UseInterceptors } from "@nestjs/common";

import { RequestContextInterceptor } from "@quicksend/nestjs-request-context";

import { RequestContext } from "./request.context";

@Controller()
export class AppController {
  @Get()
  @UseInterceptors(RequestContextInterceptor(RequestContext))
  get(): RequestContext | undefined {
    const store = RequestContext.getStore();

    store.data = "test";

    const data = RequestContext.getItem("data");
    
    console.log(item); // "test"

    return store;
  }
}
```

## Tests

Run tests using the following commands:
```bash
$ npm run test
$ npm run test:watch
```
npm package: @quicksend/nestjs-transmit
Description: Integrate Transmit into NestJS applications
Latest version: 2.0.0

# NestJS Transmit

Integrate [Transmit](https://github.com/quicksend/transmit) into your NestJS applications.

## Installation

```bash
$ npm install @quicksend/nestjs-transmit @quicksend/transmit
```

## Usage

### TransmitModule
First start by registering the `TransmitModule` in your application:

```ts
import { Module } from "@nestjs/core";
import { TransmitModule } from "@quicksend/nestjs-transmit";

@Module({
  imports: [
    TransmitModule.register()
  ]
})
export class FilesModule {}
```

You can optionally configure Transmit by passing in [TransmitOptions](https://quicksend.github.io/transmit/interfaces/transmitoptions.html) while registering the module:

```ts
import { Module } from "@nestjs/core";
import { TransmitModule } from "@quicksend/nestjs-transmit";

@Module({
  imports: [
    TransmitModule.register({
      maxFiles: 5,
      maxFileSize: 100 * 1024 * 1024,
    })
  ]
})
export class FilesModule {}
```

`TransmitModule` can also be registered asynchronously like so:

```ts
import { ConfigModule, ConfigService } from "@nestjs/config";
import { Module } from "@nestjs/core";
import { TransmitModule } from "@quicksend/nestjs-transmit";

@Module({
  imports: [
    TransmitModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        maxFiles: config.get("MAX_FILES"),
        maxFileSize: config.get('MAX_FILE_SIZE'),
      })
    })
  ]
})
export class FilesModule {}
```

### TransmitInterceptor

Once `TransmitModule` is registered, you can now use `TransmitInterceptor` in your controller to handle file uploads.

You can use the `Fields` and `Files` decorator to retrieve the fields and files that were received.

```ts
import { Controller, Post, UseInterceptors } from "@nestjs/common";

import { Field, File, ParseAsyncResults } from "@quicksend/transmit";

import { Fields, Files, TransmitInterceptor } from "@quicksend/nestjs-transmit";

@Controller()
export class FilesController {
  @Post("upload")
  @UseInterceptors(TransmitInterceptor())
  upload(@Fields() fields: Field[], @Files() files: File[]): ParseAsyncResults {
    return {
      fields,
      files
    };
  }
}
```

You can also pass in [TransmitOptions](https://quicksend.github.io/transmit/interfaces/transmitoptions.html) when using `TransmitInterceptor`. This will allow you to override certain options for a specific endpoint or endpoints.

```ts
import { Controller, Post, UseInterceptors } from "@nestjs/common";

import { Field, File, ParseAsyncResults } from "@quicksend/transmit";

import { Fields, Files, TransmitInterceptor } from "@quicksend/nestjs-transmit";

@Controller()
export class FilesController {
  @Post("upload")
  @UseInterceptors(
    TransmitInterceptor({
      maxFiles: 2,
      maxFileSize: 25 * 1024 * 1024,
    })
  )
  upload(@Fields() fields: Field[], @Files() files: File[]): ParseAsyncResults {
    return {
      fields,
      files
    };
  }
}
```

## Tests

Run tests using the following commands:
```bash
$ npm run test
$ npm run test:watch
```