@dxos/effect 0.6.13-main.548ca8d → 0.6.13-main.ed424a1
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 +67 -11
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +66 -7
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +67 -11
- 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/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/ast.test.ts +49 -0
- package/src/ast.ts +81 -0
- package/src/index.ts +1 -1
- package/dist/types/src/util.d.ts +0 -3
- package/dist/types/src/util.d.ts.map +0 -1
- package/src/util.ts +0 -11
|
@@ -10,9 +10,67 @@ import {
|
|
|
10
10
|
// packages/common/effect/src/index.ts
|
|
11
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 { 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";
|
|
16
74
|
|
|
17
75
|
// packages/common/effect/src/decamelize.ts
|
|
18
76
|
var LOW_DASH = "_".codePointAt(0);
|
|
@@ -46,7 +104,7 @@ var decamelize = (str) => {
|
|
|
46
104
|
|
|
47
105
|
// packages/common/effect/src/url.ts
|
|
48
106
|
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
49
|
-
var getParamKeyAnnotation =
|
|
107
|
+
var getParamKeyAnnotation = AST2.getAnnotation(ParamKeyAnnotationId);
|
|
50
108
|
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
51
109
|
[ParamKeyAnnotationId]: value
|
|
52
110
|
});
|
|
@@ -65,9 +123,9 @@ var UrlParser = class {
|
|
|
65
123
|
value = url.searchParams.get(key);
|
|
66
124
|
}
|
|
67
125
|
if (value != null) {
|
|
68
|
-
if (
|
|
126
|
+
if (AST2.isNumberKeyword(type.ast)) {
|
|
69
127
|
params[key] = parseInt(value);
|
|
70
|
-
} else if (
|
|
128
|
+
} else if (AST2.isBooleanKeyword(type.ast)) {
|
|
71
129
|
params[key] = value === "true" || value === "1";
|
|
72
130
|
} else {
|
|
73
131
|
params[key] = value;
|
|
@@ -85,7 +143,7 @@ var UrlParser = class {
|
|
|
85
143
|
if (value !== void 0) {
|
|
86
144
|
const field = this._schema.fields[key];
|
|
87
145
|
if (field) {
|
|
88
|
-
const { key: serializedKey } =
|
|
146
|
+
const { key: serializedKey } = pipe2(getParamKeyAnnotation(field.ast), Option2.getOrElse(() => ({
|
|
89
147
|
key: decamelize(key)
|
|
90
148
|
})));
|
|
91
149
|
url.searchParams.set(serializedKey, String(value));
|
|
@@ -95,11 +153,6 @@ var UrlParser = class {
|
|
|
95
153
|
return url;
|
|
96
154
|
}
|
|
97
155
|
};
|
|
98
|
-
|
|
99
|
-
// packages/common/effect/src/util.ts
|
|
100
|
-
import { AST as AST2 } from "@effect/schema";
|
|
101
|
-
import { Option as Option2, pipe as pipe2 } from "effect";
|
|
102
|
-
var getAnnotation = (annotationId) => (annotated) => pipe2(AST2.getAnnotation(annotationId)(annotated), Option2.getOrUndefined);
|
|
103
156
|
export {
|
|
104
157
|
AST3 as AST,
|
|
105
158
|
JSONSchema,
|
|
@@ -107,6 +160,9 @@ export {
|
|
|
107
160
|
S,
|
|
108
161
|
UrlParser,
|
|
109
162
|
getAnnotation,
|
|
110
|
-
getParamKeyAnnotation
|
|
163
|
+
getParamKeyAnnotation,
|
|
164
|
+
getProperty,
|
|
165
|
+
getType,
|
|
166
|
+
visit
|
|
111
167
|
};
|
|
112
168
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/
|
|
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 './
|
|
5
|
-
"mappings": ";;;;;;;;;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;;;ACD7B,IAAMC,WAAW,IAAIC,YAAY,CAAA;AACjC,IAAMC,UAAU,IAAID,YAAY,CAAA;AAChC,IAAME,YAAY,IAAIF,YAAY,CAAA;AAClC,IAAMG,UAAU,IAAIH,YAAY,CAAA;AAChC,IAAMI,YAAY,IAAIJ,YAAY,CAAA;AAElC,IAAMK,UAAU,CAACC,SAAiBA,QAAQL,WAAWK,QAAQH;AAE7D,IAAMI,UAAU,CAACD,SAAiBA,QAAQJ,aAAaI,QAAQF;AAE/D,IAAMI,UAAU,CAACF,SAAiBA,OAAO;AAElC,IAAMG,aAAa,CAACC,QAAAA;AACzB,QAAMC,YAAYD,IAAIE,WAAW,CAAA;AACjC,MAAI,CAACP,QAAQM,SAAAA,GAAY;AACvB,WAAOD;EACT;AACA,QAAMG,SAASH,IAAIG;AACnB,MAAIC,UAAU;AACd,QAAMC,MAAgB,CAAA;AACtB,WAASC,IAAI,GAAGA,IAAIH,QAAQ,EAAEG,GAAG;AAC/B,UAAMC,IAAIP,IAAIE,WAAWI,CAAAA;AACzB,QAAIT,QAAQU,CAAAA,GAAI;AACdF,UAAIG,KAAKnB,QAAAA;AACTgB,UAAIG,KAAKV,QAAQS,CAAAA,CAAAA;AACjBH,gBAAU;IACZ,OAAO;AACLC,UAAIG,KAAKD,CAAAA;IACX;EACF;AACA,SAAOH,UAAUK,OAAOC,aAAaC,MAAMC,QAAWP,GAAAA,IAAOL;AAC/D;;;AD1BA,IAAMa,uBAAuBC,OAAOC,IAAI,kCAAA;AAIjC,IAAMC,wBACXC,
|
|
6
|
-
"names": ["AST", "JSONSchema", "Schema", "S", "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"
|
|
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,IAAII,cAAiBC,YAAAA,EAAcC,IAAAA,GAAOL,OAAOM,cAAc;AAK/D,IAAMC,UAAU,CAACF,SAAAA;AACtB,MAAIN,IAAIS,QAAQH,IAAAA,GAAO;AACrB,WAAOA,KAAKI,MAAMC,KAAK,CAACC,SAASJ,QAAQI,IAAAA,CAAAA;EAC3C,WAAWZ,IAAIa,aAAaP,IAAAA,GAAO;AACjC,WAAOE,QAAQF,KAAKQ,IAAI;EAC1B,OAAO;AACL,WAAOR;EACT;AACF;AAKO,IAAMS,cAAc,CAACC,QAAuBC,SAAAA;AACjD,MAAIX,OAAgBU,OAAOE;AAC3B,aAAWC,QAAQF,KAAKG,MAAM,GAAA,GAAM;AAClC,UAAMC,QAAQrB,IAAIsB,sBAAsBhB,IAAAA;AACxC,UAAMiB,OAAOF,MAAMV,KAAK,CAACY,UAASA,MAAKC,SAASL,IAAAA;AAChD,QAAI,CAACI,MAAM;AACT,aAAOE;IACT;AAGA,UAAMb,OAAOJ,QAAQe,KAAKX,IAAI;AAC9BT,cAAUS,MAAM,iBAAiBK,IAAAA,IAAM;;;;;;;;;AACvCX,WAAOM;EACT;AAEA,SAAON;AACT;AAQO,IAAMoB,QAAQ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V,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":{"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
|
@@ -24,14 +24,71 @@ __export(node_exports, {
|
|
|
24
24
|
S: () => import_schema.Schema,
|
|
25
25
|
UrlParser: () => UrlParser,
|
|
26
26
|
getAnnotation: () => getAnnotation,
|
|
27
|
-
getParamKeyAnnotation: () => getParamKeyAnnotation
|
|
27
|
+
getParamKeyAnnotation: () => getParamKeyAnnotation,
|
|
28
|
+
getProperty: () => getProperty,
|
|
29
|
+
getType: () => getType,
|
|
30
|
+
visit: () => visit
|
|
28
31
|
});
|
|
29
32
|
module.exports = __toCommonJS(node_exports);
|
|
30
33
|
var import_schema = require("@effect/schema");
|
|
31
34
|
var import_schema2 = require("@effect/schema");
|
|
32
35
|
var import_effect = require("effect");
|
|
36
|
+
var import_invariant = require("@dxos/invariant");
|
|
33
37
|
var import_schema3 = require("@effect/schema");
|
|
34
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
|
+
};
|
|
35
92
|
var LOW_DASH = "_".codePointAt(0);
|
|
36
93
|
var SMALL_A = "a".codePointAt(0);
|
|
37
94
|
var CAPITAL_A = "A".codePointAt(0);
|
|
@@ -61,7 +118,7 @@ var decamelize = (str) => {
|
|
|
61
118
|
return changed ? String.fromCharCode.apply(void 0, out) : str;
|
|
62
119
|
};
|
|
63
120
|
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
64
|
-
var getParamKeyAnnotation =
|
|
121
|
+
var getParamKeyAnnotation = import_schema3.AST.getAnnotation(ParamKeyAnnotationId);
|
|
65
122
|
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
66
123
|
[ParamKeyAnnotationId]: value
|
|
67
124
|
});
|
|
@@ -80,9 +137,9 @@ var UrlParser = class {
|
|
|
80
137
|
value = url.searchParams.get(key);
|
|
81
138
|
}
|
|
82
139
|
if (value != null) {
|
|
83
|
-
if (
|
|
140
|
+
if (import_schema3.AST.isNumberKeyword(type.ast)) {
|
|
84
141
|
params[key] = parseInt(value);
|
|
85
|
-
} else if (
|
|
142
|
+
} else if (import_schema3.AST.isBooleanKeyword(type.ast)) {
|
|
86
143
|
params[key] = value === "true" || value === "1";
|
|
87
144
|
} else {
|
|
88
145
|
params[key] = value;
|
|
@@ -100,7 +157,7 @@ var UrlParser = class {
|
|
|
100
157
|
if (value !== void 0) {
|
|
101
158
|
const field = this._schema.fields[key];
|
|
102
159
|
if (field) {
|
|
103
|
-
const { key: serializedKey } = (0,
|
|
160
|
+
const { key: serializedKey } = (0, import_effect2.pipe)(getParamKeyAnnotation(field.ast), import_effect2.Option.getOrElse(() => ({
|
|
104
161
|
key: decamelize(key)
|
|
105
162
|
})));
|
|
106
163
|
url.searchParams.set(serializedKey, String(value));
|
|
@@ -110,7 +167,6 @@ var UrlParser = class {
|
|
|
110
167
|
return url;
|
|
111
168
|
}
|
|
112
169
|
};
|
|
113
|
-
var getAnnotation = (annotationId) => (annotated) => (0, import_effect2.pipe)(import_schema3.AST.getAnnotation(annotationId)(annotated), import_effect2.Option.getOrUndefined);
|
|
114
170
|
// Annotate the CommonJS export names for ESM import in node:
|
|
115
171
|
0 && (module.exports = {
|
|
116
172
|
AST,
|
|
@@ -119,6 +175,9 @@ var getAnnotation = (annotationId) => (annotated) => (0, import_effect2.pipe)(im
|
|
|
119
175
|
S,
|
|
120
176
|
UrlParser,
|
|
121
177
|
getAnnotation,
|
|
122
|
-
getParamKeyAnnotation
|
|
178
|
+
getParamKeyAnnotation,
|
|
179
|
+
getProperty,
|
|
180
|
+
getType,
|
|
181
|
+
visit
|
|
123
182
|
});
|
|
124
183
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/
|
|
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 './
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["import_schema", "import_effect", "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", "
|
|
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,oBAA6C;ACA7C,IAAAA,iBAAsC;AACtC,oBAA6B;AAE7B,uBAA0B;ACH1B,IAAAA,iBAAsC;AACtC,IAAA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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}}}
|
|
@@ -3,9 +3,67 @@ import { createRequire } from 'node:module';const require = createRequire(import
|
|
|
3
3
|
// packages/common/effect/src/index.ts
|
|
4
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 { 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";
|
|
9
67
|
|
|
10
68
|
// packages/common/effect/src/decamelize.ts
|
|
11
69
|
var LOW_DASH = "_".codePointAt(0);
|
|
@@ -39,7 +97,7 @@ var decamelize = (str) => {
|
|
|
39
97
|
|
|
40
98
|
// packages/common/effect/src/url.ts
|
|
41
99
|
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
42
|
-
var getParamKeyAnnotation =
|
|
100
|
+
var getParamKeyAnnotation = AST2.getAnnotation(ParamKeyAnnotationId);
|
|
43
101
|
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
44
102
|
[ParamKeyAnnotationId]: value
|
|
45
103
|
});
|
|
@@ -58,9 +116,9 @@ var UrlParser = class {
|
|
|
58
116
|
value = url.searchParams.get(key);
|
|
59
117
|
}
|
|
60
118
|
if (value != null) {
|
|
61
|
-
if (
|
|
119
|
+
if (AST2.isNumberKeyword(type.ast)) {
|
|
62
120
|
params[key] = parseInt(value);
|
|
63
|
-
} else if (
|
|
121
|
+
} else if (AST2.isBooleanKeyword(type.ast)) {
|
|
64
122
|
params[key] = value === "true" || value === "1";
|
|
65
123
|
} else {
|
|
66
124
|
params[key] = value;
|
|
@@ -78,7 +136,7 @@ var UrlParser = class {
|
|
|
78
136
|
if (value !== void 0) {
|
|
79
137
|
const field = this._schema.fields[key];
|
|
80
138
|
if (field) {
|
|
81
|
-
const { key: serializedKey } =
|
|
139
|
+
const { key: serializedKey } = pipe2(getParamKeyAnnotation(field.ast), Option2.getOrElse(() => ({
|
|
82
140
|
key: decamelize(key)
|
|
83
141
|
})));
|
|
84
142
|
url.searchParams.set(serializedKey, String(value));
|
|
@@ -88,11 +146,6 @@ var UrlParser = class {
|
|
|
88
146
|
return url;
|
|
89
147
|
}
|
|
90
148
|
};
|
|
91
|
-
|
|
92
|
-
// packages/common/effect/src/util.ts
|
|
93
|
-
import { AST as AST2 } from "@effect/schema";
|
|
94
|
-
import { Option as Option2, pipe as pipe2 } from "effect";
|
|
95
|
-
var getAnnotation = (annotationId) => (annotated) => pipe2(AST2.getAnnotation(annotationId)(annotated), Option2.getOrUndefined);
|
|
96
149
|
export {
|
|
97
150
|
AST3 as AST,
|
|
98
151
|
JSONSchema,
|
|
@@ -100,6 +153,9 @@ export {
|
|
|
100
153
|
S,
|
|
101
154
|
UrlParser,
|
|
102
155
|
getAnnotation,
|
|
103
|
-
getParamKeyAnnotation
|
|
156
|
+
getParamKeyAnnotation,
|
|
157
|
+
getProperty,
|
|
158
|
+
getType,
|
|
159
|
+
visit
|
|
104
160
|
};
|
|
105
161
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/
|
|
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 './
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;;;ACD7B,IAAMC,WAAW,IAAIC,YAAY,CAAA;AACjC,IAAMC,UAAU,IAAID,YAAY,CAAA;AAChC,IAAME,YAAY,IAAIF,YAAY,CAAA;AAClC,IAAMG,UAAU,IAAIH,YAAY,CAAA;AAChC,IAAMI,YAAY,IAAIJ,YAAY,CAAA;AAElC,IAAMK,UAAU,CAACC,SAAiBA,QAAQL,WAAWK,QAAQH;AAE7D,IAAMI,UAAU,CAACD,SAAiBA,QAAQJ,aAAaI,QAAQF;AAE/D,IAAMI,UAAU,CAACF,SAAiBA,OAAO;AAElC,IAAMG,aAAa,CAACC,QAAAA;AACzB,QAAMC,YAAYD,IAAIE,WAAW,CAAA;AACjC,MAAI,CAACP,QAAQM,SAAAA,GAAY;AACvB,WAAOD;EACT;AACA,QAAMG,SAASH,IAAIG;AACnB,MAAIC,UAAU;AACd,QAAMC,MAAgB,CAAA;AACtB,WAASC,IAAI,GAAGA,IAAIH,QAAQ,EAAEG,GAAG;AAC/B,UAAMC,IAAIP,IAAIE,WAAWI,CAAAA;AACzB,QAAIT,QAAQU,CAAAA,GAAI;AACdF,UAAIG,KAAKnB,QAAAA;AACTgB,UAAIG,KAAKV,QAAQS,CAAAA,CAAAA;AACjBH,gBAAU;IACZ,OAAO;AACLC,UAAIG,KAAKD,CAAAA;IACX;EACF;AACA,SAAOH,UAAUK,OAAOC,aAAaC,MAAMC,QAAWP,GAAAA,IAAOL;AAC/D;;;AD1BA,IAAMa,uBAAuBC,OAAOC,IAAI,kCAAA;AAIjC,IAAMC,wBACXC,
|
|
6
|
-
"names": ["AST", "JSONSchema", "Schema", "S", "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"
|
|
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,IAAII,cAAiBC,YAAAA,EAAcC,IAAAA,GAAOL,OAAOM,cAAc;AAK/D,IAAMC,UAAU,CAACF,SAAAA;AACtB,MAAIN,IAAIS,QAAQH,IAAAA,GAAO;AACrB,WAAOA,KAAKI,MAAMC,KAAK,CAACC,SAASJ,QAAQI,IAAAA,CAAAA;EAC3C,WAAWZ,IAAIa,aAAaP,IAAAA,GAAO;AACjC,WAAOE,QAAQF,KAAKQ,IAAI;EAC1B,OAAO;AACL,WAAOR;EACT;AACF;AAKO,IAAMS,cAAc,CAACC,QAAuBC,SAAAA;AACjD,MAAIX,OAAgBU,OAAOE;AAC3B,aAAWC,QAAQF,KAAKG,MAAM,GAAA,GAAM;AAClC,UAAMC,QAAQrB,IAAIsB,sBAAsBhB,IAAAA;AACxC,UAAMiB,OAAOF,MAAMV,KAAK,CAACY,UAASA,MAAKC,SAASL,IAAAA;AAChD,QAAI,CAACI,MAAM;AACT,aAAOE;IACT;AAGA,UAAMb,OAAOJ,QAAQe,KAAKX,IAAI;AAC9BT,cAAUS,MAAM,iBAAiBK,IAAAA,IAAM;;;;;;;;;AACvCX,WAAOM;EACT;AAEA,SAAON;AACT;AAQO,IAAMoB,QAAQ,CAACpB,MAAeqB,YAAqBC,UAAUtB,MAAMqB,OAAAA;AAE1E,IAAMC,YAAY,CAACtB,MAAeqB,SAAkBV,OAAiB,CAAA,MAAE;AACrE,aAAWM,QAAQvB,IAAIsB,sBAAsBhB,IAAAA,GAAO;AAClD,UAAMuB,cAAc;SAAIZ;MAAMM,KAAKC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C,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":""}
|
|
@@ -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;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,
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.6.13-main.
|
|
3
|
+
"version": "0.6.13-main.ed424a1",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"src"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@dxos/
|
|
28
|
+
"@dxos/invariant": "0.6.13-main.ed424a1",
|
|
29
|
+
"@dxos/node-std": "0.6.13-main.ed424a1"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@effect/schema": "^0.75.1",
|
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
|
+
};
|
package/src/index.ts
CHANGED
package/dist/types/src/util.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAGrC,eAAO,MAAM,aAAa,GACvB,CAAC,gBAAgB,MAAM,iBACZ,GAAG,CAAC,SAAS,KAAG,CAAC,GAAG,SAC4C,CAAC"}
|
package/src/util.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { AST } from '@effect/schema';
|
|
6
|
-
import { Option, pipe } from 'effect';
|
|
7
|
-
|
|
8
|
-
export const getAnnotation =
|
|
9
|
-
<T>(annotationId: symbol) =>
|
|
10
|
-
(annotated: AST.Annotated): T | undefined =>
|
|
11
|
-
pipe(AST.getAnnotation<T>(annotationId)(annotated), Option.getOrUndefined);
|