@dxos/effect 0.6.12-staging.e11e696 → 0.6.13-main.041e8aa
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 +103 -9
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +102 -9
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +103 -9
- 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 +20 -0
- package/dist/types/src/ast.d.ts.map +1 -0
- package/dist/types/src/ast.test.d.ts +2 -0
- package/dist/types/src/ast.test.d.ts.map +1 -0
- package/dist/types/src/decamelize.d.ts +2 -0
- package/dist/types/src/decamelize.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +3 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/ast.test.ts +49 -0
- package/src/ast.ts +81 -0
- package/src/decamelize.ts +36 -0
- package/src/index.ts +3 -1
- package/src/url.ts +3 -2
|
@@ -8,14 +8,103 @@ import {
|
|
|
8
8
|
} from "@dxos/node-std/inject-globals";
|
|
9
9
|
|
|
10
10
|
// packages/common/effect/src/index.ts
|
|
11
|
-
import { AST as
|
|
11
|
+
import { AST as AST3, JSONSchema, Schema as S } from "@effect/schema";
|
|
12
12
|
|
|
13
|
-
// packages/common/effect/src/
|
|
13
|
+
// packages/common/effect/src/ast.ts
|
|
14
14
|
import { AST } from "@effect/schema";
|
|
15
15
|
import { Option, pipe } from "effect";
|
|
16
|
-
import {
|
|
16
|
+
import { invariant } from "@dxos/invariant";
|
|
17
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/effect/src/ast.ts";
|
|
18
|
+
var getAnnotation = (annotationId, node) => pipe(AST.getAnnotation(annotationId)(node), Option.getOrUndefined);
|
|
19
|
+
var getType = (node) => {
|
|
20
|
+
if (AST.isUnion(node)) {
|
|
21
|
+
return node.types.find((type) => getType(type));
|
|
22
|
+
} else if (AST.isRefinement(node)) {
|
|
23
|
+
return getType(node.from);
|
|
24
|
+
} else {
|
|
25
|
+
return node;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var getProperty = (schema, path) => {
|
|
29
|
+
let node = schema.ast;
|
|
30
|
+
for (const part of path.split(".")) {
|
|
31
|
+
const props = AST.getPropertySignatures(node);
|
|
32
|
+
const prop = props.find((prop2) => prop2.name === part);
|
|
33
|
+
if (!prop) {
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
const type = getType(prop.type);
|
|
37
|
+
invariant(type, `invalid type: ${path}`, {
|
|
38
|
+
F: __dxlog_file,
|
|
39
|
+
L: 50,
|
|
40
|
+
S: void 0,
|
|
41
|
+
A: [
|
|
42
|
+
"type",
|
|
43
|
+
"`invalid type: ${path}`"
|
|
44
|
+
]
|
|
45
|
+
});
|
|
46
|
+
node = type;
|
|
47
|
+
}
|
|
48
|
+
return node;
|
|
49
|
+
};
|
|
50
|
+
var visit = (node, visitor) => visitNode(node, visitor);
|
|
51
|
+
var visitNode = (node, visitor, path = []) => {
|
|
52
|
+
for (const prop of AST.getPropertySignatures(node)) {
|
|
53
|
+
const currentPath = [
|
|
54
|
+
...path,
|
|
55
|
+
prop.name.toString()
|
|
56
|
+
];
|
|
57
|
+
const type = getType(prop.type);
|
|
58
|
+
if (type) {
|
|
59
|
+
if (AST.isTypeLiteral(type)) {
|
|
60
|
+
visitNode(type, visitor, currentPath);
|
|
61
|
+
} else {
|
|
62
|
+
const ok = visitor(type, currentPath);
|
|
63
|
+
if (ok === false) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// packages/common/effect/src/url.ts
|
|
72
|
+
import { AST as AST2 } from "@effect/schema";
|
|
73
|
+
import { Option as Option2, pipe as pipe2 } from "effect";
|
|
74
|
+
|
|
75
|
+
// packages/common/effect/src/decamelize.ts
|
|
76
|
+
var LOW_DASH = "_".codePointAt(0);
|
|
77
|
+
var SMALL_A = "a".codePointAt(0);
|
|
78
|
+
var CAPITAL_A = "A".codePointAt(0);
|
|
79
|
+
var SMALL_Z = "z".codePointAt(0);
|
|
80
|
+
var CAPITAL_Z = "Z".codePointAt(0);
|
|
81
|
+
var isLower = (char) => char >= SMALL_A && char <= SMALL_Z;
|
|
82
|
+
var isUpper = (char) => char >= CAPITAL_A && char <= CAPITAL_Z;
|
|
83
|
+
var toLower = (char) => char + 32;
|
|
84
|
+
var decamelize = (str) => {
|
|
85
|
+
const firstChar = str.charCodeAt(0);
|
|
86
|
+
if (!isLower(firstChar)) {
|
|
87
|
+
return str;
|
|
88
|
+
}
|
|
89
|
+
const length = str.length;
|
|
90
|
+
let changed = false;
|
|
91
|
+
const out = [];
|
|
92
|
+
for (let i = 0; i < length; ++i) {
|
|
93
|
+
const c = str.charCodeAt(i);
|
|
94
|
+
if (isUpper(c)) {
|
|
95
|
+
out.push(LOW_DASH);
|
|
96
|
+
out.push(toLower(c));
|
|
97
|
+
changed = true;
|
|
98
|
+
} else {
|
|
99
|
+
out.push(c);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return changed ? String.fromCharCode.apply(void 0, out) : str;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// packages/common/effect/src/url.ts
|
|
17
106
|
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
18
|
-
var getParamKeyAnnotation =
|
|
107
|
+
var getParamKeyAnnotation = AST2.getAnnotation(ParamKeyAnnotationId);
|
|
19
108
|
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
20
109
|
[ParamKeyAnnotationId]: value
|
|
21
110
|
});
|
|
@@ -34,9 +123,9 @@ var UrlParser = class {
|
|
|
34
123
|
value = url.searchParams.get(key);
|
|
35
124
|
}
|
|
36
125
|
if (value != null) {
|
|
37
|
-
if (
|
|
126
|
+
if (AST2.isNumberKeyword(type.ast)) {
|
|
38
127
|
params[key] = parseInt(value);
|
|
39
|
-
} else if (
|
|
128
|
+
} else if (AST2.isBooleanKeyword(type.ast)) {
|
|
40
129
|
params[key] = value === "true" || value === "1";
|
|
41
130
|
} else {
|
|
42
131
|
params[key] = value;
|
|
@@ -54,7 +143,7 @@ var UrlParser = class {
|
|
|
54
143
|
if (value !== void 0) {
|
|
55
144
|
const field = this._schema.fields[key];
|
|
56
145
|
if (field) {
|
|
57
|
-
const { key: serializedKey } =
|
|
146
|
+
const { key: serializedKey } = pipe2(getParamKeyAnnotation(field.ast), Option2.getOrElse(() => ({
|
|
58
147
|
key: decamelize(key)
|
|
59
148
|
})));
|
|
60
149
|
url.searchParams.set(serializedKey, String(value));
|
|
@@ -65,10 +154,15 @@ var UrlParser = class {
|
|
|
65
154
|
}
|
|
66
155
|
};
|
|
67
156
|
export {
|
|
68
|
-
|
|
157
|
+
AST3 as AST,
|
|
69
158
|
JSONSchema,
|
|
70
159
|
ParamKeyAnnotation,
|
|
71
160
|
S,
|
|
72
|
-
UrlParser
|
|
161
|
+
UrlParser,
|
|
162
|
+
getAnnotation,
|
|
163
|
+
getParamKeyAnnotation,
|
|
164
|
+
getProperty,
|
|
165
|
+
getType,
|
|
166
|
+
visit
|
|
73
167
|
};
|
|
74
168
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/url.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\n\nexport { AST, JSONSchema, S };\n\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\nimport { decamelize } from '
|
|
5
|
-
"mappings": ";;;;;;;;;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;
|
|
6
|
-
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Option", "pipe", "decamelize", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "AST", "getAnnotation", "ParamKeyAnnotation", "value", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "entries", "fields", "reduce", "params", "key", "type", "searchParams", "get", "decamelize", "isNumberKeyword", "ast", "parseInt", "isBooleanKeyword", "create", "forEach", "undefined", "field", "serializedKey", "pipe", "Option", "getOrElse", "set", "String"]
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/ast.ts", "../../../src/url.ts", "../../../src/decamelize.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, type 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\n/**\n * Get annotation or return undefined.\n */\nexport const getAnnotation = <T>(annotationId: symbol, node: AST.Annotated): T | undefined =>\n pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n\n/**\n * Get type node.\n */\nexport const getType = (node: AST.AST): AST.AST | undefined => {\n if (AST.isUnion(node)) {\n return node.types.find((type) => getType(type));\n } else if (AST.isRefinement(node)) {\n return getType(node.from);\n } else {\n return node;\n }\n};\n\n/**\n * Get the AST node for the given property (dot-path).\n */\nexport const getProperty = (schema: S.Schema<any>, path: string): AST.AST | undefined => {\n let node: AST.AST = schema.ast;\n for (const part of path.split('.')) {\n const props = AST.getPropertySignatures(node);\n const prop = props.find((prop) => prop.name === part);\n if (!prop) {\n return undefined;\n }\n\n // TODO(burdon): Check if leaf.\n const type = getType(prop.type);\n invariant(type, `invalid type: ${path}`);\n node = type;\n }\n\n return node;\n};\n\nexport type Visitor = (node: AST.AST, path: string[]) => boolean | void;\n\n/**\n * Visit leaf nodes.\n * Ref: https://www.npmjs.com/package/unist-util-visit#visitor\n */\nexport const visit = (node: AST.AST, visitor: Visitor) => visitNode(node, visitor);\n\nconst visitNode = (node: AST.AST, visitor: Visitor, path: string[] = []) => {\n for (const prop of AST.getPropertySignatures(node)) {\n const currentPath = [...path, prop.name.toString()];\n const type = getType(prop.type);\n if (type) {\n if (AST.isTypeLiteral(type)) {\n visitNode(type, visitor, currentPath);\n } else {\n // NOTE: Only visits leaf nodes.\n const ok = visitor(type, currentPath);\n if (ok === false) {\n return;\n }\n }\n }\n }\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 './decamelize';\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", "//\n// Copyright 2024 DXOS.org\n//\n\nconst LOW_DASH = '_'.codePointAt(0)!;\nconst SMALL_A = 'a'.codePointAt(0)!;\nconst CAPITAL_A = 'A'.codePointAt(0)!;\nconst SMALL_Z = 'z'.codePointAt(0)!;\nconst CAPITAL_Z = 'Z'.codePointAt(0)!;\n\nconst isLower = (char: number) => char >= SMALL_A && char <= SMALL_Z;\n\nconst isUpper = (char: number) => char >= CAPITAL_A && char <= CAPITAL_Z;\n\nconst toLower = (char: number) => char + 0x20;\n\nexport const decamelize = (str: string) => {\n const firstChar = str.charCodeAt(0);\n if (!isLower(firstChar)) {\n return str;\n }\n const length = str.length;\n let changed = false;\n const out: number[] = [];\n for (let i = 0; i < length; ++i) {\n const c = str.charCodeAt(i);\n if (isUpper(c)) {\n out.push(LOW_DASH);\n out.push(toLower(c));\n changed = true;\n } else {\n out.push(c);\n }\n }\n return changed ? String.fromCharCode.apply(undefined, out) : str;\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAU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,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Option", "pipe", "invariant", "getAnnotation", "annotationId", "node", "getOrUndefined", "getType", "isUnion", "types", "find", "type", "isRefinement", "from", "getProperty", "schema", "path", "ast", "part", "split", "props", "getPropertySignatures", "prop", "name", "undefined", "visit", "visitor", "visitNode", "currentPath", "toString", "isTypeLiteral", "ok", "AST", "Option", "pipe", "LOW_DASH", "codePointAt", "SMALL_A", "CAPITAL_A", "SMALL_Z", "CAPITAL_Z", "isLower", "char", "isUpper", "toLower", "decamelize", "str", "firstChar", "charCodeAt", "length", "changed", "out", "i", "c", "push", "String", "fromCharCode", "apply", "undefined", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "AST", "getAnnotation", "ParamKeyAnnotation", "value", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "entries", "fields", "reduce", "params", "key", "type", "searchParams", "get", "decamelize", "isNumberKeyword", "ast", "parseInt", "isBooleanKeyword", "create", "forEach", "undefined", "field", "serializedKey", "pipe", "Option", "getOrElse", "set", "String"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"inject-globals:@inject-globals":{"bytes":324,"imports":[{"path":"@dxos/node-std/inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/
|
|
1
|
+
{"inputs":{"inject-globals:@inject-globals":{"bytes":324,"imports":[{"path":"@dxos/node-std/inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/ast.ts":{"bytes":7851,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/decamelize.ts":{"bytes":3867,"imports":[{"path":"@inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7713,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"packages/common/effect/src/decamelize.ts","kind":"import-statement","original":"./decamelize"},{"path":"@inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":1034,"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"},{"path":"@inject-globals","kind":"import-statement","external":true}],"format":"esm"}},"outputs":{"packages/common/effect/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":10184},"packages/common/effect/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/node-std/inject-globals","kind":"import-statement","external":true},{"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}],"exports":["AST","JSONSchema","ParamKeyAnnotation","S","UrlParser","getAnnotation","getParamKeyAnnotation","getProperty","getType","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"inject-globals:@inject-globals":{"bytesInOutput":79},"packages/common/effect/src/index.ts":{"bytesInOutput":71},"packages/common/effect/src/ast.ts":{"bytesInOutput":1553},"packages/common/effect/src/url.ts":{"bytesInOutput":1583},"packages/common/effect/src/decamelize.ts":{"bytesInOutput":798}},"bytes":4543}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -22,15 +22,103 @@ __export(node_exports, {
|
|
|
22
22
|
JSONSchema: () => import_schema.JSONSchema,
|
|
23
23
|
ParamKeyAnnotation: () => ParamKeyAnnotation,
|
|
24
24
|
S: () => import_schema.Schema,
|
|
25
|
-
UrlParser: () => UrlParser
|
|
25
|
+
UrlParser: () => UrlParser,
|
|
26
|
+
getAnnotation: () => getAnnotation,
|
|
27
|
+
getParamKeyAnnotation: () => getParamKeyAnnotation,
|
|
28
|
+
getProperty: () => getProperty,
|
|
29
|
+
getType: () => getType,
|
|
30
|
+
visit: () => visit
|
|
26
31
|
});
|
|
27
32
|
module.exports = __toCommonJS(node_exports);
|
|
28
33
|
var import_schema = require("@effect/schema");
|
|
29
34
|
var import_schema2 = require("@effect/schema");
|
|
30
35
|
var import_effect = require("effect");
|
|
31
|
-
var
|
|
36
|
+
var import_invariant = require("@dxos/invariant");
|
|
37
|
+
var import_schema3 = require("@effect/schema");
|
|
38
|
+
var import_effect2 = require("effect");
|
|
39
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/effect/src/ast.ts";
|
|
40
|
+
var getAnnotation = (annotationId, node) => (0, import_effect.pipe)(import_schema2.AST.getAnnotation(annotationId)(node), import_effect.Option.getOrUndefined);
|
|
41
|
+
var getType = (node) => {
|
|
42
|
+
if (import_schema2.AST.isUnion(node)) {
|
|
43
|
+
return node.types.find((type) => getType(type));
|
|
44
|
+
} else if (import_schema2.AST.isRefinement(node)) {
|
|
45
|
+
return getType(node.from);
|
|
46
|
+
} else {
|
|
47
|
+
return node;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var getProperty = (schema, path) => {
|
|
51
|
+
let node = schema.ast;
|
|
52
|
+
for (const part of path.split(".")) {
|
|
53
|
+
const props = import_schema2.AST.getPropertySignatures(node);
|
|
54
|
+
const prop = props.find((prop2) => prop2.name === part);
|
|
55
|
+
if (!prop) {
|
|
56
|
+
return void 0;
|
|
57
|
+
}
|
|
58
|
+
const type = getType(prop.type);
|
|
59
|
+
(0, import_invariant.invariant)(type, `invalid type: ${path}`, {
|
|
60
|
+
F: __dxlog_file,
|
|
61
|
+
L: 50,
|
|
62
|
+
S: void 0,
|
|
63
|
+
A: [
|
|
64
|
+
"type",
|
|
65
|
+
"`invalid type: ${path}`"
|
|
66
|
+
]
|
|
67
|
+
});
|
|
68
|
+
node = type;
|
|
69
|
+
}
|
|
70
|
+
return node;
|
|
71
|
+
};
|
|
72
|
+
var visit = (node, visitor) => visitNode(node, visitor);
|
|
73
|
+
var visitNode = (node, visitor, path = []) => {
|
|
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;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
var LOW_DASH = "_".codePointAt(0);
|
|
93
|
+
var SMALL_A = "a".codePointAt(0);
|
|
94
|
+
var CAPITAL_A = "A".codePointAt(0);
|
|
95
|
+
var SMALL_Z = "z".codePointAt(0);
|
|
96
|
+
var CAPITAL_Z = "Z".codePointAt(0);
|
|
97
|
+
var isLower = (char) => char >= SMALL_A && char <= SMALL_Z;
|
|
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);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return changed ? String.fromCharCode.apply(void 0, out) : str;
|
|
119
|
+
};
|
|
32
120
|
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
33
|
-
var getParamKeyAnnotation =
|
|
121
|
+
var getParamKeyAnnotation = import_schema3.AST.getAnnotation(ParamKeyAnnotationId);
|
|
34
122
|
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
35
123
|
[ParamKeyAnnotationId]: value
|
|
36
124
|
});
|
|
@@ -44,14 +132,14 @@ var UrlParser = class {
|
|
|
44
132
|
parse(_url) {
|
|
45
133
|
const url = new URL(_url);
|
|
46
134
|
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
47
|
-
let value = url.searchParams.get(
|
|
135
|
+
let value = url.searchParams.get(decamelize(key));
|
|
48
136
|
if (value == null) {
|
|
49
137
|
value = url.searchParams.get(key);
|
|
50
138
|
}
|
|
51
139
|
if (value != null) {
|
|
52
|
-
if (
|
|
140
|
+
if (import_schema3.AST.isNumberKeyword(type.ast)) {
|
|
53
141
|
params[key] = parseInt(value);
|
|
54
|
-
} else if (
|
|
142
|
+
} else if (import_schema3.AST.isBooleanKeyword(type.ast)) {
|
|
55
143
|
params[key] = value === "true" || value === "1";
|
|
56
144
|
} else {
|
|
57
145
|
params[key] = value;
|
|
@@ -69,8 +157,8 @@ var UrlParser = class {
|
|
|
69
157
|
if (value !== void 0) {
|
|
70
158
|
const field = this._schema.fields[key];
|
|
71
159
|
if (field) {
|
|
72
|
-
const { key: serializedKey } = (0,
|
|
73
|
-
key:
|
|
160
|
+
const { key: serializedKey } = (0, import_effect2.pipe)(getParamKeyAnnotation(field.ast), import_effect2.Option.getOrElse(() => ({
|
|
161
|
+
key: decamelize(key)
|
|
74
162
|
})));
|
|
75
163
|
url.searchParams.set(serializedKey, String(value));
|
|
76
164
|
}
|
|
@@ -85,6 +173,11 @@ var UrlParser = class {
|
|
|
85
173
|
JSONSchema,
|
|
86
174
|
ParamKeyAnnotation,
|
|
87
175
|
S,
|
|
88
|
-
UrlParser
|
|
176
|
+
UrlParser,
|
|
177
|
+
getAnnotation,
|
|
178
|
+
getParamKeyAnnotation,
|
|
179
|
+
getProperty,
|
|
180
|
+
getType,
|
|
181
|
+
visit
|
|
89
182
|
});
|
|
90
183
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/url.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\n\nexport { AST, JSONSchema, S };\n\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\nimport { decamelize } from '
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["import_schema", "
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/ast.ts", "../../../src/url.ts", "../../../src/decamelize.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, type 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\n/**\n * Get annotation or return undefined.\n */\nexport const getAnnotation = <T>(annotationId: symbol, node: AST.Annotated): T | undefined =>\n pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n\n/**\n * Get type node.\n */\nexport const getType = (node: AST.AST): AST.AST | undefined => {\n if (AST.isUnion(node)) {\n return node.types.find((type) => getType(type));\n } else if (AST.isRefinement(node)) {\n return getType(node.from);\n } else {\n return node;\n }\n};\n\n/**\n * Get the AST node for the given property (dot-path).\n */\nexport const getProperty = (schema: S.Schema<any>, path: string): AST.AST | undefined => {\n let node: AST.AST = schema.ast;\n for (const part of path.split('.')) {\n const props = AST.getPropertySignatures(node);\n const prop = props.find((prop) => prop.name === part);\n if (!prop) {\n return undefined;\n }\n\n // TODO(burdon): Check if leaf.\n const type = getType(prop.type);\n invariant(type, `invalid type: ${path}`);\n node = type;\n }\n\n return node;\n};\n\nexport type Visitor = (node: AST.AST, path: string[]) => boolean | void;\n\n/**\n * Visit leaf nodes.\n * Ref: https://www.npmjs.com/package/unist-util-visit#visitor\n */\nexport const visit = (node: AST.AST, visitor: Visitor) => visitNode(node, visitor);\n\nconst visitNode = (node: AST.AST, visitor: Visitor, path: string[] = []) => {\n for (const prop of AST.getPropertySignatures(node)) {\n const currentPath = [...path, prop.name.toString()];\n const type = getType(prop.type);\n if (type) {\n if (AST.isTypeLiteral(type)) {\n visitNode(type, visitor, currentPath);\n } else {\n // NOTE: Only visits leaf nodes.\n const ok = visitor(type, currentPath);\n if (ok === false) {\n return;\n }\n }\n }\n }\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 './decamelize';\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", "//\n// Copyright 2024 DXOS.org\n//\n\nconst LOW_DASH = '_'.codePointAt(0)!;\nconst SMALL_A = 'a'.codePointAt(0)!;\nconst CAPITAL_A = 'A'.codePointAt(0)!;\nconst SMALL_Z = 'z'.codePointAt(0)!;\nconst CAPITAL_Z = 'Z'.codePointAt(0)!;\n\nconst isLower = (char: number) => char >= SMALL_A && char <= SMALL_Z;\n\nconst isUpper = (char: number) => char >= CAPITAL_A && char <= CAPITAL_Z;\n\nconst toLower = (char: number) => char + 0x20;\n\nexport const decamelize = (str: string) => {\n const firstChar = str.charCodeAt(0);\n if (!isLower(firstChar)) {\n return str;\n }\n const length = str.length;\n let changed = false;\n const out: number[] = [];\n for (let i = 0; i < length; ++i) {\n const c = str.charCodeAt(i);\n if (isUpper(c)) {\n out.push(LOW_DASH);\n out.push(toLower(c));\n changed = true;\n } else {\n out.push(c);\n }\n }\n return changed ? String.fromCharCode.apply(undefined, out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AAA,CAAA;AAGFP,cAAIQ,aAAaU,IAAIF,eAAehC,OAAOQ,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["import_schema", "import_effect", "getAnnotation", "annotationId", "node", "pipe", "AST", "Option", "getOrUndefined", "getType", "isUnion", "types", "find", "type", "isRefinement", "from", "getProperty", "schema", "path", "ast", "part", "split", "props", "getPropertySignatures", "prop", "name", "undefined", "invariant", "visit", "visitor", "visitNode", "currentPath", "toString", "isTypeLiteral", "ok", "LOW_DASH", "codePointAt", "SMALL_A", "CAPITAL_A", "SMALL_Z", "CAPITAL_Z", "isLower", "char", "isUpper", "toLower", "decamelize", "str", "firstChar", "charCodeAt", "length", "changed", "out", "i", "c", "push", "String", "fromCharCode", "apply", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "ParamKeyAnnotation", "value", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "entries", "fields", "reduce", "params", "key", "searchParams", "get", "isNumberKeyword", "parseInt", "isBooleanKeyword", "create", "forEach", "field", "serializedKey", "getOrElse", "set"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/effect/src/
|
|
1
|
+
{"inputs":{"packages/common/effect/src/ast.ts":{"bytes":7851,"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/decamelize.ts":{"bytes":3867,"imports":[],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7713,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"packages/common/effect/src/decamelize.ts","kind":"import-statement","original":"./decamelize"}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":1034,"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":10175},"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}],"exports":["AST","JSONSchema","ParamKeyAnnotation","S","UrlParser","getAnnotation","getParamKeyAnnotation","getProperty","getType","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":71},"packages/common/effect/src/ast.ts":{"bytesInOutput":1553},"packages/common/effect/src/url.ts":{"bytesInOutput":1583},"packages/common/effect/src/decamelize.ts":{"bytesInOutput":798}},"bytes":4395}}}
|
|
@@ -1,14 +1,103 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
2
|
|
|
3
3
|
// packages/common/effect/src/index.ts
|
|
4
|
-
import { AST as
|
|
4
|
+
import { AST as AST3, JSONSchema, Schema as S } from "@effect/schema";
|
|
5
5
|
|
|
6
|
-
// packages/common/effect/src/
|
|
6
|
+
// packages/common/effect/src/ast.ts
|
|
7
7
|
import { AST } from "@effect/schema";
|
|
8
8
|
import { Option, pipe } from "effect";
|
|
9
|
-
import {
|
|
9
|
+
import { invariant } from "@dxos/invariant";
|
|
10
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/effect/src/ast.ts";
|
|
11
|
+
var getAnnotation = (annotationId, node) => pipe(AST.getAnnotation(annotationId)(node), Option.getOrUndefined);
|
|
12
|
+
var getType = (node) => {
|
|
13
|
+
if (AST.isUnion(node)) {
|
|
14
|
+
return node.types.find((type) => getType(type));
|
|
15
|
+
} else if (AST.isRefinement(node)) {
|
|
16
|
+
return getType(node.from);
|
|
17
|
+
} else {
|
|
18
|
+
return node;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var getProperty = (schema, path) => {
|
|
22
|
+
let node = schema.ast;
|
|
23
|
+
for (const part of path.split(".")) {
|
|
24
|
+
const props = AST.getPropertySignatures(node);
|
|
25
|
+
const prop = props.find((prop2) => prop2.name === part);
|
|
26
|
+
if (!prop) {
|
|
27
|
+
return void 0;
|
|
28
|
+
}
|
|
29
|
+
const type = getType(prop.type);
|
|
30
|
+
invariant(type, `invalid type: ${path}`, {
|
|
31
|
+
F: __dxlog_file,
|
|
32
|
+
L: 50,
|
|
33
|
+
S: void 0,
|
|
34
|
+
A: [
|
|
35
|
+
"type",
|
|
36
|
+
"`invalid type: ${path}`"
|
|
37
|
+
]
|
|
38
|
+
});
|
|
39
|
+
node = type;
|
|
40
|
+
}
|
|
41
|
+
return node;
|
|
42
|
+
};
|
|
43
|
+
var visit = (node, visitor) => visitNode(node, visitor);
|
|
44
|
+
var visitNode = (node, visitor, path = []) => {
|
|
45
|
+
for (const prop of AST.getPropertySignatures(node)) {
|
|
46
|
+
const currentPath = [
|
|
47
|
+
...path,
|
|
48
|
+
prop.name.toString()
|
|
49
|
+
];
|
|
50
|
+
const type = getType(prop.type);
|
|
51
|
+
if (type) {
|
|
52
|
+
if (AST.isTypeLiteral(type)) {
|
|
53
|
+
visitNode(type, visitor, currentPath);
|
|
54
|
+
} else {
|
|
55
|
+
const ok = visitor(type, currentPath);
|
|
56
|
+
if (ok === false) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// packages/common/effect/src/url.ts
|
|
65
|
+
import { AST as AST2 } from "@effect/schema";
|
|
66
|
+
import { Option as Option2, pipe as pipe2 } from "effect";
|
|
67
|
+
|
|
68
|
+
// packages/common/effect/src/decamelize.ts
|
|
69
|
+
var LOW_DASH = "_".codePointAt(0);
|
|
70
|
+
var SMALL_A = "a".codePointAt(0);
|
|
71
|
+
var CAPITAL_A = "A".codePointAt(0);
|
|
72
|
+
var SMALL_Z = "z".codePointAt(0);
|
|
73
|
+
var CAPITAL_Z = "Z".codePointAt(0);
|
|
74
|
+
var isLower = (char) => char >= SMALL_A && char <= SMALL_Z;
|
|
75
|
+
var isUpper = (char) => char >= CAPITAL_A && char <= CAPITAL_Z;
|
|
76
|
+
var toLower = (char) => char + 32;
|
|
77
|
+
var decamelize = (str) => {
|
|
78
|
+
const firstChar = str.charCodeAt(0);
|
|
79
|
+
if (!isLower(firstChar)) {
|
|
80
|
+
return str;
|
|
81
|
+
}
|
|
82
|
+
const length = str.length;
|
|
83
|
+
let changed = false;
|
|
84
|
+
const out = [];
|
|
85
|
+
for (let i = 0; i < length; ++i) {
|
|
86
|
+
const c = str.charCodeAt(i);
|
|
87
|
+
if (isUpper(c)) {
|
|
88
|
+
out.push(LOW_DASH);
|
|
89
|
+
out.push(toLower(c));
|
|
90
|
+
changed = true;
|
|
91
|
+
} else {
|
|
92
|
+
out.push(c);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return changed ? String.fromCharCode.apply(void 0, out) : str;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// packages/common/effect/src/url.ts
|
|
10
99
|
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
11
|
-
var getParamKeyAnnotation =
|
|
100
|
+
var getParamKeyAnnotation = AST2.getAnnotation(ParamKeyAnnotationId);
|
|
12
101
|
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
13
102
|
[ParamKeyAnnotationId]: value
|
|
14
103
|
});
|
|
@@ -27,9 +116,9 @@ var UrlParser = class {
|
|
|
27
116
|
value = url.searchParams.get(key);
|
|
28
117
|
}
|
|
29
118
|
if (value != null) {
|
|
30
|
-
if (
|
|
119
|
+
if (AST2.isNumberKeyword(type.ast)) {
|
|
31
120
|
params[key] = parseInt(value);
|
|
32
|
-
} else if (
|
|
121
|
+
} else if (AST2.isBooleanKeyword(type.ast)) {
|
|
33
122
|
params[key] = value === "true" || value === "1";
|
|
34
123
|
} else {
|
|
35
124
|
params[key] = value;
|
|
@@ -47,7 +136,7 @@ var UrlParser = class {
|
|
|
47
136
|
if (value !== void 0) {
|
|
48
137
|
const field = this._schema.fields[key];
|
|
49
138
|
if (field) {
|
|
50
|
-
const { key: serializedKey } =
|
|
139
|
+
const { key: serializedKey } = pipe2(getParamKeyAnnotation(field.ast), Option2.getOrElse(() => ({
|
|
51
140
|
key: decamelize(key)
|
|
52
141
|
})));
|
|
53
142
|
url.searchParams.set(serializedKey, String(value));
|
|
@@ -58,10 +147,15 @@ var UrlParser = class {
|
|
|
58
147
|
}
|
|
59
148
|
};
|
|
60
149
|
export {
|
|
61
|
-
|
|
150
|
+
AST3 as AST,
|
|
62
151
|
JSONSchema,
|
|
63
152
|
ParamKeyAnnotation,
|
|
64
153
|
S,
|
|
65
|
-
UrlParser
|
|
154
|
+
UrlParser,
|
|
155
|
+
getAnnotation,
|
|
156
|
+
getParamKeyAnnotation,
|
|
157
|
+
getProperty,
|
|
158
|
+
getType,
|
|
159
|
+
visit
|
|
66
160
|
};
|
|
67
161
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/url.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\n\nexport { AST, JSONSchema, S };\n\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\nimport { decamelize } from '
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;
|
|
6
|
-
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Option", "pipe", "decamelize", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "AST", "getAnnotation", "ParamKeyAnnotation", "value", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "entries", "fields", "reduce", "params", "key", "type", "searchParams", "get", "decamelize", "isNumberKeyword", "ast", "parseInt", "isBooleanKeyword", "create", "forEach", "undefined", "field", "serializedKey", "pipe", "Option", "getOrElse", "set", "String"]
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/ast.ts", "../../../src/url.ts", "../../../src/decamelize.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, type 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\n/**\n * Get annotation or return undefined.\n */\nexport const getAnnotation = <T>(annotationId: symbol, node: AST.Annotated): T | undefined =>\n pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n\n/**\n * Get type node.\n */\nexport const getType = (node: AST.AST): AST.AST | undefined => {\n if (AST.isUnion(node)) {\n return node.types.find((type) => getType(type));\n } else if (AST.isRefinement(node)) {\n return getType(node.from);\n } else {\n return node;\n }\n};\n\n/**\n * Get the AST node for the given property (dot-path).\n */\nexport const getProperty = (schema: S.Schema<any>, path: string): AST.AST | undefined => {\n let node: AST.AST = schema.ast;\n for (const part of path.split('.')) {\n const props = AST.getPropertySignatures(node);\n const prop = props.find((prop) => prop.name === part);\n if (!prop) {\n return undefined;\n }\n\n // TODO(burdon): Check if leaf.\n const type = getType(prop.type);\n invariant(type, `invalid type: ${path}`);\n node = type;\n }\n\n return node;\n};\n\nexport type Visitor = (node: AST.AST, path: string[]) => boolean | void;\n\n/**\n * Visit leaf nodes.\n * Ref: https://www.npmjs.com/package/unist-util-visit#visitor\n */\nexport const visit = (node: AST.AST, visitor: Visitor) => visitNode(node, visitor);\n\nconst visitNode = (node: AST.AST, visitor: Visitor, path: string[] = []) => {\n for (const prop of AST.getPropertySignatures(node)) {\n const currentPath = [...path, prop.name.toString()];\n const type = getType(prop.type);\n if (type) {\n if (AST.isTypeLiteral(type)) {\n visitNode(type, visitor, currentPath);\n } else {\n // NOTE: Only visits leaf nodes.\n const ok = visitor(type, currentPath);\n if (ok === false) {\n return;\n }\n }\n }\n }\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 './decamelize';\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", "//\n// Copyright 2024 DXOS.org\n//\n\nconst LOW_DASH = '_'.codePointAt(0)!;\nconst SMALL_A = 'a'.codePointAt(0)!;\nconst CAPITAL_A = 'A'.codePointAt(0)!;\nconst SMALL_Z = 'z'.codePointAt(0)!;\nconst CAPITAL_Z = 'Z'.codePointAt(0)!;\n\nconst isLower = (char: number) => char >= SMALL_A && char <= SMALL_Z;\n\nconst isUpper = (char: number) => char >= CAPITAL_A && char <= CAPITAL_Z;\n\nconst toLower = (char: number) => char + 0x20;\n\nexport const decamelize = (str: string) => {\n const firstChar = str.charCodeAt(0);\n if (!isLower(firstChar)) {\n return str;\n }\n const length = str.length;\n let changed = false;\n const out: number[] = [];\n for (let i = 0; i < length; ++i) {\n const c = str.charCodeAt(i);\n if (isUpper(c)) {\n out.push(LOW_DASH);\n out.push(toLower(c));\n changed = true;\n } else {\n out.push(c);\n }\n }\n return changed ? String.fromCharCode.apply(undefined, out) : str;\n};\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;AAE7B,SAASC,iBAAiB;;AAYnB,IAAMC,gBAAgB,CAAIC,cAAsBC,SACrDJ,KAAKF,IAA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B;AACrB,UAAMC,MAAM,IAAIC,IAAIF,IAAAA;AACpB,WAAOG,OAAOC,QAAQ,KAAKN,QAAQO,MAAM,EAAEC,OAA4B,CAACC,QAAQ,CAACC,KAAKC,IAAAA,MAAK;AACzF,UAAIhB,QAAQQ,IAAIS,aAAaC,IAAIC,WAAWJ,GAAAA,CAAAA;AAC5C,UAAIf,SAAS,MAAM;AACjBA,gBAAQQ,IAAIS,aAAaC,IAAIH,GAAAA;MAC/B;AAEA,UAAIf,SAAS,MAAM;AACjB,YAAIH,KAAIuB,gBAAgBJ,KAAKK,GAAG,GAAG;AACjCP,iBAAOC,GAAAA,IAAOO,SAAStB,KAAAA;QACzB,WAAWH,KAAI0B,iBAAiBP,KAAKK,GAAG,GAAG;AACzCP,iBAAOC,GAAAA,IAAOf,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACLc,iBAAOC,GAAAA,IAAOf;QAChB;MACF;AAEA,aAAOc;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAU,OAAOjB,MAAcO,QAAgB;AACnC,UAAMN,MAAM,IAAIC,IAAIF,IAAAA;AACpBG,WAAOC,QAAQG,MAAAA,EAAQW,QAAQ,CAAC,CAACV,KAAKf,KAAAA,MAAM;AAC1C,UAAIA,UAAU0B,QAAW;AACvB,cAAMC,QAAQ,KAAKtB,QAAQO,OAAOG,GAAAA;AAClC,YAAIY,OAAO;AACT,gBAAM,EAAEZ,KAAKa,cAAa,IAAKC,MAC7BjC,sBAAsB+B,MAAMN,GAAG,GAC/BS,QAAOC,UAAU,OAAO;YACtBhB,KAAKI,WAAWJ,GAAAA;UAClB,EAAA,CAAA;AAGFP,cAAIS,aAAae,IAAIJ,eAAeK,OAAOjC,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Option", "pipe", "invariant", "getAnnotation", "annotationId", "node", "getOrUndefined", "getType", "isUnion", "types", "find", "type", "isRefinement", "from", "getProperty", "schema", "path", "ast", "part", "split", "props", "getPropertySignatures", "prop", "name", "undefined", "visit", "visitor", "visitNode", "currentPath", "toString", "isTypeLiteral", "ok", "AST", "Option", "pipe", "LOW_DASH", "codePointAt", "SMALL_A", "CAPITAL_A", "SMALL_Z", "CAPITAL_Z", "isLower", "char", "isUpper", "toLower", "decamelize", "str", "firstChar", "charCodeAt", "length", "changed", "out", "i", "c", "push", "String", "fromCharCode", "apply", "undefined", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "AST", "getAnnotation", "ParamKeyAnnotation", "value", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "entries", "fields", "reduce", "params", "key", "type", "searchParams", "get", "decamelize", "isNumberKeyword", "ast", "parseInt", "isBooleanKeyword", "create", "forEach", "undefined", "field", "serializedKey", "pipe", "Option", "getOrElse", "set", "String"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/effect/src/
|
|
1
|
+
{"inputs":{"packages/common/effect/src/ast.ts":{"bytes":7851,"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/decamelize.ts":{"bytes":3867,"imports":[],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7713,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"packages/common/effect/src/decamelize.ts","kind":"import-statement","original":"./decamelize"}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":1034,"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-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":10177},"packages/common/effect/dist/lib/node-esm/index.mjs":{"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}],"exports":["AST","JSONSchema","ParamKeyAnnotation","S","UrlParser","getAnnotation","getParamKeyAnnotation","getProperty","getType","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":71},"packages/common/effect/src/ast.ts":{"bytesInOutput":1553},"packages/common/effect/src/url.ts":{"bytesInOutput":1583},"packages/common/effect/src/decamelize.ts":{"bytesInOutput":798}},"bytes":4488}}}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AST, type Schema as S } from '@effect/schema';
|
|
2
|
+
/**
|
|
3
|
+
* Get annotation or return undefined.
|
|
4
|
+
*/
|
|
5
|
+
export declare const getAnnotation: <T>(annotationId: symbol, node: AST.Annotated) => T | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* Get type node.
|
|
8
|
+
*/
|
|
9
|
+
export declare const getType: (node: AST.AST) => AST.AST | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Get the AST node for the given property (dot-path).
|
|
12
|
+
*/
|
|
13
|
+
export declare const getProperty: (schema: S.Schema<any>, path: string) => AST.AST | undefined;
|
|
14
|
+
export type Visitor = (node: AST.AST, path: string[]) => boolean | void;
|
|
15
|
+
/**
|
|
16
|
+
* Visit leaf nodes.
|
|
17
|
+
* Ref: https://www.npmjs.com/package/unist-util-visit#visitor
|
|
18
|
+
*/
|
|
19
|
+
export declare const visit: (node: AST.AST, visitor: Visitor) => void;
|
|
20
|
+
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../../src/ast.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,KAAK,MAAM,IAAI,CAAC,EAAE,MAAM,gBAAgB,CAAC;AAYvD;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,gBAAgB,MAAM,QAAQ,GAAG,CAAC,SAAS,KAAG,CAAC,GAAG,SACV,CAAC;AAExE;;GAEG;AACH,eAAO,MAAM,OAAO,SAAU,GAAG,CAAC,GAAG,KAAG,GAAG,CAAC,GAAG,GAAG,SAQjD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,WAAY,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,MAAM,KAAG,GAAG,CAAC,GAAG,GAAG,SAgB3E,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,GAAG,IAAI,CAAC;AAExE;;;GAGG;AACH,eAAO,MAAM,KAAK,SAAU,GAAG,CAAC,GAAG,WAAW,OAAO,SAA6B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.test.d.ts","sourceRoot":"","sources":["../../../src/ast.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decamelize.d.ts","sourceRoot":"","sources":["../../../src/decamelize.ts"],"names":[],"mappings":"AAgBA,eAAO,MAAM,UAAU,QAAS,MAAM,WAmBrC,CAAC"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { AST, JSONSchema, Schema as S } from '@effect/schema';
|
|
2
|
-
|
|
2
|
+
import type * as Types from 'effect/Types';
|
|
3
|
+
export { AST, JSONSchema, S, Types };
|
|
4
|
+
export * from './ast';
|
|
3
5
|
export * from './url';
|
|
4
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,KAAK,KAAK,KAAK,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC;AAErC,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC"}
|
package/dist/types/src/url.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { type Schema as S } from '@effect/schema';
|
|
1
|
+
import { AST, type Schema as S } from '@effect/schema';
|
|
2
|
+
import { Option } from 'effect';
|
|
2
3
|
type ParamKeyAnnotationValue = {
|
|
3
4
|
key: string;
|
|
4
5
|
};
|
|
6
|
+
export declare const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue>;
|
|
5
7
|
export declare const ParamKeyAnnotation: (value: ParamKeyAnnotationValue) => <S extends S.Annotable.All>(self: S) => S.Annotable.Self<S>;
|
|
6
8
|
/**
|
|
7
9
|
* HTTP params parser.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../../src/url.ts"],"names":[],"mappings":"AAIA,OAAO,
|
|
1
|
+
{"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../../src/url.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,KAAK,MAAM,IAAI,CAAC,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,MAAM,EAAQ,MAAM,QAAQ,CAAC;AAMtC,KAAK,uBAAuB,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/C,eAAO,MAAM,qBAAqB,EAAE,CAAC,SAAS,EAAE,GAAG,CAAC,SAAS,KAAK,MAAM,CAAC,MAAM,CAAC,uBAAuB,CACrC,CAAC;AAEnE,eAAO,MAAM,kBAAkB,UACrB,uBAAuB,MAC9B,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,GAAG,QAAQ,CAAC,KAAG,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CACH,CAAC;AAExD;;;GAGG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IACtC,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IAEjD;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC;IAsBtB;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,GAAG;CAoBrC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.13-main.041e8aa",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -25,16 +25,16 @@
|
|
|
25
25
|
"src"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"
|
|
29
|
-
"@dxos/node-std": "0.6.
|
|
28
|
+
"@dxos/invariant": "0.6.13-main.041e8aa",
|
|
29
|
+
"@dxos/node-std": "0.6.13-main.041e8aa"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@effect/schema": "^0.
|
|
33
|
-
"effect": "^3.
|
|
32
|
+
"@effect/schema": "^0.75.1",
|
|
33
|
+
"effect": "^3.9.1"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"@effect/schema": "^0.67.16",
|
|
37
|
-
"effect": "^3.
|
|
37
|
+
"effect": "^3.9.1"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
package/src/ast.test.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema as S } from '@effect/schema';
|
|
6
|
+
import { describe, expect, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { getProperty, visit } from './ast';
|
|
9
|
+
|
|
10
|
+
describe('AST', () => {
|
|
11
|
+
test('getProperty', () => {
|
|
12
|
+
const TestSchema = S.Struct({
|
|
13
|
+
name: S.String,
|
|
14
|
+
address: S.Struct({
|
|
15
|
+
zip: S.String,
|
|
16
|
+
}),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
{
|
|
20
|
+
const prop = getProperty(TestSchema, 'name');
|
|
21
|
+
expect(prop).to.exist;
|
|
22
|
+
}
|
|
23
|
+
{
|
|
24
|
+
const prop = getProperty(TestSchema, 'address.zip');
|
|
25
|
+
expect(prop).to.exist;
|
|
26
|
+
}
|
|
27
|
+
{
|
|
28
|
+
const prop = getProperty(TestSchema, 'address.city');
|
|
29
|
+
expect(prop).not.to.exist;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('visitNode', () => {
|
|
34
|
+
const TestSchema = S.Struct({
|
|
35
|
+
name: S.optional(S.String),
|
|
36
|
+
address: S.optional(
|
|
37
|
+
S.Struct({
|
|
38
|
+
zip: S.String,
|
|
39
|
+
}),
|
|
40
|
+
),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const props: string[] = [];
|
|
44
|
+
visit(TestSchema.ast, (_node, prop) => {
|
|
45
|
+
props.push(prop.join('.'));
|
|
46
|
+
});
|
|
47
|
+
expect(props).to.deep.eq(['name', 'address.zip']);
|
|
48
|
+
});
|
|
49
|
+
});
|
package/src/ast.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { AST, type Schema as S } from '@effect/schema';
|
|
6
|
+
import { Option, pipe } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { invariant } from '@dxos/invariant';
|
|
9
|
+
|
|
10
|
+
//
|
|
11
|
+
// Refs
|
|
12
|
+
// https://effect.website/docs/guides/schema
|
|
13
|
+
// https://www.npmjs.com/package/@effect/schema
|
|
14
|
+
// https://effect-ts.github.io/effect/schema/AST.ts.html
|
|
15
|
+
//
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Get annotation or return undefined.
|
|
19
|
+
*/
|
|
20
|
+
export const getAnnotation = <T>(annotationId: symbol, node: AST.Annotated): T | undefined =>
|
|
21
|
+
pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Get type node.
|
|
25
|
+
*/
|
|
26
|
+
export const getType = (node: AST.AST): AST.AST | undefined => {
|
|
27
|
+
if (AST.isUnion(node)) {
|
|
28
|
+
return node.types.find((type) => getType(type));
|
|
29
|
+
} else if (AST.isRefinement(node)) {
|
|
30
|
+
return getType(node.from);
|
|
31
|
+
} else {
|
|
32
|
+
return node;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Get the AST node for the given property (dot-path).
|
|
38
|
+
*/
|
|
39
|
+
export const getProperty = (schema: S.Schema<any>, path: string): AST.AST | undefined => {
|
|
40
|
+
let node: AST.AST = schema.ast;
|
|
41
|
+
for (const part of path.split('.')) {
|
|
42
|
+
const props = AST.getPropertySignatures(node);
|
|
43
|
+
const prop = props.find((prop) => prop.name === part);
|
|
44
|
+
if (!prop) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// TODO(burdon): Check if leaf.
|
|
49
|
+
const type = getType(prop.type);
|
|
50
|
+
invariant(type, `invalid type: ${path}`);
|
|
51
|
+
node = type;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return node;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type Visitor = (node: AST.AST, path: string[]) => boolean | void;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Visit leaf nodes.
|
|
61
|
+
* Ref: https://www.npmjs.com/package/unist-util-visit#visitor
|
|
62
|
+
*/
|
|
63
|
+
export const visit = (node: AST.AST, visitor: Visitor) => visitNode(node, visitor);
|
|
64
|
+
|
|
65
|
+
const visitNode = (node: AST.AST, visitor: Visitor, path: string[] = []) => {
|
|
66
|
+
for (const prop of AST.getPropertySignatures(node)) {
|
|
67
|
+
const currentPath = [...path, prop.name.toString()];
|
|
68
|
+
const type = getType(prop.type);
|
|
69
|
+
if (type) {
|
|
70
|
+
if (AST.isTypeLiteral(type)) {
|
|
71
|
+
visitNode(type, visitor, currentPath);
|
|
72
|
+
} else {
|
|
73
|
+
// NOTE: Only visits leaf nodes.
|
|
74
|
+
const ok = visitor(type, currentPath);
|
|
75
|
+
if (ok === false) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
const LOW_DASH = '_'.codePointAt(0)!;
|
|
6
|
+
const SMALL_A = 'a'.codePointAt(0)!;
|
|
7
|
+
const CAPITAL_A = 'A'.codePointAt(0)!;
|
|
8
|
+
const SMALL_Z = 'z'.codePointAt(0)!;
|
|
9
|
+
const CAPITAL_Z = 'Z'.codePointAt(0)!;
|
|
10
|
+
|
|
11
|
+
const isLower = (char: number) => char >= SMALL_A && char <= SMALL_Z;
|
|
12
|
+
|
|
13
|
+
const isUpper = (char: number) => char >= CAPITAL_A && char <= CAPITAL_Z;
|
|
14
|
+
|
|
15
|
+
const toLower = (char: number) => char + 0x20;
|
|
16
|
+
|
|
17
|
+
export const decamelize = (str: string) => {
|
|
18
|
+
const firstChar = str.charCodeAt(0);
|
|
19
|
+
if (!isLower(firstChar)) {
|
|
20
|
+
return str;
|
|
21
|
+
}
|
|
22
|
+
const length = str.length;
|
|
23
|
+
let changed = false;
|
|
24
|
+
const out: number[] = [];
|
|
25
|
+
for (let i = 0; i < length; ++i) {
|
|
26
|
+
const c = str.charCodeAt(i);
|
|
27
|
+
if (isUpper(c)) {
|
|
28
|
+
out.push(LOW_DASH);
|
|
29
|
+
out.push(toLower(c));
|
|
30
|
+
changed = true;
|
|
31
|
+
} else {
|
|
32
|
+
out.push(c);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return changed ? String.fromCharCode.apply(undefined, out) : str;
|
|
36
|
+
};
|
package/src/index.ts
CHANGED
package/src/url.ts
CHANGED
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
|
|
5
5
|
import { AST, type Schema as S } from '@effect/schema';
|
|
6
6
|
import { Option, pipe } from 'effect';
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
import { decamelize } from './decamelize';
|
|
8
9
|
|
|
9
10
|
const ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');
|
|
10
11
|
|
|
11
12
|
type ParamKeyAnnotationValue = { key: string };
|
|
12
13
|
|
|
13
|
-
const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =
|
|
14
|
+
export const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =
|
|
14
15
|
AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);
|
|
15
16
|
|
|
16
17
|
export const ParamKeyAnnotation =
|