@zairakai/js-utils 1.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 +21 -0
- package/README.md +270 -0
- package/dist/arrays.cjs +210 -0
- package/dist/arrays.d.cts +119 -0
- package/dist/arrays.d.ts +119 -0
- package/dist/arrays.js +32 -0
- package/dist/chunk-27YHP2CK.js +407 -0
- package/dist/chunk-3WNRYKPG.js +37 -0
- package/dist/chunk-42CHLXT7.js +214 -0
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-7SXRFZBB.js +173 -0
- package/dist/chunk-F6RSTW65.js +156 -0
- package/dist/chunk-G7ZJ23DW.js +253 -0
- package/dist/chunk-IPP7PA6H.js +136 -0
- package/dist/chunk-LDSWHSRX.js +96 -0
- package/dist/chunk-TY75OOIQ.js +700 -0
- package/dist/chunk-W6JEMFAF.js +54 -0
- package/dist/chunk-XEJLBAXE.js +164 -0
- package/dist/chunk-Z7G3SIQH.js +270 -0
- package/dist/chunk-ZJPKS2MQ.js +101 -0
- package/dist/collections.cjs +797 -0
- package/dist/collections.d.cts +353 -0
- package/dist/collections.d.ts +353 -0
- package/dist/collections.js +17 -0
- package/dist/datetime.cjs +80 -0
- package/dist/datetime.d.cts +75 -0
- package/dist/datetime.d.ts +75 -0
- package/dist/datetime.js +24 -0
- package/dist/equals.cjs +121 -0
- package/dist/equals.d.cts +24 -0
- package/dist/equals.d.ts +24 -0
- package/dist/equals.js +8 -0
- package/dist/formatters.cjs +201 -0
- package/dist/formatters.d.cts +180 -0
- package/dist/formatters.d.ts +180 -0
- package/dist/formatters.js +48 -0
- package/dist/index.cjs +2906 -0
- package/dist/index.d.cts +120 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +348 -0
- package/dist/number.cjs +279 -0
- package/dist/number.d.cts +177 -0
- package/dist/number.d.ts +177 -0
- package/dist/number.js +10 -0
- package/dist/obj.cjs +427 -0
- package/dist/obj.d.cts +177 -0
- package/dist/obj.d.ts +177 -0
- package/dist/obj.js +12 -0
- package/dist/php-arrays.cjs +954 -0
- package/dist/php-arrays.d.cts +256 -0
- package/dist/php-arrays.d.ts +256 -0
- package/dist/php-arrays.js +70 -0
- package/dist/runtime.cjs +134 -0
- package/dist/runtime.d.cts +90 -0
- package/dist/runtime.d.ts +90 -0
- package/dist/runtime.js +24 -0
- package/dist/schemas.cjs +86 -0
- package/dist/schemas.d.cts +108 -0
- package/dist/schemas.d.ts +108 -0
- package/dist/schemas.js +22 -0
- package/dist/str.cjs +499 -0
- package/dist/str.d.cts +282 -0
- package/dist/str.d.ts +282 -0
- package/dist/str.js +11 -0
- package/dist/types.cjs +18 -0
- package/dist/types.d.cts +13 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/validator.cjs +251 -0
- package/dist/validator.d.cts +99 -0
- package/dist/validator.d.ts +99 -0
- package/dist/validator.js +11 -0
- package/dist/validators.cjs +217 -0
- package/dist/validators.d.cts +216 -0
- package/dist/validators.d.ts +216 -0
- package/dist/validators.js +64 -0
- package/package.json +180 -0
- package/src/arrays.ts +316 -0
- package/src/collections.ts +866 -0
- package/src/datetime.ts +103 -0
- package/src/equals.ts +134 -0
- package/src/formatters.ts +342 -0
- package/src/index.ts +36 -0
- package/src/number.ts +281 -0
- package/src/obj.ts +303 -0
- package/src/php-arrays.ts +445 -0
- package/src/pipe.ts +29 -0
- package/src/runtime.ts +194 -0
- package/src/schemas.ts +136 -0
- package/src/str.ts +438 -0
- package/src/types.ts +13 -0
- package/src/validator.ts +157 -0
- package/src/validators.ts +359 -0
package/dist/runtime.cjs
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/runtime.ts
|
|
21
|
+
var runtime_exports = {};
|
|
22
|
+
__export(runtime_exports, {
|
|
23
|
+
data_get: () => data_get,
|
|
24
|
+
data_set: () => data_set,
|
|
25
|
+
isRecord: () => isRecord,
|
|
26
|
+
optional: () => optional,
|
|
27
|
+
retry: () => retry,
|
|
28
|
+
tap: () => tap,
|
|
29
|
+
throw_if: () => throw_if,
|
|
30
|
+
throw_unless: () => throw_unless,
|
|
31
|
+
value: () => value,
|
|
32
|
+
when: () => when
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(runtime_exports);
|
|
35
|
+
function isRecord(value2) {
|
|
36
|
+
return null !== value2 && "object" === typeof value2 && !Array.isArray(value2);
|
|
37
|
+
}
|
|
38
|
+
var tap = (value2, callback) => {
|
|
39
|
+
callback(value2);
|
|
40
|
+
return value2;
|
|
41
|
+
};
|
|
42
|
+
var when = (condition, onTrue, onFalse) => {
|
|
43
|
+
const shouldExecute = "function" === typeof condition ? condition() : Boolean(condition);
|
|
44
|
+
if (shouldExecute) {
|
|
45
|
+
return onTrue();
|
|
46
|
+
} else if (onFalse) {
|
|
47
|
+
return onFalse();
|
|
48
|
+
}
|
|
49
|
+
return void 0;
|
|
50
|
+
};
|
|
51
|
+
var value = (val) => {
|
|
52
|
+
return "function" === typeof val ? val() : val;
|
|
53
|
+
};
|
|
54
|
+
var data_get = (target, key, defaultValue = null) => {
|
|
55
|
+
if (!isRecord(target) && !Array.isArray(target)) {
|
|
56
|
+
return defaultValue;
|
|
57
|
+
}
|
|
58
|
+
const parts = Array.isArray(key) ? key : key.split(".");
|
|
59
|
+
let result = target;
|
|
60
|
+
for (const part of parts) {
|
|
61
|
+
if (!isRecord(result) && !Array.isArray(result)) {
|
|
62
|
+
return defaultValue;
|
|
63
|
+
}
|
|
64
|
+
if (part in result) {
|
|
65
|
+
result = result[part];
|
|
66
|
+
} else {
|
|
67
|
+
return defaultValue;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
};
|
|
72
|
+
var data_set = (target, key, value2) => {
|
|
73
|
+
if (!isRecord(target) && !Array.isArray(target)) {
|
|
74
|
+
return target;
|
|
75
|
+
}
|
|
76
|
+
const parts = Array.isArray(key) ? key : key.split(".");
|
|
77
|
+
let current = target;
|
|
78
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
79
|
+
const part = parts[i];
|
|
80
|
+
if (!isRecord(current)) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
const currentRecord = current;
|
|
84
|
+
if (!(part in currentRecord) || !isRecord(currentRecord[part])) {
|
|
85
|
+
currentRecord[part] = {};
|
|
86
|
+
}
|
|
87
|
+
current = currentRecord[part];
|
|
88
|
+
}
|
|
89
|
+
if (isRecord(current)) {
|
|
90
|
+
;
|
|
91
|
+
current[parts[parts.length - 1]] = value2;
|
|
92
|
+
}
|
|
93
|
+
return target;
|
|
94
|
+
};
|
|
95
|
+
var optional = (value2) => {
|
|
96
|
+
return value2 ?? null;
|
|
97
|
+
};
|
|
98
|
+
var retry = async (times, callback, sleepMilliseconds = 0) => {
|
|
99
|
+
let lastError;
|
|
100
|
+
for (let attempt = 1; attempt <= times; attempt++) {
|
|
101
|
+
try {
|
|
102
|
+
return await callback(attempt);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
105
|
+
if (attempt < times && 0 < sleepMilliseconds) {
|
|
106
|
+
await new Promise((resolve) => setTimeout(resolve, sleepMilliseconds));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
throw lastError ?? new Error(`Retry failed after ${times} attempts`);
|
|
111
|
+
};
|
|
112
|
+
var throw_if = (condition, exception) => {
|
|
113
|
+
if ("function" === typeof condition ? condition() : Boolean(condition)) {
|
|
114
|
+
throw "string" === typeof exception ? new Error(exception) : exception;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var throw_unless = (condition, exception) => {
|
|
118
|
+
if (!("function" === typeof condition ? condition() : Boolean(condition))) {
|
|
119
|
+
throw "string" === typeof exception ? new Error(exception) : exception;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
123
|
+
0 && (module.exports = {
|
|
124
|
+
data_get,
|
|
125
|
+
data_set,
|
|
126
|
+
isRecord,
|
|
127
|
+
optional,
|
|
128
|
+
retry,
|
|
129
|
+
tap,
|
|
130
|
+
throw_if,
|
|
131
|
+
throw_unless,
|
|
132
|
+
value,
|
|
133
|
+
when
|
|
134
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { GenericRecord } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runtime utility functions inspired by Laravel
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Check if a value is a plain object (Record)
|
|
8
|
+
*
|
|
9
|
+
* @param {unknown} value The value to check
|
|
10
|
+
* @returns {value is GenericRecord} True if the value is a plain object
|
|
11
|
+
*/
|
|
12
|
+
declare function isRecord(value: unknown): value is GenericRecord;
|
|
13
|
+
/**
|
|
14
|
+
* Execute a callback and return the given value
|
|
15
|
+
* Useful for side effects in a chain
|
|
16
|
+
*
|
|
17
|
+
* @param {T} value The value to pass to the callback
|
|
18
|
+
* @param {(value: T) => void} callback The callback to execute
|
|
19
|
+
* @returns {T} The original value
|
|
20
|
+
*/
|
|
21
|
+
declare const tap: <T>(value: T, callback: (value: T) => void) => T;
|
|
22
|
+
/**
|
|
23
|
+
* Conditionally execute a callback
|
|
24
|
+
*
|
|
25
|
+
* @param {unknown} condition The condition to check (value or zero-arg function returning boolean)
|
|
26
|
+
* @param {() => R} onTrue The callback to execute if the condition is truthy
|
|
27
|
+
* @param {() => R} [onFalse] The callback to execute if the condition is falsy
|
|
28
|
+
* @returns {R | undefined} The result of the executed callback or undefined
|
|
29
|
+
*/
|
|
30
|
+
declare const when: <R>(condition: unknown, onTrue: () => R, onFalse?: () => R) => R | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Return the first argument if it's truthy, otherwise return the second
|
|
33
|
+
*
|
|
34
|
+
* @param {T | (() => T)} val The value or a function that returns the value
|
|
35
|
+
* @returns {T} The resolved value
|
|
36
|
+
*/
|
|
37
|
+
declare const value: <T>(val: T | (() => T)) => T;
|
|
38
|
+
/**
|
|
39
|
+
* Get an item from an array or object using dot notation
|
|
40
|
+
*
|
|
41
|
+
* @param {unknown} target The object or array to search
|
|
42
|
+
* @param {string | string[]} key The dot-notated key or an array of keys
|
|
43
|
+
* @param {unknown} [defaultValue=null] The default value to return if the key is not found
|
|
44
|
+
* @returns {unknown} The found value or the default value
|
|
45
|
+
*/
|
|
46
|
+
declare const data_get: (target: unknown, key: string | string[], defaultValue?: unknown) => unknown;
|
|
47
|
+
/**
|
|
48
|
+
* Set an item in an array or object using dot notation
|
|
49
|
+
*
|
|
50
|
+
* @param {T} target The object or array to modify
|
|
51
|
+
* @param {string | string[]} key The dot-notated key or an array of keys
|
|
52
|
+
* @param {unknown} value The value to set
|
|
53
|
+
* @returns {T} The modified target
|
|
54
|
+
*/
|
|
55
|
+
declare const data_set: <T>(target: T, key: string | string[], value: unknown) => T;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the object if it exists, otherwise returns null (similar to optional() helper)
|
|
58
|
+
*
|
|
59
|
+
* @param {T} value The value to return if it exists
|
|
60
|
+
* @returns {T | null} The value or null
|
|
61
|
+
*/
|
|
62
|
+
declare const optional: <T>(value: T) => T | null;
|
|
63
|
+
/**
|
|
64
|
+
* Execute a callback with retry logic
|
|
65
|
+
*
|
|
66
|
+
* @param {number} times The number of times to retry
|
|
67
|
+
* @param {(attempt: number) => Promise<T> | T} callback The callback to execute
|
|
68
|
+
* @param {number} [sleepMilliseconds=0] The amount of time to wait between retries
|
|
69
|
+
* @returns {Promise<T>} The result of the callback
|
|
70
|
+
* @throws {Error} If all attempts fail
|
|
71
|
+
*/
|
|
72
|
+
declare const retry: <T>(times: number, callback: (attempt: number) => Promise<T> | T, sleepMilliseconds?: number) => Promise<T>;
|
|
73
|
+
/**
|
|
74
|
+
* Throw an error if a condition is met
|
|
75
|
+
*
|
|
76
|
+
* @param {unknown} condition The condition to check
|
|
77
|
+
* @param {Error | string} exception The error to throw or the error message
|
|
78
|
+
* @throws {Error} If the condition is met
|
|
79
|
+
*/
|
|
80
|
+
declare const throw_if: (condition: unknown, exception: Error | string) => void;
|
|
81
|
+
/**
|
|
82
|
+
* Throw an error unless a condition is met
|
|
83
|
+
*
|
|
84
|
+
* @param {unknown} condition The condition to check
|
|
85
|
+
* @param {Error | string} exception The error to throw or the error message
|
|
86
|
+
* @throws {Error} If the condition is not met
|
|
87
|
+
*/
|
|
88
|
+
declare const throw_unless: (condition: unknown, exception: Error | string) => void;
|
|
89
|
+
|
|
90
|
+
export { data_get, data_set, isRecord, optional, retry, tap, throw_if, throw_unless, value, when };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { GenericRecord } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runtime utility functions inspired by Laravel
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Check if a value is a plain object (Record)
|
|
8
|
+
*
|
|
9
|
+
* @param {unknown} value The value to check
|
|
10
|
+
* @returns {value is GenericRecord} True if the value is a plain object
|
|
11
|
+
*/
|
|
12
|
+
declare function isRecord(value: unknown): value is GenericRecord;
|
|
13
|
+
/**
|
|
14
|
+
* Execute a callback and return the given value
|
|
15
|
+
* Useful for side effects in a chain
|
|
16
|
+
*
|
|
17
|
+
* @param {T} value The value to pass to the callback
|
|
18
|
+
* @param {(value: T) => void} callback The callback to execute
|
|
19
|
+
* @returns {T} The original value
|
|
20
|
+
*/
|
|
21
|
+
declare const tap: <T>(value: T, callback: (value: T) => void) => T;
|
|
22
|
+
/**
|
|
23
|
+
* Conditionally execute a callback
|
|
24
|
+
*
|
|
25
|
+
* @param {unknown} condition The condition to check (value or zero-arg function returning boolean)
|
|
26
|
+
* @param {() => R} onTrue The callback to execute if the condition is truthy
|
|
27
|
+
* @param {() => R} [onFalse] The callback to execute if the condition is falsy
|
|
28
|
+
* @returns {R | undefined} The result of the executed callback or undefined
|
|
29
|
+
*/
|
|
30
|
+
declare const when: <R>(condition: unknown, onTrue: () => R, onFalse?: () => R) => R | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Return the first argument if it's truthy, otherwise return the second
|
|
33
|
+
*
|
|
34
|
+
* @param {T | (() => T)} val The value or a function that returns the value
|
|
35
|
+
* @returns {T} The resolved value
|
|
36
|
+
*/
|
|
37
|
+
declare const value: <T>(val: T | (() => T)) => T;
|
|
38
|
+
/**
|
|
39
|
+
* Get an item from an array or object using dot notation
|
|
40
|
+
*
|
|
41
|
+
* @param {unknown} target The object or array to search
|
|
42
|
+
* @param {string | string[]} key The dot-notated key or an array of keys
|
|
43
|
+
* @param {unknown} [defaultValue=null] The default value to return if the key is not found
|
|
44
|
+
* @returns {unknown} The found value or the default value
|
|
45
|
+
*/
|
|
46
|
+
declare const data_get: (target: unknown, key: string | string[], defaultValue?: unknown) => unknown;
|
|
47
|
+
/**
|
|
48
|
+
* Set an item in an array or object using dot notation
|
|
49
|
+
*
|
|
50
|
+
* @param {T} target The object or array to modify
|
|
51
|
+
* @param {string | string[]} key The dot-notated key or an array of keys
|
|
52
|
+
* @param {unknown} value The value to set
|
|
53
|
+
* @returns {T} The modified target
|
|
54
|
+
*/
|
|
55
|
+
declare const data_set: <T>(target: T, key: string | string[], value: unknown) => T;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the object if it exists, otherwise returns null (similar to optional() helper)
|
|
58
|
+
*
|
|
59
|
+
* @param {T} value The value to return if it exists
|
|
60
|
+
* @returns {T | null} The value or null
|
|
61
|
+
*/
|
|
62
|
+
declare const optional: <T>(value: T) => T | null;
|
|
63
|
+
/**
|
|
64
|
+
* Execute a callback with retry logic
|
|
65
|
+
*
|
|
66
|
+
* @param {number} times The number of times to retry
|
|
67
|
+
* @param {(attempt: number) => Promise<T> | T} callback The callback to execute
|
|
68
|
+
* @param {number} [sleepMilliseconds=0] The amount of time to wait between retries
|
|
69
|
+
* @returns {Promise<T>} The result of the callback
|
|
70
|
+
* @throws {Error} If all attempts fail
|
|
71
|
+
*/
|
|
72
|
+
declare const retry: <T>(times: number, callback: (attempt: number) => Promise<T> | T, sleepMilliseconds?: number) => Promise<T>;
|
|
73
|
+
/**
|
|
74
|
+
* Throw an error if a condition is met
|
|
75
|
+
*
|
|
76
|
+
* @param {unknown} condition The condition to check
|
|
77
|
+
* @param {Error | string} exception The error to throw or the error message
|
|
78
|
+
* @throws {Error} If the condition is met
|
|
79
|
+
*/
|
|
80
|
+
declare const throw_if: (condition: unknown, exception: Error | string) => void;
|
|
81
|
+
/**
|
|
82
|
+
* Throw an error unless a condition is met
|
|
83
|
+
*
|
|
84
|
+
* @param {unknown} condition The condition to check
|
|
85
|
+
* @param {Error | string} exception The error to throw or the error message
|
|
86
|
+
* @throws {Error} If the condition is not met
|
|
87
|
+
*/
|
|
88
|
+
declare const throw_unless: (condition: unknown, exception: Error | string) => void;
|
|
89
|
+
|
|
90
|
+
export { data_get, data_set, isRecord, optional, retry, tap, throw_if, throw_unless, value, when };
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {
|
|
2
|
+
data_get,
|
|
3
|
+
data_set,
|
|
4
|
+
isRecord,
|
|
5
|
+
optional,
|
|
6
|
+
retry,
|
|
7
|
+
tap,
|
|
8
|
+
throw_if,
|
|
9
|
+
throw_unless,
|
|
10
|
+
value,
|
|
11
|
+
when
|
|
12
|
+
} from "./chunk-ZJPKS2MQ.js";
|
|
13
|
+
export {
|
|
14
|
+
data_get,
|
|
15
|
+
data_set,
|
|
16
|
+
isRecord,
|
|
17
|
+
optional,
|
|
18
|
+
retry,
|
|
19
|
+
tap,
|
|
20
|
+
throw_if,
|
|
21
|
+
throw_unless,
|
|
22
|
+
value,
|
|
23
|
+
when
|
|
24
|
+
};
|
package/dist/schemas.cjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/schemas.ts
|
|
21
|
+
var schemas_exports = {};
|
|
22
|
+
__export(schemas_exports, {
|
|
23
|
+
ApiResponseSchema: () => ApiResponseSchema,
|
|
24
|
+
DateSchema: () => DateSchema,
|
|
25
|
+
EmailSchema: () => EmailSchema,
|
|
26
|
+
PaginatedResponseSchema: () => PaginatedResponseSchema,
|
|
27
|
+
PaginationSchema: () => PaginationSchema,
|
|
28
|
+
PhoneSchema: () => PhoneSchema,
|
|
29
|
+
UrlSchema: () => UrlSchema,
|
|
30
|
+
safeValidateSchema: () => safeValidateSchema,
|
|
31
|
+
validateSchema: () => validateSchema
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(schemas_exports);
|
|
34
|
+
var import_zod = require("zod");
|
|
35
|
+
var EmailSchema = import_zod.z.string().email("Invalid email format").min(1, "Email is required");
|
|
36
|
+
var PhoneSchema = import_zod.z.string().regex(/^\+?[1-9]\d{1,14}$/, "Invalid phone number format");
|
|
37
|
+
var UrlSchema = import_zod.z.string().url("Invalid URL format");
|
|
38
|
+
var DateSchema = import_zod.z.union([
|
|
39
|
+
import_zod.z.string().datetime(),
|
|
40
|
+
import_zod.z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Invalid date format (YYYY-MM-DD)"),
|
|
41
|
+
import_zod.z.date()
|
|
42
|
+
]);
|
|
43
|
+
var ApiResponseSchema = (dataSchema) => import_zod.z.object({
|
|
44
|
+
success: import_zod.z.boolean(),
|
|
45
|
+
data: dataSchema.optional(),
|
|
46
|
+
message: import_zod.z.string().optional(),
|
|
47
|
+
errors: import_zod.z.array(import_zod.z.string()).optional()
|
|
48
|
+
});
|
|
49
|
+
var PaginationSchema = import_zod.z.object({
|
|
50
|
+
page: import_zod.z.number().int().positive().default(1),
|
|
51
|
+
perPage: import_zod.z.number().int().min(1).max(100).default(15),
|
|
52
|
+
total: import_zod.z.number().int().nonnegative(),
|
|
53
|
+
lastPage: import_zod.z.number().int().positive()
|
|
54
|
+
});
|
|
55
|
+
var PaginatedResponseSchema = (itemSchema) => import_zod.z.object({
|
|
56
|
+
data: import_zod.z.array(itemSchema),
|
|
57
|
+
pagination: PaginationSchema
|
|
58
|
+
});
|
|
59
|
+
var validateSchema = (schema, data) => {
|
|
60
|
+
const result = schema.safeParse(data);
|
|
61
|
+
if (!result.success) {
|
|
62
|
+
const errors = result.error.issues.map((err) => `${err.path.join(".")}: ${err.message}`);
|
|
63
|
+
throw new Error(`Validation failed: ${errors.join(", ")}`);
|
|
64
|
+
}
|
|
65
|
+
return result.data;
|
|
66
|
+
};
|
|
67
|
+
var safeValidateSchema = (schema, data) => {
|
|
68
|
+
const result = schema.safeParse(data);
|
|
69
|
+
if (result.success) {
|
|
70
|
+
return { success: true, data: result.data };
|
|
71
|
+
}
|
|
72
|
+
const errors = result.error.issues.map((err) => `${err.path.join(".")}: ${err.message}`);
|
|
73
|
+
return { success: false, errors };
|
|
74
|
+
};
|
|
75
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
76
|
+
0 && (module.exports = {
|
|
77
|
+
ApiResponseSchema,
|
|
78
|
+
DateSchema,
|
|
79
|
+
EmailSchema,
|
|
80
|
+
PaginatedResponseSchema,
|
|
81
|
+
PaginationSchema,
|
|
82
|
+
PhoneSchema,
|
|
83
|
+
UrlSchema,
|
|
84
|
+
safeValidateSchema,
|
|
85
|
+
validateSchema
|
|
86
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runtime type validation schemas using Zod
|
|
5
|
+
* Provides runtime type checking that persists after TypeScript compilation
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Email validation schema
|
|
10
|
+
*/
|
|
11
|
+
declare const EmailSchema: z.ZodString;
|
|
12
|
+
/**
|
|
13
|
+
* Phone validation schema (E.164 format)
|
|
14
|
+
*/
|
|
15
|
+
declare const PhoneSchema: z.ZodString;
|
|
16
|
+
/**
|
|
17
|
+
* URL validation schema
|
|
18
|
+
*/
|
|
19
|
+
declare const UrlSchema: z.ZodString;
|
|
20
|
+
/**
|
|
21
|
+
* Date validation schema (ISO datetime, YYYY-MM-DD, or Date object)
|
|
22
|
+
*/
|
|
23
|
+
declare const DateSchema: z.ZodUnion<readonly [z.ZodString, z.ZodString, z.ZodDate]>;
|
|
24
|
+
/**
|
|
25
|
+
* API Response schema factory
|
|
26
|
+
*
|
|
27
|
+
* @param {T} dataSchema The schema for the data property
|
|
28
|
+
* @returns {z.ZodObject} The API response schema
|
|
29
|
+
*/
|
|
30
|
+
declare const ApiResponseSchema: <T extends z.ZodTypeAny>(dataSchema: T) => z.ZodObject<{
|
|
31
|
+
success: z.ZodBoolean;
|
|
32
|
+
data: z.ZodOptional<T>;
|
|
33
|
+
message: z.ZodOptional<z.ZodString>;
|
|
34
|
+
errors: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
35
|
+
}, z.core.$strip>;
|
|
36
|
+
/**
|
|
37
|
+
* Pagination schema
|
|
38
|
+
*/
|
|
39
|
+
declare const PaginationSchema: z.ZodObject<{
|
|
40
|
+
page: z.ZodDefault<z.ZodNumber>;
|
|
41
|
+
perPage: z.ZodDefault<z.ZodNumber>;
|
|
42
|
+
total: z.ZodNumber;
|
|
43
|
+
lastPage: z.ZodNumber;
|
|
44
|
+
}, z.core.$strip>;
|
|
45
|
+
/**
|
|
46
|
+
* Paginated response schema factory
|
|
47
|
+
*
|
|
48
|
+
* @param {T} itemSchema The schema for individual items in the data array
|
|
49
|
+
* @returns {z.ZodObject} The paginated response schema
|
|
50
|
+
*/
|
|
51
|
+
declare const PaginatedResponseSchema: <T extends z.ZodTypeAny>(itemSchema: T) => z.ZodObject<{
|
|
52
|
+
data: z.ZodArray<T>;
|
|
53
|
+
pagination: z.ZodObject<{
|
|
54
|
+
page: z.ZodDefault<z.ZodNumber>;
|
|
55
|
+
perPage: z.ZodDefault<z.ZodNumber>;
|
|
56
|
+
total: z.ZodNumber;
|
|
57
|
+
lastPage: z.ZodNumber;
|
|
58
|
+
}, z.core.$strip>;
|
|
59
|
+
}, z.core.$strip>;
|
|
60
|
+
/**
|
|
61
|
+
* Type inferred from EmailSchema
|
|
62
|
+
*/
|
|
63
|
+
type Email = z.infer<typeof EmailSchema>;
|
|
64
|
+
/**
|
|
65
|
+
* Type inferred from PhoneSchema
|
|
66
|
+
*/
|
|
67
|
+
type Phone = z.infer<typeof PhoneSchema>;
|
|
68
|
+
/**
|
|
69
|
+
* Type inferred from UrlSchema
|
|
70
|
+
*/
|
|
71
|
+
type Url = z.infer<typeof UrlSchema>;
|
|
72
|
+
/**
|
|
73
|
+
* Type inferred from ApiResponseSchema
|
|
74
|
+
*/
|
|
75
|
+
type ApiResponse<T> = z.infer<ReturnType<typeof ApiResponseSchema<z.ZodType<T>>>>;
|
|
76
|
+
/**
|
|
77
|
+
* Type inferred from PaginationSchema
|
|
78
|
+
*/
|
|
79
|
+
type Pagination = z.infer<typeof PaginationSchema>;
|
|
80
|
+
/**
|
|
81
|
+
* Type inferred from PaginatedResponseSchema
|
|
82
|
+
*/
|
|
83
|
+
type PaginatedResponse<T> = z.infer<ReturnType<typeof PaginatedResponseSchema<z.ZodType<T>>>>;
|
|
84
|
+
/**
|
|
85
|
+
* Validates data against a schema and throws detailed errors
|
|
86
|
+
*
|
|
87
|
+
* @param {z.ZodSchema<T>} schema The schema to validate against
|
|
88
|
+
* @param {unknown} data The data to validate
|
|
89
|
+
* @returns {T} The validated data
|
|
90
|
+
* @throws {Error} If validation fails
|
|
91
|
+
*/
|
|
92
|
+
declare const validateSchema: <T>(schema: z.ZodSchema<T>, data: unknown) => T;
|
|
93
|
+
/**
|
|
94
|
+
* Validates data against a schema and returns result with success flag
|
|
95
|
+
*
|
|
96
|
+
* @param {z.ZodSchema<T>} schema The schema to validate against
|
|
97
|
+
* @param {unknown} data The data to validate
|
|
98
|
+
* @returns {{ success: true; data: T } | { success: false; errors: string[] }} The validation result
|
|
99
|
+
*/
|
|
100
|
+
declare const safeValidateSchema: <T>(schema: z.ZodSchema<T>, data: unknown) => {
|
|
101
|
+
success: true;
|
|
102
|
+
data: T;
|
|
103
|
+
} | {
|
|
104
|
+
success: false;
|
|
105
|
+
errors: string[];
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export { type ApiResponse, ApiResponseSchema, DateSchema, type Email, EmailSchema, type PaginatedResponse, PaginatedResponseSchema, type Pagination, PaginationSchema, type Phone, PhoneSchema, type Url, UrlSchema, safeValidateSchema, validateSchema };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runtime type validation schemas using Zod
|
|
5
|
+
* Provides runtime type checking that persists after TypeScript compilation
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Email validation schema
|
|
10
|
+
*/
|
|
11
|
+
declare const EmailSchema: z.ZodString;
|
|
12
|
+
/**
|
|
13
|
+
* Phone validation schema (E.164 format)
|
|
14
|
+
*/
|
|
15
|
+
declare const PhoneSchema: z.ZodString;
|
|
16
|
+
/**
|
|
17
|
+
* URL validation schema
|
|
18
|
+
*/
|
|
19
|
+
declare const UrlSchema: z.ZodString;
|
|
20
|
+
/**
|
|
21
|
+
* Date validation schema (ISO datetime, YYYY-MM-DD, or Date object)
|
|
22
|
+
*/
|
|
23
|
+
declare const DateSchema: z.ZodUnion<readonly [z.ZodString, z.ZodString, z.ZodDate]>;
|
|
24
|
+
/**
|
|
25
|
+
* API Response schema factory
|
|
26
|
+
*
|
|
27
|
+
* @param {T} dataSchema The schema for the data property
|
|
28
|
+
* @returns {z.ZodObject} The API response schema
|
|
29
|
+
*/
|
|
30
|
+
declare const ApiResponseSchema: <T extends z.ZodTypeAny>(dataSchema: T) => z.ZodObject<{
|
|
31
|
+
success: z.ZodBoolean;
|
|
32
|
+
data: z.ZodOptional<T>;
|
|
33
|
+
message: z.ZodOptional<z.ZodString>;
|
|
34
|
+
errors: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
35
|
+
}, z.core.$strip>;
|
|
36
|
+
/**
|
|
37
|
+
* Pagination schema
|
|
38
|
+
*/
|
|
39
|
+
declare const PaginationSchema: z.ZodObject<{
|
|
40
|
+
page: z.ZodDefault<z.ZodNumber>;
|
|
41
|
+
perPage: z.ZodDefault<z.ZodNumber>;
|
|
42
|
+
total: z.ZodNumber;
|
|
43
|
+
lastPage: z.ZodNumber;
|
|
44
|
+
}, z.core.$strip>;
|
|
45
|
+
/**
|
|
46
|
+
* Paginated response schema factory
|
|
47
|
+
*
|
|
48
|
+
* @param {T} itemSchema The schema for individual items in the data array
|
|
49
|
+
* @returns {z.ZodObject} The paginated response schema
|
|
50
|
+
*/
|
|
51
|
+
declare const PaginatedResponseSchema: <T extends z.ZodTypeAny>(itemSchema: T) => z.ZodObject<{
|
|
52
|
+
data: z.ZodArray<T>;
|
|
53
|
+
pagination: z.ZodObject<{
|
|
54
|
+
page: z.ZodDefault<z.ZodNumber>;
|
|
55
|
+
perPage: z.ZodDefault<z.ZodNumber>;
|
|
56
|
+
total: z.ZodNumber;
|
|
57
|
+
lastPage: z.ZodNumber;
|
|
58
|
+
}, z.core.$strip>;
|
|
59
|
+
}, z.core.$strip>;
|
|
60
|
+
/**
|
|
61
|
+
* Type inferred from EmailSchema
|
|
62
|
+
*/
|
|
63
|
+
type Email = z.infer<typeof EmailSchema>;
|
|
64
|
+
/**
|
|
65
|
+
* Type inferred from PhoneSchema
|
|
66
|
+
*/
|
|
67
|
+
type Phone = z.infer<typeof PhoneSchema>;
|
|
68
|
+
/**
|
|
69
|
+
* Type inferred from UrlSchema
|
|
70
|
+
*/
|
|
71
|
+
type Url = z.infer<typeof UrlSchema>;
|
|
72
|
+
/**
|
|
73
|
+
* Type inferred from ApiResponseSchema
|
|
74
|
+
*/
|
|
75
|
+
type ApiResponse<T> = z.infer<ReturnType<typeof ApiResponseSchema<z.ZodType<T>>>>;
|
|
76
|
+
/**
|
|
77
|
+
* Type inferred from PaginationSchema
|
|
78
|
+
*/
|
|
79
|
+
type Pagination = z.infer<typeof PaginationSchema>;
|
|
80
|
+
/**
|
|
81
|
+
* Type inferred from PaginatedResponseSchema
|
|
82
|
+
*/
|
|
83
|
+
type PaginatedResponse<T> = z.infer<ReturnType<typeof PaginatedResponseSchema<z.ZodType<T>>>>;
|
|
84
|
+
/**
|
|
85
|
+
* Validates data against a schema and throws detailed errors
|
|
86
|
+
*
|
|
87
|
+
* @param {z.ZodSchema<T>} schema The schema to validate against
|
|
88
|
+
* @param {unknown} data The data to validate
|
|
89
|
+
* @returns {T} The validated data
|
|
90
|
+
* @throws {Error} If validation fails
|
|
91
|
+
*/
|
|
92
|
+
declare const validateSchema: <T>(schema: z.ZodSchema<T>, data: unknown) => T;
|
|
93
|
+
/**
|
|
94
|
+
* Validates data against a schema and returns result with success flag
|
|
95
|
+
*
|
|
96
|
+
* @param {z.ZodSchema<T>} schema The schema to validate against
|
|
97
|
+
* @param {unknown} data The data to validate
|
|
98
|
+
* @returns {{ success: true; data: T } | { success: false; errors: string[] }} The validation result
|
|
99
|
+
*/
|
|
100
|
+
declare const safeValidateSchema: <T>(schema: z.ZodSchema<T>, data: unknown) => {
|
|
101
|
+
success: true;
|
|
102
|
+
data: T;
|
|
103
|
+
} | {
|
|
104
|
+
success: false;
|
|
105
|
+
errors: string[];
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export { type ApiResponse, ApiResponseSchema, DateSchema, type Email, EmailSchema, type PaginatedResponse, PaginatedResponseSchema, type Pagination, PaginationSchema, type Phone, PhoneSchema, type Url, UrlSchema, safeValidateSchema, validateSchema };
|
package/dist/schemas.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ApiResponseSchema,
|
|
3
|
+
DateSchema,
|
|
4
|
+
EmailSchema,
|
|
5
|
+
PaginatedResponseSchema,
|
|
6
|
+
PaginationSchema,
|
|
7
|
+
PhoneSchema,
|
|
8
|
+
UrlSchema,
|
|
9
|
+
safeValidateSchema,
|
|
10
|
+
validateSchema
|
|
11
|
+
} from "./chunk-W6JEMFAF.js";
|
|
12
|
+
export {
|
|
13
|
+
ApiResponseSchema,
|
|
14
|
+
DateSchema,
|
|
15
|
+
EmailSchema,
|
|
16
|
+
PaginatedResponseSchema,
|
|
17
|
+
PaginationSchema,
|
|
18
|
+
PhoneSchema,
|
|
19
|
+
UrlSchema,
|
|
20
|
+
safeValidateSchema,
|
|
21
|
+
validateSchema
|
|
22
|
+
};
|