@orion-js/helpers 3.11.15 → 4.0.0-alpha.3

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 (64) hide show
  1. package/dist/index.cjs +1018 -0
  2. package/dist/index.d.ts +362 -0
  3. package/dist/index.js +992 -0
  4. package/package.json +21 -15
  5. package/LICENSE +0 -21
  6. package/jest.config.js +0 -8
  7. package/lib/Errors/OrionError.d.ts +0 -39
  8. package/lib/Errors/OrionError.js +0 -23
  9. package/lib/Errors/OrionError.test.d.ts +0 -1
  10. package/lib/Errors/OrionError.test.js +0 -52
  11. package/lib/Errors/PermissionsError.d.ts +0 -37
  12. package/lib/Errors/PermissionsError.js +0 -57
  13. package/lib/Errors/PermissionsError.test.d.ts +0 -1
  14. package/lib/Errors/PermissionsError.test.js +0 -62
  15. package/lib/Errors/UserError.d.ts +0 -33
  16. package/lib/Errors/UserError.js +0 -54
  17. package/lib/Errors/UserError.test.d.ts +0 -1
  18. package/lib/Errors/UserError.test.js +0 -49
  19. package/lib/Errors/index.d.ts +0 -43
  20. package/lib/Errors/index.js +0 -57
  21. package/lib/Errors/index.test.d.ts +0 -1
  22. package/lib/Errors/index.test.js +0 -56
  23. package/lib/composeMiddlewares.d.ts +0 -6
  24. package/lib/composeMiddlewares.js +0 -44
  25. package/lib/createMap.d.ts +0 -17
  26. package/lib/createMap.js +0 -26
  27. package/lib/createMap.test.d.ts +0 -1
  28. package/lib/createMap.test.js +0 -74
  29. package/lib/createMapArray.d.ts +0 -22
  30. package/lib/createMapArray.js +0 -32
  31. package/lib/createMapArray.test.d.ts +0 -1
  32. package/lib/createMapArray.test.js +0 -101
  33. package/lib/generateId.d.ts +0 -6
  34. package/lib/generateId.js +0 -54
  35. package/lib/generateId.test.d.ts +0 -1
  36. package/lib/generateId.test.js +0 -11
  37. package/lib/generateUUID.d.ts +0 -2
  38. package/lib/generateUUID.js +0 -12
  39. package/lib/generateUUID.test.d.ts +0 -1
  40. package/lib/generateUUID.test.js +0 -15
  41. package/lib/hashObject.d.ts +0 -1
  42. package/lib/hashObject.js +0 -10
  43. package/lib/hashObject.test.d.ts +0 -1
  44. package/lib/hashObject.test.js +0 -30
  45. package/lib/index.d.ts +0 -13
  46. package/lib/index.js +0 -40
  47. package/lib/normalize.d.ts +0 -70
  48. package/lib/normalize.js +0 -111
  49. package/lib/normalize.test.d.ts +0 -1
  50. package/lib/normalize.test.js +0 -101
  51. package/lib/retries.d.ts +0 -23
  52. package/lib/retries.js +0 -45
  53. package/lib/retries.test.d.ts +0 -1
  54. package/lib/retries.test.js +0 -47
  55. package/lib/searchTokens.d.ts +0 -61
  56. package/lib/searchTokens.js +0 -78
  57. package/lib/searchTokens.test.d.ts +0 -1
  58. package/lib/searchTokens.test.js +0 -101
  59. package/lib/shortenMongoId.d.ts +0 -1
  60. package/lib/shortenMongoId.js +0 -10
  61. package/lib/sleep.d.ts +0 -5
  62. package/lib/sleep.js +0 -8
  63. package/tsconfig.json +0 -16
  64. package/yarn-error.log +0 -710
@@ -0,0 +1,362 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ /**
4
+ * Creates a timeout with a promise
5
+ */
6
+ declare const _default: (time: number) => Promise<void>;
7
+ declare function _default$1(object: any): string;
8
+ /**
9
+ * Returns a random ID
10
+ * @param charsCount length of the ID
11
+ * @param chars characters used to generate the ID
12
+ */
13
+ export function generateId(charsCount?: number, chars?: string): string;
14
+ /**
15
+ * Creates a map (object) from an array of items, using a specified property as the key.
16
+ *
17
+ * This utility transforms an array of objects into a lookup object/dictionary where
18
+ * each item in the array becomes a value in the map, indexed by the specified property.
19
+ * If multiple items have the same key value, only the last one will be preserved.
20
+ *
21
+ * @template T The type of items in the input array
22
+ * @param array - The input array of items to transform into a map
23
+ * @param key - The property name to use as keys in the resulting map (defaults to '_id')
24
+ * @returns A record object where keys are values of the specified property and values are the original items
25
+ *
26
+ * @example
27
+ * // Returns { '1': { id: 1, name: 'Item 1' }, '2': { id: 2, name: 'Item 2' } }
28
+ * createMap([{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }], 'id')
29
+ */
30
+ export function createMap<T>(array: Array<T>, key?: string): Record<string, T>;
31
+ /**
32
+ * Creates a grouped map from an array of items, using a specified property as the key.
33
+ *
34
+ * This utility transforms an array of objects into a lookup object/dictionary where
35
+ * each value is an array of items sharing the same key value. Unlike createMap,
36
+ * this function preserves all items with the same key by grouping them in arrays.
37
+ *
38
+ * @template T The type of items in the input array
39
+ * @param array - The input array of items to transform into a grouped map
40
+ * @param key - The property name to use as keys in the resulting map (defaults to '_id')
41
+ * @returns A record object where keys are values of the specified property and values are arrays of items
42
+ *
43
+ * @example
44
+ * // Returns { 'category1': [{ id: 1, category: 'category1' }, { id: 3, category: 'category1' }],
45
+ * // 'category2': [{ id: 2, category: 'category2' }] }
46
+ * createMapArray([
47
+ * { id: 1, category: 'category1' },
48
+ * { id: 2, category: 'category2' },
49
+ * { id: 3, category: 'category1' }
50
+ * ], 'category')
51
+ */
52
+ export function createMapArray<T>(array: Array<T>, key?: string): Record<string, Array<T>>;
53
+ /**
54
+ * Interface representing the standardized error information structure for Orion errors.
55
+ * This is used by the getInfo method to provide consistent error reporting.
56
+ */
57
+ export interface OrionErrorInformation {
58
+ /** The error code or identifier */
59
+ error: string;
60
+ /** Human-readable error message */
61
+ message: string;
62
+ /** Additional error metadata or context */
63
+ extra: any;
64
+ /** The sub-type of error. For example for permissions errors it could be 'read', 'write', 'admin' */
65
+ type?: string;
66
+ }
67
+ /**
68
+ * Base error class for all Orion-specific errors.
69
+ *
70
+ * This abstract class provides common properties and methods for all error types
71
+ * used in the Orion framework. It's extended by more specific error classes
72
+ * like UserError and PermissionsError.
73
+ *
74
+ * @property isOrionError - Flag indicating this is an Orion error (always true)
75
+ * @property isUserError - Flag indicating if this is a user-facing error
76
+ * @property isPermissionsError - Flag indicating if this is a permissions-related error
77
+ * @property code - Error code for identifying the error type
78
+ * @property extra - Additional error context or metadata
79
+ */
80
+ export declare class OrionError extends Error {
81
+ isOrionError: boolean;
82
+ isUserError: boolean;
83
+ isPermissionsError: boolean;
84
+ code: string;
85
+ extra: any;
86
+ /**
87
+ * Returns a standardized representation of the error information.
88
+ * @returns An object containing error details in a consistent format
89
+ */
90
+ getInfo: () => OrionErrorInformation;
91
+ }
92
+ /**
93
+ * Error class for permission-related errors in the Orion framework.
94
+ *
95
+ * PermissionsError represents authorization failures where a user or client
96
+ * attempts to perform an action they don't have permission to execute.
97
+ * This is used to distinguish security/permissions errors from other types
98
+ * of errors for proper error handling and user feedback.
99
+ *
100
+ * @extends OrionError
101
+ */
102
+ export declare class PermissionsError extends OrionError {
103
+ /**
104
+ * Creates a new PermissionsError instance.
105
+ *
106
+ * @param permissionErrorType - Identifies the specific permission that was violated
107
+ * (e.g., 'read', 'write', 'admin')
108
+ * @param extra - Additional error context or metadata. Can include a custom message
109
+ * via the message property.
110
+ *
111
+ * @example
112
+ * // Basic usage
113
+ * throw new PermissionsError('delete_document')
114
+ *
115
+ * @example
116
+ * // With custom message
117
+ * throw new PermissionsError('access_admin', { message: 'Admin access required' })
118
+ *
119
+ * @example
120
+ * // With additional context
121
+ * throw new PermissionsError('edit_user', {
122
+ * userId: 'user123',
123
+ * requiredRole: 'admin'
124
+ * })
125
+ */
126
+ constructor(permissionErrorType: any, extra?: any);
127
+ }
128
+ /**
129
+ * Error class for user-facing errors in the Orion framework.
130
+ *
131
+ * UserError is designed to represent errors that should be displayed to end users,
132
+ * as opposed to system errors or unexpected failures. These errors typically represent
133
+ * validation issues, business rule violations, or other expected error conditions.
134
+ *
135
+ * @extends OrionError
136
+ */
137
+ export declare class UserError extends OrionError {
138
+ /**
139
+ * Creates a new UserError instance.
140
+ *
141
+ * @param code - Error code identifier. If only one parameter is provided,
142
+ * this will be used as the message and code will default to 'error'.
143
+ * @param message - Human-readable error message. Optional if code is provided.
144
+ * @param extra - Additional error context or metadata.
145
+ *
146
+ * @example
147
+ * // Basic usage
148
+ * throw new UserError('invalid_input', 'The provided email is invalid')
149
+ *
150
+ * @example
151
+ * // Using only a message (code will be 'error')
152
+ * throw new UserError('Input validation failed')
153
+ *
154
+ * @example
155
+ * // With extra metadata
156
+ * throw new UserError('rate_limit', 'Too many requests', { maxRequests: 100 })
157
+ */
158
+ constructor(code: string, message?: string, extra?: any);
159
+ }
160
+ /**
161
+ * Type guard to check if an error is an OrionError
162
+ *
163
+ * @param error - Any error object to test
164
+ * @returns True if the error is an OrionError instance
165
+ *
166
+ * @example
167
+ * try {
168
+ * // some code that might throw
169
+ * } catch (error) {
170
+ * if (isOrionError(error)) {
171
+ * // Handle Orion-specific error
172
+ * console.log(error.code, error.getInfo())
173
+ * } else {
174
+ * // Handle general error
175
+ * }
176
+ * }
177
+ */
178
+ export declare function isOrionError(error: any): error is OrionError;
179
+ /**
180
+ * Type guard to check if an error is a UserError
181
+ *
182
+ * @param error - Any error object to test
183
+ * @returns True if the error is a UserError instance
184
+ */
185
+ export declare function isUserError(error: any): error is UserError;
186
+ /**
187
+ * Type guard to check if an error is a PermissionsError
188
+ *
189
+ * @param error - Any error object to test
190
+ * @returns True if the error is a PermissionsError instance
191
+ */
192
+ export declare function isPermissionsError(error: any): error is PermissionsError;
193
+ /**
194
+ * Compose `middleware` returning
195
+ * a fully valid middleware comprised
196
+ * of all those which are passed.
197
+ */
198
+ export declare function composeMiddlewares(middleware: any): (context: any, next?: any) => Promise<any>;
199
+ /**
200
+ * Executes an asynchronous function with automatic retries on failure.
201
+ *
202
+ * This utility attempts to execute the provided function and automatically
203
+ * retries if it fails, with a specified delay between attempts. It will
204
+ * continue retrying until either the function succeeds or the maximum
205
+ * number of retries is reached.
206
+ *
207
+ * @template TFunc Type of the function to execute (must return a Promise)
208
+ * @param fn - The asynchronous function to execute
209
+ * @param retries - The maximum number of retry attempts after the initial attempt
210
+ * @param timeout - The delay in milliseconds between retry attempts
211
+ * @returns A promise that resolves with the result of the function or rejects with the last error
212
+ *
213
+ * @example
214
+ * // Retry an API call up to 3 times with 1 second between attempts
215
+ * const result = await executeWithRetries(
216
+ * () => fetchDataFromApi(),
217
+ * 3,
218
+ * 1000
219
+ * );
220
+ */
221
+ export declare function executeWithRetries<TFunc extends () => Promise<any>>(fn: TFunc, retries: number, timeout: number): Promise<ReturnType<TFunc>>;
222
+ export declare function generateUUID(): any;
223
+ export declare function generateUUIDWithPrefix(prefix: string): string;
224
+ /**
225
+ * Removes diacritical marks (accents) from text without any other modifications.
226
+ * This is the most basic normalization function that others build upon.
227
+ *
228
+ * @param text - The input string to process
229
+ * @returns String with accents removed but otherwise unchanged
230
+ */
231
+ export declare function removeAccentsOnly(text: string): string;
232
+ /**
233
+ * Normalizes text by removing diacritical marks (accents) and trimming whitespace.
234
+ * Builds on removeAccentsOnly and adds whitespace trimming.
235
+ *
236
+ * @param text - The input string to normalize
237
+ * @returns Normalized string with accents removed and whitespace trimmed
238
+ */
239
+ export declare function removeAccentsAndTrim(text: string): string;
240
+ /**
241
+ * Normalizes text for search purposes by:
242
+ * - Removing diacritical marks (accents)
243
+ * - Converting to lowercase
244
+ * - Trimming whitespace
245
+ *
246
+ * Builds on removeAccentsAndTrim and adds lowercase conversion.
247
+ * Useful for case-insensitive and accent-insensitive text searching.
248
+ *
249
+ * @param text - The input string to normalize for search
250
+ * @returns Search-optimized string in lowercase with accents removed
251
+ */
252
+ export declare function normalizeForSearch(text: string): string;
253
+ /**
254
+ * Normalizes text for search purposes by:
255
+ * - Removing diacritical marks (accents)
256
+ * - Converting to lowercase
257
+ * - Trimming whitespace
258
+ * - Removing all spaces
259
+ *
260
+ * Builds on normalizeForSearch and removes all whitespace.
261
+ * Useful for compact search indexes or when spaces should be ignored in searches.
262
+ *
263
+ * @param text - The input string to normalize for compact search
264
+ * @returns Compact search-optimized string with no spaces
265
+ */
266
+ export declare function normalizeForCompactSearch(text: string): string;
267
+ /**
268
+ * Normalizes text for search token processing by:
269
+ * - Removing diacritical marks (accents)
270
+ * - Converting to lowercase
271
+ * - Trimming whitespace
272
+ * - Replacing all non-alphanumeric characters with spaces
273
+ *
274
+ * Builds on normalizeForSearch and replaces non-alphanumeric characters with spaces.
275
+ * Useful for tokenizing search terms where special characters should be treated as word separators.
276
+ *
277
+ * @param text - The input string to normalize for tokenized search
278
+ * @returns Search token string with only alphanumeric characters and spaces
279
+ */
280
+ export declare function normalizeForSearchToken(text: string): string;
281
+ /**
282
+ * Normalizes a string specifically for use as a file key (e.g., in S3 or other storage systems).
283
+ * Performs the following transformations:
284
+ * - Removes accents/diacritical marks
285
+ * - Replaces special characters with hyphens
286
+ * - Ensures only alphanumeric characters, hyphens, periods, and underscores remain
287
+ * - Replaces multiple consecutive hyphens with a single hyphen
288
+ * - Removes leading/trailing hyphens
289
+ *
290
+ * @param text - The input string to normalize for file key usage
291
+ * @returns A storage-safe string suitable for use as a file key
292
+ */
293
+ export declare function normalizeForFileKey(text: string): string;
294
+ /**
295
+ * Generates an array of search tokens from input text and optional metadata.
296
+ *
297
+ * This function processes text by:
298
+ * 1. Converting it to an array of strings (if not already)
299
+ * 2. Filtering out falsy values
300
+ * 3. Normalizing each string (removing accents, special characters)
301
+ * 4. Splitting by spaces to create individual tokens
302
+ * 5. Optionally adding metadata tokens in the format "_key:value"
303
+ *
304
+ * @param text - String or array of strings to tokenize
305
+ * @param meta - Optional metadata object where each key-value pair becomes a token
306
+ * @returns Array of normalized search tokens
307
+ *
308
+ * @example
309
+ * // Returns ['hello', 'world']
310
+ * getSearchTokens('Hello, World!')
311
+ *
312
+ * @example
313
+ * // Returns ['hello', 'world', '_id:123']
314
+ * getSearchTokens('Hello, World!', { id: '123' })
315
+ */
316
+ export declare function getSearchTokens(text: string[] | string, meta?: Record<string, string>): string[];
317
+ /**
318
+ * Interface for parameters used in generating search queries from tokens.
319
+ *
320
+ * @property filter - Optional string to filter search results
321
+ * @property [key: string] - Additional key-value pairs for metadata filtering
322
+ */
323
+ export interface SearchQueryForTokensParams {
324
+ filter?: string;
325
+ [key: string]: string;
326
+ }
327
+ /**
328
+ * Options for customizing the search query generation behavior.
329
+ * Currently empty but provided for future extensibility.
330
+ */
331
+ export type SearchQueryForTokensOptions = {};
332
+ /**
333
+ * Generates a MongoDB-compatible query object based on the provided parameters.
334
+ *
335
+ * This function:
336
+ * 1. Processes any filter text into RegExp tokens for prefix matching
337
+ * 2. Adds metadata filters based on additional properties in the params object
338
+ * 3. Returns a query object with the $all operator for MongoDB queries
339
+ *
340
+ * @param params - Parameters for generating the search query
341
+ * @param _options - Options for customizing search behavior (reserved for future use)
342
+ * @returns A MongoDB-compatible query object with format { $all: [...tokens] }
343
+ *
344
+ * @example
345
+ * // Returns { $all: [/^hello/, /^world/] }
346
+ * getSearchQueryForTokens({ filter: 'Hello World' })
347
+ *
348
+ * @example
349
+ * // Returns { $all: [/^search/, '_category:books'] }
350
+ * getSearchQueryForTokens({ filter: 'search', category: 'books' })
351
+ */
352
+ export declare function getSearchQueryForTokens(params?: SearchQueryForTokensParams, _options?: SearchQueryForTokensOptions): {
353
+ $all: (string | RegExp)[];
354
+ };
355
+ export declare function shortenMongoId(string: string): string;
356
+
357
+ export {
358
+ _default as sleep,
359
+ _default$1 as hashObject,
360
+ };
361
+
362
+ export {};