6.0.7 • Published 1 year ago

@origins-digital/nestjs-commons v6.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

@origins-digital/nestjs-commons

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

Installation

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

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

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

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

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:

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

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.

5.0.8

1 year ago

5.0.7

1 year ago

5.0.6

1 year ago

5.0.5

1 year ago

5.0.4

1 year ago

5.0.3

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

6.0.7

1 year ago

6.0.6

1 year ago

6.0.1

1 year ago

6.0.0

1 year ago

6.0.3

1 year ago

6.0.2

1 year ago

6.0.5

1 year ago

6.0.4

1 year ago

4.1.4

1 year ago

4.1.3

1 year ago

4.1.6

1 year ago

4.1.5

1 year ago

4.1.0

2 years ago

4.1.2

1 year ago

4.1.1

1 year ago

4.0.17

2 years ago

4.0.16

2 years ago

4.0.15

2 years ago

4.0.12

2 years ago

4.0.14

2 years ago

4.0.13

2 years ago

4.0.11

2 years ago

4.0.10

2 years ago

4.0.9

2 years ago

4.0.8

2 years ago

4.0.7

2 years ago

4.0.6

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.1.0

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.10.4

2 years ago

2.10.1

2 years ago

2.10.2

2 years ago

2.10.0

2 years ago

2.10.3

2 years ago

2.9.1

2 years ago

2.9.0

2 years ago

2.8.9

2 years ago

2.8.7

2 years ago

2.8.6

2 years ago

2.8.8

2 years ago

2.8.5

2 years ago

2.8.4

2 years ago

2.8.3

2 years ago

2.8.1

2 years ago

2.8.2

2 years ago

2.8.0

2 years ago

2.7.0

2 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.4.0

3 years ago

2.5.1

3 years ago

2.3.5

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.0

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.4

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.23.0

3 years ago

1.22.0

3 years ago

1.21.0

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.0

3 years ago

1.17.0

3 years ago

1.16.0

3 years ago

1.15.0

3 years ago

1.14.0

3 years ago

1.13.0

3 years ago

1.12.0

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago