npm package: @origins-digital/nestjs-commons
Description: Common nest resources for all nestjs projects
Author: Origins Digital
Latest version: 7.1.4

# @origins-digital/nestjs-commons

A collection of common utilities, decorators, and schemas for NestJS applications.

> **Zod compatibility**: Zod-based helpers and schemas are compatible with **Zod v4.x**.

## Installation

```bash
npm install @origins-digital/nestjs-commons
```

## Features

- Database pagination and sorting decorators
- Query and parameter validation decorators
- Authentication header decorators
- Zod schemas for pagination and sorting
- Reflection utilities for method metadata

## Usage

### Pagination Decorators

```typescript
import { Controller, Get } from '@nestjs/common';
import { Pagination } from '@origins-digital/nestjs-commons';

@Controller('users')
export class UserController {
  @Get()
  @Pagination()
  async getUsers(
    @Query('cursor') cursor?: string,
    @Query('limit') limit?: number,
  ) {
    // Your pagination logic here
  }
}
```

### Sorting Decorators

```typescript
import { Controller, Get } from '@nestjs/common';
import { OrderBy } from '@origins-digital/nestjs-commons';

@Controller('users')
export class UserController {
  @Get()
  @OrderBy(['name', 'email', 'createdAt'])
  async getUsers(@Query('sort') sort?: string) {
    // Your sorting logic here
  }
}
```

### Query and Parameter Validation

```typescript
import { Controller, Get, Param } from '@nestjs/common';
import { QueryDefined, ParamDefined } from '@origins-digital/nestjs-commons';

@Controller('users')
export class UserController {
  @Get()
  @QueryDefined('status')
  async getUsersByStatus(@Query('status') status: string) {
    // status parameter is guaranteed to be defined
  }

  @Get(':id')
  @ParamDefined('id')
  async getUser(@Param('id') id: string) {
    // id parameter is guaranteed to be defined
  }
}
```

### Authentication Headers

```typescript
import { Controller, Get } from '@nestjs/common';
import { Header } from '@origins-digital/nestjs-commons';

@Controller('users')
export class UserController {
  @Get('profile')
  @Header('Authorization')
  async getProfile(@Headers('authorization') token: string) {
    // Your authentication logic here
  }
}
```

### Zod Schemas

The package includes Zod schemas for common operations:

```typescript
import { z } from 'zod';
import {
  CursorPagingQuerySchema,
  OrderBySchema,
} from '@origins-digital/nestjs-commons';

// Pagination schema
const paginationSchema = CursorPagingQuerySchema.extend({
  status: z.enum(['active', 'inactive']),
});

// Sorting schema
const sortingSchema = OrderBySchema(['name', 'email', 'createdAt']);
```

### Reflection Utilities

```typescript
import { copyOriginalMethodMetadata } from '@origins-digital/nestjs-commons';

// Use in decorators to preserve method metadata
function MyDecorator() {
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    const originalMethod = descriptor.value;
    // Your decorator logic here
    copyOriginalMethodMetadata(descriptor, originalMethod);
  };
}
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.