@oxog/querystring 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/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/README.md +447 -0
- package/dist/builder.d.ts +42 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +216 -0
- package/dist/builder.js.map +1 -0
- package/dist/esm/builder.d.ts +42 -0
- package/dist/esm/builder.d.ts.map +1 -0
- package/dist/esm/builder.js.map +1 -0
- package/dist/esm/builder.mjs +212 -0
- package/dist/esm/index.d.ts +49 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.mjs +49 -0
- package/dist/esm/parser.d.ts +7 -0
- package/dist/esm/parser.d.ts.map +1 -0
- package/dist/esm/parser.js.map +1 -0
- package/dist/esm/parser.mjs +170 -0
- package/dist/esm/plugins.d.ts +25 -0
- package/dist/esm/plugins.d.ts.map +1 -0
- package/dist/esm/plugins.js.map +1 -0
- package/dist/esm/plugins.mjs +223 -0
- package/dist/esm/schema.d.ts +23 -0
- package/dist/esm/schema.d.ts.map +1 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/esm/schema.mjs +674 -0
- package/dist/esm/security.d.ts +14 -0
- package/dist/esm/security.d.ts.map +1 -0
- package/dist/esm/security.js.map +1 -0
- package/dist/esm/security.mjs +224 -0
- package/dist/esm/stringifier.d.ts +4 -0
- package/dist/esm/stringifier.d.ts.map +1 -0
- package/dist/esm/stringifier.js.map +1 -0
- package/dist/esm/stringifier.mjs +193 -0
- package/dist/esm/types/index.d.ts +184 -0
- package/dist/esm/types/index.d.ts.map +1 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/index.mjs +2 -0
- package/dist/esm/types/schema.d.ts +37 -0
- package/dist/esm/types/schema.d.ts.map +1 -0
- package/dist/esm/types/schema.js.map +1 -0
- package/dist/esm/types/schema.mjs +2 -0
- package/dist/esm/utils/array.d.ts +11 -0
- package/dist/esm/utils/array.d.ts.map +1 -0
- package/dist/esm/utils/array.js.map +1 -0
- package/dist/esm/utils/array.mjs +116 -0
- package/dist/esm/utils/encoding.d.ts +13 -0
- package/dist/esm/utils/encoding.d.ts.map +1 -0
- package/dist/esm/utils/encoding.js.map +1 -0
- package/dist/esm/utils/encoding.mjs +126 -0
- package/dist/esm/utils/object.d.ts +12 -0
- package/dist/esm/utils/object.d.ts.map +1 -0
- package/dist/esm/utils/object.js.map +1 -0
- package/dist/esm/utils/object.mjs +140 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +92 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +49 -0
- package/dist/parser.d.ts +7 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +174 -0
- package/dist/parser.js.map +1 -0
- package/dist/plugins.d.ts +25 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +229 -0
- package/dist/plugins.js.map +1 -0
- package/dist/schema.d.ts +23 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +680 -0
- package/dist/schema.js.map +1 -0
- package/dist/security.d.ts +14 -0
- package/dist/security.d.ts.map +1 -0
- package/dist/security.js +233 -0
- package/dist/security.js.map +1 -0
- package/dist/stringifier.d.ts +4 -0
- package/dist/stringifier.d.ts.map +1 -0
- package/dist/stringifier.js +197 -0
- package/dist/stringifier.js.map +1 -0
- package/dist/types/index.d.ts +184 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/schema.d.ts +37 -0
- package/dist/types/schema.d.ts.map +1 -0
- package/dist/types/schema.js +3 -0
- package/dist/types/schema.js.map +1 -0
- package/dist/utils/array.d.ts +11 -0
- package/dist/utils/array.d.ts.map +1 -0
- package/dist/utils/array.js +127 -0
- package/dist/utils/array.js.map +1 -0
- package/dist/utils/encoding.d.ts +13 -0
- package/dist/utils/encoding.d.ts.map +1 -0
- package/dist/utils/encoding.js +137 -0
- package/dist/utils/encoding.js.map +1 -0
- package/dist/utils/object.d.ts +12 -0
- package/dist/utils/object.d.ts.map +1 -0
- package/dist/utils/object.js +150 -0
- package/dist/utils/object.js.map +1 -0
- package/package.json +65 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.querystring = exports.createSecureParser = exports.secureStringify = exports.detectXSS = exports.hasPrototypePollution = exports.countKeys = exports.validateSecurity = exports.sanitizeInput = exports.createCustomPlugin = exports.normalizePlugin = exports.compressPlugin = exports.base64Plugin = exports.filterEmptyPlugin = exports.lowercaseKeysPlugin = exports.sortKeysPlugin = exports.timestampPlugin = exports.createPlugin = exports.PluginManager = exports.isValid = exports.validate = exports.schema = exports.q = exports.QueryBuilder = exports.stringifyUrl = exports.stringify = exports.parseUrl = exports.parse = void 0;
|
|
18
|
+
var parser_1 = require("./parser");
|
|
19
|
+
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parser_1.parse; } });
|
|
20
|
+
Object.defineProperty(exports, "parseUrl", { enumerable: true, get: function () { return parser_1.parseUrl; } });
|
|
21
|
+
var stringifier_1 = require("./stringifier");
|
|
22
|
+
Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return stringifier_1.stringify; } });
|
|
23
|
+
Object.defineProperty(exports, "stringifyUrl", { enumerable: true, get: function () { return stringifier_1.stringifyUrl; } });
|
|
24
|
+
var builder_1 = require("./builder");
|
|
25
|
+
Object.defineProperty(exports, "QueryBuilder", { enumerable: true, get: function () { return builder_1.QueryBuilder; } });
|
|
26
|
+
var schema_1 = require("./schema");
|
|
27
|
+
Object.defineProperty(exports, "q", { enumerable: true, get: function () { return schema_1.q; } });
|
|
28
|
+
Object.defineProperty(exports, "schema", { enumerable: true, get: function () { return schema_1.schema; } });
|
|
29
|
+
Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return schema_1.validate; } });
|
|
30
|
+
Object.defineProperty(exports, "isValid", { enumerable: true, get: function () { return schema_1.isValid; } });
|
|
31
|
+
var plugins_1 = require("./plugins");
|
|
32
|
+
Object.defineProperty(exports, "PluginManager", { enumerable: true, get: function () { return plugins_1.PluginManager; } });
|
|
33
|
+
Object.defineProperty(exports, "createPlugin", { enumerable: true, get: function () { return plugins_1.createPlugin; } });
|
|
34
|
+
Object.defineProperty(exports, "timestampPlugin", { enumerable: true, get: function () { return plugins_1.timestampPlugin; } });
|
|
35
|
+
Object.defineProperty(exports, "sortKeysPlugin", { enumerable: true, get: function () { return plugins_1.sortKeysPlugin; } });
|
|
36
|
+
Object.defineProperty(exports, "lowercaseKeysPlugin", { enumerable: true, get: function () { return plugins_1.lowercaseKeysPlugin; } });
|
|
37
|
+
Object.defineProperty(exports, "filterEmptyPlugin", { enumerable: true, get: function () { return plugins_1.filterEmptyPlugin; } });
|
|
38
|
+
Object.defineProperty(exports, "base64Plugin", { enumerable: true, get: function () { return plugins_1.base64Plugin; } });
|
|
39
|
+
Object.defineProperty(exports, "compressPlugin", { enumerable: true, get: function () { return plugins_1.compressPlugin; } });
|
|
40
|
+
Object.defineProperty(exports, "normalizePlugin", { enumerable: true, get: function () { return plugins_1.normalizePlugin; } });
|
|
41
|
+
Object.defineProperty(exports, "createCustomPlugin", { enumerable: true, get: function () { return plugins_1.createCustomPlugin; } });
|
|
42
|
+
var security_1 = require("./security");
|
|
43
|
+
Object.defineProperty(exports, "sanitizeInput", { enumerable: true, get: function () { return security_1.sanitizeInput; } });
|
|
44
|
+
Object.defineProperty(exports, "validateSecurity", { enumerable: true, get: function () { return security_1.validateSecurity; } });
|
|
45
|
+
Object.defineProperty(exports, "countKeys", { enumerable: true, get: function () { return security_1.countKeys; } });
|
|
46
|
+
Object.defineProperty(exports, "hasPrototypePollution", { enumerable: true, get: function () { return security_1.hasPrototypePollution; } });
|
|
47
|
+
Object.defineProperty(exports, "detectXSS", { enumerable: true, get: function () { return security_1.detectXSS; } });
|
|
48
|
+
Object.defineProperty(exports, "secureStringify", { enumerable: true, get: function () { return security_1.secureStringify; } });
|
|
49
|
+
Object.defineProperty(exports, "createSecureParser", { enumerable: true, get: function () { return security_1.createSecureParser; } });
|
|
50
|
+
__exportStar(require("./types"), exports);
|
|
51
|
+
const parser_2 = require("./parser");
|
|
52
|
+
const stringifier_2 = require("./stringifier");
|
|
53
|
+
const builder_2 = require("./builder");
|
|
54
|
+
const schema_2 = require("./schema");
|
|
55
|
+
const plugins_2 = require("./plugins");
|
|
56
|
+
const security_2 = require("./security");
|
|
57
|
+
const defaultPluginManager = new plugins_2.PluginManager();
|
|
58
|
+
exports.querystring = {
|
|
59
|
+
parse: (input, options) => {
|
|
60
|
+
const { plugins, ...parseOptions } = options || {};
|
|
61
|
+
const pluginManager = plugins === true ? defaultPluginManager : plugins instanceof plugins_2.PluginManager ? plugins : null;
|
|
62
|
+
if (pluginManager) {
|
|
63
|
+
const processedInput = pluginManager.applyBeforeParse(input, parseOptions);
|
|
64
|
+
const result = (0, parser_2.parse)(processedInput, parseOptions);
|
|
65
|
+
return pluginManager.applyAfterParse(result, parseOptions);
|
|
66
|
+
}
|
|
67
|
+
return (0, parser_2.parse)(input, parseOptions);
|
|
68
|
+
},
|
|
69
|
+
stringify: (obj, options) => {
|
|
70
|
+
const { plugins, ...stringifyOptions } = options || {};
|
|
71
|
+
const pluginManager = plugins === true ? defaultPluginManager : plugins instanceof plugins_2.PluginManager ? plugins : null;
|
|
72
|
+
if (pluginManager) {
|
|
73
|
+
const processedObj = pluginManager.applyBeforeStringify(obj, stringifyOptions);
|
|
74
|
+
const result = (0, stringifier_2.stringify)(processedObj, stringifyOptions);
|
|
75
|
+
return pluginManager.applyAfterStringify(result, stringifyOptions);
|
|
76
|
+
}
|
|
77
|
+
return (0, stringifier_2.stringify)(obj, stringifyOptions);
|
|
78
|
+
},
|
|
79
|
+
parseUrl: parser_2.parseUrl,
|
|
80
|
+
stringifyUrl: stringifier_2.stringifyUrl,
|
|
81
|
+
QueryBuilder: builder_2.QueryBuilder,
|
|
82
|
+
builder: (options) => new builder_2.QueryBuilder(options),
|
|
83
|
+
q: schema_2.q,
|
|
84
|
+
schema: schema_2.schema,
|
|
85
|
+
validate: schema_2.validate,
|
|
86
|
+
isValid: schema_2.isValid,
|
|
87
|
+
plugins: defaultPluginManager,
|
|
88
|
+
createSecureParser: (options) => (0, security_2.createSecureParser)(parser_2.parse, options),
|
|
89
|
+
version: '1.0.0',
|
|
90
|
+
};
|
|
91
|
+
exports.default = exports.querystring;
|
|
92
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mCAA2C;AAAlC,+FAAA,KAAK,OAAA;AAAE,kGAAA,QAAQ,OAAA;AACxB,6CAAwD;AAA/C,wGAAA,SAAS,OAAA;AAAE,2GAAA,YAAY,OAAA;AAChC,qCAAyC;AAAhC,uGAAA,YAAY,OAAA;AACrB,mCAAwD;AAA/C,2FA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package/dist/index.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export { parse, parseUrl } from './parser';
|
|
2
|
+
export { stringify, stringifyUrl } from './stringifier';
|
|
3
|
+
export { QueryBuilder } from './builder';
|
|
4
|
+
export { q, schema, validate, isValid } from './schema';
|
|
5
|
+
export { PluginManager, createPlugin, timestampPlugin, sortKeysPlugin, lowercaseKeysPlugin, filterEmptyPlugin, base64Plugin, compressPlugin, normalizePlugin, createCustomPlugin, } from './plugins';
|
|
6
|
+
export { sanitizeInput, validateSecurity, countKeys, hasPrototypePollution, detectXSS, secureStringify, createSecureParser, } from './security';
|
|
7
|
+
export * from './types';
|
|
8
|
+
import { parse as _parse, parseUrl as _parseUrl } from './parser';
|
|
9
|
+
import { stringify as _stringify, stringifyUrl as _stringifyUrl } from './stringifier';
|
|
10
|
+
import { QueryBuilder as _QueryBuilder } from './builder';
|
|
11
|
+
import { q as _q, schema as _schema, validate as _validate, isValid as _isValid } from './schema';
|
|
12
|
+
import { PluginManager } from './plugins';
|
|
13
|
+
import { createSecureParser } from './security';
|
|
14
|
+
const defaultPluginManager = new PluginManager();
|
|
15
|
+
export const querystring = {
|
|
16
|
+
parse: (input, options) => {
|
|
17
|
+
const { plugins, ...parseOptions } = options || {};
|
|
18
|
+
const pluginManager = plugins === true ? defaultPluginManager : plugins instanceof PluginManager ? plugins : null;
|
|
19
|
+
if (pluginManager) {
|
|
20
|
+
const processedInput = pluginManager.applyBeforeParse(input, parseOptions);
|
|
21
|
+
const result = _parse(processedInput, parseOptions);
|
|
22
|
+
return pluginManager.applyAfterParse(result, parseOptions);
|
|
23
|
+
}
|
|
24
|
+
return _parse(input, parseOptions);
|
|
25
|
+
},
|
|
26
|
+
stringify: (obj, options) => {
|
|
27
|
+
const { plugins, ...stringifyOptions } = options || {};
|
|
28
|
+
const pluginManager = plugins === true ? defaultPluginManager : plugins instanceof PluginManager ? plugins : null;
|
|
29
|
+
if (pluginManager) {
|
|
30
|
+
const processedObj = pluginManager.applyBeforeStringify(obj, stringifyOptions);
|
|
31
|
+
const result = _stringify(processedObj, stringifyOptions);
|
|
32
|
+
return pluginManager.applyAfterStringify(result, stringifyOptions);
|
|
33
|
+
}
|
|
34
|
+
return _stringify(obj, stringifyOptions);
|
|
35
|
+
},
|
|
36
|
+
parseUrl: _parseUrl,
|
|
37
|
+
stringifyUrl: _stringifyUrl,
|
|
38
|
+
QueryBuilder: _QueryBuilder,
|
|
39
|
+
builder: (options) => new _QueryBuilder(options),
|
|
40
|
+
q: _q,
|
|
41
|
+
schema: _schema,
|
|
42
|
+
validate: _validate,
|
|
43
|
+
isValid: _isValid,
|
|
44
|
+
plugins: defaultPluginManager,
|
|
45
|
+
createSecureParser: (options) => createSecureParser(_parse, options),
|
|
46
|
+
version: '1.0.0',
|
|
47
|
+
};
|
|
48
|
+
export default querystring;
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ParseOptions, ParsedQuery } from './types';
|
|
2
|
+
export declare function parse(input: string, options?: ParseOptions): ParsedQuery;
|
|
3
|
+
export declare function parseUrl(url: string, options?: ParseOptions): {
|
|
4
|
+
url: string;
|
|
5
|
+
query: ParsedQuery;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAA2B,MAAM,SAAS,CAAC;AAgC7E,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,WAAW,CAqIxE;AAsDD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,WAAW,CAAA;CAAE,CAgBjG"}
|
package/dist/parser.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parse = parse;
|
|
4
|
+
exports.parseUrl = parseUrl;
|
|
5
|
+
const encoding_1 = require("./utils/encoding");
|
|
6
|
+
const object_1 = require("./utils/object");
|
|
7
|
+
const array_1 = require("./utils/array");
|
|
8
|
+
const defaults = {
|
|
9
|
+
delimiter: '&',
|
|
10
|
+
depth: 5,
|
|
11
|
+
arrayFormat: 'repeat',
|
|
12
|
+
arrayFormatSeparator: ',',
|
|
13
|
+
decode: true,
|
|
14
|
+
decoder: encoding_1.decode,
|
|
15
|
+
charset: 'utf-8',
|
|
16
|
+
charsetSentinel: false,
|
|
17
|
+
interpretNumericEntities: false,
|
|
18
|
+
parameterLimit: 1000,
|
|
19
|
+
parseArrays: true,
|
|
20
|
+
allowDots: false,
|
|
21
|
+
plainObjects: false,
|
|
22
|
+
allowPrototypes: false,
|
|
23
|
+
allowSparse: false,
|
|
24
|
+
strictNullHandling: false,
|
|
25
|
+
comma: false,
|
|
26
|
+
commaRoundTrip: false,
|
|
27
|
+
ignoreQueryPrefix: false,
|
|
28
|
+
duplicates: 'combine',
|
|
29
|
+
parseNumbers: false,
|
|
30
|
+
parseBooleans: false,
|
|
31
|
+
parseDates: false,
|
|
32
|
+
typeCoercion: false,
|
|
33
|
+
};
|
|
34
|
+
function parse(input, options) {
|
|
35
|
+
const opts = { ...defaults, ...options };
|
|
36
|
+
const { delimiter, depth, arrayFormat, arrayFormatSeparator, decode: shouldDecode, decoder, charset, charsetSentinel, interpretNumericEntities: shouldInterpretNumericEntities, parameterLimit, parseArrays, allowDots, plainObjects, allowPrototypes, strictNullHandling, comma, ignoreQueryPrefix, duplicates, parseNumbers, parseBooleans, parseDates, typeCoercion, } = opts;
|
|
37
|
+
if (!input || typeof input !== 'string') {
|
|
38
|
+
return plainObjects ? Object.create(null) : {};
|
|
39
|
+
}
|
|
40
|
+
let queryString = input;
|
|
41
|
+
if (ignoreQueryPrefix && queryString.charAt(0) === '?') {
|
|
42
|
+
queryString = queryString.slice(1);
|
|
43
|
+
}
|
|
44
|
+
if (charsetSentinel) {
|
|
45
|
+
const charsetMatch = queryString.match(/utf8=%E2%9C%93/);
|
|
46
|
+
if (charsetMatch) {
|
|
47
|
+
queryString = queryString.replace(/utf8=%E2%9C%93&?/, '');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const pairs = queryString.split(delimiter);
|
|
51
|
+
const result = plainObjects ? Object.create(null) : {};
|
|
52
|
+
if (pairs.length > parameterLimit) {
|
|
53
|
+
throw new Error(`Parameter limit exceeded. Maximum allowed: ${parameterLimit}`);
|
|
54
|
+
}
|
|
55
|
+
const typeCoercionOpts = typeof typeCoercion === 'object' ? typeCoercion : {
|
|
56
|
+
numbers: typeCoercion === true || parseNumbers,
|
|
57
|
+
booleans: typeCoercion === true || parseBooleans,
|
|
58
|
+
dates: typeCoercion === true || parseDates,
|
|
59
|
+
arrays: parseArrays,
|
|
60
|
+
objects: true,
|
|
61
|
+
};
|
|
62
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
63
|
+
const pair = pairs[i];
|
|
64
|
+
// Skip empty pairs
|
|
65
|
+
if (!pair) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const equalIndex = pair.indexOf('=');
|
|
69
|
+
let key;
|
|
70
|
+
let val;
|
|
71
|
+
if (equalIndex === -1) {
|
|
72
|
+
key = shouldDecode ? decoder(pair, encoding_1.decode) : pair;
|
|
73
|
+
val = strictNullHandling ? null : '';
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
key = shouldDecode ? decoder(pair.slice(0, equalIndex), encoding_1.decode) : pair.slice(0, equalIndex);
|
|
77
|
+
val = shouldDecode ? decoder(pair.slice(equalIndex + 1), encoding_1.decode) : pair.slice(equalIndex + 1);
|
|
78
|
+
}
|
|
79
|
+
if (shouldInterpretNumericEntities && charset === 'iso-8859-1') {
|
|
80
|
+
val = (0, encoding_1.interpretNumericEntities)(val);
|
|
81
|
+
}
|
|
82
|
+
if (comma && val.indexOf(',') > -1) {
|
|
83
|
+
val = val.split(',').join(',');
|
|
84
|
+
}
|
|
85
|
+
let parsedValue = val;
|
|
86
|
+
if (typeCoercionOpts.numbers || typeCoercionOpts.booleans) {
|
|
87
|
+
parsedValue = (0, encoding_1.parseArrayValue)(val, {
|
|
88
|
+
parseNumbers: typeCoercionOpts.numbers,
|
|
89
|
+
parseBooleans: typeCoercionOpts.booleans,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (typeCoercionOpts.dates && typeof parsedValue === 'string') {
|
|
93
|
+
const dateValue = (0, encoding_1.tryParseDate)(parsedValue);
|
|
94
|
+
if (dateValue instanceof Date) {
|
|
95
|
+
parsedValue = dateValue;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (parseArrays) {
|
|
99
|
+
if ((0, array_1.isArrayKey)(key, arrayFormat)) {
|
|
100
|
+
key = (0, array_1.normalizeArrayKey)(key, arrayFormat);
|
|
101
|
+
}
|
|
102
|
+
if (arrayFormat === 'comma' || arrayFormat === 'separator' || arrayFormat === 'json' || arrayFormat === 'bracket-separator') {
|
|
103
|
+
parsedValue = (0, array_1.parseArrayFormat)(key, parsedValue, arrayFormat, arrayFormatSeparator);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
let keys;
|
|
107
|
+
if (allowDots) {
|
|
108
|
+
keys = key.split('.');
|
|
109
|
+
}
|
|
110
|
+
else if (key.includes('[') && key.includes(']')) {
|
|
111
|
+
// Handle bracket notation like a[b][c]
|
|
112
|
+
keys = key.split(/[\[\]]+/).filter(Boolean);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
keys = [key];
|
|
116
|
+
}
|
|
117
|
+
parseKeys(result, keys, parsedValue, {
|
|
118
|
+
depth,
|
|
119
|
+
plainObjects,
|
|
120
|
+
allowPrototypes,
|
|
121
|
+
duplicates,
|
|
122
|
+
arrayFormat,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
function parseKeys(obj, keys, value, options) {
|
|
128
|
+
if (keys.length === 0) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const { depth, plainObjects, duplicates } = options;
|
|
132
|
+
let target = obj;
|
|
133
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
134
|
+
const key = keys[i];
|
|
135
|
+
const nextKey = keys[i + 1];
|
|
136
|
+
const isArrayIndex = /^\d+$/.test(nextKey);
|
|
137
|
+
if (i >= depth) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (!(0, object_1.hasOwn)(target, key)) {
|
|
141
|
+
if (isArrayIndex) {
|
|
142
|
+
target[key] = [];
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
target[key] = plainObjects ? Object.create(null) : {};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
target = target[key];
|
|
149
|
+
}
|
|
150
|
+
const lastKey = keys[keys.length - 1];
|
|
151
|
+
if (duplicates === 'first' && (0, object_1.hasOwn)(target, lastKey)) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (duplicates === 'last' || !(0, object_1.hasOwn)(target, lastKey)) {
|
|
155
|
+
target[lastKey] = value;
|
|
156
|
+
}
|
|
157
|
+
else if (duplicates === 'combine') {
|
|
158
|
+
target[lastKey] = (0, array_1.combineArrayValues)(target[lastKey], value);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function parseUrl(url, options) {
|
|
162
|
+
if (!url || typeof url !== 'string') {
|
|
163
|
+
return { url: '', query: {} };
|
|
164
|
+
}
|
|
165
|
+
const queryIndex = url.indexOf('?');
|
|
166
|
+
if (queryIndex === -1) {
|
|
167
|
+
return { url, query: {} };
|
|
168
|
+
}
|
|
169
|
+
const baseUrl = url.slice(0, queryIndex);
|
|
170
|
+
const queryString = url.slice(queryIndex + 1);
|
|
171
|
+
const query = parse(queryString, options);
|
|
172
|
+
return { url: baseUrl, query };
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":";;AAgCA,sBAqIC;AAsDD,4BAgBC;AA1OD,+CAAmG;AACnG,2CAAwC;AACxC,yCAAoG;AAEpG,MAAM,QAAQ,GAA2B;IACvC,SAAS,EAAE,GAAG;IACd,KAAK,EAAE,CAAC;IACR,WAAW,EAAE,QAAQ;IACrB,oBAAoB,EAAE,GAAG;IACzB,MAAM,EAAE,IAAI;IACZ,OAAO,EAAE,iBAAM;IACf,OAAO,EAAE,OAAO;IAChB,eAAe,EAAE,KAAK;IACtB,wBAAwB,EAAE,KAAK;IAC/B,cAAc,EAAE,IAAI;IACpB,WAAW,EAAE,IAAI;IACjB,SAAS,EAAE,KAAK;IAChB,YAAY,EAAE,KAAK;IACnB,eAAe,EAAE,KAAK;IACtB,WAAW,EAAE,KAAK;IAClB,kBAAkB,EAAE,KAAK;IACzB,KAAK,EAAE,KAAK;IACZ,cAAc,EAAE,KAAK;IACrB,iBAAiB,EAAE,KAAK;IACxB,UAAU,EAAE,SAAS;IACrB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,KAAK;IACpB,UAAU,EAAE,KAAK;IACjB,YAAY,EAAE,KAAK;CACpB,CAAC;AAEF,SAAgB,KAAK,CAAC,KAAa,EAAE,OAAsB;IACzD,MAAM,IAAI,GAAG,EAAE,GAAG,QAAQ,EAAE,GAAG,OAAO,EAAE,CAAC;IACzC,MAAM,EACJ,SAAS,EACT,KAAK,EACL,WAAW,EACX,oBAAoB,EACpB,MAAM,EAAE,YAAY,EACpB,OAAO,EACP,OAAO,EACP,eAAe,EACf,wBAAwB,EAAE,8BAA8B,EACxD,cAAc,EACd,WAAW,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,CAAC,CAAC;IACzC,MAAM,WAAW,GAAG,GAAG,CAAC,KAAK,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;IAC9C,MAAM,KAAK,GAAG,KAAK,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;IAE1C,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AACjC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { QueryStringPlugin, ParseOptions, StringifyOptions, ParsedQuery } from './types';
|
|
2
|
+
export declare class PluginManager {
|
|
3
|
+
private plugins;
|
|
4
|
+
register(plugin: QueryStringPlugin): this;
|
|
5
|
+
unregister(pluginName: string): boolean;
|
|
6
|
+
has(pluginName: string): boolean;
|
|
7
|
+
get(pluginName: string): QueryStringPlugin | undefined;
|
|
8
|
+
list(): string[];
|
|
9
|
+
clear(): void;
|
|
10
|
+
applyBeforeParse(input: string, options: ParseOptions): string;
|
|
11
|
+
applyAfterParse(result: ParsedQuery, options: ParseOptions): ParsedQuery;
|
|
12
|
+
applyBeforeStringify(obj: ParsedQuery, options: StringifyOptions): ParsedQuery;
|
|
13
|
+
applyAfterStringify(result: string, options: StringifyOptions): string;
|
|
14
|
+
static create(plugins?: QueryStringPlugin[]): PluginManager;
|
|
15
|
+
}
|
|
16
|
+
export declare const createPlugin: (plugin: QueryStringPlugin) => QueryStringPlugin;
|
|
17
|
+
export declare const timestampPlugin: QueryStringPlugin;
|
|
18
|
+
export declare const sortKeysPlugin: QueryStringPlugin;
|
|
19
|
+
export declare const lowercaseKeysPlugin: QueryStringPlugin;
|
|
20
|
+
export declare const filterEmptyPlugin: QueryStringPlugin;
|
|
21
|
+
export declare const base64Plugin: QueryStringPlugin;
|
|
22
|
+
export declare const compressPlugin: QueryStringPlugin;
|
|
23
|
+
export declare const normalizePlugin: QueryStringPlugin;
|
|
24
|
+
export declare const createCustomPlugin: (name: string, handlers: Partial<Omit<QueryStringPlugin, "name">>) => QueryStringPlugin;
|
|
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CAAC;AAEH,eAAO,MAAM,eAAe,mBAK1B,CAAC;AAsCH,eAAO,MAAM,kBAAkB,GAC7B,MAAM,MAAM,EACZ,UAAU,OAAO,CAAC,IAAI,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,KACjD,iBAKF,CAAC"}
|
package/dist/plugins.js
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCustomPlugin = exports.normalizePlugin = exports.compressPlugin = exports.base64Plugin = exports.filterEmptyPlugin = exports.lowercaseKeysPlugin = exports.sortKeysPlugin = exports.timestampPlugin = exports.createPlugin = exports.PluginManager = void 0;
|
|
4
|
+
class PluginManager {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.plugins = new Map();
|
|
7
|
+
}
|
|
8
|
+
register(plugin) {
|
|
9
|
+
if (!plugin.name) {
|
|
10
|
+
throw new Error('Plugin must have a name');
|
|
11
|
+
}
|
|
12
|
+
if (this.plugins.has(plugin.name)) {
|
|
13
|
+
throw new Error(`Plugin "${plugin.name}" is already registered`);
|
|
14
|
+
}
|
|
15
|
+
this.plugins.set(plugin.name, plugin);
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
unregister(pluginName) {
|
|
19
|
+
return this.plugins.delete(pluginName);
|
|
20
|
+
}
|
|
21
|
+
has(pluginName) {
|
|
22
|
+
return this.plugins.has(pluginName);
|
|
23
|
+
}
|
|
24
|
+
get(pluginName) {
|
|
25
|
+
return this.plugins.get(pluginName);
|
|
26
|
+
}
|
|
27
|
+
list() {
|
|
28
|
+
return Array.from(this.plugins.keys());
|
|
29
|
+
}
|
|
30
|
+
clear() {
|
|
31
|
+
this.plugins.clear();
|
|
32
|
+
}
|
|
33
|
+
applyBeforeParse(input, options) {
|
|
34
|
+
let result = input;
|
|
35
|
+
for (const [_, plugin] of this.plugins) {
|
|
36
|
+
if (plugin.beforeParse) {
|
|
37
|
+
result = plugin.beforeParse(result, options);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
applyAfterParse(result, options) {
|
|
43
|
+
let output = result;
|
|
44
|
+
for (const [_, plugin] of this.plugins) {
|
|
45
|
+
if (plugin.afterParse) {
|
|
46
|
+
output = plugin.afterParse(output, options);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return output;
|
|
50
|
+
}
|
|
51
|
+
applyBeforeStringify(obj, options) {
|
|
52
|
+
let result = obj;
|
|
53
|
+
for (const [_, plugin] of this.plugins) {
|
|
54
|
+
if (plugin.beforeStringify) {
|
|
55
|
+
result = plugin.beforeStringify(result, options);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
applyAfterStringify(result, options) {
|
|
61
|
+
let output = result;
|
|
62
|
+
for (const [_, plugin] of this.plugins) {
|
|
63
|
+
if (plugin.afterStringify) {
|
|
64
|
+
output = plugin.afterStringify(output, options);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return output;
|
|
68
|
+
}
|
|
69
|
+
static create(plugins) {
|
|
70
|
+
const manager = new PluginManager();
|
|
71
|
+
if (plugins) {
|
|
72
|
+
for (const plugin of plugins) {
|
|
73
|
+
manager.register(plugin);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return manager;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.PluginManager = PluginManager;
|
|
80
|
+
const createPlugin = (plugin) => plugin;
|
|
81
|
+
exports.createPlugin = createPlugin;
|
|
82
|
+
exports.timestampPlugin = (0, exports.createPlugin)({
|
|
83
|
+
name: 'timestamp',
|
|
84
|
+
beforeStringify: (obj) => {
|
|
85
|
+
return {
|
|
86
|
+
...obj,
|
|
87
|
+
_timestamp: Date.now(),
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
exports.sortKeysPlugin = (0, exports.createPlugin)({
|
|
92
|
+
name: 'sortKeys',
|
|
93
|
+
beforeStringify: (obj) => {
|
|
94
|
+
const sorted = {};
|
|
95
|
+
const keys = Object.keys(obj).sort();
|
|
96
|
+
for (const key of keys) {
|
|
97
|
+
sorted[key] = obj[key];
|
|
98
|
+
}
|
|
99
|
+
return sorted;
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
exports.lowercaseKeysPlugin = (0, exports.createPlugin)({
|
|
103
|
+
name: 'lowercaseKeys',
|
|
104
|
+
afterParse: (result) => {
|
|
105
|
+
const transformed = {};
|
|
106
|
+
for (const [key, value] of Object.entries(result)) {
|
|
107
|
+
transformed[key.toLowerCase()] = value;
|
|
108
|
+
}
|
|
109
|
+
return transformed;
|
|
110
|
+
},
|
|
111
|
+
beforeStringify: (obj) => {
|
|
112
|
+
const transformed = {};
|
|
113
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
114
|
+
transformed[key.toLowerCase()] = value;
|
|
115
|
+
}
|
|
116
|
+
return transformed;
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
exports.filterEmptyPlugin = (0, exports.createPlugin)({
|
|
120
|
+
name: 'filterEmpty',
|
|
121
|
+
beforeStringify: (obj) => {
|
|
122
|
+
const filtered = {};
|
|
123
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
124
|
+
if (value !== '' && value !== null && value !== undefined) {
|
|
125
|
+
if (Array.isArray(value) && value.length === 0) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
filtered[key] = value;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return filtered;
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
exports.base64Plugin = (0, exports.createPlugin)({
|
|
135
|
+
name: 'base64',
|
|
136
|
+
afterStringify: (result) => {
|
|
137
|
+
const globalObj = globalThis;
|
|
138
|
+
if (typeof globalObj.btoa === 'function') {
|
|
139
|
+
return globalObj.btoa(result);
|
|
140
|
+
}
|
|
141
|
+
// Node.js environment
|
|
142
|
+
return globalObj.Buffer?.from(result).toString('base64') || result;
|
|
143
|
+
},
|
|
144
|
+
beforeParse: (input) => {
|
|
145
|
+
try {
|
|
146
|
+
const globalObj = globalThis;
|
|
147
|
+
if (typeof globalObj.atob === 'function') {
|
|
148
|
+
// Browser environment
|
|
149
|
+
const decoded = globalObj.atob(input);
|
|
150
|
+
const reencoded = globalObj.btoa(decoded);
|
|
151
|
+
if (reencoded.replace(/=+$/, '') === input.replace(/=+$/, '')) {
|
|
152
|
+
return decoded;
|
|
153
|
+
}
|
|
154
|
+
return input;
|
|
155
|
+
}
|
|
156
|
+
// Node.js environment
|
|
157
|
+
const Buffer = globalObj.Buffer;
|
|
158
|
+
if (Buffer) {
|
|
159
|
+
const decoded = Buffer.from(input, 'base64').toString('utf-8');
|
|
160
|
+
const reencoded = Buffer.from(decoded).toString('base64');
|
|
161
|
+
if (reencoded.replace(/=+$/, '') === input.replace(/=+$/, '')) {
|
|
162
|
+
return decoded;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return input;
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
return input;
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
exports.compressPlugin = (0, exports.createPlugin)({
|
|
173
|
+
name: 'compress',
|
|
174
|
+
afterStringify: (result) => {
|
|
175
|
+
return result
|
|
176
|
+
.replace(/(%20|%2520|\+)/g, ' ')
|
|
177
|
+
.replace(/%2F/g, '/')
|
|
178
|
+
.replace(/%3A/g, ':')
|
|
179
|
+
.replace(/%3D/g, '=')
|
|
180
|
+
.replace(/%26/g, '&')
|
|
181
|
+
.replace(/%3F/g, '?');
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
exports.normalizePlugin = (0, exports.createPlugin)({
|
|
185
|
+
name: 'normalize',
|
|
186
|
+
afterParse: (result) => {
|
|
187
|
+
return normalizeValues(result);
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
function normalizeValues(obj) {
|
|
191
|
+
if (obj === null || obj === undefined) {
|
|
192
|
+
return obj;
|
|
193
|
+
}
|
|
194
|
+
if (typeof obj === 'string') {
|
|
195
|
+
const trimmed = obj.trim();
|
|
196
|
+
if (trimmed === 'true')
|
|
197
|
+
return true;
|
|
198
|
+
if (trimmed === 'false')
|
|
199
|
+
return false;
|
|
200
|
+
if (trimmed === 'null')
|
|
201
|
+
return null;
|
|
202
|
+
if (trimmed === 'undefined')
|
|
203
|
+
return undefined;
|
|
204
|
+
const num = Number(trimmed);
|
|
205
|
+
if (!isNaN(num) && trimmed === String(num)) {
|
|
206
|
+
return num;
|
|
207
|
+
}
|
|
208
|
+
return trimmed;
|
|
209
|
+
}
|
|
210
|
+
if (Array.isArray(obj)) {
|
|
211
|
+
return obj.map(normalizeValues);
|
|
212
|
+
}
|
|
213
|
+
if (typeof obj === 'object') {
|
|
214
|
+
const normalized = {};
|
|
215
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
216
|
+
normalized[key] = normalizeValues(value);
|
|
217
|
+
}
|
|
218
|
+
return normalized;
|
|
219
|
+
}
|
|
220
|
+
return obj;
|
|
221
|
+
}
|
|
222
|
+
const createCustomPlugin = (name, handlers) => {
|
|
223
|
+
return {
|
|
224
|
+
name,
|
|
225
|
+
...handlers,
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
exports.createCustomPlugin = createCustomPlugin;
|
|
229
|
+
//# sourceMappingURL=plugins.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugins.js","sourceRoot":"","sources":["../src/plugins.ts"],"names":[],"mappings":";;;AAEA,MAAa,aAAa;IAA1B;QACU,YAAO,GAAmC,IAAI,GAAG,EAAE,CAAC;IA8F9D,CAAC;IA5FC,QAAQ,CAAC,MAAyB;QAChC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;QAC7C,CAAC;QAED,IAA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U,GAA4B,EAAE,CAAC;QAC/C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAC/C,UAAU,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;QAC3C,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC;AAEM,MAAM,kBAAkB,GAAG,CAChC,IAAY,EACZ,QAAkD,EAC/B,EAAE;IACrB,OAAO;QACL,IAAI;QACJ,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,kBAAkB,sBAQ7B"}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SchemaType, BaseSchema, StringSchema, NumberSchema, BooleanSchema, DateSchema, ArraySchema, ObjectSchema, EnumSchema, UnionSchema } from './types';
|
|
2
|
+
export declare const q: {
|
|
3
|
+
string: () => StringSchema;
|
|
4
|
+
number: () => NumberSchema;
|
|
5
|
+
boolean: () => BooleanSchema;
|
|
6
|
+
date: () => DateSchema;
|
|
7
|
+
array: <T>(schema?: BaseSchema<T>) => ArraySchema<T>;
|
|
8
|
+
object: <T extends Record<string, unknown> = Record<string, unknown>>() => ObjectSchema<T>;
|
|
9
|
+
enum: <T extends readonly [string, ...string[]]>(...values: T) => EnumSchema<T>;
|
|
10
|
+
union: <T extends readonly SchemaType[]>(...schemas: T) => UnionSchema<T>;
|
|
11
|
+
literal: <T extends string | number | boolean>(value: T) => BaseSchema<T>;
|
|
12
|
+
any: () => BaseSchema<any>;
|
|
13
|
+
unknown: () => BaseSchema<unknown>;
|
|
14
|
+
null: () => BaseSchema<null>;
|
|
15
|
+
undefined: () => BaseSchema<undefined>;
|
|
16
|
+
void: () => BaseSchema<void>;
|
|
17
|
+
};
|
|
18
|
+
export declare function schema<T extends Record<string, SchemaType>>(shape: T): ObjectSchema<{
|
|
19
|
+
[K in keyof T]: T[K] extends BaseSchema<infer U> ? U : never;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function validate<T>(schema: BaseSchema<T>, value: unknown): T;
|
|
22
|
+
export declare function isValid<T>(schema: BaseSchema<T>, value: unknown): boolean;
|
|
23
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,UAAU,EACV,WAAW,EACX,YAAY,EACZ,UAAU,EACV,WAAW,EAIZ,MAAM,SAAS,CAAC;AAurBjB,eAAO,MAAM,CAAC;kBACA,YAAY;kBACZ,YAAY;mBACX,aAAa;gBAChB,UAAU;YACZ,CAAC,WAAW,UAAU,CAAC,CAAC,CAAC,KAAG,WAAW,CAAC,CAAC,CAAC;aACzC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,iCAA+B,YAAY,CAAC,CAAC,CAAC;WACjF,CAAC,SAAS,SAAS,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,aAAa,CAAC,KAAG,UAAU,CAAC,CAAC,CAAC;YACrE,CAAC,SAAS,SAAS,UAAU,EAAE,cAAc,CAAC,KAAG,WAAW,CAAC,CAAC,CAAC;cAC7D,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,OAAO,SAAS,CAAC,KAAG,UAAU,CAAC,CAAC,CAAC;eAW9D,UAAU,CAAC,GAAG,CAAC;mBAQX,UAAU,CAAC,OAAO,CAAC;gBAQtB,UAAU,CAAC,IAAI,CAAC;qBAWX,UAAU,CAAC,SAAS,CAAC;gBAW1B,UAAU,CAAC,IAAI,CAAC;CAW3B,CAAC;AAEF,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,KAAK,EA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