@catbee/utils 2.0.0-next.0 → 2.0.0
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/LICENSE +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/request/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -61,7 +61,7 @@ function parseNumberParam(value, options = {}) {
|
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
63
|
const num = Number(value);
|
|
64
|
-
if (isNaN(num)) {
|
|
64
|
+
if (Number.isNaN(num)) {
|
|
65
65
|
if (throwOnError) throw new exception.BadRequestException(errorMessage);
|
|
66
66
|
return {
|
|
67
67
|
isValid: false,
|
|
@@ -143,38 +143,50 @@ function extractPaginationParams(query, defaultPage = 1, defaultLimit = 20, maxL
|
|
|
143
143
|
};
|
|
144
144
|
}
|
|
145
145
|
__name(extractPaginationParams, "extractPaginationParams");
|
|
146
|
-
|
|
146
|
+
var DEFAULT_SORT = {
|
|
147
147
|
sortBy: "createdAt",
|
|
148
148
|
sortOrder: "desc"
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
} else if (typeof query.sort === "string") {
|
|
157
|
-
sortBy = query.sort.startsWith("-") ? query.sort.slice(1) : query.sort;
|
|
158
|
-
sortOrder = query.sort.startsWith("-") ? "desc" : "asc";
|
|
159
|
-
} else if (Array.isArray(query.sort)) {
|
|
160
|
-
sortBy = query.sort[0].startsWith("-") ? query.sort[0].slice(1) : query.sort[0];
|
|
161
|
-
sortOrder = query.sort[0].startsWith("-") ? "desc" : "asc";
|
|
162
|
-
}
|
|
163
|
-
if (typeof query.sortOrder === "string") {
|
|
164
|
-
const order = query.sortOrder.toLowerCase();
|
|
165
|
-
if (order === "asc" || order === "desc") {
|
|
166
|
-
sortOrder = order;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (allowedFields.length && !allowedFields.includes(sortBy)) {
|
|
170
|
-
sortBy = defaultSort.sortBy;
|
|
149
|
+
};
|
|
150
|
+
function extractSortParams(query, allowedFields, defaultSort = DEFAULT_SORT) {
|
|
151
|
+
const inferredSortBy = getSortByFromQuery(query, defaultSort);
|
|
152
|
+
let sortOrder = getSortOrderFromQuery(query, defaultSort);
|
|
153
|
+
const explicit = applyExplicitSortOrder(query.sortOrder);
|
|
154
|
+
if (explicit) {
|
|
155
|
+
sortOrder = explicit;
|
|
171
156
|
}
|
|
157
|
+
const sortBy = validateSortField(inferredSortBy, allowedFields, defaultSort.sortBy);
|
|
172
158
|
return {
|
|
173
159
|
sortBy,
|
|
174
160
|
sortOrder
|
|
175
161
|
};
|
|
176
162
|
}
|
|
177
163
|
__name(extractSortParams, "extractSortParams");
|
|
164
|
+
function getSortByFromQuery(query, defaultSort) {
|
|
165
|
+
if (typeof query.sortBy === "string") return query.sortBy;
|
|
166
|
+
if (Array.isArray(query.sortBy)) return query.sortBy[0];
|
|
167
|
+
if (typeof query.sort === "string") return query.sort.startsWith("-") ? query.sort.slice(1) : query.sort;
|
|
168
|
+
if (Array.isArray(query.sort)) return query.sort[0].startsWith("-") ? query.sort[0].slice(1) : query.sort[0];
|
|
169
|
+
return defaultSort.sortBy;
|
|
170
|
+
}
|
|
171
|
+
__name(getSortByFromQuery, "getSortByFromQuery");
|
|
172
|
+
function getSortOrderFromQuery(query, defaultSort) {
|
|
173
|
+
if (typeof query.sort === "string") return query.sort.startsWith("-") ? "desc" : "asc";
|
|
174
|
+
if (Array.isArray(query.sort)) return query.sort[0].startsWith("-") ? "desc" : "asc";
|
|
175
|
+
return defaultSort.sortOrder;
|
|
176
|
+
}
|
|
177
|
+
__name(getSortOrderFromQuery, "getSortOrderFromQuery");
|
|
178
|
+
function applyExplicitSortOrder(sortOrderParam) {
|
|
179
|
+
if (typeof sortOrderParam !== "string") return null;
|
|
180
|
+
const order = sortOrderParam.toLowerCase();
|
|
181
|
+
if (order === "asc" || order === "desc") return order;
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
__name(applyExplicitSortOrder, "applyExplicitSortOrder");
|
|
185
|
+
function validateSortField(sortBy, allowedFields, defaultSortBy) {
|
|
186
|
+
if (allowedFields.length && !allowedFields.includes(sortBy)) return defaultSortBy;
|
|
187
|
+
return sortBy;
|
|
188
|
+
}
|
|
189
|
+
__name(validateSortField, "validateSortField");
|
|
178
190
|
function extractFilterParams(query, allowedFilters) {
|
|
179
191
|
const filters = {};
|
|
180
192
|
for (const field of allowedFilters) {
|
package/request/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,5 +22,244 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
import { Request } from 'express';
|
|
26
|
+
import { WithPagination as WithPagination$1 } from '@catbee/utils/response';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Options for parsing and validating request parameters.
|
|
30
|
+
*/
|
|
31
|
+
interface ValidationOptions {
|
|
32
|
+
/** Whether to throw an error on validation failure */
|
|
33
|
+
throwOnError?: boolean;
|
|
34
|
+
/** Custom error message for validation failures */
|
|
35
|
+
errorMessage?: string;
|
|
36
|
+
/** Default value to use if parameter is missing */
|
|
37
|
+
defaultValue?: any;
|
|
38
|
+
/** Whether the parameter is required */
|
|
39
|
+
required?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Result of a parameter validation operation.
|
|
43
|
+
*/
|
|
44
|
+
interface ValidationResult<T> {
|
|
45
|
+
/** Whether validation was successful */
|
|
46
|
+
isValid: boolean;
|
|
47
|
+
/** The validated and potentially transformed value */
|
|
48
|
+
value: T | null;
|
|
49
|
+
/** Error message if validation failed */
|
|
50
|
+
error?: string;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Extracts pagination, sorting, and optional search params from a request.
|
|
54
|
+
*
|
|
55
|
+
* @param req - Express request
|
|
56
|
+
* @returns Object containing validated pagination, sorting, and additional query params
|
|
57
|
+
*/
|
|
58
|
+
declare const getPaginationParams: <T = {}>(req: Request) => WithPagination$1<T>;
|
|
59
|
+
/**
|
|
60
|
+
* Safely parses a string parameter to a number.
|
|
61
|
+
*
|
|
62
|
+
* @param value - String value to parse
|
|
63
|
+
* @param options - Validation options
|
|
64
|
+
* @returns Validation result containing the parsed number or error
|
|
65
|
+
*/
|
|
66
|
+
declare function parseNumberParam(value: string | undefined, options?: ValidationOptions): ValidationResult<number>;
|
|
67
|
+
/**
|
|
68
|
+
* Safely parses a string parameter to a boolean.
|
|
69
|
+
*
|
|
70
|
+
* @param value - String value to parse
|
|
71
|
+
* @param options - Validation options
|
|
72
|
+
* @returns Validation result containing the parsed boolean or error
|
|
73
|
+
*/
|
|
74
|
+
declare function parseBooleanParam(value: string | undefined, options?: ValidationOptions): ValidationResult<boolean>;
|
|
75
|
+
/**
|
|
76
|
+
* Extracts pagination parameters from request query parameters.
|
|
77
|
+
*
|
|
78
|
+
* @param query - Query parameters object
|
|
79
|
+
* @param defaultPage - Default page number if not specified (defaults to 1)
|
|
80
|
+
* @param defaultLimit - Default per page size if not specified (defaults to 20)
|
|
81
|
+
* @param maxLimitSize - Maximum allowed per page size (defaults to 100)
|
|
82
|
+
* @returns Object containing validated page and limit
|
|
83
|
+
*/
|
|
84
|
+
declare function extractPaginationParams(query: Record<string, string | string[]>, defaultPage?: number, defaultLimit?: number, maxLimitSize?: number): {
|
|
85
|
+
page: number;
|
|
86
|
+
limit: number;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Extracts sorting parameters from request query parameters.
|
|
90
|
+
*
|
|
91
|
+
* @param query - Query parameters object
|
|
92
|
+
* @param allowedFields - Array of field names that are allowed to be sorted
|
|
93
|
+
* @param defaultSort - Default sort configuration if not specified
|
|
94
|
+
* @returns Object containing sort field and direction
|
|
95
|
+
*/
|
|
96
|
+
declare function extractSortParams(query: Record<string, string | string[]>, allowedFields: string[], defaultSort?: {
|
|
97
|
+
sortBy: string;
|
|
98
|
+
sortOrder: 'asc' | 'desc';
|
|
99
|
+
}): {
|
|
100
|
+
sortBy: string;
|
|
101
|
+
sortOrder: 'asc' | 'desc';
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Extracts filter parameters from query parameters based on allowed filter fields.
|
|
105
|
+
*
|
|
106
|
+
* @param query - Query parameters object
|
|
107
|
+
* @param allowedFilters - Array of field names that are allowed to be used as filters
|
|
108
|
+
* @returns Object containing the filters as key-value pairs
|
|
109
|
+
*/
|
|
110
|
+
declare function extractFilterParams(query: Record<string, string | string[]>, allowedFilters: string[]): Record<string, string | string[]>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Generic API response format.
|
|
114
|
+
* Used to wrap any successful or failed response from the server.
|
|
115
|
+
*/
|
|
116
|
+
interface ApiResponse<T = any> {
|
|
117
|
+
/** Payload returned from the API. Can be any shape depending on the endpoint. */
|
|
118
|
+
data: T | null;
|
|
119
|
+
/** Indicates whether an error occurred (true = error, false = success). */
|
|
120
|
+
error: boolean;
|
|
121
|
+
/** Success message describing the result of the operation. */
|
|
122
|
+
message: string;
|
|
123
|
+
/** Unique request ID for traceability in logs (e.g., from a middleware). */
|
|
124
|
+
requestId: string;
|
|
125
|
+
/** ISO timestamp when the response was generated. */
|
|
126
|
+
timestamp: string;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Generic pagination structure used for paged lists (e.g., /users?page=1).
|
|
130
|
+
*/
|
|
131
|
+
interface Pagination<T = any> {
|
|
132
|
+
/** List of records for the current page. */
|
|
133
|
+
content: T[];
|
|
134
|
+
/** Metadata about the pagination state. */
|
|
135
|
+
pagination: {
|
|
136
|
+
/** Total number of records across all pages. */
|
|
137
|
+
totalRecords: number;
|
|
138
|
+
/** Total number of pages available. */
|
|
139
|
+
totalPages: number;
|
|
140
|
+
/** Current page number (1-based index). */
|
|
141
|
+
page: number;
|
|
142
|
+
/** Number of records per page. */
|
|
143
|
+
limit: number;
|
|
144
|
+
/** Field by which the data is sorted. */
|
|
145
|
+
sortBy: string;
|
|
146
|
+
/** Sort order: ascending or descending. */
|
|
147
|
+
sortOrder: 'asc' | 'desc';
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Alias for paginated API response.
|
|
152
|
+
* Allows semantic naming like `PaginationResponse<User>` or `PaginationResponse<Post>`.
|
|
153
|
+
*/
|
|
154
|
+
type PaginationResponse<T = any> = Pagination<T>;
|
|
155
|
+
/**
|
|
156
|
+
* Error response structure with additional metadata.
|
|
157
|
+
* Used for providing richer error information to clients.
|
|
158
|
+
*/
|
|
159
|
+
interface ApiErrorResponse extends Omit<ApiResponse<never>, 'data'> {
|
|
160
|
+
/** Error always true for error responses */
|
|
161
|
+
error: true;
|
|
162
|
+
/** HTTP status code */
|
|
163
|
+
status: number;
|
|
164
|
+
/** Path to the resource that caused the error */
|
|
165
|
+
path: string;
|
|
166
|
+
/** Stack trace of the error (if available) */
|
|
167
|
+
stack?: string[];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Success response structure with strongly typed data.
|
|
171
|
+
* Used for providing successful responses to clients.
|
|
172
|
+
*/
|
|
173
|
+
interface ApiSuccessResponse<T = any> extends ApiResponse<T> {
|
|
174
|
+
/** Error always false for success responses */
|
|
175
|
+
error: false;
|
|
176
|
+
/** HTTP status code (usually 200) */
|
|
177
|
+
status?: number;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Response structure for batch operations.
|
|
181
|
+
* Used when multiple operations are performed in a single request.
|
|
182
|
+
*/
|
|
183
|
+
interface BatchResponse<T = any> {
|
|
184
|
+
/** Overall success/failure indicator */
|
|
185
|
+
success: boolean;
|
|
186
|
+
/** Total number of operations */
|
|
187
|
+
total: number;
|
|
188
|
+
/** Number of successful operations */
|
|
189
|
+
successful: number;
|
|
190
|
+
/** Number of failed operations */
|
|
191
|
+
failed: number;
|
|
192
|
+
/** Results of individual operations */
|
|
193
|
+
results: Array<{
|
|
194
|
+
/** Identifier for this operation */
|
|
195
|
+
id: string | number;
|
|
196
|
+
/** Success/failure indicator for this operation */
|
|
197
|
+
success: boolean;
|
|
198
|
+
/** Response data for this operation */
|
|
199
|
+
data?: T;
|
|
200
|
+
/** Error information if this operation failed */
|
|
201
|
+
error?: {
|
|
202
|
+
message: string;
|
|
203
|
+
code?: string;
|
|
204
|
+
};
|
|
205
|
+
}>;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Response structure for asynchronous operations.
|
|
209
|
+
* Used when the operation will complete in the future.
|
|
210
|
+
*/
|
|
211
|
+
interface AsyncOperationResponse {
|
|
212
|
+
/** Always true for async operations */
|
|
213
|
+
async: true;
|
|
214
|
+
/** Job or task ID to check status later */
|
|
215
|
+
jobId: string;
|
|
216
|
+
/** Estimated completion time in seconds (if known) */
|
|
217
|
+
estimatedTime?: number;
|
|
218
|
+
/** URL to check status */
|
|
219
|
+
statusUrl: string;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Response structure for streaming operations.
|
|
223
|
+
* Used when data is returned as a stream rather than all at once.
|
|
224
|
+
*/
|
|
225
|
+
interface StreamResponse {
|
|
226
|
+
/** Stream identifier */
|
|
227
|
+
streamId: string;
|
|
228
|
+
/** Stream type (e.g., 'json', 'binary') */
|
|
229
|
+
streamType: string;
|
|
230
|
+
/** Total size in bytes (if known) */
|
|
231
|
+
totalSize?: number;
|
|
232
|
+
/** Chunk size in bytes */
|
|
233
|
+
chunkSize: number;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Sort direction enumeration.
|
|
237
|
+
*/
|
|
238
|
+
declare enum SortDirection {
|
|
239
|
+
/** Ascending sort order */
|
|
240
|
+
ASC = "asc",
|
|
241
|
+
/** Descending sort order */
|
|
242
|
+
DESC = "desc"
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Pagination parameters for API requests.
|
|
246
|
+
*/
|
|
247
|
+
interface PaginationParams {
|
|
248
|
+
/** Current page number (1-based index) */
|
|
249
|
+
page: number;
|
|
250
|
+
/** Number of records per page */
|
|
251
|
+
limit: number;
|
|
252
|
+
/** Field by which the data is sorted */
|
|
253
|
+
sortBy: string;
|
|
254
|
+
/** Sort order: ascending or descending */
|
|
255
|
+
sortOrder: SortDirection;
|
|
256
|
+
/** Optional search query */
|
|
257
|
+
search?: string;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Type that combines pagination parameters with additional data.
|
|
261
|
+
*/
|
|
262
|
+
type WithPagination<T = {}> = PaginationParams & T;
|
|
263
|
+
|
|
264
|
+
export { SortDirection, extractFilterParams, extractPaginationParams, extractSortParams, getPaginationParams, parseBooleanParam, parseNumberParam };
|
|
265
|
+
export type { ApiErrorResponse, ApiResponse, ApiSuccessResponse, AsyncOperationResponse, BatchResponse, Pagination, PaginationParams, PaginationResponse, StreamResponse, ValidationOptions, ValidationResult, WithPagination };
|
package/request/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -59,7 +59,7 @@ function parseNumberParam(value, options = {}) {
|
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
61
|
const num = Number(value);
|
|
62
|
-
if (isNaN(num)) {
|
|
62
|
+
if (Number.isNaN(num)) {
|
|
63
63
|
if (throwOnError) throw new BadRequestException(errorMessage);
|
|
64
64
|
return {
|
|
65
65
|
isValid: false,
|
|
@@ -141,38 +141,50 @@ function extractPaginationParams(query, defaultPage = 1, defaultLimit = 20, maxL
|
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
143
|
__name(extractPaginationParams, "extractPaginationParams");
|
|
144
|
-
|
|
144
|
+
var DEFAULT_SORT = {
|
|
145
145
|
sortBy: "createdAt",
|
|
146
146
|
sortOrder: "desc"
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
} else if (typeof query.sort === "string") {
|
|
155
|
-
sortBy = query.sort.startsWith("-") ? query.sort.slice(1) : query.sort;
|
|
156
|
-
sortOrder = query.sort.startsWith("-") ? "desc" : "asc";
|
|
157
|
-
} else if (Array.isArray(query.sort)) {
|
|
158
|
-
sortBy = query.sort[0].startsWith("-") ? query.sort[0].slice(1) : query.sort[0];
|
|
159
|
-
sortOrder = query.sort[0].startsWith("-") ? "desc" : "asc";
|
|
160
|
-
}
|
|
161
|
-
if (typeof query.sortOrder === "string") {
|
|
162
|
-
const order = query.sortOrder.toLowerCase();
|
|
163
|
-
if (order === "asc" || order === "desc") {
|
|
164
|
-
sortOrder = order;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (allowedFields.length && !allowedFields.includes(sortBy)) {
|
|
168
|
-
sortBy = defaultSort.sortBy;
|
|
147
|
+
};
|
|
148
|
+
function extractSortParams(query, allowedFields, defaultSort = DEFAULT_SORT) {
|
|
149
|
+
const inferredSortBy = getSortByFromQuery(query, defaultSort);
|
|
150
|
+
let sortOrder = getSortOrderFromQuery(query, defaultSort);
|
|
151
|
+
const explicit = applyExplicitSortOrder(query.sortOrder);
|
|
152
|
+
if (explicit) {
|
|
153
|
+
sortOrder = explicit;
|
|
169
154
|
}
|
|
155
|
+
const sortBy = validateSortField(inferredSortBy, allowedFields, defaultSort.sortBy);
|
|
170
156
|
return {
|
|
171
157
|
sortBy,
|
|
172
158
|
sortOrder
|
|
173
159
|
};
|
|
174
160
|
}
|
|
175
161
|
__name(extractSortParams, "extractSortParams");
|
|
162
|
+
function getSortByFromQuery(query, defaultSort) {
|
|
163
|
+
if (typeof query.sortBy === "string") return query.sortBy;
|
|
164
|
+
if (Array.isArray(query.sortBy)) return query.sortBy[0];
|
|
165
|
+
if (typeof query.sort === "string") return query.sort.startsWith("-") ? query.sort.slice(1) : query.sort;
|
|
166
|
+
if (Array.isArray(query.sort)) return query.sort[0].startsWith("-") ? query.sort[0].slice(1) : query.sort[0];
|
|
167
|
+
return defaultSort.sortBy;
|
|
168
|
+
}
|
|
169
|
+
__name(getSortByFromQuery, "getSortByFromQuery");
|
|
170
|
+
function getSortOrderFromQuery(query, defaultSort) {
|
|
171
|
+
if (typeof query.sort === "string") return query.sort.startsWith("-") ? "desc" : "asc";
|
|
172
|
+
if (Array.isArray(query.sort)) return query.sort[0].startsWith("-") ? "desc" : "asc";
|
|
173
|
+
return defaultSort.sortOrder;
|
|
174
|
+
}
|
|
175
|
+
__name(getSortOrderFromQuery, "getSortOrderFromQuery");
|
|
176
|
+
function applyExplicitSortOrder(sortOrderParam) {
|
|
177
|
+
if (typeof sortOrderParam !== "string") return null;
|
|
178
|
+
const order = sortOrderParam.toLowerCase();
|
|
179
|
+
if (order === "asc" || order === "desc") return order;
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
__name(applyExplicitSortOrder, "applyExplicitSortOrder");
|
|
183
|
+
function validateSortField(sortBy, allowedFields, defaultSortBy) {
|
|
184
|
+
if (allowedFields.length && !allowedFields.includes(sortBy)) return defaultSortBy;
|
|
185
|
+
return sortBy;
|
|
186
|
+
}
|
|
187
|
+
__name(validateSortField, "validateSortField");
|
|
176
188
|
function extractFilterParams(query, allowedFilters) {
|
|
177
189
|
const filters = {};
|
|
178
190
|
for (const field of allowedFilters) {
|
package/response/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|