npm package: @gotocva/nestjs-utils
Description: Common utility functions for NestJS applications, including a lightweight in-memory event queue for managing asynchronous tasks efficiently without requiring Redis or any external dependencies. It supports job retries with customizable attempts and backof
Author: sivabharathy
Latest version: 1.0.0


# 🚀 @gotocva/nestjs-utils

A reusable utility library for NestJS applications. This package includes cryptographic helpers, JWT handling, global exception filtering, response interceptors, and more.

---

## 📦 Features

- 🔐 AES-256-CBC encryption/decryption
- 🔑 JWT token generation, verification, and decoding
- ⚠️ Global exception handler with MongoDB error awareness
- 📦 Standardized API response formatting
- 🧾 SHA-256 hashing utilities

---

## 📦 Installation

```bash
npm install @gotocva/nestjs-utils
````

Or via GitHub:

```bash
npm install git+https://github.com/your-org/nestjs-utils.git
```

> Ensure you have `@nestjs/jwt` and `@nestjs/common` installed.

---

## 🔧 Usage

### 1. Crypto Utilities

```ts
import { createTransactionHash, createContractAddress, encrypt, decrypt } from '@gotocva/nestjs-utils';

// Hashing
const txHash = createTransactionHash({ sender: 'alice', amount: 100 });
const contractAddr = createContractAddress('SomeContractIdentifier');

// Encryption/Decryption
const key = crypto.randomBytes(32); // Or load from config
const encrypted = encrypt('my-secret-data', key);
const decrypted = decrypt(encrypted, key);
```

---

### 2. JWT Handler

```ts
import { JwtHandler } from '@gotocva/nestjs-utils';
import { JwtService } from '@nestjs/jwt';

const jwtHandler = new JwtHandler(new JwtService({ secret: process.env.JWT_SECRET }));

const token = jwtHandler.generateToken({ userId: 123 });
const verified = jwtHandler.verifyToken(token);
const decoded = jwtHandler.decodeToken(token);
```

---

### 3. Global Exception Filter

```ts
import { ExceptionHandler } from '@gotocva/nestjs-utils';
import { APP_FILTER } from '@nestjs/core';

@Module({
  providers: [
    {
      provide: APP_FILTER,
      useClass: ExceptionHandler,
    },
  ],
})
export class AppModule {}
```

Handles:

* Mongoose validation errors
* MongoDB error codes (duplicate key, validation, unauthorized, etc.)
* Custom `HttpException`

---

### 4. Response Interceptor

```ts
import { ResponseInterceptor } from '@gotocva/nestjs-utils';
import { APP_INTERCEPTOR } from '@nestjs/core';

@Module({
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: ResponseInterceptor,
    },
  ],
})
export class AppModule {}
```

All HTTP responses will follow a consistent format:

```json
{
  "message": "Success message",
  "status": true,
  "result": { /* your data */ },
  "error": null,
  "timestamps": "2025-05-11T00:00:00.000Z",
  "status_code": 200,
  "path": "/api/example"
}
```

---

## 🛠 Configuration

* Set `JWT_SECRET` in your environment variables for the `JwtHandler`.
* AES encryption keys must be 32 bytes for `aes-256-cbc`.

---

## 🧪 Testing

To test the utility functions independently:

```bash
npm run test
```

> Add test coverage for each utility as needed.

---

## 🧑‍💻 Contributing

1. Fork the repository
2. Create your feature branch: `git checkout -b feature/my-utils`
3. Commit your changes
4. Push to the branch
5. Open a Pull Request

---

## 📄 License

MIT License © Sivabharathy

---