|
1 |
| -import { |
2 |
| - ArgumentMetadata, |
3 |
| - BadRequestException, |
4 |
| - Injectable, |
5 |
| - PipeTransform, |
6 |
| -} from '@nestjs/common'; |
| 1 | +import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common'; |
7 | 2 | import { WHERE_PIPE_FORMAT } from '../errors/errors.constants';
|
8 | 3 | import { parseObjectLiteral } from '../helpers/parse-object-literal';
|
9 | 4 |
|
10 | 5 | /** Convert a string like "id: 12, b: 'Anand'" to { id: 12, name: "Anand" } */
|
11 | 6 | @Injectable()
|
12 | 7 | export class WherePipe implements PipeTransform {
|
13 |
| - transform( |
14 |
| - value: string, |
15 |
| - metadata: ArgumentMetadata, |
16 |
| - ): Record<string, number | string> | undefined { |
| 8 | + transform(value: string): Record<string, any> | undefined { |
17 | 9 | if (value == null) return undefined;
|
18 | 10 | try {
|
19 | 11 | const rules = parseObjectLiteral(value);
|
20 |
| - const items: Record<string, number | string> = {}; |
| 12 | + const items: Record<string, any> = {}; |
21 | 13 | rules.forEach((rule) => {
|
22 |
| - const num = Number(rule[1]); |
23 |
| - if (!isNaN(num)) items[rule[0]] = num; |
24 |
| - else if (rule[1]) items[rule[0]] = rule[1]; |
| 14 | + const ruleKey = rule[0]; |
| 15 | + let ruleValue: any = rule[1]; |
| 16 | + if (ruleValue.endsWith(')')) { |
| 17 | + if (ruleValue.startsWith('int(')) |
| 18 | + ruleValue = parseInt(/\(([^)]+)\)/.exec(ruleValue)[1]); |
| 19 | + else if ( |
| 20 | + ruleValue.startsWith('date(') || |
| 21 | + ruleValue.startsWith('datetime(') |
| 22 | + ) |
| 23 | + ruleValue = new Date( |
| 24 | + /\(([^)]+)\)/.exec(ruleValue)[1], |
| 25 | + ).toISOString(); |
| 26 | + else if (ruleValue.startsWith('float(')) |
| 27 | + ruleValue = parseFloat(/\(([^)]+)\)/.exec(ruleValue)[1]); |
| 28 | + else if (ruleValue.startsWith('string(')) |
| 29 | + ruleValue = /\(([^)]+)\)/.exec(ruleValue)[1]; |
| 30 | + else if ( |
| 31 | + ruleValue.startsWith('boolean(') || |
| 32 | + ruleValue.startsWith('bool(') |
| 33 | + ) |
| 34 | + ruleValue = /\(([^)]+)\)/.exec(ruleValue)[1] === 'true'; |
| 35 | + } |
| 36 | + [ |
| 37 | + 'lt', |
| 38 | + 'lte', |
| 39 | + 'gt', |
| 40 | + 'gte', |
| 41 | + 'equals', |
| 42 | + 'not', |
| 43 | + 'contains', |
| 44 | + 'startsWith', |
| 45 | + 'endsWith', |
| 46 | + 'every', |
| 47 | + 'some', |
| 48 | + 'none', |
| 49 | + ].forEach((val) => { |
| 50 | + if (rule[1].startsWith(`${val} `)) { |
| 51 | + const data: Record<string, any> = {}; |
| 52 | + data[val] = ruleValue.replace(`${val} `, ''); |
| 53 | + if (data[val].includes(':') && !data[val].endsWith(':')) { |
| 54 | + const record: Record<string, any> = {}; |
| 55 | + record[data[val].split(':')[0].trim()] = data[val] |
| 56 | + .split(':')[1] |
| 57 | + .trim(); |
| 58 | + data[val] = record; |
| 59 | + } |
| 60 | + items[ruleKey] = data; |
| 61 | + } |
| 62 | + }); |
| 63 | + if (ruleValue != null && ruleValue !== '') |
| 64 | + items[ruleKey] = items[ruleKey] || ruleValue; |
25 | 65 | });
|
26 | 66 | return items;
|
27 |
| - } catch (_) { |
| 67 | + } catch (error) { |
28 | 68 | throw new BadRequestException(WHERE_PIPE_FORMAT);
|
29 | 69 | }
|
30 | 70 | }
|
|
0 commit comments