@viardex/viardex-libs 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -0
- package/dist/common/cards/card.utils.d.ts +9 -0
- package/dist/common/cards/card.utils.js +48 -0
- package/dist/common/cards/index.d.ts +1 -0
- package/dist/common/cards/index.js +17 -0
- package/dist/common/crypto/crypto.utils.d.ts +7 -0
- package/dist/common/crypto/crypto.utils.js +42 -0
- package/dist/common/crypto/index.d.ts +1 -0
- package/dist/common/crypto/index.js +17 -0
- package/dist/common/dto/index.d.ts +1 -0
- package/dist/common/dto/index.js +17 -0
- package/dist/common/dto/pagination-query.dto.d.ts +7 -0
- package/dist/common/dto/pagination-query.dto.js +51 -0
- package/dist/common/index.d.ts +6 -0
- package/dist/common/index.js +6 -0
- package/dist/common/interfaces/index.d.ts +1 -0
- package/dist/common/interfaces/index.js +17 -0
- package/dist/common/interfaces/response.interface.d.ts +27 -0
- package/dist/common/interfaces/response.interface.js +2 -0
- package/dist/common/pipes/file-validation.pipe.d.ts +22 -0
- package/dist/common/pipes/file-validation.pipe.js +78 -0
- package/dist/common/pipes/index.d.ts +2 -0
- package/dist/common/pipes/index.js +18 -0
- package/dist/common/pipes/pagination-query-cleaner.pipe.d.ts +4 -0
- package/dist/common/pipes/pagination-query-cleaner.pipe.js +24 -0
- package/dist/common/response/http-exception.filter.d.ts +6 -0
- package/dist/common/response/http-exception.filter.js +61 -0
- package/dist/common/response/index.d.ts +3 -0
- package/dist/common/response/index.js +19 -0
- package/dist/common/response/response.interceptor.d.ts +6 -0
- package/dist/common/response/response.interceptor.js +57 -0
- package/dist/common/response/response.utils.d.ts +6 -0
- package/dist/common/response/response.utils.js +34 -0
- package/dist/common/utils/country.utils.d.ts +1 -0
- package/dist/common/utils/country.utils.js +38 -0
- package/dist/common/utils/date.utils.d.ts +6 -0
- package/dist/common/utils/date.utils.js +48 -0
- package/dist/common/utils/decimal.utils.d.ts +3 -0
- package/dist/common/utils/decimal.utils.js +22 -0
- package/dist/common/utils/format.utils.d.ts +13 -0
- package/dist/common/utils/format.utils.js +127 -0
- package/dist/common/utils/index.d.ts +5 -0
- package/dist/common/utils/index.js +5 -0
- package/dist/common/utils/validation.utils.d.ts +8 -0
- package/dist/common/utils/validation.utils.js +44 -0
- package/dist/nats/constants/nats.constants.d.ts +148 -2
- package/dist/nats/constants/nats.constants.js +90 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -119,6 +119,75 @@ Preferred APIs:
|
|
|
119
119
|
- `scan()` instead of `keys()` for production-safe pattern reads
|
|
120
120
|
- `ping()` for lightweight health checks
|
|
121
121
|
|
|
122
|
+
## Common
|
|
123
|
+
|
|
124
|
+
`@viardex/viardex-libs` also exposes shared DTOs, pipes, response helpers, and safe formatting utilities through the root barrel.
|
|
125
|
+
|
|
126
|
+
### Response Shape
|
|
127
|
+
|
|
128
|
+
Use the shared response helpers to keep HTTP payloads consistent across Nest services.
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import { AppResponse } from '@viardex/viardex-libs';
|
|
132
|
+
|
|
133
|
+
return AppResponse.success('User fetched successfully', user);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
For paginated endpoints:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
return AppResponse.paginated('Users fetched successfully', items, {
|
|
140
|
+
page,
|
|
141
|
+
limit,
|
|
142
|
+
totalItems,
|
|
143
|
+
totalPages,
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Shared response exports include:
|
|
148
|
+
|
|
149
|
+
- `ApiResponse`
|
|
150
|
+
- `PaginationMeta`
|
|
151
|
+
- `ResponseMeta`
|
|
152
|
+
- `ErrorDetail`
|
|
153
|
+
- `AppResponse`
|
|
154
|
+
- `ResponseInterceptor`
|
|
155
|
+
- `HttpExceptionFilter`
|
|
156
|
+
|
|
157
|
+
### DTOs And Pipes
|
|
158
|
+
|
|
159
|
+
Shared request helpers include:
|
|
160
|
+
|
|
161
|
+
- `PaginationQueryDto`
|
|
162
|
+
- `PaginationQueryCleanerPipe`
|
|
163
|
+
- `FileValidationPipe`
|
|
164
|
+
|
|
165
|
+
### Cards
|
|
166
|
+
|
|
167
|
+
Shared card-safe helpers are available for masking and display use cases:
|
|
168
|
+
|
|
169
|
+
- `maskPan`
|
|
170
|
+
- `extractLast4`
|
|
171
|
+
- `extractBin`
|
|
172
|
+
- `formatCardExpiry`
|
|
173
|
+
- `normalizeCardExpiry`
|
|
174
|
+
- `formatCardLabel`
|
|
175
|
+
|
|
176
|
+
These are for safe formatting and display only. Sensitive authentication data like CVV must never be stored or handled through shared helpers.
|
|
177
|
+
|
|
178
|
+
### Crypto
|
|
179
|
+
|
|
180
|
+
Shared crypto helpers are intentionally limited to generic amount and formatting concerns:
|
|
181
|
+
|
|
182
|
+
- `toAtomicUnits`
|
|
183
|
+
- `fromAtomicUnits`
|
|
184
|
+
- `formatCryptoAmount`
|
|
185
|
+
- `normalizeTxHash`
|
|
186
|
+
- `isHexTxHash`
|
|
187
|
+
- `isPositiveAmount`
|
|
188
|
+
|
|
189
|
+
They are useful for asset formatting and validation, but custody, compliance, signing, and chain-specific business logic should stay inside the owning service.
|
|
190
|
+
|
|
122
191
|
## Nest And Go Mapping
|
|
123
192
|
|
|
124
193
|
The Go shared package in `viardex-go` follows the same auth concepts, but uses middleware and request context instead of Nest metadata and guards.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const maskPan: (pan: string, visiblePrefix?: number, visibleSuffix?: number) => string;
|
|
2
|
+
export declare const extractLast4: (pan: string) => string;
|
|
3
|
+
export declare const extractBin: (pan: string, length?: number) => string;
|
|
4
|
+
export declare const formatCardExpiry: (month: number | string, year: number | string) => string;
|
|
5
|
+
export declare const normalizeCardExpiry: (expiry: string) => {
|
|
6
|
+
month: string;
|
|
7
|
+
year: string;
|
|
8
|
+
} | null;
|
|
9
|
+
export declare const formatCardLabel: (brand: string | undefined, last4: string) => string;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatCardLabel = exports.normalizeCardExpiry = exports.formatCardExpiry = exports.extractBin = exports.extractLast4 = exports.maskPan = void 0;
|
|
4
|
+
const maskPan = (pan, visiblePrefix = 0, visibleSuffix = 4) => {
|
|
5
|
+
const digits = pan.replace(/\s+/g, '');
|
|
6
|
+
if (digits.length <= visibleSuffix) {
|
|
7
|
+
return digits;
|
|
8
|
+
}
|
|
9
|
+
const prefix = digits.slice(0, visiblePrefix);
|
|
10
|
+
const suffix = digits.slice(-visibleSuffix);
|
|
11
|
+
const maskedLength = Math.max(0, digits.length - visiblePrefix - visibleSuffix);
|
|
12
|
+
return `${prefix}${'*'.repeat(maskedLength)}${suffix}`;
|
|
13
|
+
};
|
|
14
|
+
exports.maskPan = maskPan;
|
|
15
|
+
const extractLast4 = (pan) => {
|
|
16
|
+
const digits = pan.replace(/\s+/g, '');
|
|
17
|
+
return digits.slice(-4);
|
|
18
|
+
};
|
|
19
|
+
exports.extractLast4 = extractLast4;
|
|
20
|
+
const extractBin = (pan, length = 6) => {
|
|
21
|
+
const digits = pan.replace(/\s+/g, '');
|
|
22
|
+
return digits.slice(0, length);
|
|
23
|
+
};
|
|
24
|
+
exports.extractBin = extractBin;
|
|
25
|
+
const formatCardExpiry = (month, year) => {
|
|
26
|
+
const normalizedMonth = String(month).padStart(2, '0');
|
|
27
|
+
const normalizedYear = String(year).slice(-2);
|
|
28
|
+
return `${normalizedMonth}/${normalizedYear}`;
|
|
29
|
+
};
|
|
30
|
+
exports.formatCardExpiry = formatCardExpiry;
|
|
31
|
+
const normalizeCardExpiry = (expiry) => {
|
|
32
|
+
const normalized = expiry.trim();
|
|
33
|
+
const match = normalized.match(/^(\d{1,2})\s*\/\s*(\d{2,4})$/);
|
|
34
|
+
if (!match) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const month = match[1].padStart(2, '0');
|
|
38
|
+
const year = match[2].length === 2 ? `20${match[2]}` : match[2];
|
|
39
|
+
if (Number(month) < 1 || Number(month) > 12) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return { month, year };
|
|
43
|
+
};
|
|
44
|
+
exports.normalizeCardExpiry = normalizeCardExpiry;
|
|
45
|
+
const formatCardLabel = (brand, last4) => {
|
|
46
|
+
return `${brand?.trim() || 'Card'} •••• ${last4}`;
|
|
47
|
+
};
|
|
48
|
+
exports.formatCardLabel = formatCardLabel;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './card.utils';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./card.utils"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Decimal from 'decimal.js';
|
|
2
|
+
export declare const toAtomicUnits: (value: string | number | Decimal, decimals: number) => string;
|
|
3
|
+
export declare const fromAtomicUnits: (value: string | number | Decimal, decimals: number) => string;
|
|
4
|
+
export declare const formatCryptoAmount: (value: string | number | Decimal, decimals?: number) => string;
|
|
5
|
+
export declare const normalizeTxHash: (hash: string) => string;
|
|
6
|
+
export declare const isHexTxHash: (hash: string, expectedLength?: number) => boolean;
|
|
7
|
+
export declare const isPositiveAmount: (value: string | number | Decimal) => boolean;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isPositiveAmount = exports.isHexTxHash = exports.normalizeTxHash = exports.formatCryptoAmount = exports.fromAtomicUnits = exports.toAtomicUnits = void 0;
|
|
7
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
8
|
+
const DECIMAL_ZERO = new decimal_js_1.default(0);
|
|
9
|
+
const toAtomicUnits = (value, decimals) => {
|
|
10
|
+
return new decimal_js_1.default(value)
|
|
11
|
+
.mul(new decimal_js_1.default(10).pow(decimals))
|
|
12
|
+
.toDecimalPlaces(0, decimal_js_1.default.ROUND_DOWN)
|
|
13
|
+
.toFixed(0);
|
|
14
|
+
};
|
|
15
|
+
exports.toAtomicUnits = toAtomicUnits;
|
|
16
|
+
const fromAtomicUnits = (value, decimals) => {
|
|
17
|
+
return new decimal_js_1.default(value).div(new decimal_js_1.default(10).pow(decimals)).toFixed();
|
|
18
|
+
};
|
|
19
|
+
exports.fromAtomicUnits = fromAtomicUnits;
|
|
20
|
+
const formatCryptoAmount = (value, decimals = 8) => {
|
|
21
|
+
return new decimal_js_1.default(value).toDecimalPlaces(decimals).toString();
|
|
22
|
+
};
|
|
23
|
+
exports.formatCryptoAmount = formatCryptoAmount;
|
|
24
|
+
const normalizeTxHash = (hash) => {
|
|
25
|
+
return hash.trim().toLowerCase();
|
|
26
|
+
};
|
|
27
|
+
exports.normalizeTxHash = normalizeTxHash;
|
|
28
|
+
const isHexTxHash = (hash, expectedLength) => {
|
|
29
|
+
const normalized = hash.trim().toLowerCase();
|
|
30
|
+
if (!/^0x[a-f0-9]+$/.test(normalized)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
if (expectedLength && normalized.length !== expectedLength) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
};
|
|
38
|
+
exports.isHexTxHash = isHexTxHash;
|
|
39
|
+
const isPositiveAmount = (value) => {
|
|
40
|
+
return new decimal_js_1.default(value).greaterThan(DECIMAL_ZERO);
|
|
41
|
+
};
|
|
42
|
+
exports.isPositiveAmount = isPositiveAmount;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './crypto.utils';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./crypto.utils"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './pagination-query.dto';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./pagination-query.dto"), exports);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PaginationQueryDto = void 0;
|
|
13
|
+
const class_transformer_1 = require("class-transformer");
|
|
14
|
+
const class_validator_1 = require("class-validator");
|
|
15
|
+
class PaginationQueryDto {
|
|
16
|
+
page;
|
|
17
|
+
limit;
|
|
18
|
+
search;
|
|
19
|
+
sortBy;
|
|
20
|
+
sortOrder;
|
|
21
|
+
}
|
|
22
|
+
exports.PaginationQueryDto = PaginationQueryDto;
|
|
23
|
+
__decorate([
|
|
24
|
+
(0, class_transformer_1.Type)(() => Number),
|
|
25
|
+
(0, class_validator_1.IsInt)(),
|
|
26
|
+
(0, class_validator_1.Min)(1),
|
|
27
|
+
(0, class_validator_1.IsOptional)(),
|
|
28
|
+
__metadata("design:type", Number)
|
|
29
|
+
], PaginationQueryDto.prototype, "page", void 0);
|
|
30
|
+
__decorate([
|
|
31
|
+
(0, class_transformer_1.Type)(() => Number),
|
|
32
|
+
(0, class_validator_1.IsInt)(),
|
|
33
|
+
(0, class_validator_1.Min)(1),
|
|
34
|
+
(0, class_validator_1.IsOptional)(),
|
|
35
|
+
__metadata("design:type", Number)
|
|
36
|
+
], PaginationQueryDto.prototype, "limit", void 0);
|
|
37
|
+
__decorate([
|
|
38
|
+
(0, class_validator_1.IsString)(),
|
|
39
|
+
(0, class_validator_1.IsOptional)(),
|
|
40
|
+
__metadata("design:type", String)
|
|
41
|
+
], PaginationQueryDto.prototype, "search", void 0);
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, class_validator_1.IsString)(),
|
|
44
|
+
(0, class_validator_1.IsOptional)(),
|
|
45
|
+
__metadata("design:type", String)
|
|
46
|
+
], PaginationQueryDto.prototype, "sortBy", void 0);
|
|
47
|
+
__decorate([
|
|
48
|
+
(0, class_validator_1.IsIn)(['asc', 'desc']),
|
|
49
|
+
(0, class_validator_1.IsOptional)(),
|
|
50
|
+
__metadata("design:type", String)
|
|
51
|
+
], PaginationQueryDto.prototype, "sortOrder", void 0);
|
package/dist/common/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export * from './constants';
|
|
2
|
+
export * from './cards';
|
|
3
|
+
export * from './crypto';
|
|
2
4
|
export * from './enums';
|
|
5
|
+
export * from './dto';
|
|
6
|
+
export * from './interfaces';
|
|
7
|
+
export * from './pipes';
|
|
8
|
+
export * from './response';
|
|
3
9
|
export * from './utils';
|
|
4
10
|
export * from './config';
|
package/dist/common/index.js
CHANGED
|
@@ -15,6 +15,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./constants"), exports);
|
|
18
|
+
__exportStar(require("./cards"), exports);
|
|
19
|
+
__exportStar(require("./crypto"), exports);
|
|
18
20
|
__exportStar(require("./enums"), exports);
|
|
21
|
+
__exportStar(require("./dto"), exports);
|
|
22
|
+
__exportStar(require("./interfaces"), exports);
|
|
23
|
+
__exportStar(require("./pipes"), exports);
|
|
24
|
+
__exportStar(require("./response"), exports);
|
|
19
25
|
__exportStar(require("./utils"), exports);
|
|
20
26
|
__exportStar(require("./config"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './response.interface';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./response.interface"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface PaginationMeta {
|
|
2
|
+
page: number;
|
|
3
|
+
limit: number;
|
|
4
|
+
totalItems: number;
|
|
5
|
+
totalPages: number;
|
|
6
|
+
hasNextPage: boolean;
|
|
7
|
+
hasPreviousPage: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface ResponseMeta {
|
|
10
|
+
requestId?: string;
|
|
11
|
+
path?: string;
|
|
12
|
+
timestamp?: string;
|
|
13
|
+
pagination?: PaginationMeta;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
export interface ErrorDetail {
|
|
17
|
+
field?: string;
|
|
18
|
+
message: string;
|
|
19
|
+
code?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface ApiResponse<T = unknown> {
|
|
22
|
+
success: boolean;
|
|
23
|
+
message: string;
|
|
24
|
+
data: T;
|
|
25
|
+
meta?: ResponseMeta;
|
|
26
|
+
errors?: ErrorDetail[];
|
|
27
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { PipeTransform } from '@nestjs/common';
|
|
2
|
+
export interface FileValidationOptions {
|
|
3
|
+
allowedMimeTypes?: string[];
|
|
4
|
+
maxSizeBytes?: number;
|
|
5
|
+
requiredFields?: string[];
|
|
6
|
+
}
|
|
7
|
+
type UploadedFile = {
|
|
8
|
+
mimetype?: string;
|
|
9
|
+
size?: number;
|
|
10
|
+
fieldname?: string;
|
|
11
|
+
originalname?: string;
|
|
12
|
+
};
|
|
13
|
+
type UploadedFilesValue = UploadedFile[] | Record<string, UploadedFile[] | undefined> | undefined | null;
|
|
14
|
+
export declare class FileValidationPipe implements PipeTransform {
|
|
15
|
+
private readonly options;
|
|
16
|
+
constructor(options?: FileValidationOptions);
|
|
17
|
+
transform(value: UploadedFilesValue): UploadedFilesValue;
|
|
18
|
+
private validateRequiredFields;
|
|
19
|
+
private normalizeFiles;
|
|
20
|
+
private formatFileSize;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.FileValidationPipe = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const DEFAULT_ALLOWED_MIME_TYPES = ['image/jpeg', 'image/jpg', 'image/png'];
|
|
15
|
+
const DEFAULT_MAX_SIZE_BYTES = 5 * 1024 * 1024;
|
|
16
|
+
let FileValidationPipe = class FileValidationPipe {
|
|
17
|
+
options;
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
}
|
|
21
|
+
transform(value) {
|
|
22
|
+
const files = this.normalizeFiles(value);
|
|
23
|
+
if (files.length === 0) {
|
|
24
|
+
throw new common_1.BadRequestException('No files uploaded');
|
|
25
|
+
}
|
|
26
|
+
this.validateRequiredFields(value);
|
|
27
|
+
const allowedMimeTypes = this.options.allowedMimeTypes ?? DEFAULT_ALLOWED_MIME_TYPES;
|
|
28
|
+
const maxSizeBytes = this.options.maxSizeBytes ?? DEFAULT_MAX_SIZE_BYTES;
|
|
29
|
+
for (const file of files) {
|
|
30
|
+
const label = file.fieldname || file.originalname || 'file';
|
|
31
|
+
if (file.mimetype && !allowedMimeTypes.includes(file.mimetype)) {
|
|
32
|
+
throw new common_1.BadRequestException(`${label} must be one of: ${allowedMimeTypes.join(', ')}`);
|
|
33
|
+
}
|
|
34
|
+
if (typeof file.size === 'number' && file.size > maxSizeBytes) {
|
|
35
|
+
throw new common_1.BadRequestException(`${label} size must not exceed ${this.formatFileSize(maxSizeBytes)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
validateRequiredFields(value) {
|
|
41
|
+
const requiredFields = this.options.requiredFields ?? [];
|
|
42
|
+
if (requiredFields.length === 0 ||
|
|
43
|
+
!value ||
|
|
44
|
+
Array.isArray(value) ||
|
|
45
|
+
typeof value !== 'object') {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
for (const field of requiredFields) {
|
|
49
|
+
const files = value[field];
|
|
50
|
+
if (!files || files.length === 0) {
|
|
51
|
+
throw new common_1.BadRequestException(`${field} file is required`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
normalizeFiles(value) {
|
|
56
|
+
if (!value) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(value)) {
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
return Object.values(value).flatMap((files) => files ?? []);
|
|
63
|
+
}
|
|
64
|
+
formatFileSize(bytes) {
|
|
65
|
+
if (bytes < 1024) {
|
|
66
|
+
return `${bytes} Bytes`;
|
|
67
|
+
}
|
|
68
|
+
if (bytes < 1024 * 1024) {
|
|
69
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
70
|
+
}
|
|
71
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
exports.FileValidationPipe = FileValidationPipe;
|
|
75
|
+
exports.FileValidationPipe = FileValidationPipe = __decorate([
|
|
76
|
+
(0, common_1.Injectable)(),
|
|
77
|
+
__metadata("design:paramtypes", [Object])
|
|
78
|
+
], FileValidationPipe);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./file-validation.pipe"), exports);
|
|
18
|
+
__exportStar(require("./pagination-query-cleaner.pipe"), exports);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.PaginationQueryCleanerPipe = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
let PaginationQueryCleanerPipe = class PaginationQueryCleanerPipe {
|
|
12
|
+
transform(value, _metadata) {
|
|
13
|
+
if (!value || typeof value !== 'object') {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
delete value.page;
|
|
17
|
+
delete value.limit;
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
exports.PaginationQueryCleanerPipe = PaginationQueryCleanerPipe;
|
|
22
|
+
exports.PaginationQueryCleanerPipe = PaginationQueryCleanerPipe = __decorate([
|
|
23
|
+
(0, common_1.Injectable)()
|
|
24
|
+
], PaginationQueryCleanerPipe);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var HttpExceptionFilter_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.HttpExceptionFilter = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const response_utils_1 = require("./response.utils");
|
|
13
|
+
let HttpExceptionFilter = HttpExceptionFilter_1 = class HttpExceptionFilter {
|
|
14
|
+
logger = new common_1.Logger(HttpExceptionFilter_1.name);
|
|
15
|
+
catch(exception, host) {
|
|
16
|
+
const ctx = host.switchToHttp();
|
|
17
|
+
const response = ctx.getResponse();
|
|
18
|
+
const request = ctx.getRequest();
|
|
19
|
+
const status = exception instanceof common_1.HttpException
|
|
20
|
+
? exception.getStatus()
|
|
21
|
+
: common_1.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
22
|
+
const message = exception instanceof common_1.HttpException
|
|
23
|
+
? exception.message
|
|
24
|
+
: 'An unexpected error occurred';
|
|
25
|
+
const errors = this.getValidationErrors(exception);
|
|
26
|
+
this.logger.error({
|
|
27
|
+
method: request.method,
|
|
28
|
+
url: request.url,
|
|
29
|
+
statusCode: status,
|
|
30
|
+
error: message,
|
|
31
|
+
stack: exception instanceof Error ? exception.stack : undefined,
|
|
32
|
+
}, 'HTTP request failed');
|
|
33
|
+
const body = response_utils_1.AppResponse.error(message, errors, {
|
|
34
|
+
path: request.url ? `${request.method ?? 'GET'} ${request.url}` : undefined,
|
|
35
|
+
requestId: request.requestId ?? request.id,
|
|
36
|
+
timestamp: new Date().toISOString(),
|
|
37
|
+
});
|
|
38
|
+
response.status(status).send(body);
|
|
39
|
+
}
|
|
40
|
+
getValidationErrors(exception) {
|
|
41
|
+
if (!(exception instanceof common_1.BadRequestException)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const payload = exception.getResponse();
|
|
45
|
+
if (typeof payload === 'object' && payload !== null) {
|
|
46
|
+
const errors = payload.errors;
|
|
47
|
+
if (Array.isArray(errors)) {
|
|
48
|
+
return errors;
|
|
49
|
+
}
|
|
50
|
+
const message = payload.message;
|
|
51
|
+
if (Array.isArray(message)) {
|
|
52
|
+
return message.map((item) => ({ message: String(item) }));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
exports.HttpExceptionFilter = HttpExceptionFilter;
|
|
59
|
+
exports.HttpExceptionFilter = HttpExceptionFilter = HttpExceptionFilter_1 = __decorate([
|
|
60
|
+
(0, common_1.Catch)()
|
|
61
|
+
], HttpExceptionFilter);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./http-exception.filter"), exports);
|
|
18
|
+
__exportStar(require("./response.interceptor"), exports);
|
|
19
|
+
__exportStar(require("./response.utils"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import type { ApiResponse } from '../interfaces';
|
|
4
|
+
export declare class ResponseInterceptor<T> implements NestInterceptor<T, ApiResponse<T>> {
|
|
5
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<ApiResponse<T>>;
|
|
6
|
+
}
|