@dxos/effect 0.6.14-staging.e15392e → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,90 +1,170 @@
1
1
  import "@dxos/node-std/globals";
2
2
 
3
3
  // packages/common/effect/src/index.ts
4
- import { AST as AST3, JSONSchema, Schema as S } from "@effect/schema";
4
+ import { AST as AST3, JSONSchema, Schema as S2 } from "@effect/schema";
5
5
 
6
6
  // packages/common/effect/src/ast.ts
7
- import { AST } from "@effect/schema";
7
+ import { AST, Schema as S } from "@effect/schema";
8
8
  import { Option, pipe } from "effect";
9
9
  import { invariant } from "@dxos/invariant";
10
10
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/effect/src/ast.ts";
11
- var isLeafType = (node) => !AST.isTupleType(node) && !AST.isTypeLiteral(node);
12
- var getAnnotation = (annotationId, node) => pipe(AST.getAnnotation(annotationId)(node), Option.getOrUndefined);
13
- var getType = (node) => {
14
- if (AST.isUnion(node)) {
15
- return node.types.find((type) => getType(type));
16
- } else if (AST.isRefinement(node)) {
17
- return getType(node.from);
18
- } else {
19
- return node;
11
+ var getSimpleType = (node) => {
12
+ if (AST.isObjectKeyword(node)) {
13
+ return "object";
20
14
  }
21
- };
22
- var getProperty = (schema, path) => {
23
- let node = schema.ast;
24
- for (const part of path.split(".")) {
25
- const props = AST.getPropertySignatures(node);
26
- const prop = props.find((prop2) => prop2.name === part);
27
- if (!prop) {
28
- return void 0;
29
- }
30
- const type = getType(prop.type);
31
- invariant(type, `invalid type: ${path}`, {
32
- F: __dxlog_file,
33
- L: 52,
34
- S: void 0,
35
- A: [
36
- "type",
37
- "`invalid type: ${path}`"
38
- ]
39
- });
40
- node = type;
15
+ if (AST.isStringKeyword(node)) {
16
+ return "string";
17
+ }
18
+ if (AST.isNumberKeyword(node)) {
19
+ return "number";
20
+ }
21
+ if (AST.isBooleanKeyword(node)) {
22
+ return "boolean";
23
+ }
24
+ if (AST.isEnums(node)) {
25
+ return "enum";
26
+ }
27
+ if (AST.isLiteral(node)) {
28
+ return "literal";
41
29
  }
42
- return node;
43
30
  };
31
+ var isSimpleType = (node) => !!getSimpleType(node);
32
+ var PATH_REGEX = /[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*/;
33
+ var PROP_REGEX = /\w+/;
34
+ var JsonPath = S.NonEmptyString.pipe(S.pattern(PATH_REGEX));
35
+ var JsonProp = S.NonEmptyString.pipe(S.pattern(PROP_REGEX));
36
+ var getAnnotation = (annotationId) => (node) => pipe(AST.getAnnotation(annotationId)(node), Option.getOrUndefined);
44
37
  var VisitResult;
45
38
  (function(VisitResult2) {
46
39
  VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
47
40
  VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
48
41
  VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
49
42
  })(VisitResult || (VisitResult = {}));
43
+ var defaultTest = (node) => isSimpleType(node) ? 0 : 1;
50
44
  var visit = (node, testOrVisitor, visitor) => {
51
45
  if (!visitor) {
52
- visitNode(node, void 0, testOrVisitor);
46
+ visitNode(node, defaultTest, testOrVisitor);
53
47
  } else {
54
48
  visitNode(node, testOrVisitor, visitor);
55
49
  }
56
50
  };
57
51
  var visitNode = (node, test, visitor, path = [], depth = 0) => {
58
- for (const prop of AST.getPropertySignatures(node)) {
59
- const currentPath = [
60
- ...path,
61
- prop.name.toString()
62
- ];
63
- const type = getType(prop.type);
64
- if (type) {
65
- const result = test?.(node, path, depth) ?? 0;
66
- if (result === 2) {
67
- return result;
52
+ const result = test?.(node, path, depth) ?? 0;
53
+ if (result === 2) {
54
+ return result;
55
+ }
56
+ if (result !== 1) {
57
+ visitor(node, path, depth);
58
+ }
59
+ if (AST.isTypeLiteral(node)) {
60
+ for (const prop of AST.getPropertySignatures(node)) {
61
+ const currentPath = [
62
+ ...path,
63
+ prop.name.toString()
64
+ ];
65
+ const result2 = visitNode(prop.type, test, visitor, currentPath, depth + 1);
66
+ if (result2 === 2) {
67
+ return result2;
68
68
  }
69
- visitor(type, currentPath, depth);
70
- if (result !== 1) {
71
- if (AST.isTypeLiteral(type)) {
72
- visitNode(type, test, visitor, currentPath, depth + 1);
73
- } else if (AST.isTupleType(type)) {
74
- for (const [i, elementType] of type.elements.entries()) {
75
- const type2 = getType(elementType.type);
76
- if (type2) {
77
- visitNode(type2, test, visitor, [
78
- i,
79
- ...currentPath
80
- ], depth);
81
- }
82
- }
83
- }
69
+ }
70
+ } else if (AST.isTupleType(node)) {
71
+ for (const [i, element] of node.elements.entries()) {
72
+ const currentPath = [
73
+ ...path,
74
+ i
75
+ ];
76
+ const result2 = visitNode(element.type, test, visitor, currentPath, depth);
77
+ if (result2 === 2) {
78
+ return result2;
84
79
  }
85
80
  }
81
+ } else if (AST.isUnion(node)) {
82
+ for (const type of node.types) {
83
+ const result2 = visitNode(type, test, visitor, path, depth);
84
+ if (result2 === 2) {
85
+ return result2;
86
+ }
87
+ }
88
+ } else if (AST.isRefinement(node)) {
89
+ const result2 = visitNode(node.from, test, visitor, path, depth);
90
+ if (result2 === 2) {
91
+ return result2;
92
+ }
86
93
  }
87
94
  };
95
+ var findNode = (node, test) => {
96
+ if (test(node)) {
97
+ return node;
98
+ } else if (AST.isTypeLiteral(node)) {
99
+ for (const prop of AST.getPropertySignatures(node)) {
100
+ const child = findNode(prop.type, test);
101
+ if (child) {
102
+ return child;
103
+ }
104
+ }
105
+ } else if (AST.isTupleType(node)) {
106
+ for (const [_, element] of node.elements.entries()) {
107
+ const child = findNode(element.type, test);
108
+ if (child) {
109
+ return child;
110
+ }
111
+ }
112
+ } else if (AST.isUnion(node)) {
113
+ for (const type of node.types) {
114
+ const child = findNode(type, test);
115
+ if (child) {
116
+ return child;
117
+ }
118
+ }
119
+ } else if (AST.isRefinement(node)) {
120
+ return findNode(node.from, test);
121
+ }
122
+ return void 0;
123
+ };
124
+ var findProperty = (schema, path) => {
125
+ const getProp = (node, path2) => {
126
+ const [name, ...rest] = path2;
127
+ const typeNode = findNode(node, AST.isTypeLiteral);
128
+ invariant(typeNode, void 0, {
129
+ F: __dxlog_file,
130
+ L: 214,
131
+ S: void 0,
132
+ A: [
133
+ "typeNode",
134
+ ""
135
+ ]
136
+ });
137
+ for (const prop of AST.getPropertySignatures(typeNode)) {
138
+ if (prop.name === name) {
139
+ if (rest.length) {
140
+ return getProp(prop.type, rest);
141
+ } else {
142
+ return prop.type;
143
+ }
144
+ }
145
+ }
146
+ return void 0;
147
+ };
148
+ return getProp(schema.ast, path.split("."));
149
+ };
150
+ var findAnnotation = (node, annotationId) => {
151
+ const getAnnotationById = getAnnotation(annotationId);
152
+ const getBaseAnnotation = (node2) => {
153
+ const value = getAnnotationById(node2);
154
+ if (value !== void 0) {
155
+ return value;
156
+ }
157
+ if (AST.isUnion(node2)) {
158
+ for (const type of node2.types) {
159
+ const value2 = getBaseAnnotation(type);
160
+ if (value2 !== void 0) {
161
+ return value2;
162
+ }
163
+ }
164
+ }
165
+ };
166
+ return getBaseAnnotation(node);
167
+ };
88
168
 
89
169
  // packages/common/effect/src/url.ts
90
170
  import { AST as AST2 } from "@effect/schema";
@@ -143,15 +223,19 @@ var UrlParser = class {
143
223
  export {
144
224
  AST3 as AST,
145
225
  JSONSchema,
226
+ JsonPath,
227
+ JsonProp,
146
228
  ParamKeyAnnotation,
147
- S,
229
+ S2 as S,
148
230
  UrlParser,
149
231
  VisitResult,
232
+ findAnnotation,
233
+ findNode,
234
+ findProperty,
150
235
  getAnnotation,
151
236
  getParamKeyAnnotation,
152
- getProperty,
153
- getType,
154
- isLeafType,
237
+ getSimpleType,
238
+ isSimpleType,
155
239
  visit
156
240
  };
157
241
  //# sourceMappingURL=index.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/index.ts", "../../../src/ast.ts", "../../../src/url.ts"],
4
- "sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\nimport type * as Types from 'effect/Types';\n\nexport { AST, JSONSchema, S, Types };\n\nexport * from './ast';\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, 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\nexport const isLeafType = (node: AST.AST) => !AST.isTupleType(node) && !AST.isTypeLiteral(node);\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 enum VisitResult {\n CONTINUE = 0,\n /**\n * Skip visiting children.\n */\n SKIP = 1,\n /**\n * Stop traversing immeditaely.\n */\n EXIT = 2,\n}\n\nexport type Path = (string | number)[];\n\nexport type Tester = (node: AST.AST, path: Path, depth: number) => VisitResult;\nexport type Visitor = (node: AST.AST, path: Path, depth: number) => void;\n\n/**\n * Visit leaf nodes.\n * Refs:\n * - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor\n * - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test\n */\nexport const visit: {\n (node: AST.AST, visitor: Visitor): void;\n (node: AST.AST, test: Tester, visitor: Visitor): void;\n} = (node: AST.AST, testOrVisitor: Tester | Visitor, visitor?: Visitor): void => {\n if (!visitor) {\n visitNode(node, undefined, testOrVisitor);\n } else {\n visitNode(node, testOrVisitor as Tester, visitor);\n }\n};\n\nconst visitNode = (\n node: AST.AST,\n test: Tester | undefined,\n visitor: Visitor,\n path: Path = [],\n depth = 0,\n): VisitResult | undefined => {\n for (const prop of AST.getPropertySignatures(node)) {\n const currentPath = [...path, prop.name.toString()];\n const type = getType(prop.type);\n if (type) {\n const result = test?.(node, path, depth) ?? VisitResult.CONTINUE;\n if (result === VisitResult.EXIT) {\n return result;\n }\n\n visitor(type, currentPath, depth);\n\n if (result !== VisitResult.SKIP) {\n if (AST.isTypeLiteral(type)) {\n visitNode(type, test, visitor, currentPath, depth + 1);\n } else if (AST.isTupleType(type)) {\n for (const [i, elementType] of type.elements.entries()) {\n const type = getType(elementType.type);\n if (type) {\n visitNode(type, test, visitor, [i, ...currentPath], depth);\n }\n }\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 '@dxos/util';\n\nconst ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');\n\ntype ParamKeyAnnotationValue = { key: string };\n\nexport const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =\n AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);\n\nexport const ParamKeyAnnotation =\n (value: ParamKeyAnnotationValue) =>\n <S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>\n self.annotations({ [ParamKeyAnnotationId]: value });\n\n/**\n * HTTP params parser.\n * Supports custom key serialization.\n */\nexport class UrlParser<T extends Record<string, any>> {\n constructor(private readonly _schema: S.Struct<T>) {}\n\n /**\n * Parse URL params.\n */\n parse(_url: string): T {\n const url = new URL(_url);\n return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {\n let value = url.searchParams.get(decamelize(key));\n if (value == null) {\n value = url.searchParams.get(key);\n }\n\n if (value != null) {\n if (AST.isNumberKeyword(type.ast)) {\n params[key] = parseInt(value);\n } else if (AST.isBooleanKeyword(type.ast)) {\n params[key] = value === 'true' || value === '1';\n } else {\n params[key] = value;\n }\n }\n\n return params;\n }, {}) as T;\n }\n\n /**\n * Return URL with encoded params.\n */\n create(_url: string, params: T): URL {\n const url = new URL(_url);\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined) {\n const field = this._schema.fields[key];\n if (field) {\n const { key: serializedKey } = pipe(\n getParamKeyAnnotation(field.ast),\n Option.getOrElse(() => ({\n key: decamelize(key),\n })),\n );\n\n url.searchParams.set(serializedKey, String(value));\n }\n }\n });\n\n return url;\n }\n}\n"],
5
- "mappings": ";;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;AAE7B,SAASC,iBAAiB;;AASnB,IAAMC,aAAa,CAACC,SAAkB,CAACL,IAAIM,YAAYD,IAAAA,KAAS,CAACL,IAAIO,cAAcF,IAAAA;AAKnF,IAAMG,gBAAgB,CAAIC,cAAsBJ,SACrDH,KAAKF,IAAIQ,cAAiBC,YAAAA,EAAcJ,IAAAA,GAAOJ,OAAOS,cAAc;AAK/D,IAAMC,UAAU,CAACN,SAAAA;AACtB,MAAIL,IAAIY,QAAQP,IAAAA,GAAO;AACrB,WAAOA,KAAKQ,MAAMC,KAAK,CAACC,SAASJ,QAAQI,IAAAA,CAAAA;EAC3C,WAAWf,IAAIgB,aAAaX,IAAAA,GAAO;AACjC,WAAOM,QAAQN,KAAKY,IAAI;EAC1B,OAAO;AACL,WAAOZ;EACT;AACF;AAKO,IAAMa,cAAc,CAACC,QAAuBC,SAAAA;AACjD,MAAIf,OAAgBc,O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AG,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", "isLeafType", "node", "isTupleType", "isTypeLiteral", "getAnnotation", "annotationId", "getOrUndefined", "getType", "isUnion", "types", "find", "type", "isRefinement", "from", "getProperty", "schema", "path", "ast", "part", "split", "props", "getPropertySignatures", "prop", "name", "undefined", "VisitResult", "visit", "testOrVisitor", "visitor", "visitNode", "test", "depth", "currentPath", "toString", "result", "i", "elementType", "elements", "entries", "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"]
4
+ "sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\nimport type * as Types from 'effect/Types';\n\n// TODO(dmaretskyi): Remove re-exports.\nexport { AST, JSONSchema, S, Types };\n\nexport * from './ast';\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\n\nimport { invariant } from '@dxos/invariant';\n\n//\n// Refs\n// https://effect.website/docs/guides/schema\n// https://www.npmjs.com/package/@effect/schema\n// https://effect-ts.github.io/effect/schema/AST.ts.html\n//\n\nexport type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';\n\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node)) {\n return 'object';\n }\n if (AST.isStringKeyword(node)) {\n return 'string';\n }\n if (AST.isNumberKeyword(node)) {\n return 'number';\n }\n if (AST.isBooleanKeyword(node)) {\n return 'boolean';\n }\n if (AST.isEnums(node)) {\n return 'enum';\n }\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST) => !!getSimpleType(node);\n\n//\n// Branded types\n//\n\nexport type JsonProp = string & { __JsonPath: true; __JsonProp: true };\nexport type JsonPath = string & { __JsonPath: true };\n\nconst PATH_REGEX = /[a-zA-Z_$][\\w$]*(?:\\.[a-zA-Z_$][\\w$]*)*/;\nconst PROP_REGEX = /\\w+/;\n\n/**\n * https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html\n */\nexport const JsonPath = S.NonEmptyString.pipe(S.pattern(PATH_REGEX)) as any as S.Schema<JsonPath>;\nexport const JsonProp = S.NonEmptyString.pipe(S.pattern(PROP_REGEX)) as any as S.Schema<JsonProp>;\n\n/**\n * Get annotation or return undefined.\n */\nexport const getAnnotation =\n <T>(annotationId: symbol) =>\n (node: AST.Annotated): T | undefined =>\n pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n\nexport enum VisitResult {\n CONTINUE = 0,\n /**\n * Skip visiting children.\n */\n SKIP = 1,\n /**\n * Stop traversing immediately.\n */\n EXIT = 2,\n}\n\nexport type Path = (string | number)[];\n\nexport type Tester = (node: AST.AST, path: Path, depth: number) => VisitResult | undefined;\nexport type Visitor = (node: AST.AST, path: Path, depth: number) => void;\n\nconst defaultTest: Tester = (node) => (isSimpleType(node) ? VisitResult.CONTINUE : VisitResult.SKIP);\n\n/**\n * Visit leaf nodes.\n * Refs:\n * - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor\n * - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test\n */\nexport const visit: {\n (node: AST.AST, visitor: Visitor): void;\n (node: AST.AST, test: Tester, visitor: Visitor): void;\n} = (node: AST.AST, testOrVisitor: Tester | Visitor, visitor?: Visitor): void => {\n if (!visitor) {\n visitNode(node, defaultTest, testOrVisitor);\n } else {\n visitNode(node, testOrVisitor as Tester, visitor);\n }\n};\n\nconst visitNode = (\n node: AST.AST,\n test: Tester | undefined,\n visitor: Visitor,\n path: Path = [],\n depth = 0,\n): VisitResult | undefined => {\n const result = test?.(node, path, depth) ?? VisitResult.CONTINUE;\n if (result === VisitResult.EXIT) {\n return result;\n }\n if (result !== VisitResult.SKIP) {\n visitor(node, path, depth);\n }\n\n // Object.\n if (AST.isTypeLiteral(node)) {\n for (const prop of AST.getPropertySignatures(node)) {\n const currentPath = [...path, prop.name.toString()];\n const result = visitNode(prop.type, test, visitor, currentPath, depth + 1);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Array.\n else if (AST.isTupleType(node)) {\n for (const [i, element] of node.elements.entries()) {\n const currentPath = [...path, i];\n const result = visitNode(element.type, test, visitor, currentPath, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Branching union.\n else if (AST.isUnion(node)) {\n for (const type of node.types) {\n const result = visitNode(type, test, visitor, path, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Refinement.\n else if (AST.isRefinement(node)) {\n const result = visitNode(node.from, test, visitor, path, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n\n // TODO(burdon): Transform?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Reuse visitor.\nexport const findNode = (node: AST.AST, test: (node: AST.AST) => boolean): AST.AST | undefined => {\n if (test(node)) {\n return node;\n }\n\n // Object.\n else if (AST.isTypeLiteral(node)) {\n for (const prop of AST.getPropertySignatures(node)) {\n const child = findNode(prop.type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Array.\n else if (AST.isTupleType(node)) {\n for (const [_, element] of node.elements.entries()) {\n const child = findNode(element.type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Branching union.\n else if (AST.isUnion(node)) {\n for (const type of node.types) {\n const child = findNode(type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\n\n return undefined;\n};\n\n/**\n * Get the AST node for the given property (dot-path).\n */\nexport const findProperty = (schema: S.Schema<any>, path: JsonPath | JsonProp): AST.AST | undefined => {\n const getProp = (node: AST.AST, path: JsonProp[]): AST.AST | undefined => {\n const [name, ...rest] = path;\n const typeNode = findNode(node, AST.isTypeLiteral);\n invariant(typeNode);\n for (const prop of AST.getPropertySignatures(typeNode)) {\n if (prop.name === name) {\n if (rest.length) {\n return getProp(prop.type, rest);\n } else {\n return prop.type;\n }\n }\n }\n\n return undefined;\n };\n\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n/**\n * Recursively descend into AST to find first matching annotations\n */\nexport const findAnnotation = <T>(node: AST.AST, annotationId: symbol): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId);\n const getBaseAnnotation = (node: AST.AST): T | undefined => {\n const value = getAnnotationById(node);\n if (value !== undefined) {\n return value as T;\n }\n\n if (AST.isUnion(node)) {\n for (const type of node.types) {\n const value = getBaseAnnotation(type);\n if (value !== undefined) {\n return value as T;\n }\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\n\nimport { decamelize } from '@dxos/util';\n\nconst ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');\n\ntype ParamKeyAnnotationValue = { key: string };\n\nexport const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =\n AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);\n\nexport const ParamKeyAnnotation =\n (value: ParamKeyAnnotationValue) =>\n <S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>\n self.annotations({ [ParamKeyAnnotationId]: value });\n\n/**\n * HTTP params parser.\n * Supports custom key serialization.\n */\nexport class UrlParser<T extends Record<string, any>> {\n constructor(private readonly _schema: S.Struct<T>) {}\n\n /**\n * Parse URL params.\n */\n parse(_url: string): T {\n const url = new URL(_url);\n return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {\n let value = url.searchParams.get(decamelize(key));\n if (value == null) {\n value = url.searchParams.get(key);\n }\n\n if (value != null) {\n if (AST.isNumberKeyword(type.ast)) {\n params[key] = parseInt(value);\n } else if (AST.isBooleanKeyword(type.ast)) {\n params[key] = value === 'true' || value === '1';\n } else {\n params[key] = value;\n }\n }\n\n return params;\n }, {}) as T;\n }\n\n /**\n * Return URL with encoded params.\n */\n create(_url: string, params: T): URL {\n const url = new URL(_url);\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined) {\n const field = this._schema.fields[key];\n if (field) {\n const { key: serializedKey } = pipe(\n getParamKeyAnnotation(field.ast),\n Option.getOrElse(() => ({\n key: decamelize(key),\n })),\n );\n\n url.searchParams.set(serializedKey, String(value));\n }\n }\n });\n\n return url;\n }\n}\n"],
5
+ "mappings": ";;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,UAAS;;;ACA7C,SAASC,KAAKC,UAAUC,SAAS;AACjC,SAASC,QAAQC,YAAY;AAE7B,SAASC,iBAAiB;;AAWnB,IAAMC,gBA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AAOf,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", "Schema", "S", "Option", "pipe", "invariant", "getSimpleType", "node", "isObjectKeyword", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "NonEmptyString", "pattern", "JsonProp", "getAnnotation", "annotationId", "getOrUndefined", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "result", "isTypeLiteral", "prop", "getPropertySignatures", "currentPath", "name", "toString", "type", "isTupleType", "i", "element", "elements", "entries", "isUnion", "types", "isRefinement", "from", "findNode", "child", "_", "undefined", "findProperty", "schema", "getProp", "rest", "typeNode", "length", "ast", "split", "findAnnotation", "getAnnotationById", "getBaseAnnotation", "value", "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"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/common/effect/src/ast.ts":{"bytes":11581,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7711,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":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/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":10203},"packages/common/effect/dist/lib/browser/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},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["AST","JSONSchema","ParamKeyAnnotation","S","UrlParser","VisitResult","getAnnotation","getParamKeyAnnotation","getProperty","getType","isLeafType","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":71},"packages/common/effect/src/ast.ts":{"bytesInOutput":2437},"packages/common/effect/src/url.ts":{"bytesInOutput":1624}},"bytes":4502}}}
1
+ {"inputs":{"packages/common/effect/src/ast.ts":{"bytes":21205,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7711,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":1150,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"packages/common/effect/src/ast.ts","kind":"import-statement","original":"./ast"},{"path":"packages/common/effect/src/url.ts","kind":"import-statement","original":"./url"}],"format":"esm"}},"outputs":{"packages/common/effect/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":15614},"packages/common/effect/dist/lib/browser/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},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["AST","JSONSchema","JsonPath","JsonProp","ParamKeyAnnotation","S","UrlParser","VisitResult","findAnnotation","findNode","findProperty","getAnnotation","getParamKeyAnnotation","getSimpleType","isSimpleType","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":72},"packages/common/effect/src/ast.ts":{"bytesInOutput":4515},"packages/common/effect/src/url.ts":{"bytesInOutput":1624}},"bytes":6650}}}
@@ -20,15 +20,19 @@ var node_exports = {};
20
20
  __export(node_exports, {
21
21
  AST: () => import_schema.AST,
22
22
  JSONSchema: () => import_schema.JSONSchema,
23
+ JsonPath: () => JsonPath,
24
+ JsonProp: () => JsonProp,
23
25
  ParamKeyAnnotation: () => ParamKeyAnnotation,
24
26
  S: () => import_schema.Schema,
25
27
  UrlParser: () => UrlParser,
26
28
  VisitResult: () => VisitResult,
29
+ findAnnotation: () => findAnnotation,
30
+ findNode: () => findNode,
31
+ findProperty: () => findProperty,
27
32
  getAnnotation: () => getAnnotation,
28
33
  getParamKeyAnnotation: () => getParamKeyAnnotation,
29
- getProperty: () => getProperty,
30
- getType: () => getType,
31
- isLeafType: () => isLeafType,
34
+ getSimpleType: () => getSimpleType,
35
+ isSimpleType: () => isSimpleType,
32
36
  visit: () => visit
33
37
  });
34
38
  module.exports = __toCommonJS(node_exports);
@@ -40,83 +44,163 @@ var import_schema3 = require("@effect/schema");
40
44
  var import_effect2 = require("effect");
41
45
  var import_util = require("@dxos/util");
42
46
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/effect/src/ast.ts";
43
- var isLeafType = (node) => !import_schema2.AST.isTupleType(node) && !import_schema2.AST.isTypeLiteral(node);
44
- var getAnnotation = (annotationId, node) => (0, import_effect.pipe)(import_schema2.AST.getAnnotation(annotationId)(node), import_effect.Option.getOrUndefined);
45
- var getType = (node) => {
46
- if (import_schema2.AST.isUnion(node)) {
47
- return node.types.find((type) => getType(type));
48
- } else if (import_schema2.AST.isRefinement(node)) {
49
- return getType(node.from);
50
- } else {
51
- return node;
47
+ var getSimpleType = (node) => {
48
+ if (import_schema2.AST.isObjectKeyword(node)) {
49
+ return "object";
52
50
  }
53
- };
54
- var getProperty = (schema, path) => {
55
- let node = schema.ast;
56
- for (const part of path.split(".")) {
57
- const props = import_schema2.AST.getPropertySignatures(node);
58
- const prop = props.find((prop2) => prop2.name === part);
59
- if (!prop) {
60
- return void 0;
61
- }
62
- const type = getType(prop.type);
63
- (0, import_invariant.invariant)(type, `invalid type: ${path}`, {
64
- F: __dxlog_file,
65
- L: 52,
66
- S: void 0,
67
- A: [
68
- "type",
69
- "`invalid type: ${path}`"
70
- ]
71
- });
72
- node = type;
51
+ if (import_schema2.AST.isStringKeyword(node)) {
52
+ return "string";
53
+ }
54
+ if (import_schema2.AST.isNumberKeyword(node)) {
55
+ return "number";
56
+ }
57
+ if (import_schema2.AST.isBooleanKeyword(node)) {
58
+ return "boolean";
59
+ }
60
+ if (import_schema2.AST.isEnums(node)) {
61
+ return "enum";
62
+ }
63
+ if (import_schema2.AST.isLiteral(node)) {
64
+ return "literal";
73
65
  }
74
- return node;
75
66
  };
67
+ var isSimpleType = (node) => !!getSimpleType(node);
68
+ var PATH_REGEX = /[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*/;
69
+ var PROP_REGEX = /\w+/;
70
+ var JsonPath = import_schema2.Schema.NonEmptyString.pipe(import_schema2.Schema.pattern(PATH_REGEX));
71
+ var JsonProp = import_schema2.Schema.NonEmptyString.pipe(import_schema2.Schema.pattern(PROP_REGEX));
72
+ var getAnnotation = (annotationId) => (node) => (0, import_effect.pipe)(import_schema2.AST.getAnnotation(annotationId)(node), import_effect.Option.getOrUndefined);
76
73
  var VisitResult;
77
74
  (function(VisitResult2) {
78
75
  VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
79
76
  VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
80
77
  VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
81
78
  })(VisitResult || (VisitResult = {}));
79
+ var defaultTest = (node) => isSimpleType(node) ? 0 : 1;
82
80
  var visit = (node, testOrVisitor, visitor) => {
83
81
  if (!visitor) {
84
- visitNode(node, void 0, testOrVisitor);
82
+ visitNode(node, defaultTest, testOrVisitor);
85
83
  } else {
86
84
  visitNode(node, testOrVisitor, visitor);
87
85
  }
88
86
  };
89
87
  var visitNode = (node, test, visitor, path = [], depth = 0) => {
90
- for (const prop of import_schema2.AST.getPropertySignatures(node)) {
91
- const currentPath = [
92
- ...path,
93
- prop.name.toString()
94
- ];
95
- const type = getType(prop.type);
96
- if (type) {
97
- const result = test?.(node, path, depth) ?? 0;
98
- if (result === 2) {
99
- return result;
88
+ const result = test?.(node, path, depth) ?? 0;
89
+ if (result === 2) {
90
+ return result;
91
+ }
92
+ if (result !== 1) {
93
+ visitor(node, path, depth);
94
+ }
95
+ if (import_schema2.AST.isTypeLiteral(node)) {
96
+ for (const prop of import_schema2.AST.getPropertySignatures(node)) {
97
+ const currentPath = [
98
+ ...path,
99
+ prop.name.toString()
100
+ ];
101
+ const result2 = visitNode(prop.type, test, visitor, currentPath, depth + 1);
102
+ if (result2 === 2) {
103
+ return result2;
100
104
  }
101
- visitor(type, currentPath, depth);
102
- if (result !== 1) {
103
- if (import_schema2.AST.isTypeLiteral(type)) {
104
- visitNode(type, test, visitor, currentPath, depth + 1);
105
- } else if (import_schema2.AST.isTupleType(type)) {
106
- for (const [i, elementType] of type.elements.entries()) {
107
- const type2 = getType(elementType.type);
108
- if (type2) {
109
- visitNode(type2, test, visitor, [
110
- i,
111
- ...currentPath
112
- ], depth);
113
- }
114
- }
115
- }
105
+ }
106
+ } else if (import_schema2.AST.isTupleType(node)) {
107
+ for (const [i, element] of node.elements.entries()) {
108
+ const currentPath = [
109
+ ...path,
110
+ i
111
+ ];
112
+ const result2 = visitNode(element.type, test, visitor, currentPath, depth);
113
+ if (result2 === 2) {
114
+ return result2;
116
115
  }
117
116
  }
117
+ } else if (import_schema2.AST.isUnion(node)) {
118
+ for (const type of node.types) {
119
+ const result2 = visitNode(type, test, visitor, path, depth);
120
+ if (result2 === 2) {
121
+ return result2;
122
+ }
123
+ }
124
+ } else if (import_schema2.AST.isRefinement(node)) {
125
+ const result2 = visitNode(node.from, test, visitor, path, depth);
126
+ if (result2 === 2) {
127
+ return result2;
128
+ }
118
129
  }
119
130
  };
131
+ var findNode = (node, test) => {
132
+ if (test(node)) {
133
+ return node;
134
+ } else if (import_schema2.AST.isTypeLiteral(node)) {
135
+ for (const prop of import_schema2.AST.getPropertySignatures(node)) {
136
+ const child = findNode(prop.type, test);
137
+ if (child) {
138
+ return child;
139
+ }
140
+ }
141
+ } else if (import_schema2.AST.isTupleType(node)) {
142
+ for (const [_, element] of node.elements.entries()) {
143
+ const child = findNode(element.type, test);
144
+ if (child) {
145
+ return child;
146
+ }
147
+ }
148
+ } else if (import_schema2.AST.isUnion(node)) {
149
+ for (const type of node.types) {
150
+ const child = findNode(type, test);
151
+ if (child) {
152
+ return child;
153
+ }
154
+ }
155
+ } else if (import_schema2.AST.isRefinement(node)) {
156
+ return findNode(node.from, test);
157
+ }
158
+ return void 0;
159
+ };
160
+ var findProperty = (schema, path) => {
161
+ const getProp = (node, path2) => {
162
+ const [name, ...rest] = path2;
163
+ const typeNode = findNode(node, import_schema2.AST.isTypeLiteral);
164
+ (0, import_invariant.invariant)(typeNode, void 0, {
165
+ F: __dxlog_file,
166
+ L: 214,
167
+ S: void 0,
168
+ A: [
169
+ "typeNode",
170
+ ""
171
+ ]
172
+ });
173
+ for (const prop of import_schema2.AST.getPropertySignatures(typeNode)) {
174
+ if (prop.name === name) {
175
+ if (rest.length) {
176
+ return getProp(prop.type, rest);
177
+ } else {
178
+ return prop.type;
179
+ }
180
+ }
181
+ }
182
+ return void 0;
183
+ };
184
+ return getProp(schema.ast, path.split("."));
185
+ };
186
+ var findAnnotation = (node, annotationId) => {
187
+ const getAnnotationById = getAnnotation(annotationId);
188
+ const getBaseAnnotation = (node2) => {
189
+ const value = getAnnotationById(node2);
190
+ if (value !== void 0) {
191
+ return value;
192
+ }
193
+ if (import_schema2.AST.isUnion(node2)) {
194
+ for (const type of node2.types) {
195
+ const value2 = getBaseAnnotation(type);
196
+ if (value2 !== void 0) {
197
+ return value2;
198
+ }
199
+ }
200
+ }
201
+ };
202
+ return getBaseAnnotation(node);
203
+ };
120
204
  var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
121
205
  var getParamKeyAnnotation = import_schema3.AST.getAnnotation(ParamKeyAnnotationId);
122
206
  var ParamKeyAnnotation = (value) => (self) => self.annotations({
@@ -171,15 +255,19 @@ var UrlParser = class {
171
255
  0 && (module.exports = {
172
256
  AST,
173
257
  JSONSchema,
258
+ JsonPath,
259
+ JsonProp,
174
260
  ParamKeyAnnotation,
175
261
  S,
176
262
  UrlParser,
177
263
  VisitResult,
264
+ findAnnotation,
265
+ findNode,
266
+ findProperty,
178
267
  getAnnotation,
179
268
  getParamKeyAnnotation,
180
- getProperty,
181
- getType,
182
- isLeafType,
269
+ getSimpleType,
270
+ isSimpleType,
183
271
  visit
184
272
  });
185
273
  //# sourceMappingURL=index.cjs.map