Skip to content

Commit

Permalink
feat(core): add params decorator to pagination, sorting and filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rudemex committed Jan 27, 2024
1 parent 17b6d50 commit ef418e1
Show file tree
Hide file tree
Showing 42 changed files with 1,514 additions and 77 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"jest": true
},
"rules": {
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.490.0",
"@nestjs/axios": "^3.0.0",
"@nestjs/class-transformer": "^0.4.0",
"@nestjs/class-validator": "^0.13.4",
"@nestjs/common": "^10.3.0",
"@nestjs/config": "^3.1.1",
Expand All @@ -74,6 +75,7 @@
"axios": "^1.6.5",
"axios-retry": "^4.0.0",
"camunda-external-task-client-js": "^2.3.1",
"class-transformer": "^0.5.1",
"dynamoose": "^4.0.0",
"fast-redact": "^3.3.0",
"lodash": "^4.17.21",
Expand All @@ -91,12 +93,14 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@automock/jest": "^2.1.0",
"@babel/core": "^7.23.7",
"@babel/parser": "^7.23.6",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/plugin-proposal-decorators": "^7.23.7",
"@babel/plugin-proposal-decorators": "^7.23.9",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/plugin-proposal-optional-chaining": "^7.20.7",
"@babel/plugin-syntax-decorators": "^7.16.7",
"@babel/preset-typescript": "^7.23.3",
"@babel/traverse": "^7.23.7",
"@commitlint/cli": "^17.8.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/archetype/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "@tresdoce-nestjs-toolkit/config",
"compilerOptions": {
"rootDir": "./src"
"rootDir": "./src",
},
"include": ["src/**/*"],
"exclude": [
Expand All @@ -15,6 +15,6 @@
"**/*test.ts",
"**/*e2e.ts",
"**/*e2e-spec.ts",
"coverage"
]
"coverage",
],
}
6 changes: 3 additions & 3 deletions packages/camunda/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "@tresdoce-nestjs-toolkit/config",
"compilerOptions": {
"rootDir": "./src"
"rootDir": "./src",
},
"include": ["src/**/*"],
"exclude": [
Expand All @@ -15,6 +15,6 @@
"**/*test.ts",
"**/*e2e.ts",
"**/*e2e-spec.ts",
"coverage"
]
"coverage",
],
}
6 changes: 3 additions & 3 deletions packages/commons/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "@tresdoce-nestjs-toolkit/config",
"compilerOptions": {
"rootDir": "./src"
"rootDir": "./src",
},
"include": ["src/**/*"],
"exclude": [
Expand All @@ -15,6 +15,6 @@
"**/*test.ts",
"**/*e2e.ts",
"**/*e2e-spec.ts",
"coverage"
]
"coverage",
],
}
2 changes: 1 addition & 1 deletion packages/config/jest-mochawesome-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const createSuite = (_isRoot = false) => {
const findTestCase = (_testCaseName, _suitTitle, _testFileContent) => {
const ast = parser.parse(_testFileContent, {
sourceType: 'module',
plugins: ['jsx', 'typescript'],
plugins: ['jsx', 'typescript', ['decorators', { decoratorsBeforeExport: true, legacy: true }]],
});
let testCaseCode = '';
let hasDescribe = false;
Expand Down
4 changes: 2 additions & 2 deletions packages/config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"esModuleInterop": true,
"strict": false,
"skipLibCheck": true,
"types": ["tresdoce-nestjs-toolkit", "jest", "node"]
}
"types": ["tresdoce-nestjs-toolkit", "jest", "node"],
},
}
143 changes: 143 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,149 @@ export class AppController {
}
```

## Param Decorators

### Pagination

Decorador para manejar la paginación.

```typescript
// ./src/users/controllers/users.controller.ts
import { Controller, Get } from '@nestjs/common';
import { Pagination, PaginationParamsDto } from '@tresdoce-nestjs-toolkit/core';

@Controller()
export class UsersController {
//...

@Get()
findAll(@Pagination() pagination: PaginationParamsDto) {
const { page, size } = pagination;
console.log('Current page: ', page);
console.log('Items per page: ', size);
//...
}
}
```

<details>
<summary>💬 Para ver en detalle todas las propiedades de la configuración, hace clic acá.</summary>

`page`: El número de la página actual, proporciona un punto de referencia claro para el usuario.

- Type: `number`
- Default: `1`
- Example: `3`

`size`: El número de elementos por página, este puede ser un valor predeterminado o especificado por el usuario con un
valor máximo de 100 items por repuesta.

- Type: `number`
- Default: `10`
- Example: `25`

</details>

#### URL Example

- Schema: `<http|https>://<server_url><:port>/<app-context>/<endpoint-path>?page=<value-page>&size=<value-size>`
- Example: `http://localhost:8080/v1/users?page=2&size=20`

### Sorting

Decorador para manejar el ordenamiento de los resultados.

```typescript
// ./src/users/controllers/users.controller.ts
import { Controller, Get } from '@nestjs/common';
import { Sorting, SortingParamsDto } from '@tresdoce-nestjs-toolkit/core';

@Controller()
export class UsersController {
//...

@Get()
findAll(@Sorting(['id', 'email']) sorting: SortingParamsDto) {
const { fields } = sorting;
console.log('Sorting Fields: ', fields);
//...
}
}
```

<details>
<summary>💬 Para ver en detalle todas las propiedades de la configuración, hace clic acá.</summary>

`field`: El nombre del campo por el cual se ordenará.

- Type: `string`
- Example: `'id', 'email'`

`order`: La dirección del ordenamiento, puede ser ascendente (`asc`) o descendente (`desc`).

- Type: `asc | desc`
- Default: `asc`
- Example: `desc`

</details>

#### URL Example

- Schema: `<http|https>://<server_url><:port>/<app-context>/<endpoint-path>?sort=<field1>:<order1>,<field2>:<order2>,...`
- Example: `http://localhost:8080/v1/items?sort=id:asc,email:desc`

### Filtering

Decorador para manejar el filtrado de los resultados basado en varios criterios.

```typescript
// ./src/users/controllers/users.controller.ts
import { Controller, Get } from '@nestjs/common';
import { FilteringParams, FilteringParamsDto } from '@tresdoce-nestjs-toolkit/core';

@Controller()
export class UsersController {
//...

@Get()
findAll(@FilteringParams(['firstName', 'email', 'id']) filters: FilteringParamsDto) {
filters.forEach((filter) => {
console.log('Filter Property: ', filter.property);
console.log('Filter Rule: ', filter.rule);
console.log('Filter Values: ', filter.values);
});
//...
}
}
```

<details>
<summary>💬 Para ver en detalle todas las propiedades de la configuración, hace clic acá.</summary>

`property`: El nombre de la propiedad por la cual se filtra.

- Type: `string`
- Example: `'firstName', 'id'`

`rule`: La regla utilizada para filtrar, determina cómo se compara el valor de la propiedad.

- Type: `FilterRule`
- Enum: `eq | neq | gt | gte | lt | lte | like | nlike | in | nin | isnull | isnotnull`
- Example: `gte`

`values`: Los valores utilizados para el filtro según la regla.

- Type: `string[] | number[] | boolean[]`
- Example ('gt', 'gte', 'lt', 'lte'): `[30]`
- Example ('in', 'nin'): `['John', 'Doe']`

</details>

#### URL Example

- Schema: `<http|https>://<server_url><:port>/<app-context>/<endpoint-path>?filter=<property1>:<rule1>:<value1>,<property2>:<rule2>:<value2>,<value22>,...`
- Example: `http://localhost:8080/v1/users?filter=age:gte:30,name:like:John,status:in:active,inactive`

## 📄 Changelog

Todos los cambios notables de este paquete se documentarán en el archivo [Changelog](./CHANGELOG.md).
Expand Down
18 changes: 7 additions & 11 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@
"directory": "../../dist/core"
},
"peerDependencies": {
"@nestjs/class-transformer": "^0.4.0",
"@nestjs/class-validator": "^0.13.4",
"@nestjs/common": "^10.3.0",
"@nestjs/core": "^10.3.0",
"@nestjs/platform-express": "^10.3.0",
"class-transformer": "^0.5.1",
"joi": "^17.11.1",
"reflect-metadata": "^0.2.1",
"rxjs": "^7.5.5"
},
"dependencies": {
"@nestjs/class-transformer": "^0.4.0",
"@nestjs/class-validator": "^0.13.4",
"class-transformer": "^0.5.1",
"joi": "^17.11.1"
},
"devDependencies": {
Expand All @@ -60,7 +66,7 @@
"@pika/pack": {
"pipeline": [
[
"@pika/plugin-standard-pkg",
"@pika/plugin-ts-standard-pkg",
{
"exclude": [
"**/__test__/*",
Expand All @@ -78,16 +84,6 @@
"**/__test__/**/*.key"
]
}
],
[
"@pika/plugin-build-types",
{
"exclude": [
"**/__test__/*",
"**/__test__/**/*.crt",
"**/__test__/**/*.key"
]
}
]
]
}
Expand Down
Loading

0 comments on commit ef418e1

Please sign in to comment.