@dxos/effect 0.6.13-main.ed424a1 → 0.6.14-main.1366248
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/dist/lib/browser/index.mjs +155 -82
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +161 -71
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +155 -75
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/ast.d.ts +45 -9
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/package.json +6 -8
- package/src/ast.test.ts +94 -19
- package/src/ast.ts +213 -41
- package/src/index.ts +1 -0
- package/src/url.ts +1 -1
- package/dist/types/src/decamelize.d.ts +0 -2
- package/dist/types/src/decamelize.d.ts.map +0 -1
- package/src/decamelize.ts +0 -36
package/dist/lib/node/index.cjs
CHANGED
|
@@ -20,13 +20,19 @@ var node_exports = {};
|
|
|
20
20
|
__export(node_exports, {
|
|
21
21
|
AST: () => import_schema.AST,
|
|
22
22
|
JSONSchema: () => import_schema.JSONSchema,
|
|
23
|
+
JsonPath: () => JsonPath,
|
|
24
|
+
JsonProp: () => JsonProp,
|
|
23
25
|
ParamKeyAnnotation: () => ParamKeyAnnotation,
|
|
24
26
|
S: () => import_schema.Schema,
|
|
25
27
|
UrlParser: () => UrlParser,
|
|
28
|
+
VisitResult: () => VisitResult,
|
|
29
|
+
findAnnotation: () => findAnnotation,
|
|
30
|
+
findNode: () => findNode,
|
|
31
|
+
findProperty: () => findProperty,
|
|
26
32
|
getAnnotation: () => getAnnotation,
|
|
27
33
|
getParamKeyAnnotation: () => getParamKeyAnnotation,
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
getSimpleType: () => getSimpleType,
|
|
35
|
+
isSimpleType: () => isSimpleType,
|
|
30
36
|
visit: () => visit
|
|
31
37
|
});
|
|
32
38
|
module.exports = __toCommonJS(node_exports);
|
|
@@ -36,86 +42,164 @@ var import_effect = require("effect");
|
|
|
36
42
|
var import_invariant = require("@dxos/invariant");
|
|
37
43
|
var import_schema3 = require("@effect/schema");
|
|
38
44
|
var import_effect2 = require("effect");
|
|
45
|
+
var import_util = require("@dxos/util");
|
|
39
46
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/effect/src/ast.ts";
|
|
40
|
-
var
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return
|
|
47
|
+
var getSimpleType = (node) => {
|
|
48
|
+
if (import_schema2.AST.isObjectKeyword(node)) {
|
|
49
|
+
return "object";
|
|
50
|
+
}
|
|
51
|
+
if (import_schema2.AST.isStringKeyword(node)) {
|
|
52
|
+
return "string";
|
|
53
|
+
}
|
|
54
|
+
if (import_schema2.AST.isNumberKeyword(node)) {
|
|
55
|
+
return "number";
|
|
56
|
+
}
|
|
57
|
+
if (import_schema2.AST.isBooleanKeyword(node)) {
|
|
58
|
+
return "boolean";
|
|
59
|
+
}
|
|
60
|
+
if (import_schema2.AST.isEnums(node)) {
|
|
61
|
+
return "enum";
|
|
62
|
+
}
|
|
63
|
+
if (import_schema2.AST.isLiteral(node)) {
|
|
64
|
+
return "literal";
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var isSimpleType = (node) => !!getSimpleType(node);
|
|
68
|
+
var PATH_REGEX = /[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*/;
|
|
69
|
+
var PROP_REGEX = /\w+/;
|
|
70
|
+
var JsonPath = import_schema2.Schema.NonEmptyString.pipe(import_schema2.Schema.pattern(PATH_REGEX));
|
|
71
|
+
var JsonProp = import_schema2.Schema.NonEmptyString.pipe(import_schema2.Schema.pattern(PROP_REGEX));
|
|
72
|
+
var getAnnotation = (annotationId) => (node) => (0, import_effect.pipe)(import_schema2.AST.getAnnotation(annotationId)(node), import_effect.Option.getOrUndefined);
|
|
73
|
+
var VisitResult;
|
|
74
|
+
(function(VisitResult2) {
|
|
75
|
+
VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
|
|
76
|
+
VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
|
|
77
|
+
VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
|
|
78
|
+
})(VisitResult || (VisitResult = {}));
|
|
79
|
+
var defaultTest = (node) => isSimpleType(node) ? 0 : 1;
|
|
80
|
+
var visit = (node, testOrVisitor, visitor) => {
|
|
81
|
+
if (!visitor) {
|
|
82
|
+
visitNode(node, defaultTest, testOrVisitor);
|
|
46
83
|
} else {
|
|
47
|
-
|
|
84
|
+
visitNode(node, testOrVisitor, visitor);
|
|
48
85
|
}
|
|
49
86
|
};
|
|
50
|
-
var
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
87
|
+
var visitNode = (node, test, visitor, path = [], depth = 0) => {
|
|
88
|
+
const result = test?.(node, path, depth) ?? 0;
|
|
89
|
+
if (result === 2) {
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
if (result !== 1) {
|
|
93
|
+
visitor(node, path, depth);
|
|
94
|
+
}
|
|
95
|
+
if (import_schema2.AST.isTypeLiteral(node)) {
|
|
96
|
+
for (const prop of import_schema2.AST.getPropertySignatures(node)) {
|
|
97
|
+
const currentPath = [
|
|
98
|
+
...path,
|
|
99
|
+
prop.name.toString()
|
|
100
|
+
];
|
|
101
|
+
const result2 = visitNode(prop.type, test, visitor, currentPath, depth + 1);
|
|
102
|
+
if (result2 === 2) {
|
|
103
|
+
return result2;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} else if (import_schema2.AST.isTupleType(node)) {
|
|
107
|
+
for (const [i, element] of node.elements.entries()) {
|
|
108
|
+
const currentPath = [
|
|
109
|
+
...path,
|
|
110
|
+
i
|
|
111
|
+
];
|
|
112
|
+
const result2 = visitNode(element.type, test, visitor, currentPath, depth);
|
|
113
|
+
if (result2 === 2) {
|
|
114
|
+
return result2;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} else if (import_schema2.AST.isUnion(node)) {
|
|
118
|
+
for (const type of node.types) {
|
|
119
|
+
const result2 = visitNode(type, test, visitor, path, depth);
|
|
120
|
+
if (result2 === 2) {
|
|
121
|
+
return result2;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
} else if (import_schema2.AST.isRefinement(node)) {
|
|
125
|
+
const result2 = visitNode(node.from, test, visitor, path, depth);
|
|
126
|
+
if (result2 === 2) {
|
|
127
|
+
return result2;
|
|
57
128
|
}
|
|
58
|
-
|
|
59
|
-
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var findNode = (node, test) => {
|
|
132
|
+
if (test(node)) {
|
|
133
|
+
return node;
|
|
134
|
+
} else if (import_schema2.AST.isTypeLiteral(node)) {
|
|
135
|
+
for (const prop of import_schema2.AST.getPropertySignatures(node)) {
|
|
136
|
+
const child = findNode(prop.type, test);
|
|
137
|
+
if (child) {
|
|
138
|
+
return child;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
} else if (import_schema2.AST.isTupleType(node)) {
|
|
142
|
+
for (const [_, element] of node.elements.entries()) {
|
|
143
|
+
const child = findNode(element.type, test);
|
|
144
|
+
if (child) {
|
|
145
|
+
return child;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} else if (import_schema2.AST.isUnion(node)) {
|
|
149
|
+
for (const type of node.types) {
|
|
150
|
+
const child = findNode(type, test);
|
|
151
|
+
if (child) {
|
|
152
|
+
return child;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
} else if (import_schema2.AST.isRefinement(node)) {
|
|
156
|
+
return findNode(node.from, test);
|
|
157
|
+
}
|
|
158
|
+
return void 0;
|
|
159
|
+
};
|
|
160
|
+
var findProperty = (schema, path) => {
|
|
161
|
+
const getProp = (node, path2) => {
|
|
162
|
+
const [name, ...rest] = path2;
|
|
163
|
+
const typeNode = findNode(node, import_schema2.AST.isTypeLiteral);
|
|
164
|
+
(0, import_invariant.invariant)(typeNode, void 0, {
|
|
60
165
|
F: __dxlog_file,
|
|
61
|
-
L:
|
|
166
|
+
L: 214,
|
|
62
167
|
S: void 0,
|
|
63
168
|
A: [
|
|
64
|
-
"
|
|
65
|
-
"
|
|
169
|
+
"typeNode",
|
|
170
|
+
""
|
|
66
171
|
]
|
|
67
172
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
for (const prop of import_schema2.AST.getPropertySignatures(node)) {
|
|
75
|
-
const currentPath = [
|
|
76
|
-
...path,
|
|
77
|
-
prop.name.toString()
|
|
78
|
-
];
|
|
79
|
-
const type = getType(prop.type);
|
|
80
|
-
if (type) {
|
|
81
|
-
if (import_schema2.AST.isTypeLiteral(type)) {
|
|
82
|
-
visitNode(type, visitor, currentPath);
|
|
83
|
-
} else {
|
|
84
|
-
const ok = visitor(type, currentPath);
|
|
85
|
-
if (ok === false) {
|
|
86
|
-
return;
|
|
173
|
+
for (const prop of import_schema2.AST.getPropertySignatures(typeNode)) {
|
|
174
|
+
if (prop.name === name) {
|
|
175
|
+
if (rest.length) {
|
|
176
|
+
return getProp(prop.type, rest);
|
|
177
|
+
} else {
|
|
178
|
+
return prop.type;
|
|
87
179
|
}
|
|
88
180
|
}
|
|
89
181
|
}
|
|
90
|
-
|
|
182
|
+
return void 0;
|
|
183
|
+
};
|
|
184
|
+
return getProp(schema.ast, path.split("."));
|
|
91
185
|
};
|
|
92
|
-
var
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
var isUpper = (char) => char >= CAPITAL_A && char <= CAPITAL_Z;
|
|
99
|
-
var toLower = (char) => char + 32;
|
|
100
|
-
var decamelize = (str) => {
|
|
101
|
-
const firstChar = str.charCodeAt(0);
|
|
102
|
-
if (!isLower(firstChar)) {
|
|
103
|
-
return str;
|
|
104
|
-
}
|
|
105
|
-
const length = str.length;
|
|
106
|
-
let changed = false;
|
|
107
|
-
const out = [];
|
|
108
|
-
for (let i = 0; i < length; ++i) {
|
|
109
|
-
const c = str.charCodeAt(i);
|
|
110
|
-
if (isUpper(c)) {
|
|
111
|
-
out.push(LOW_DASH);
|
|
112
|
-
out.push(toLower(c));
|
|
113
|
-
changed = true;
|
|
114
|
-
} else {
|
|
115
|
-
out.push(c);
|
|
186
|
+
var findAnnotation = (node, annotationId) => {
|
|
187
|
+
const getAnnotationById = getAnnotation(annotationId);
|
|
188
|
+
const getBaseAnnotation = (node2) => {
|
|
189
|
+
const value = getAnnotationById(node2);
|
|
190
|
+
if (value !== void 0) {
|
|
191
|
+
return value;
|
|
116
192
|
}
|
|
117
|
-
|
|
118
|
-
|
|
193
|
+
if (import_schema2.AST.isUnion(node2)) {
|
|
194
|
+
for (const type of node2.types) {
|
|
195
|
+
const value2 = getBaseAnnotation(type);
|
|
196
|
+
if (value2 !== void 0) {
|
|
197
|
+
return value2;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
return getBaseAnnotation(node);
|
|
119
203
|
};
|
|
120
204
|
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
121
205
|
var getParamKeyAnnotation = import_schema3.AST.getAnnotation(ParamKeyAnnotationId);
|
|
@@ -132,7 +216,7 @@ var UrlParser = class {
|
|
|
132
216
|
parse(_url) {
|
|
133
217
|
const url = new URL(_url);
|
|
134
218
|
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
135
|
-
let value = url.searchParams.get(decamelize(key));
|
|
219
|
+
let value = url.searchParams.get((0, import_util.decamelize)(key));
|
|
136
220
|
if (value == null) {
|
|
137
221
|
value = url.searchParams.get(key);
|
|
138
222
|
}
|
|
@@ -158,7 +242,7 @@ var UrlParser = class {
|
|
|
158
242
|
const field = this._schema.fields[key];
|
|
159
243
|
if (field) {
|
|
160
244
|
const { key: serializedKey } = (0, import_effect2.pipe)(getParamKeyAnnotation(field.ast), import_effect2.Option.getOrElse(() => ({
|
|
161
|
-
key: decamelize(key)
|
|
245
|
+
key: (0, import_util.decamelize)(key)
|
|
162
246
|
})));
|
|
163
247
|
url.searchParams.set(serializedKey, String(value));
|
|
164
248
|
}
|
|
@@ -171,13 +255,19 @@ var UrlParser = class {
|
|
|
171
255
|
0 && (module.exports = {
|
|
172
256
|
AST,
|
|
173
257
|
JSONSchema,
|
|
258
|
+
JsonPath,
|
|
259
|
+
JsonProp,
|
|
174
260
|
ParamKeyAnnotation,
|
|
175
261
|
S,
|
|
176
262
|
UrlParser,
|
|
263
|
+
VisitResult,
|
|
264
|
+
findAnnotation,
|
|
265
|
+
findNode,
|
|
266
|
+
findProperty,
|
|
177
267
|
getAnnotation,
|
|
178
268
|
getParamKeyAnnotation,
|
|
179
|
-
|
|
180
|
-
|
|
269
|
+
getSimpleType,
|
|
270
|
+
isSimpleType,
|
|
181
271
|
visit
|
|
182
272
|
});
|
|
183
273
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/ast.ts", "../../../src/url.ts"
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\nimport type * as Types from 'effect/Types';\n\nexport { AST, JSONSchema, S, Types };\n\nexport * from './ast';\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST,
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["import_schema", "import_effect", "
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/ast.ts", "../../../src/url.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\nimport type * as Types from 'effect/Types';\n\n// TODO(dmaretskyi): Remove re-exports.\nexport { AST, JSONSchema, S, Types };\n\nexport * from './ast';\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\n\nimport { invariant } from '@dxos/invariant';\n\n//\n// Refs\n// https://effect.website/docs/guides/schema\n// https://www.npmjs.com/package/@effect/schema\n// https://effect-ts.github.io/effect/schema/AST.ts.html\n//\n\nexport type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';\n\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node)) {\n return 'object';\n }\n if (AST.isStringKeyword(node)) {\n return 'string';\n }\n if (AST.isNumberKeyword(node)) {\n return 'number';\n }\n if (AST.isBooleanKeyword(node)) {\n return 'boolean';\n }\n if (AST.isEnums(node)) {\n return 'enum';\n }\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST) => !!getSimpleType(node);\n\n//\n// Branded types\n//\n\nexport type JsonProp = string & { __JsonProp: true };\nexport type JsonPath = string & { __JsonPath: true };\n\nconst PATH_REGEX = /[a-zA-Z_$][\\w$]*(?:\\.[a-zA-Z_$][\\w$]*)*/;\nconst PROP_REGEX = /\\w+/;\n\n/**\n * https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html\n */\nexport const JsonPath = S.NonEmptyString.pipe(S.pattern(PATH_REGEX)) as any as S.Schema<JsonPath>;\nexport const JsonProp = S.NonEmptyString.pipe(S.pattern(PROP_REGEX)) as any as S.Schema<JsonProp>;\n\n/**\n * Get annotation or return undefined.\n */\nexport const getAnnotation =\n <T>(annotationId: symbol) =>\n (node: AST.Annotated): T | undefined =>\n pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n\nexport enum VisitResult {\n CONTINUE = 0,\n /**\n * Skip visiting children.\n */\n SKIP = 1,\n /**\n * Stop traversing immediately.\n */\n EXIT = 2,\n}\n\nexport type Path = (string | number)[];\n\nexport type Tester = (node: AST.AST, path: Path, depth: number) => VisitResult | undefined;\nexport type Visitor = (node: AST.AST, path: Path, depth: number) => void;\n\nconst defaultTest: Tester = (node) => (isSimpleType(node) ? VisitResult.CONTINUE : VisitResult.SKIP);\n\n/**\n * Visit leaf nodes.\n * Refs:\n * - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor\n * - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test\n */\nexport const visit: {\n (node: AST.AST, visitor: Visitor): void;\n (node: AST.AST, test: Tester, visitor: Visitor): void;\n} = (node: AST.AST, testOrVisitor: Tester | Visitor, visitor?: Visitor): void => {\n if (!visitor) {\n visitNode(node, defaultTest, testOrVisitor);\n } else {\n visitNode(node, testOrVisitor as Tester, visitor);\n }\n};\n\nconst visitNode = (\n node: AST.AST,\n test: Tester | undefined,\n visitor: Visitor,\n path: Path = [],\n depth = 0,\n): VisitResult | undefined => {\n const result = test?.(node, path, depth) ?? VisitResult.CONTINUE;\n if (result === VisitResult.EXIT) {\n return result;\n }\n if (result !== VisitResult.SKIP) {\n visitor(node, path, depth);\n }\n\n // Object.\n if (AST.isTypeLiteral(node)) {\n for (const prop of AST.getPropertySignatures(node)) {\n const currentPath = [...path, prop.name.toString()];\n const result = visitNode(prop.type, test, visitor, currentPath, depth + 1);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Array.\n else if (AST.isTupleType(node)) {\n for (const [i, element] of node.elements.entries()) {\n const currentPath = [...path, i];\n const result = visitNode(element.type, test, visitor, currentPath, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Branching union.\n else if (AST.isUnion(node)) {\n for (const type of node.types) {\n const result = visitNode(type, test, visitor, path, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Refinement.\n else if (AST.isRefinement(node)) {\n const result = visitNode(node.from, test, visitor, path, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n\n // TODO(burdon): Transform?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Reuse visitor.\nexport const findNode = (node: AST.AST, test: (node: AST.AST) => boolean): AST.AST | undefined => {\n if (test(node)) {\n return node;\n }\n\n // Object.\n else if (AST.isTypeLiteral(node)) {\n for (const prop of AST.getPropertySignatures(node)) {\n const child = findNode(prop.type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Array.\n else if (AST.isTupleType(node)) {\n for (const [_, element] of node.elements.entries()) {\n const child = findNode(element.type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Branching union.\n else if (AST.isUnion(node)) {\n for (const type of node.types) {\n const child = findNode(type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\n\n return undefined;\n};\n\n/**\n * Get the AST node for the given property (dot-path).\n */\nexport const findProperty = (schema: S.Schema<any>, path: JsonPath | JsonProp): AST.AST | undefined => {\n const getProp = (node: AST.AST, path: JsonProp[]): AST.AST | undefined => {\n const [name, ...rest] = path;\n const typeNode = findNode(node, AST.isTypeLiteral);\n invariant(typeNode);\n for (const prop of AST.getPropertySignatures(typeNode)) {\n if (prop.name === name) {\n if (rest.length) {\n return getProp(prop.type, rest);\n } else {\n return prop.type;\n }\n }\n }\n\n return undefined;\n };\n\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n/**\n * Recursively descend into AST to find first matching annotations\n */\nexport const findAnnotation = <T>(node: AST.AST, annotationId: symbol): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId);\n const getBaseAnnotation = (node: AST.AST): T | undefined => {\n const value = getAnnotationById(node);\n if (value !== undefined) {\n return value as T;\n }\n\n if (AST.isUnion(node)) {\n for (const type of node.types) {\n const value = getBaseAnnotation(type);\n if (value !== undefined) {\n return value as T;\n }\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\n\nimport { decamelize } from '@dxos/util';\n\nconst ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');\n\ntype ParamKeyAnnotationValue = { key: string };\n\nexport const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =\n AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);\n\nexport const ParamKeyAnnotation =\n (value: ParamKeyAnnotationValue) =>\n <S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>\n self.annotations({ [ParamKeyAnnotationId]: value });\n\n/**\n * HTTP params parser.\n * Supports custom key serialization.\n */\nexport class UrlParser<T extends Record<string, any>> {\n constructor(private readonly _schema: S.Struct<T>) {}\n\n /**\n * Parse URL params.\n */\n parse(_url: string): T {\n const url = new URL(_url);\n return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {\n let value = url.searchParams.get(decamelize(key));\n if (value == null) {\n value = url.searchParams.get(key);\n }\n\n if (value != null) {\n if (AST.isNumberKeyword(type.ast)) {\n params[key] = parseInt(value);\n } else if (AST.isBooleanKeyword(type.ast)) {\n params[key] = value === 'true' || value === '1';\n } else {\n params[key] = value;\n }\n }\n\n return params;\n }, {}) as T;\n }\n\n /**\n * Return URL with encoded params.\n */\n create(_url: string, params: T): URL {\n const url = new URL(_url);\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined) {\n const field = this._schema.fields[key];\n if (field) {\n const { key: serializedKey } = pipe(\n getParamKeyAnnotation(field.ast),\n Option.getOrElse(() => ({\n key: decamelize(key),\n })),\n );\n\n url.searchParams.set(serializedKey, String(value));\n }\n }\n });\n\n return url;\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,oBAA6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BAAOC,GAAAA,IAAOI,SAASvB,KAAAA;QACzB,WAAW9D,eAAAA,IAAII,iBAAiBgC,KAAKqB,GAAG,GAAG;AACzCuB,iBAAOC,GAAAA,IAAOnB,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACLkB,iBAAOC,GAAAA,IAAOnB;QAChB;MACF;AAEA,aAAOkB;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAM,OAAOZ,MAAcM,QAAgB;AACnC,UAAML,MAAM,IAAIC,IAAIF,IAAAA;AACpBG,WAAOpC,QAAQuC,MAAAA,EAAQO,QAAQ,CAAC,CAACN,KAAKnB,KAAAA,MAAM;AAC1C,UAAIA,UAAUb,QAAW;AACvB,cAAMuC,QAAQ,KAAKhB,QAAQM,OAAOG,GAAAA;AAClC,YAAIO,OAAO;AACT,gBAAM,EAAEP,KAAKQ,cAAa,QAAK5E,eAAAA,MAC7BqD,sBAAsBsB,MAAM/B,GAAG,GAC/BvC,eAAAA,OAAOwE,UAAU,OAAO;YACtBT,SAAKG,wBAAWH,GAAAA;UAClB,EAAA,CAAA;AAGFN,cAAIO,aAAaS,IAAIF,eAAeG,OAAO9B,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOa;EACT;AACF;",
|
|
6
|
+
"names": ["import_schema", "import_effect", "getSimpleType", "node", "AST", "isObjectKeyword", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "S", "NonEmptyString", "pipe", "pattern", "JsonProp", "getAnnotation", "annotationId", "Option", "getOrUndefined", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "result", "isTypeLiteral", "prop", "getPropertySignatures", "currentPath", "name", "toString", "type", "isTupleType", "i", "element", "elements", "entries", "isUnion", "types", "isRefinement", "from", "findNode", "child", "_", "undefined", "findProperty", "schema", "getProp", "rest", "typeNode", "invariant", "length", "ast", "split", "findAnnotation", "getAnnotationById", "getBaseAnnotation", "value", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "ParamKeyAnnotation", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "fields", "reduce", "params", "key", "searchParams", "get", "decamelize", "parseInt", "create", "forEach", "field", "serializedKey", "getOrElse", "set", "String"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/effect/src/ast.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/common/effect/src/ast.ts":{"bytes":21181,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7711,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":1150,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"packages/common/effect/src/ast.ts","kind":"import-statement","original":"./ast"},{"path":"packages/common/effect/src/url.ts","kind":"import-statement","original":"./url"}],"format":"esm"}},"outputs":{"packages/common/effect/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":15594},"packages/common/effect/dist/lib/node/index.cjs":{"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["AST","JSONSchema","JsonPath","JsonProp","ParamKeyAnnotation","S","UrlParser","VisitResult","findAnnotation","findNode","findProperty","getAnnotation","getParamKeyAnnotation","getSimpleType","isSimpleType","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":72},"packages/common/effect/src/ast.ts":{"bytesInOutput":4515},"packages/common/effect/src/url.ts":{"bytesInOutput":1624}},"bytes":6616}}}
|