@catbee/utils 1.0.5 → 2.0.0-next.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.
Files changed (121) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +27 -27
  3. package/array/array.utils.d.ts +191 -0
  4. package/array/index.cjs +246 -0
  5. package/array/index.d.ts +25 -0
  6. package/array/index.mjs +228 -0
  7. package/async/async.utils.d.ts +296 -0
  8. package/async/index.cjs +428 -0
  9. package/async/index.d.ts +25 -0
  10. package/async/index.mjs +407 -0
  11. package/cache/cache.utils.d.ts +176 -0
  12. package/cache/index.cjs +292 -0
  13. package/cache/index.d.ts +25 -0
  14. package/cache/index.mjs +290 -0
  15. package/config/config.d.ts +57 -0
  16. package/config/index.cjs +136 -0
  17. package/config/index.d.ts +26 -0
  18. package/config/index.mjs +131 -0
  19. package/context-store/context-store.utils.d.ts +212 -0
  20. package/context-store/index.cjs +267 -0
  21. package/context-store/index.d.ts +25 -0
  22. package/context-store/index.mjs +261 -0
  23. package/crypto/crypto.utils.d.ts +183 -0
  24. package/crypto/index.cjs +182 -0
  25. package/crypto/index.d.ts +25 -0
  26. package/crypto/index.mjs +166 -0
  27. package/date/date.utils.d.ts +190 -0
  28. package/date/index.cjs +295 -0
  29. package/date/index.d.ts +25 -0
  30. package/date/index.mjs +283 -0
  31. package/decorators/decorators.utils.d.ts +705 -0
  32. package/decorators/index.cjs +913 -0
  33. package/decorators/index.d.ts +25 -0
  34. package/decorators/index.mjs +872 -0
  35. package/dir/dir.utils.d.ts +216 -0
  36. package/dir/index.cjs +416 -0
  37. package/dir/index.d.ts +25 -0
  38. package/dir/index.mjs +389 -0
  39. package/env/env.utils.d.ts +400 -0
  40. package/env/index.cjs +761 -0
  41. package/env/index.d.ts +25 -0
  42. package/env/index.mjs +758 -0
  43. package/exception/exception.utils.d.ts +253 -0
  44. package/exception/index.cjs +362 -0
  45. package/exception/index.d.ts +25 -0
  46. package/exception/index.mjs +338 -0
  47. package/fs/fs.utils.d.ts +196 -0
  48. package/fs/index.cjs +253 -0
  49. package/fs/index.d.ts +25 -0
  50. package/fs/index.mjs +228 -0
  51. package/http-status-codes/http-status-codes.d.ts +289 -0
  52. package/http-status-codes/index.cjs +96 -0
  53. package/http-status-codes/index.d.ts +25 -0
  54. package/http-status-codes/index.mjs +94 -0
  55. package/id/id.utils.d.ts +59 -0
  56. package/id/index.cjs +62 -0
  57. package/id/index.d.ts +25 -0
  58. package/id/index.mjs +56 -0
  59. package/index.cjs +218 -0
  60. package/index.d.ts +51 -0
  61. package/index.mjs +51 -0
  62. package/logger/index.cjs +334 -0
  63. package/logger/index.d.ts +25 -0
  64. package/logger/index.mjs +313 -0
  65. package/logger/logger.utils.d.ts +210 -0
  66. package/middleware/index.cjs +177 -0
  67. package/middleware/index.d.ts +25 -0
  68. package/middleware/index.mjs +170 -0
  69. package/middleware/middleware.utils.d.ts +123 -0
  70. package/obj/index.cjs +317 -0
  71. package/obj/index.d.ts +25 -0
  72. package/obj/index.mjs +301 -0
  73. package/obj/obj.utils.d.ts +156 -0
  74. package/package.json +172 -20
  75. package/performance/index.cjs +231 -0
  76. package/performance/index.d.ts +25 -0
  77. package/performance/index.mjs +225 -0
  78. package/performance/performance.utils.d.ts +159 -0
  79. package/request/index.cjs +202 -0
  80. package/request/index.d.ts +26 -0
  81. package/request/index.mjs +194 -0
  82. package/request/request.utils.d.ts +109 -0
  83. package/response/index.cjs +234 -0
  84. package/response/index.d.ts +26 -0
  85. package/response/index.mjs +222 -0
  86. package/response/response.utils.d.ts +186 -0
  87. package/server/index.cjs +1627 -0
  88. package/server/index.d.ts +28 -0
  89. package/server/index.mjs +1617 -0
  90. package/server/server.builder.d.ts +531 -0
  91. package/server/server.d.ts +303 -0
  92. package/stream/index.cjs +151 -0
  93. package/stream/index.d.ts +25 -0
  94. package/stream/index.mjs +144 -0
  95. package/stream/stream.utils.d.ts +111 -0
  96. package/string/index.cjs +109 -0
  97. package/string/index.d.ts +25 -0
  98. package/string/index.mjs +95 -0
  99. package/string/string.utils.d.ts +124 -0
  100. package/type/index.cjs +129 -0
  101. package/type/index.d.ts +25 -0
  102. package/type/index.mjs +119 -0
  103. package/type/type.utils.d.ts +129 -0
  104. package/types/api-response.d.ts +175 -0
  105. package/types/common.d.ts +148 -0
  106. package/types/config.d.ts +88 -0
  107. package/types/index.cjs +34 -0
  108. package/types/index.d.ts +28 -0
  109. package/types/index.mjs +32 -0
  110. package/types/server.d.ts +291 -0
  111. package/url/index.cjs +201 -0
  112. package/url/index.d.ts +25 -0
  113. package/url/index.mjs +189 -0
  114. package/url/url.utils.d.ts +164 -0
  115. package/validate/index.cjs +212 -0
  116. package/validate/index.d.ts +25 -0
  117. package/validate/index.mjs +188 -0
  118. package/validate/validate.utils.d.ts +200 -0
  119. package/build/index.cjs +0 -7579
  120. package/build/index.d.ts +0 -5774
  121. package/build/index.mjs +0 -7274
@@ -0,0 +1,202 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ 'use strict';
26
+
27
+ var exception = require('@catbee/utils/exception');
28
+
29
+ var __defProp = Object.defineProperty;
30
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
31
+ var getPaginationParams = /* @__PURE__ */ __name((req) => {
32
+ const query = req.query;
33
+ const { page, limit } = extractPaginationParams(query, 1, 20, 100);
34
+ const { sortBy, sortOrder } = extractSortParams(query, []);
35
+ const search = typeof query.search === "string" ? query.search : void 0;
36
+ return {
37
+ ...query,
38
+ page,
39
+ limit,
40
+ sortBy,
41
+ sortOrder,
42
+ search
43
+ };
44
+ }, "getPaginationParams");
45
+ function parseNumberParam(value, options = {}) {
46
+ const { throwOnError = false, errorMessage = "Invalid number parameter", defaultValue, required = false } = options;
47
+ if (value === void 0) {
48
+ if (required) {
49
+ const error = `Required number parameter is missing`;
50
+ if (throwOnError) throw new exception.BadRequestException(error);
51
+ return {
52
+ isValid: false,
53
+ value: null,
54
+ error
55
+ };
56
+ }
57
+ return {
58
+ isValid: defaultValue !== void 0,
59
+ value: defaultValue !== void 0 ? defaultValue : null,
60
+ error: defaultValue === void 0 ? "Missing parameter with no default" : void 0
61
+ };
62
+ }
63
+ const num = Number(value);
64
+ if (isNaN(num)) {
65
+ if (throwOnError) throw new exception.BadRequestException(errorMessage);
66
+ return {
67
+ isValid: false,
68
+ value: null,
69
+ error: errorMessage
70
+ };
71
+ }
72
+ return {
73
+ isValid: true,
74
+ value: num
75
+ };
76
+ }
77
+ __name(parseNumberParam, "parseNumberParam");
78
+ function parseBooleanParam(value, options = {}) {
79
+ const { throwOnError = false, errorMessage = "Invalid boolean parameter", defaultValue, required = false } = options;
80
+ if (value === void 0) {
81
+ if (required) {
82
+ const error = `Required boolean parameter is missing`;
83
+ if (throwOnError) throw new exception.BadRequestException(error);
84
+ return {
85
+ isValid: false,
86
+ value: null,
87
+ error
88
+ };
89
+ }
90
+ return {
91
+ isValid: defaultValue !== void 0,
92
+ value: defaultValue !== void 0 ? defaultValue : null,
93
+ error: defaultValue === void 0 ? "Missing parameter with no default" : void 0
94
+ };
95
+ }
96
+ const lowercaseValue = value.toLowerCase();
97
+ if ([
98
+ "true",
99
+ "t",
100
+ "yes",
101
+ "y",
102
+ "1"
103
+ ].includes(lowercaseValue)) {
104
+ return {
105
+ isValid: true,
106
+ value: true
107
+ };
108
+ }
109
+ if ([
110
+ "false",
111
+ "f",
112
+ "no",
113
+ "n",
114
+ "0"
115
+ ].includes(lowercaseValue)) {
116
+ return {
117
+ isValid: true,
118
+ value: false
119
+ };
120
+ }
121
+ if (throwOnError) throw new exception.BadRequestException(errorMessage);
122
+ return {
123
+ isValid: false,
124
+ value: null,
125
+ error: errorMessage
126
+ };
127
+ }
128
+ __name(parseBooleanParam, "parseBooleanParam");
129
+ function extractPaginationParams(query, defaultPage = 1, defaultLimit = 20, maxLimitSize = 100) {
130
+ const pageParam = typeof query.page === "string" ? query.page : void 0;
131
+ const pageResult = parseNumberParam(pageParam, {
132
+ defaultValue: defaultPage
133
+ });
134
+ const limitParam = typeof query.limit === "string" ? query.limit : void 0;
135
+ const limitResult = parseNumberParam(limitParam, {
136
+ defaultValue: defaultLimit
137
+ });
138
+ const page = pageResult.value && pageResult.value > 0 ? pageResult.value : defaultPage;
139
+ const limit = limitResult.value && limitResult.value > 0 ? Math.min(limitResult.value, maxLimitSize) : defaultLimit;
140
+ return {
141
+ page,
142
+ limit
143
+ };
144
+ }
145
+ __name(extractPaginationParams, "extractPaginationParams");
146
+ function extractSortParams(query, allowedFields, defaultSort = {
147
+ sortBy: "createdAt",
148
+ sortOrder: "desc"
149
+ }) {
150
+ let sortBy = defaultSort.sortBy;
151
+ let sortOrder = defaultSort.sortOrder;
152
+ if (typeof query.sortBy === "string") {
153
+ sortBy = query.sortBy;
154
+ } else if (Array.isArray(query.sortBy)) {
155
+ sortBy = query.sortBy[0];
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;
171
+ }
172
+ return {
173
+ sortBy,
174
+ sortOrder
175
+ };
176
+ }
177
+ __name(extractSortParams, "extractSortParams");
178
+ function extractFilterParams(query, allowedFilters) {
179
+ const filters = {};
180
+ for (const field of allowedFilters) {
181
+ if (field in query) {
182
+ filters[field] = query[field];
183
+ }
184
+ }
185
+ return filters;
186
+ }
187
+ __name(extractFilterParams, "extractFilterParams");
188
+
189
+ // src/types/api-response.ts
190
+ var SortDirection = /* @__PURE__ */ (function(SortDirection2) {
191
+ SortDirection2["ASC"] = "asc";
192
+ SortDirection2["DESC"] = "desc";
193
+ return SortDirection2;
194
+ })({});
195
+
196
+ exports.SortDirection = SortDirection;
197
+ exports.extractFilterParams = extractFilterParams;
198
+ exports.extractPaginationParams = extractPaginationParams;
199
+ exports.extractSortParams = extractSortParams;
200
+ exports.getPaginationParams = getPaginationParams;
201
+ exports.parseBooleanParam = parseBooleanParam;
202
+ exports.parseNumberParam = parseNumberParam;
@@ -0,0 +1,26 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ export * from './request.utils';
26
+ export * from '../types/api-response';
@@ -0,0 +1,194 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { BadRequestException } from '@catbee/utils/exception';
26
+
27
+ var __defProp = Object.defineProperty;
28
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
29
+ var getPaginationParams = /* @__PURE__ */ __name((req) => {
30
+ const query = req.query;
31
+ const { page, limit } = extractPaginationParams(query, 1, 20, 100);
32
+ const { sortBy, sortOrder } = extractSortParams(query, []);
33
+ const search = typeof query.search === "string" ? query.search : void 0;
34
+ return {
35
+ ...query,
36
+ page,
37
+ limit,
38
+ sortBy,
39
+ sortOrder,
40
+ search
41
+ };
42
+ }, "getPaginationParams");
43
+ function parseNumberParam(value, options = {}) {
44
+ const { throwOnError = false, errorMessage = "Invalid number parameter", defaultValue, required = false } = options;
45
+ if (value === void 0) {
46
+ if (required) {
47
+ const error = `Required number parameter is missing`;
48
+ if (throwOnError) throw new BadRequestException(error);
49
+ return {
50
+ isValid: false,
51
+ value: null,
52
+ error
53
+ };
54
+ }
55
+ return {
56
+ isValid: defaultValue !== void 0,
57
+ value: defaultValue !== void 0 ? defaultValue : null,
58
+ error: defaultValue === void 0 ? "Missing parameter with no default" : void 0
59
+ };
60
+ }
61
+ const num = Number(value);
62
+ if (isNaN(num)) {
63
+ if (throwOnError) throw new BadRequestException(errorMessage);
64
+ return {
65
+ isValid: false,
66
+ value: null,
67
+ error: errorMessage
68
+ };
69
+ }
70
+ return {
71
+ isValid: true,
72
+ value: num
73
+ };
74
+ }
75
+ __name(parseNumberParam, "parseNumberParam");
76
+ function parseBooleanParam(value, options = {}) {
77
+ const { throwOnError = false, errorMessage = "Invalid boolean parameter", defaultValue, required = false } = options;
78
+ if (value === void 0) {
79
+ if (required) {
80
+ const error = `Required boolean parameter is missing`;
81
+ if (throwOnError) throw new BadRequestException(error);
82
+ return {
83
+ isValid: false,
84
+ value: null,
85
+ error
86
+ };
87
+ }
88
+ return {
89
+ isValid: defaultValue !== void 0,
90
+ value: defaultValue !== void 0 ? defaultValue : null,
91
+ error: defaultValue === void 0 ? "Missing parameter with no default" : void 0
92
+ };
93
+ }
94
+ const lowercaseValue = value.toLowerCase();
95
+ if ([
96
+ "true",
97
+ "t",
98
+ "yes",
99
+ "y",
100
+ "1"
101
+ ].includes(lowercaseValue)) {
102
+ return {
103
+ isValid: true,
104
+ value: true
105
+ };
106
+ }
107
+ if ([
108
+ "false",
109
+ "f",
110
+ "no",
111
+ "n",
112
+ "0"
113
+ ].includes(lowercaseValue)) {
114
+ return {
115
+ isValid: true,
116
+ value: false
117
+ };
118
+ }
119
+ if (throwOnError) throw new BadRequestException(errorMessage);
120
+ return {
121
+ isValid: false,
122
+ value: null,
123
+ error: errorMessage
124
+ };
125
+ }
126
+ __name(parseBooleanParam, "parseBooleanParam");
127
+ function extractPaginationParams(query, defaultPage = 1, defaultLimit = 20, maxLimitSize = 100) {
128
+ const pageParam = typeof query.page === "string" ? query.page : void 0;
129
+ const pageResult = parseNumberParam(pageParam, {
130
+ defaultValue: defaultPage
131
+ });
132
+ const limitParam = typeof query.limit === "string" ? query.limit : void 0;
133
+ const limitResult = parseNumberParam(limitParam, {
134
+ defaultValue: defaultLimit
135
+ });
136
+ const page = pageResult.value && pageResult.value > 0 ? pageResult.value : defaultPage;
137
+ const limit = limitResult.value && limitResult.value > 0 ? Math.min(limitResult.value, maxLimitSize) : defaultLimit;
138
+ return {
139
+ page,
140
+ limit
141
+ };
142
+ }
143
+ __name(extractPaginationParams, "extractPaginationParams");
144
+ function extractSortParams(query, allowedFields, defaultSort = {
145
+ sortBy: "createdAt",
146
+ sortOrder: "desc"
147
+ }) {
148
+ let sortBy = defaultSort.sortBy;
149
+ let sortOrder = defaultSort.sortOrder;
150
+ if (typeof query.sortBy === "string") {
151
+ sortBy = query.sortBy;
152
+ } else if (Array.isArray(query.sortBy)) {
153
+ sortBy = query.sortBy[0];
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;
169
+ }
170
+ return {
171
+ sortBy,
172
+ sortOrder
173
+ };
174
+ }
175
+ __name(extractSortParams, "extractSortParams");
176
+ function extractFilterParams(query, allowedFilters) {
177
+ const filters = {};
178
+ for (const field of allowedFilters) {
179
+ if (field in query) {
180
+ filters[field] = query[field];
181
+ }
182
+ }
183
+ return filters;
184
+ }
185
+ __name(extractFilterParams, "extractFilterParams");
186
+
187
+ // src/types/api-response.ts
188
+ var SortDirection = /* @__PURE__ */ (function(SortDirection2) {
189
+ SortDirection2["ASC"] = "asc";
190
+ SortDirection2["DESC"] = "desc";
191
+ return SortDirection2;
192
+ })({});
193
+
194
+ export { SortDirection, extractFilterParams, extractPaginationParams, extractSortParams, getPaginationParams, parseBooleanParam, parseNumberParam };
@@ -0,0 +1,109 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { Request } from 'express';
26
+ import type { WithPagination } from '@catbee/utils/response';
27
+ /**
28
+ * Options for parsing and validating request parameters.
29
+ */
30
+ export interface ValidationOptions {
31
+ /** Whether to throw an error on validation failure */
32
+ throwOnError?: boolean;
33
+ /** Custom error message for validation failures */
34
+ errorMessage?: string;
35
+ /** Default value to use if parameter is missing */
36
+ defaultValue?: any;
37
+ /** Whether the parameter is required */
38
+ required?: boolean;
39
+ }
40
+ /**
41
+ * Result of a parameter validation operation.
42
+ */
43
+ export interface ValidationResult<T> {
44
+ /** Whether validation was successful */
45
+ isValid: boolean;
46
+ /** The validated and potentially transformed value */
47
+ value: T | null;
48
+ /** Error message if validation failed */
49
+ error?: string;
50
+ }
51
+ /**
52
+ * Extracts pagination, sorting, and optional search params from a request.
53
+ *
54
+ * @param req - Express request
55
+ * @returns Object containing validated pagination, sorting, and additional query params
56
+ */
57
+ export declare const getPaginationParams: <T = {}>(req: Request) => WithPagination<T>;
58
+ /**
59
+ * Safely parses a string parameter to a number.
60
+ *
61
+ * @param value - String value to parse
62
+ * @param options - Validation options
63
+ * @returns Validation result containing the parsed number or error
64
+ */
65
+ export declare function parseNumberParam(value: string | undefined, options?: ValidationOptions): ValidationResult<number>;
66
+ /**
67
+ * Safely parses a string parameter to a boolean.
68
+ *
69
+ * @param value - String value to parse
70
+ * @param options - Validation options
71
+ * @returns Validation result containing the parsed boolean or error
72
+ */
73
+ export declare function parseBooleanParam(value: string | undefined, options?: ValidationOptions): ValidationResult<boolean>;
74
+ /**
75
+ * Extracts pagination parameters from request query parameters.
76
+ *
77
+ * @param query - Query parameters object
78
+ * @param defaultPage - Default page number if not specified (defaults to 1)
79
+ * @param defaultLimit - Default per page size if not specified (defaults to 20)
80
+ * @param maxLimitSize - Maximum allowed per page size (defaults to 100)
81
+ * @returns Object containing validated page and limit
82
+ */
83
+ export declare function extractPaginationParams(query: Record<string, string | string[]>, defaultPage?: number, defaultLimit?: number, maxLimitSize?: number): {
84
+ page: number;
85
+ limit: number;
86
+ };
87
+ /**
88
+ * Extracts sorting parameters from request query parameters.
89
+ *
90
+ * @param query - Query parameters object
91
+ * @param allowedFields - Array of field names that are allowed to be sorted
92
+ * @param defaultSort - Default sort configuration if not specified
93
+ * @returns Object containing sort field and direction
94
+ */
95
+ export declare function extractSortParams(query: Record<string, string | string[]>, allowedFields: string[], defaultSort?: {
96
+ sortBy: string;
97
+ sortOrder: 'asc' | 'desc';
98
+ }): {
99
+ sortBy: string;
100
+ sortOrder: 'asc' | 'desc';
101
+ };
102
+ /**
103
+ * Extracts filter parameters from query parameters based on allowed filter fields.
104
+ *
105
+ * @param query - Query parameters object
106
+ * @param allowedFilters - Array of field names that are allowed to be used as filters
107
+ * @returns Object containing the filters as key-value pairs
108
+ */
109
+ export declare function extractFilterParams(query: Record<string, string | string[]>, allowedFilters: string[]): Record<string, string | string[]>;