@dxos/effect 0.7.1 → 0.7.2-staging.6d26b2a
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 +35 -14
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +36 -14
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +35 -14
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/ast.d.ts +17 -11
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/sanity.test.d.ts +2 -0
- package/dist/types/src/sanity.test.d.ts.map +1 -0
- package/package.json +6 -5
- package/src/ast.test.ts +1 -2
- package/src/ast.ts +73 -28
- package/src/sanity.test.ts +69 -0
|
@@ -34,14 +34,13 @@ var PATH_REGEX = /[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*/;
|
|
|
34
34
|
var PROP_REGEX = /\w+/;
|
|
35
35
|
var JsonPath = S.NonEmptyString.pipe(S.pattern(PATH_REGEX));
|
|
36
36
|
var JsonProp = S.NonEmptyString.pipe(S.pattern(PROP_REGEX));
|
|
37
|
-
var getAnnotation = (annotationId) => (node) => pipe(AST.getAnnotation(annotationId)(node), Option.getOrUndefined);
|
|
38
37
|
var VisitResult;
|
|
39
38
|
(function(VisitResult2) {
|
|
40
39
|
VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
|
|
41
40
|
VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
|
|
42
41
|
VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
|
|
43
42
|
})(VisitResult || (VisitResult = {}));
|
|
44
|
-
var defaultTest =
|
|
43
|
+
var defaultTest = isSimpleType;
|
|
45
44
|
var visit = (node, testOrVisitor, visitor) => {
|
|
46
45
|
if (!visitor) {
|
|
47
46
|
visitNode(node, defaultTest, testOrVisitor);
|
|
@@ -50,7 +49,8 @@ var visit = (node, testOrVisitor, visitor) => {
|
|
|
50
49
|
}
|
|
51
50
|
};
|
|
52
51
|
var visitNode = (node, test, visitor, path = [], depth = 0) => {
|
|
53
|
-
const
|
|
52
|
+
const _result = test?.(node, path, depth);
|
|
53
|
+
const result = _result === void 0 ? 0 : typeof _result === "boolean" ? _result ? 0 : 1 : _result;
|
|
54
54
|
if (result === 2) {
|
|
55
55
|
return result;
|
|
56
56
|
}
|
|
@@ -129,7 +129,7 @@ var findProperty = (schema, path) => {
|
|
|
129
129
|
const typeNode = findNode(node, AST.isTypeLiteral);
|
|
130
130
|
invariant(typeNode, void 0, {
|
|
131
131
|
F: __dxlog_file,
|
|
132
|
-
L:
|
|
132
|
+
L: 223,
|
|
133
133
|
S: void 0,
|
|
134
134
|
A: [
|
|
135
135
|
"typeNode",
|
|
@@ -154,14 +154,18 @@ var defaultAnnotations = {
|
|
|
154
154
|
["NumberKeyword"]: AST.numberKeyword,
|
|
155
155
|
["BooleanKeyword"]: AST.booleanKeyword
|
|
156
156
|
};
|
|
157
|
-
var
|
|
158
|
-
const
|
|
157
|
+
var getAnnotation = (annotationId, noDefault = true) => (node) => {
|
|
158
|
+
const value = pipe(AST.getAnnotation(annotationId)(node), Option.getOrUndefined);
|
|
159
|
+
if (noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {
|
|
160
|
+
return void 0;
|
|
161
|
+
}
|
|
162
|
+
return value;
|
|
163
|
+
};
|
|
164
|
+
var findAnnotation = (node, annotationId, noDefault = true) => {
|
|
165
|
+
const getAnnotationById = getAnnotation(annotationId, noDefault);
|
|
159
166
|
const getBaseAnnotation = (node2) => {
|
|
160
167
|
const value = getAnnotationById(node2);
|
|
161
168
|
if (value !== void 0) {
|
|
162
|
-
if (options?.noDefault && value === defaultAnnotations[node2._tag]?.annotations[annotationId]) {
|
|
163
|
-
return void 0;
|
|
164
|
-
}
|
|
165
169
|
return value;
|
|
166
170
|
}
|
|
167
171
|
if (AST.isUnion(node2)) {
|
|
@@ -184,7 +188,7 @@ var isDiscriminatedUnion = (node) => {
|
|
|
184
188
|
var getDiscriminatingProps = (node) => {
|
|
185
189
|
invariant(AST.isUnion(node), void 0, {
|
|
186
190
|
F: __dxlog_file,
|
|
187
|
-
L:
|
|
191
|
+
L: 318,
|
|
188
192
|
S: void 0,
|
|
189
193
|
A: [
|
|
190
194
|
"AST.isUnion(node)",
|
|
@@ -202,7 +206,7 @@ var getDiscriminatingProps = (node) => {
|
|
|
202
206
|
var getDiscriminatedType = (node, value = {}) => {
|
|
203
207
|
invariant(AST.isUnion(node), void 0, {
|
|
204
208
|
F: __dxlog_file,
|
|
205
|
-
L:
|
|
209
|
+
L: 339,
|
|
206
210
|
S: void 0,
|
|
207
211
|
A: [
|
|
208
212
|
"AST.isUnion(node)",
|
|
@@ -211,7 +215,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
211
215
|
});
|
|
212
216
|
invariant(value, void 0, {
|
|
213
217
|
F: __dxlog_file,
|
|
214
|
-
L:
|
|
218
|
+
L: 340,
|
|
215
219
|
S: void 0,
|
|
216
220
|
A: [
|
|
217
221
|
"value",
|
|
@@ -226,7 +230,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
226
230
|
const match = AST.getPropertySignatures(type).filter((prop) => props?.includes(prop.name.toString())).every((prop) => {
|
|
227
231
|
invariant(AST.isLiteral(prop.type), void 0, {
|
|
228
232
|
F: __dxlog_file,
|
|
229
|
-
L:
|
|
233
|
+
L: 351,
|
|
230
234
|
S: void 0,
|
|
231
235
|
A: [
|
|
232
236
|
"AST.isLiteral(prop.type)",
|
|
@@ -244,7 +248,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
244
248
|
const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop);
|
|
245
249
|
invariant(AST.isLiteral(literal.type), void 0, {
|
|
246
250
|
F: __dxlog_file,
|
|
247
|
-
L:
|
|
251
|
+
L: 369,
|
|
248
252
|
S: void 0,
|
|
249
253
|
A: [
|
|
250
254
|
"AST.isLiteral(literal.type)",
|
|
@@ -261,6 +265,22 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
261
265
|
const schema = S.Struct(fields);
|
|
262
266
|
return schema.ast;
|
|
263
267
|
};
|
|
268
|
+
var mapAst = (ast, f) => {
|
|
269
|
+
switch (ast._tag) {
|
|
270
|
+
case "TypeLiteral":
|
|
271
|
+
return new AST.TypeLiteral(ast.propertySignatures.map((prop) => new AST.PropertySignature(prop.name, f(prop.type), prop.isOptional, prop.isReadonly, prop.annotations)), ast.indexSignatures);
|
|
272
|
+
case "Union":
|
|
273
|
+
return AST.Union.make(ast.types.map(f), ast.annotations);
|
|
274
|
+
case "TupleType":
|
|
275
|
+
return new AST.TupleType(ast.elements.map((t) => new AST.OptionalType(f(t.type), t.isOptional, t.annotations)), ast.rest.map((t) => new AST.Type(f(t.type), t.annotations)), ast.isReadonly, ast.annotations);
|
|
276
|
+
case "Suspend": {
|
|
277
|
+
const newAst = f(ast.f());
|
|
278
|
+
return new AST.Suspend(() => newAst, ast.annotations);
|
|
279
|
+
}
|
|
280
|
+
default:
|
|
281
|
+
return ast;
|
|
282
|
+
}
|
|
283
|
+
};
|
|
264
284
|
|
|
265
285
|
// packages/common/effect/src/url.ts
|
|
266
286
|
import { AST as AST2 } from "@effect/schema";
|
|
@@ -337,6 +357,7 @@ export {
|
|
|
337
357
|
isLiteralUnion,
|
|
338
358
|
isOption,
|
|
339
359
|
isSimpleType,
|
|
360
|
+
mapAst,
|
|
340
361
|
visit
|
|
341
362
|
};
|
|
342
363
|
//# 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\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';\nimport { nonNullable } from '@dxos/util';\n\n//\n// Refs\n// https://effect.website/docs/schema/introduction\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\n/**\n * Get the base type; e.g., traverse through refinements.\n */\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node) || AST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {\n return 'object';\n }\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\n if (AST.isEnums(node)) {\n return 'enum';\n }\n\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST): boolean => !!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 (e.g., optional, discriminated unions).\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): Transforms?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Rewrite using visitNode?\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 // Tuple.\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 (e.g., optional, discriminated unions).\n else if (AST.isUnion(node)) {\n if (isOption(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\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\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\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n//\n// Annotations\n//\n\nconst defaultAnnotations: Record<string, AST.Annotated> = {\n ['ObjectKeyword' as const]: AST.objectKeyword,\n ['StringKeyword' as const]: AST.stringKeyword,\n ['NumberKeyword' as const]: AST.numberKeyword,\n ['BooleanKeyword' as const]: AST.booleanKeyword,\n};\n\n/**\n * Recursively descend into AST to find first matching annotations.\n * Optionally skips default annotations for basic types (e.g., 'a string').\n */\nexport const findAnnotation = <T>(\n node: AST.AST,\n annotationId: symbol,\n options?: { noDefault: boolean },\n): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId);\n\n const getBaseAnnotation = (node: AST.AST): T | undefined => {\n const value = getAnnotationById(node);\n if (value !== undefined) {\n if (options?.noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {\n return undefined;\n }\n\n return value as T;\n }\n\n if (AST.isUnion(node)) {\n if (isOption(node)) {\n return getAnnotationById(node.types[0]) as T;\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n\n//\n// Unions\n//\n\n/**\n * Effect S.optional creates a union type with undefined as the second type.\n */\nexport const isOption = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.length === 2 && AST.isUndefinedKeyword(node.types[1]);\n};\n\n/**\n * Determines if the node is a union of literal types.\n */\nexport const isLiteralUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.every(AST.isLiteral);\n};\n\n/**\n * Determines if the node is a discriminated union.\n */\nexport const isDiscriminatedUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && !!getDiscriminatingProps(node)?.length;\n};\n\n/**\n * Get the discriminating properties for the given union type.\n */\nexport const getDiscriminatingProps = (node: AST.AST): string[] | undefined => {\n invariant(AST.isUnion(node));\n if (isOption(node)) {\n return;\n }\n\n // Get common literals across all types.\n return node.types.reduce<string[]>((shared, type) => {\n const props = AST.getPropertySignatures(type)\n // TODO(burdon): Should check each literal is unique.\n .filter((p) => AST.isLiteral(p.type))\n .map((p) => p.name.toString());\n\n // Return common literals.\n return shared.length === 0 ? props : shared.filter((prop) => props.includes(prop));\n }, []);\n};\n\n/**\n * Get the discriminated type for the given value.\n */\nexport const getDiscriminatedType = (node: AST.AST, value: Record<string, any> = {}): AST.AST | undefined => {\n invariant(AST.isUnion(node));\n invariant(value);\n const props = getDiscriminatingProps(node);\n if (!props?.length) {\n return;\n }\n\n // Match provided values.\n for (const type of node.types) {\n const match = AST.getPropertySignatures(type)\n .filter((prop) => props?.includes(prop.name.toString()))\n .every((prop) => {\n invariant(AST.isLiteral(prop.type));\n return prop.type.literal === value[prop.name.toString()];\n });\n\n if (match) {\n return type;\n }\n }\n\n // Create union of discriminating properties.\n // NOTE: This may not work with non-overlapping variants.\n // TODO(burdon): Iterate through props and knock-out variants that don't match.\n const fields = Object.fromEntries(\n props\n .map((prop) => {\n const literals = node.types\n .map((type) => {\n const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop)!;\n invariant(AST.isLiteral(literal.type));\n return literal.type.literal;\n })\n .filter(nonNullable);\n\n return literals.length ? [prop, S.Literal(...literals)] : undefined;\n })\n .filter(nonNullable),\n );\n\n const schema = S.Struct(fields);\n return schema.ast;\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;AAC1B,SAASC,mBAAmB;;AAcrB,IAAMC,gBAAgB,CAACC,SAAAA;AAC5B,MAAIR,IAAIS,gBAAgBD,IAAAA,KAASR,IAAIU,cAAcF,IAAAA,KAASG,qBAAqBH,IAAAA,GAAO;AACtF,WAAO;EACT;AAEA,MAAIR,IAAIY,gBAAgBJ,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIR,IAAIa,gBAAgBL,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIR,IAAIc,iBAAiBN,IAAAA,GAAO;AAC9B,WAAO;EACT;AAEA,MAAIR,IAAIe,QAAQP,IAAAA,GAAO;AACrB,WAAO;EACT;AAEA,MAAIR,IAAIgB,UAAUR,IAAAA,GAAO;AACvB,WAAO;EACT;AACF;AAEO,IAAMS,eAAe,CAACT,SAA2B,CAAC,CAACD,cAAcC,IAAAA;AASxE,IAAMU,aAAa;AACnB,IAAMC,aAAa;AAKZ,IAAMC,WAAWlB,EAAEmB,eAAejB,KAAKF,EAAEoB,QAAQJ,UAAAA,CAAAA;AACjD,IAAMK,WAAWrB,EAAEmB,eAAejB,KAAKF,EAAEoB,QAAQH,UAAAA,CAAAA
|
|
6
|
-
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Schema", "S", "Option", "pipe", "invariant", "nonNullable", "getSimpleType", "node", "isObjectKeyword", "isTypeLiteral", "isDiscriminatedUnion", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "NonEmptyString", "pattern", "JsonProp", "
|
|
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';\nimport { nonNullable } from '@dxos/util';\n\n//\n// Refs\n// https://effect.website/docs/schema/introduction\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\n/**\n * Get the base type; e.g., traverse through refinements.\n */\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node) || AST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {\n return 'object';\n }\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\n if (AST.isEnums(node)) {\n return 'enum';\n }\n\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST): boolean => !!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\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 TestFn = (node: AST.AST, path: Path, depth: number) => VisitResult | boolean | undefined;\n\nexport type VisitorFn = (node: AST.AST, path: Path, depth: number) => void;\n\nconst defaultTest: TestFn = isSimpleType;\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: VisitorFn): void;\n (node: AST.AST, test: TestFn, visitor: VisitorFn): void;\n} = (node: AST.AST, testOrVisitor: TestFn | VisitorFn, visitor?: VisitorFn): void => {\n if (!visitor) {\n visitNode(node, defaultTest, testOrVisitor);\n } else {\n visitNode(node, testOrVisitor as TestFn, visitor);\n }\n};\n\nconst visitNode = (\n node: AST.AST,\n test: TestFn | undefined,\n visitor: VisitorFn,\n path: Path = [],\n depth = 0,\n): VisitResult | undefined => {\n const _result = test?.(node, path, depth);\n const result: VisitResult =\n _result === undefined\n ? VisitResult.CONTINUE\n : typeof _result === 'boolean'\n ? _result\n ? VisitResult.CONTINUE\n : VisitResult.SKIP\n : _result;\n\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 (e.g., optional, discriminated unions).\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): Transforms?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Rewrite using visitNode?\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 // Tuple.\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 (e.g., optional, discriminated unions).\n else if (AST.isUnion(node)) {\n if (isOption(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\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\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\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n//\n// Annotations\n//\n\nconst defaultAnnotations: Record<string, AST.Annotated> = {\n ['ObjectKeyword' as const]: AST.objectKeyword,\n ['StringKeyword' as const]: AST.stringKeyword,\n ['NumberKeyword' as const]: AST.numberKeyword,\n ['BooleanKeyword' as const]: AST.booleanKeyword,\n};\n\n/**\n * Get annotation or return undefined.\n * @param annotationId\n * @param noDefault If true, then return undefined for effect library defined values.\n */\nexport const getAnnotation =\n <T>(annotationId: symbol, noDefault = true) =>\n (node: AST.AST): T | undefined => {\n const value = pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n if (noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {\n return undefined;\n }\n\n return value;\n };\n\n/**\n * Recursively descend into AST to find first matching annotations.\n * Optionally skips default annotations for basic types (e.g., 'a string').\n */\n// TODO(burdon): Convert to effect pattern (i.e., return operator like getAnnotation).\nexport const findAnnotation = <T>(node: AST.AST, annotationId: symbol, noDefault = true): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId, noDefault);\n\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 if (isOption(node)) {\n return getAnnotationById(node.types[0]) as T;\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n\n//\n// Unions\n//\n\n/**\n * Effect S.optional creates a union type with undefined as the second type.\n */\nexport const isOption = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.length === 2 && AST.isUndefinedKeyword(node.types[1]);\n};\n\n/**\n * Determines if the node is a union of literal types.\n */\nexport const isLiteralUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.every(AST.isLiteral);\n};\n\n/**\n * Determines if the node is a discriminated union.\n */\nexport const isDiscriminatedUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && !!getDiscriminatingProps(node)?.length;\n};\n\n/**\n * Get the discriminating properties for the given union type.\n */\nexport const getDiscriminatingProps = (node: AST.AST): string[] | undefined => {\n invariant(AST.isUnion(node));\n if (isOption(node)) {\n return;\n }\n\n // Get common literals across all types.\n return node.types.reduce<string[]>((shared, type) => {\n const props = AST.getPropertySignatures(type)\n // TODO(burdon): Should check each literal is unique.\n .filter((p) => AST.isLiteral(p.type))\n .map((p) => p.name.toString());\n\n // Return common literals.\n return shared.length === 0 ? props : shared.filter((prop) => props.includes(prop));\n }, []);\n};\n\n/**\n * Get the discriminated type for the given value.\n */\nexport const getDiscriminatedType = (node: AST.AST, value: Record<string, any> = {}): AST.AST | undefined => {\n invariant(AST.isUnion(node));\n invariant(value);\n const props = getDiscriminatingProps(node);\n if (!props?.length) {\n return;\n }\n\n // Match provided values.\n for (const type of node.types) {\n const match = AST.getPropertySignatures(type)\n .filter((prop) => props?.includes(prop.name.toString()))\n .every((prop) => {\n invariant(AST.isLiteral(prop.type));\n return prop.type.literal === value[prop.name.toString()];\n });\n\n if (match) {\n return type;\n }\n }\n\n // Create union of discriminating properties.\n // NOTE: This may not work with non-overlapping variants.\n // TODO(burdon): Iterate through props and knock-out variants that don't match.\n const fields = Object.fromEntries(\n props\n .map((prop) => {\n const literals = node.types\n .map((type) => {\n const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop)!;\n invariant(AST.isLiteral(literal.type));\n return literal.type.literal;\n })\n .filter(nonNullable);\n\n return literals.length ? [prop, S.Literal(...literals)] : undefined;\n })\n .filter(nonNullable),\n );\n\n const schema = S.Struct(fields);\n return schema.ast;\n};\n\n/**\n * Maps AST nodes.\n * The user is responsible for recursively calling {@link mapAst} on the AST.\n * NOTE: Will evaluate suspended ASTs.\n */\nexport const mapAst = (ast: AST.AST, f: (ast: AST.AST) => AST.AST): AST.AST => {\n switch (ast._tag) {\n case 'TypeLiteral':\n return new AST.TypeLiteral(\n ast.propertySignatures.map(\n (prop) =>\n new AST.PropertySignature(prop.name, f(prop.type), prop.isOptional, prop.isReadonly, prop.annotations),\n ),\n ast.indexSignatures,\n );\n case 'Union':\n return AST.Union.make(ast.types.map(f), ast.annotations);\n case 'TupleType':\n return new AST.TupleType(\n ast.elements.map((t) => new AST.OptionalType(f(t.type), t.isOptional, t.annotations)),\n ast.rest.map((t) => new AST.Type(f(t.type), t.annotations)),\n ast.isReadonly,\n ast.annotations,\n );\n case 'Suspend': {\n const newAst = f(ast.f());\n return new AST.Suspend(() => newAst, ast.annotations);\n }\n default:\n // TODO(dmaretskyi): Support more nodes.\n return ast;\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,UAAS;;;ACA7C,SAASC,KAAKC,UAAUC,SAAS;AACjC,SAASC,QAAQC,YAAY;AAE7B,SAASC,iBAAiB;AAC1B,SAASC,mBAAmB;;AAcrB,IAAMC,gBAAgB,CAACC,SAAAA;AAC5B,MAAIR,IAAIS,gBAAgBD,IAAAA,KAASR,IAAIU,cAAcF,IAAAA,KAASG,qBAAqBH,IAAAA,GAAO;AACtF,WAAO;EACT;AAEA,MAAIR,IAAIY,gBAAgBJ,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIR,IAAIa,gBAAgBL,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIR,IAAIc,iBAAiBN,IAAAA,GAAO;AAC9B,WAAO;EACT;AAEA,MAAIR,IAAIe,QAAQP,IAAAA,GAAO;AACrB,WAAO;EACT;AAEA,MAAIR,IAAIgB,UAAUR,IAAAA,GAAO;AACvB,WAAO;EACT;AACF;AAEO,IAAMS,eAAe,CAACT,SAA2B,CAAC,CAACD,cAAcC,IAAAA;AASxE,IAAMU,aAAa;AACnB,IAAMC,aAAa;AAKZ,IAAMC,WAAWlB,EAAEmB,eAAejB,KAAKF,EAAEoB,QAAQJ,UAAAA,CAAAA;AACjD,IAAMK,WAAWrB,EAAEmB,eAAejB,KA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aC,IAAIC,WAAWJ,GAAAA,CAAAA;AAC5C,UAAIf,SAAS,MAAM;AACjBA,gBAAQQ,IAAIS,aAAaC,IAAIH,GAAAA;MAC/B;AAEA,UAAIf,SAAS,MAAM;AACjB,YAAIH,KAAIuB,gBAAgBJ,KAAKK,GAAG,GAAG;AACjCP,iBAAOC,GAAAA,IAAOO,SAAStB,KAAAA;QACzB,WAAWH,KAAI0B,iBAAiBP,KAAKK,GAAG,GAAG;AACzCP,iBAAOC,GAAAA,IAAOf,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACLc,iBAAOC,GAAAA,IAAOf;QAChB;MACF;AAEA,aAAOc;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAU,OAAOjB,MAAcO,QAAgB;AACnC,UAAMN,MAAM,IAAIC,IAAIF,IAAAA;AACpBG,WAAOC,QAAQG,MAAAA,EAAQW,QAAQ,CAAC,CAACV,KAAKf,KAAAA,MAAM;AAC1C,UAAIA,UAAU0B,QAAW;AACvB,cAAMC,QAAQ,KAAKtB,QAAQO,OAAOG,GAAAA;AAClC,YAAIY,OAAO;AACT,gBAAM,EAAEZ,KAAKa,cAAa,IAAKC,MAC7BjC,sBAAsB+B,MAAMN,GAAG,GAC/BS,QAAOC,UAAU,OAAO;YACtBhB,KAAKI,WAAWJ,GAAAA;UAClB,EAAA,CAAA;AAGFP,cAAIS,aAAae,IAAIJ,eAAeK,OAAOjC,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Schema", "S", "Option", "pipe", "invariant", "nonNullable", "getSimpleType", "node", "isObjectKeyword", "isTypeLiteral", "isDiscriminatedUnion", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "NonEmptyString", "pattern", "JsonProp", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "_result", "result", "undefined", "prop", "getPropertySignatures", "currentPath", "name", "toString", "type", "isTupleType", "i", "element", "elements", "entries", "isUnion", "types", "isRefinement", "from", "findNode", "child", "_", "isOption", "findProperty", "schema", "getProp", "rest", "typeNode", "length", "ast", "split", "defaultAnnotations", "objectKeyword", "stringKeyword", "numberKeyword", "booleanKeyword", "getAnnotation", "annotationId", "noDefault", "value", "getOrUndefined", "_tag", "annotations", "findAnnotation", "getAnnotationById", "getBaseAnnotation", "isUndefinedKeyword", "isLiteralUnion", "every", "getDiscriminatingProps", "reduce", "shared", "props", "filter", "p", "map", "includes", "getDiscriminatedType", "match", "literal", "fields", "Object", "fromEntries", "literals", "find", "Literal", "Struct", "mapAst", "f", "TypeLiteral", "propertySignatures", "PropertySignature", "isOptional", "isReadonly", "indexSignatures", "Union", "make", "TupleType", "t", "OptionalType", "Type", "newAst", "Suspend", "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":
|
|
1
|
+
{"inputs":{"packages/common/effect/src/ast.ts":{"bytes":38855,"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":"@dxos/util","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":24310},"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":"@dxos/util","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","getDiscriminatedType","getDiscriminatingProps","getParamKeyAnnotation","getSimpleType","isDiscriminatedUnion","isLiteralUnion","isOption","isSimpleType","mapAst","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":72},"packages/common/effect/src/ast.ts":{"bytesInOutput":8099},"packages/common/effect/src/url.ts":{"bytesInOutput":1624}},"bytes":10348}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -38,6 +38,7 @@ __export(node_exports, {
|
|
|
38
38
|
isLiteralUnion: () => isLiteralUnion,
|
|
39
39
|
isOption: () => isOption,
|
|
40
40
|
isSimpleType: () => isSimpleType,
|
|
41
|
+
mapAst: () => mapAst,
|
|
41
42
|
visit: () => visit
|
|
42
43
|
});
|
|
43
44
|
module.exports = __toCommonJS(node_exports);
|
|
@@ -75,14 +76,13 @@ var PATH_REGEX = /[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*/;
|
|
|
75
76
|
var PROP_REGEX = /\w+/;
|
|
76
77
|
var JsonPath = import_schema2.Schema.NonEmptyString.pipe(import_schema2.Schema.pattern(PATH_REGEX));
|
|
77
78
|
var JsonProp = import_schema2.Schema.NonEmptyString.pipe(import_schema2.Schema.pattern(PROP_REGEX));
|
|
78
|
-
var getAnnotation = (annotationId) => (node) => (0, import_effect.pipe)(import_schema2.AST.getAnnotation(annotationId)(node), import_effect.Option.getOrUndefined);
|
|
79
79
|
var VisitResult;
|
|
80
80
|
(function(VisitResult2) {
|
|
81
81
|
VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
|
|
82
82
|
VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
|
|
83
83
|
VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
|
|
84
84
|
})(VisitResult || (VisitResult = {}));
|
|
85
|
-
var defaultTest =
|
|
85
|
+
var defaultTest = isSimpleType;
|
|
86
86
|
var visit = (node, testOrVisitor, visitor) => {
|
|
87
87
|
if (!visitor) {
|
|
88
88
|
visitNode(node, defaultTest, testOrVisitor);
|
|
@@ -91,7 +91,8 @@ var visit = (node, testOrVisitor, visitor) => {
|
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
93
|
var visitNode = (node, test, visitor, path = [], depth = 0) => {
|
|
94
|
-
const
|
|
94
|
+
const _result = test?.(node, path, depth);
|
|
95
|
+
const result = _result === void 0 ? 0 : typeof _result === "boolean" ? _result ? 0 : 1 : _result;
|
|
95
96
|
if (result === 2) {
|
|
96
97
|
return result;
|
|
97
98
|
}
|
|
@@ -170,7 +171,7 @@ var findProperty = (schema, path) => {
|
|
|
170
171
|
const typeNode = findNode(node, import_schema2.AST.isTypeLiteral);
|
|
171
172
|
(0, import_invariant.invariant)(typeNode, void 0, {
|
|
172
173
|
F: __dxlog_file,
|
|
173
|
-
L:
|
|
174
|
+
L: 223,
|
|
174
175
|
S: void 0,
|
|
175
176
|
A: [
|
|
176
177
|
"typeNode",
|
|
@@ -195,14 +196,18 @@ var defaultAnnotations = {
|
|
|
195
196
|
["NumberKeyword"]: import_schema2.AST.numberKeyword,
|
|
196
197
|
["BooleanKeyword"]: import_schema2.AST.booleanKeyword
|
|
197
198
|
};
|
|
198
|
-
var
|
|
199
|
-
const
|
|
199
|
+
var getAnnotation = (annotationId, noDefault = true) => (node) => {
|
|
200
|
+
const value = (0, import_effect.pipe)(import_schema2.AST.getAnnotation(annotationId)(node), import_effect.Option.getOrUndefined);
|
|
201
|
+
if (noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {
|
|
202
|
+
return void 0;
|
|
203
|
+
}
|
|
204
|
+
return value;
|
|
205
|
+
};
|
|
206
|
+
var findAnnotation = (node, annotationId, noDefault = true) => {
|
|
207
|
+
const getAnnotationById = getAnnotation(annotationId, noDefault);
|
|
200
208
|
const getBaseAnnotation = (node2) => {
|
|
201
209
|
const value = getAnnotationById(node2);
|
|
202
210
|
if (value !== void 0) {
|
|
203
|
-
if (options?.noDefault && value === defaultAnnotations[node2._tag]?.annotations[annotationId]) {
|
|
204
|
-
return void 0;
|
|
205
|
-
}
|
|
206
211
|
return value;
|
|
207
212
|
}
|
|
208
213
|
if (import_schema2.AST.isUnion(node2)) {
|
|
@@ -225,7 +230,7 @@ var isDiscriminatedUnion = (node) => {
|
|
|
225
230
|
var getDiscriminatingProps = (node) => {
|
|
226
231
|
(0, import_invariant.invariant)(import_schema2.AST.isUnion(node), void 0, {
|
|
227
232
|
F: __dxlog_file,
|
|
228
|
-
L:
|
|
233
|
+
L: 318,
|
|
229
234
|
S: void 0,
|
|
230
235
|
A: [
|
|
231
236
|
"AST.isUnion(node)",
|
|
@@ -243,7 +248,7 @@ var getDiscriminatingProps = (node) => {
|
|
|
243
248
|
var getDiscriminatedType = (node, value = {}) => {
|
|
244
249
|
(0, import_invariant.invariant)(import_schema2.AST.isUnion(node), void 0, {
|
|
245
250
|
F: __dxlog_file,
|
|
246
|
-
L:
|
|
251
|
+
L: 339,
|
|
247
252
|
S: void 0,
|
|
248
253
|
A: [
|
|
249
254
|
"AST.isUnion(node)",
|
|
@@ -252,7 +257,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
252
257
|
});
|
|
253
258
|
(0, import_invariant.invariant)(value, void 0, {
|
|
254
259
|
F: __dxlog_file,
|
|
255
|
-
L:
|
|
260
|
+
L: 340,
|
|
256
261
|
S: void 0,
|
|
257
262
|
A: [
|
|
258
263
|
"value",
|
|
@@ -267,7 +272,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
267
272
|
const match = import_schema2.AST.getPropertySignatures(type).filter((prop) => props?.includes(prop.name.toString())).every((prop) => {
|
|
268
273
|
(0, import_invariant.invariant)(import_schema2.AST.isLiteral(prop.type), void 0, {
|
|
269
274
|
F: __dxlog_file,
|
|
270
|
-
L:
|
|
275
|
+
L: 351,
|
|
271
276
|
S: void 0,
|
|
272
277
|
A: [
|
|
273
278
|
"AST.isLiteral(prop.type)",
|
|
@@ -285,7 +290,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
285
290
|
const literal = import_schema2.AST.getPropertySignatures(type).find((p) => p.name.toString() === prop);
|
|
286
291
|
(0, import_invariant.invariant)(import_schema2.AST.isLiteral(literal.type), void 0, {
|
|
287
292
|
F: __dxlog_file,
|
|
288
|
-
L:
|
|
293
|
+
L: 369,
|
|
289
294
|
S: void 0,
|
|
290
295
|
A: [
|
|
291
296
|
"AST.isLiteral(literal.type)",
|
|
@@ -302,6 +307,22 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
302
307
|
const schema = import_schema2.Schema.Struct(fields);
|
|
303
308
|
return schema.ast;
|
|
304
309
|
};
|
|
310
|
+
var mapAst = (ast, f) => {
|
|
311
|
+
switch (ast._tag) {
|
|
312
|
+
case "TypeLiteral":
|
|
313
|
+
return new import_schema2.AST.TypeLiteral(ast.propertySignatures.map((prop) => new import_schema2.AST.PropertySignature(prop.name, f(prop.type), prop.isOptional, prop.isReadonly, prop.annotations)), ast.indexSignatures);
|
|
314
|
+
case "Union":
|
|
315
|
+
return import_schema2.AST.Union.make(ast.types.map(f), ast.annotations);
|
|
316
|
+
case "TupleType":
|
|
317
|
+
return new import_schema2.AST.TupleType(ast.elements.map((t) => new import_schema2.AST.OptionalType(f(t.type), t.isOptional, t.annotations)), ast.rest.map((t) => new import_schema2.AST.Type(f(t.type), t.annotations)), ast.isReadonly, ast.annotations);
|
|
318
|
+
case "Suspend": {
|
|
319
|
+
const newAst = f(ast.f());
|
|
320
|
+
return new import_schema2.AST.Suspend(() => newAst, ast.annotations);
|
|
321
|
+
}
|
|
322
|
+
default:
|
|
323
|
+
return ast;
|
|
324
|
+
}
|
|
325
|
+
};
|
|
305
326
|
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
306
327
|
var getParamKeyAnnotation = import_schema3.AST.getAnnotation(ParamKeyAnnotationId);
|
|
307
328
|
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
@@ -374,6 +395,7 @@ var UrlParser = class {
|
|
|
374
395
|
isLiteralUnion,
|
|
375
396
|
isOption,
|
|
376
397
|
isSimpleType,
|
|
398
|
+
mapAst,
|
|
377
399
|
visit
|
|
378
400
|
});
|
|
379
401
|
//# sourceMappingURL=index.cjs.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\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';\nimport { nonNullable } from '@dxos/util';\n\n//\n// Refs\n// https://effect.website/docs/schema/introduction\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\n/**\n * Get the base type; e.g., traverse through refinements.\n */\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node) || AST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {\n return 'object';\n }\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\n if (AST.isEnums(node)) {\n return 'enum';\n }\n\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST): boolean => !!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 (e.g., optional, discriminated unions).\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): Transforms?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Rewrite using visitNode?\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 // Tuple.\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 (e.g., optional, discriminated unions).\n else if (AST.isUnion(node)) {\n if (isOption(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\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\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\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n//\n// Annotations\n//\n\nconst defaultAnnotations: Record<string, AST.Annotated> = {\n ['ObjectKeyword' as const]: AST.objectKeyword,\n ['StringKeyword' as const]: AST.stringKeyword,\n ['NumberKeyword' as const]: AST.numberKeyword,\n ['BooleanKeyword' as const]: AST.booleanKeyword,\n};\n\n/**\n * Recursively descend into AST to find first matching annotations.\n * Optionally skips default annotations for basic types (e.g., 'a string').\n */\nexport const findAnnotation = <T>(\n node: AST.AST,\n annotationId: symbol,\n options?: { noDefault: boolean },\n): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId);\n\n const getBaseAnnotation = (node: AST.AST): T | undefined => {\n const value = getAnnotationById(node);\n if (value !== undefined) {\n if (options?.noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {\n return undefined;\n }\n\n return value as T;\n }\n\n if (AST.isUnion(node)) {\n if (isOption(node)) {\n return getAnnotationById(node.types[0]) as T;\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n\n//\n// Unions\n//\n\n/**\n * Effect S.optional creates a union type with undefined as the second type.\n */\nexport const isOption = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.length === 2 && AST.isUndefinedKeyword(node.types[1]);\n};\n\n/**\n * Determines if the node is a union of literal types.\n */\nexport const isLiteralUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.every(AST.isLiteral);\n};\n\n/**\n * Determines if the node is a discriminated union.\n */\nexport const isDiscriminatedUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && !!getDiscriminatingProps(node)?.length;\n};\n\n/**\n * Get the discriminating properties for the given union type.\n */\nexport const getDiscriminatingProps = (node: AST.AST): string[] | undefined => {\n invariant(AST.isUnion(node));\n if (isOption(node)) {\n return;\n }\n\n // Get common literals across all types.\n return node.types.reduce<string[]>((shared, type) => {\n const props = AST.getPropertySignatures(type)\n // TODO(burdon): Should check each literal is unique.\n .filter((p) => AST.isLiteral(p.type))\n .map((p) => p.name.toString());\n\n // Return common literals.\n return shared.length === 0 ? props : shared.filter((prop) => props.includes(prop));\n }, []);\n};\n\n/**\n * Get the discriminated type for the given value.\n */\nexport const getDiscriminatedType = (node: AST.AST, value: Record<string, any> = {}): AST.AST | undefined => {\n invariant(AST.isUnion(node));\n invariant(value);\n const props = getDiscriminatingProps(node);\n if (!props?.length) {\n return;\n }\n\n // Match provided values.\n for (const type of node.types) {\n const match = AST.getPropertySignatures(type)\n .filter((prop) => props?.includes(prop.name.toString()))\n .every((prop) => {\n invariant(AST.isLiteral(prop.type));\n return prop.type.literal === value[prop.name.toString()];\n });\n\n if (match) {\n return type;\n }\n }\n\n // Create union of discriminating properties.\n // NOTE: This may not work with non-overlapping variants.\n // TODO(burdon): Iterate through props and knock-out variants that don't match.\n const fields = Object.fromEntries(\n props\n .map((prop) => {\n const literals = node.types\n .map((type) => {\n const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop)!;\n invariant(AST.isLiteral(literal.type));\n return literal.type.literal;\n })\n .filter(nonNullable);\n\n return literals.length ? [prop, S.Literal(...literals)] : undefined;\n })\n .filter(nonNullable),\n );\n\n const schema = S.Struct(fields);\n return schema.ast;\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": "
|
|
6
|
-
"names": ["import_schema", "import_effect", "import_util", "getSimpleType", "node", "AST", "isObjectKeyword", "isTypeLiteral", "isDiscriminatedUnion", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "S", "NonEmptyString", "pipe", "pattern", "JsonProp", "
|
|
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';\nimport { nonNullable } from '@dxos/util';\n\n//\n// Refs\n// https://effect.website/docs/schema/introduction\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\n/**\n * Get the base type; e.g., traverse through refinements.\n */\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node) || AST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {\n return 'object';\n }\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\n if (AST.isEnums(node)) {\n return 'enum';\n }\n\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST): boolean => !!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\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 TestFn = (node: AST.AST, path: Path, depth: number) => VisitResult | boolean | undefined;\n\nexport type VisitorFn = (node: AST.AST, path: Path, depth: number) => void;\n\nconst defaultTest: TestFn = isSimpleType;\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: VisitorFn): void;\n (node: AST.AST, test: TestFn, visitor: VisitorFn): void;\n} = (node: AST.AST, testOrVisitor: TestFn | VisitorFn, visitor?: VisitorFn): void => {\n if (!visitor) {\n visitNode(node, defaultTest, testOrVisitor);\n } else {\n visitNode(node, testOrVisitor as TestFn, visitor);\n }\n};\n\nconst visitNode = (\n node: AST.AST,\n test: TestFn | undefined,\n visitor: VisitorFn,\n path: Path = [],\n depth = 0,\n): VisitResult | undefined => {\n const _result = test?.(node, path, depth);\n const result: VisitResult =\n _result === undefined\n ? VisitResult.CONTINUE\n : typeof _result === 'boolean'\n ? _result\n ? VisitResult.CONTINUE\n : VisitResult.SKIP\n : _result;\n\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 (e.g., optional, discriminated unions).\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): Transforms?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Rewrite using visitNode?\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 // Tuple.\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 (e.g., optional, discriminated unions).\n else if (AST.isUnion(node)) {\n if (isOption(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\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\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\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n//\n// Annotations\n//\n\nconst defaultAnnotations: Record<string, AST.Annotated> = {\n ['ObjectKeyword' as const]: AST.objectKeyword,\n ['StringKeyword' as const]: AST.stringKeyword,\n ['NumberKeyword' as const]: AST.numberKeyword,\n ['BooleanKeyword' as const]: AST.booleanKeyword,\n};\n\n/**\n * Get annotation or return undefined.\n * @param annotationId\n * @param noDefault If true, then return undefined for effect library defined values.\n */\nexport const getAnnotation =\n <T>(annotationId: symbol, noDefault = true) =>\n (node: AST.AST): T | undefined => {\n const value = pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n if (noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {\n return undefined;\n }\n\n return value;\n };\n\n/**\n * Recursively descend into AST to find first matching annotations.\n * Optionally skips default annotations for basic types (e.g., 'a string').\n */\n// TODO(burdon): Convert to effect pattern (i.e., return operator like getAnnotation).\nexport const findAnnotation = <T>(node: AST.AST, annotationId: symbol, noDefault = true): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId, noDefault);\n\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 if (isOption(node)) {\n return getAnnotationById(node.types[0]) as T;\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n\n//\n// Unions\n//\n\n/**\n * Effect S.optional creates a union type with undefined as the second type.\n */\nexport const isOption = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.length === 2 && AST.isUndefinedKeyword(node.types[1]);\n};\n\n/**\n * Determines if the node is a union of literal types.\n */\nexport const isLiteralUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.every(AST.isLiteral);\n};\n\n/**\n * Determines if the node is a discriminated union.\n */\nexport const isDiscriminatedUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && !!getDiscriminatingProps(node)?.length;\n};\n\n/**\n * Get the discriminating properties for the given union type.\n */\nexport const getDiscriminatingProps = (node: AST.AST): string[] | undefined => {\n invariant(AST.isUnion(node));\n if (isOption(node)) {\n return;\n }\n\n // Get common literals across all types.\n return node.types.reduce<string[]>((shared, type) => {\n const props = AST.getPropertySignatures(type)\n // TODO(burdon): Should check each literal is unique.\n .filter((p) => AST.isLiteral(p.type))\n .map((p) => p.name.toString());\n\n // Return common literals.\n return shared.length === 0 ? props : shared.filter((prop) => props.includes(prop));\n }, []);\n};\n\n/**\n * Get the discriminated type for the given value.\n */\nexport const getDiscriminatedType = (node: AST.AST, value: Record<string, any> = {}): AST.AST | undefined => {\n invariant(AST.isUnion(node));\n invariant(value);\n const props = getDiscriminatingProps(node);\n if (!props?.length) {\n return;\n }\n\n // Match provided values.\n for (const type of node.types) {\n const match = AST.getPropertySignatures(type)\n .filter((prop) => props?.includes(prop.name.toString()))\n .every((prop) => {\n invariant(AST.isLiteral(prop.type));\n return prop.type.literal === value[prop.name.toString()];\n });\n\n if (match) {\n return type;\n }\n }\n\n // Create union of discriminating properties.\n // NOTE: This may not work with non-overlapping variants.\n // TODO(burdon): Iterate through props and knock-out variants that don't match.\n const fields = Object.fromEntries(\n props\n .map((prop) => {\n const literals = node.types\n .map((type) => {\n const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop)!;\n invariant(AST.isLiteral(literal.type));\n return literal.type.literal;\n })\n .filter(nonNullable);\n\n return literals.length ? [prop, S.Literal(...literals)] : undefined;\n })\n .filter(nonNullable),\n );\n\n const schema = S.Struct(fields);\n return schema.ast;\n};\n\n/**\n * Maps AST nodes.\n * The user is responsible for recursively calling {@link mapAst} on the AST.\n * NOTE: Will evaluate suspended ASTs.\n */\nexport const mapAst = (ast: AST.AST, f: (ast: AST.AST) => AST.AST): AST.AST => {\n switch (ast._tag) {\n case 'TypeLiteral':\n return new AST.TypeLiteral(\n ast.propertySignatures.map(\n (prop) =>\n new AST.PropertySignature(prop.name, f(prop.type), prop.isOptional, prop.isReadonly, prop.annotations),\n ),\n ast.indexSignatures,\n );\n case 'Union':\n return AST.Union.make(ast.types.map(f), ast.annotations);\n case 'TupleType':\n return new AST.TupleType(\n ast.elements.map((t) => new AST.OptionalType(f(t.type), t.isOptional, t.annotations)),\n ast.rest.map((t) => new AST.Type(f(t.type), t.annotations)),\n ast.isReadonly,\n ast.annotations,\n );\n case 'Suspend': {\n const newAst = f(ast.f());\n return new AST.Suspend(() => newAst, ast.annotations);\n }\n default:\n // TODO(dmaretskyi): Support more nodes.\n return ast;\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,oBAA6C;ACA7C,IAAAA,iBAAiC;AACjC,oBAA6B;AAE7B,uBAA0B;AAC1B,kBAA4B;ACJ5B,IAAAA,iBAAsC;AACtC,IAAAC,iBAA6B;AAE7B,IAAAC,eAA2B;;ADepB,IAAMC,gBAAgB,CAACC,SAAAA;AAC5B,MAAIC,mBAAIC,gBAAgBF,IAAAA,KAASC,mBAAIE,cAAcH,IAAAA,KAASI,qBAAqBJ,IAAAA,GAAO;AACtF,WAAO;EACT;AAEA,MAAIC,mBAAII,gBAAgBL,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIC,mBAAIK,gBAAgBN,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIC,mBAAIM,iBAAiBP,IAAAA,GAAO;AAC9B,WAAO;EACT;AAEA,MAAIC,mBAAIO,QAAQR,IAAAA,GAAO;AACrB,WAAO;EACT;AAEA,MAAIC,mBAAIQ,UAAUT,IAAAA,GAAO;AACvB,WAAO;EACT;AACF;AAEO,IAAMU,eAAe,CAACV,SAA2B,CAAC,CAACD,cAAcC,IAAAA;AASxE,IAAMW,aAAa;AACnB,IAAMC,aAAa;A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B,WAAOjC,OAAOjD,QAAQ,KAAKgF,QAAQhC,MAAM,EAAEV,OAA4B,CAAC+C,QAAQ,CAACC,KAAK3F,IAAAA,MAAK;AACzF,UAAI+B,QAAQyD,IAAII,aAAaC,QAAIC,yBAAWH,GAAAA,CAAAA;AAC5C,UAAI5D,SAAS,MAAM;AACjBA,gBAAQyD,IAAII,aAAaC,IAAIF,GAAAA;MAC/B;AAEA,UAAI5D,SAAS,MAAM;AACjB,YAAIlE,eAAAA,IAAIK,gBAAgB8B,KAAKqB,GAAG,GAAG;AACjCqE,iBAAOC,GAAAA,IAAOI,SAAShE,KAAAA;QACzB,WAAWlE,eAAAA,IAAIM,iBAAiB6B,KAAKqB,GAAG,GAAG;AACzCqE,iBAAOC,GAAAA,IAAO5D,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACL2D,iBAAOC,GAAAA,IAAO5D;QAChB;MACF;AAEA,aAAO2D;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAM,OAAOT,MAAcG,QAAgB;AACnC,UAAMF,MAAM,IAAIC,IAAIF,IAAAA;AACpBjC,WAAOjD,QAAQqF,MAAAA,EAAQO,QAAQ,CAAC,CAACN,KAAK5D,KAAAA,MAAM;AAC1C,UAAIA,UAAUrC,QAAW;AACvB,cAAMwG,QAAQ,KAAKb,QAAQhC,OAAOsC,GAAAA;AAClC,YAAIO,OAAO;AACT,gBAAM,EAAEP,KAAKQ,cAAa,QAAKvH,eAAAA,MAC7BoG,sBAAsBkB,MAAM7E,GAAG,GAC/BW,eAAAA,OAAOoE,UAAU,OAAO;YACtBT,SAAKG,yBAAWH,GAAAA;UAClB,EAAA,CAAA;AAGFH,cAAII,aAAaS,IAAIF,eAAeG,OAAOvE,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOyD;EACT;AACF;",
|
|
6
|
+
"names": ["import_schema", "import_effect", "import_util", "getSimpleType", "node", "AST", "isObjectKeyword", "isTypeLiteral", "isDiscriminatedUnion", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "S", "NonEmptyString", "pipe", "pattern", "JsonProp", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "_result", "result", "undefined", "prop", "getPropertySignatures", "currentPath", "name", "toString", "type", "isTupleType", "i", "element", "elements", "entries", "isUnion", "types", "isRefinement", "from", "findNode", "child", "_", "isOption", "findProperty", "schema", "getProp", "rest", "typeNode", "invariant", "length", "ast", "split", "defaultAnnotations", "objectKeyword", "stringKeyword", "numberKeyword", "booleanKeyword", "getAnnotation", "annotationId", "noDefault", "value", "Option", "getOrUndefined", "_tag", "annotations", "findAnnotation", "getAnnotationById", "getBaseAnnotation", "isUndefinedKeyword", "isLiteralUnion", "every", "getDiscriminatingProps", "reduce", "shared", "props", "filter", "p", "map", "includes", "getDiscriminatedType", "match", "literal", "fields", "Object", "fromEntries", "literals", "find", "nonNullable", "Literal", "Struct", "mapAst", "f", "TypeLiteral", "propertySignatures", "PropertySignature", "isOptional", "isReadonly", "indexSignatures", "Union", "make", "TupleType", "t", "OptionalType", "Type", "newAst", "Suspend", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "ParamKeyAnnotation", "self", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "params", "key", "searchParams", "get", "decamelize", "parseInt", "create", "forEach", "field", "serializedKey", "getOrElse", "set", "String"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/effect/src/ast.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/common/effect/src/ast.ts":{"bytes":38855,"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":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7711,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":1150,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"packages/common/effect/src/ast.ts","kind":"import-statement","original":"./ast"},{"path":"packages/common/effect/src/url.ts","kind":"import-statement","original":"./url"}],"format":"esm"}},"outputs":{"packages/common/effect/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":24308},"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":"@dxos/util","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","getDiscriminatedType","getDiscriminatingProps","getParamKeyAnnotation","getSimpleType","isDiscriminatedUnion","isLiteralUnion","isOption","isSimpleType","mapAst","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":72},"packages/common/effect/src/ast.ts":{"bytesInOutput":8099},"packages/common/effect/src/url.ts":{"bytesInOutput":1624}},"bytes":10314}}}
|
|
@@ -34,14 +34,13 @@ var PATH_REGEX = /[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*/;
|
|
|
34
34
|
var PROP_REGEX = /\w+/;
|
|
35
35
|
var JsonPath = S.NonEmptyString.pipe(S.pattern(PATH_REGEX));
|
|
36
36
|
var JsonProp = S.NonEmptyString.pipe(S.pattern(PROP_REGEX));
|
|
37
|
-
var getAnnotation = (annotationId) => (node) => pipe(AST.getAnnotation(annotationId)(node), Option.getOrUndefined);
|
|
38
37
|
var VisitResult;
|
|
39
38
|
(function(VisitResult2) {
|
|
40
39
|
VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
|
|
41
40
|
VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
|
|
42
41
|
VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
|
|
43
42
|
})(VisitResult || (VisitResult = {}));
|
|
44
|
-
var defaultTest =
|
|
43
|
+
var defaultTest = isSimpleType;
|
|
45
44
|
var visit = (node, testOrVisitor, visitor) => {
|
|
46
45
|
if (!visitor) {
|
|
47
46
|
visitNode(node, defaultTest, testOrVisitor);
|
|
@@ -50,7 +49,8 @@ var visit = (node, testOrVisitor, visitor) => {
|
|
|
50
49
|
}
|
|
51
50
|
};
|
|
52
51
|
var visitNode = (node, test, visitor, path = [], depth = 0) => {
|
|
53
|
-
const
|
|
52
|
+
const _result = test?.(node, path, depth);
|
|
53
|
+
const result = _result === void 0 ? 0 : typeof _result === "boolean" ? _result ? 0 : 1 : _result;
|
|
54
54
|
if (result === 2) {
|
|
55
55
|
return result;
|
|
56
56
|
}
|
|
@@ -129,7 +129,7 @@ var findProperty = (schema, path) => {
|
|
|
129
129
|
const typeNode = findNode(node, AST.isTypeLiteral);
|
|
130
130
|
invariant(typeNode, void 0, {
|
|
131
131
|
F: __dxlog_file,
|
|
132
|
-
L:
|
|
132
|
+
L: 223,
|
|
133
133
|
S: void 0,
|
|
134
134
|
A: [
|
|
135
135
|
"typeNode",
|
|
@@ -154,14 +154,18 @@ var defaultAnnotations = {
|
|
|
154
154
|
["NumberKeyword"]: AST.numberKeyword,
|
|
155
155
|
["BooleanKeyword"]: AST.booleanKeyword
|
|
156
156
|
};
|
|
157
|
-
var
|
|
158
|
-
const
|
|
157
|
+
var getAnnotation = (annotationId, noDefault = true) => (node) => {
|
|
158
|
+
const value = pipe(AST.getAnnotation(annotationId)(node), Option.getOrUndefined);
|
|
159
|
+
if (noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {
|
|
160
|
+
return void 0;
|
|
161
|
+
}
|
|
162
|
+
return value;
|
|
163
|
+
};
|
|
164
|
+
var findAnnotation = (node, annotationId, noDefault = true) => {
|
|
165
|
+
const getAnnotationById = getAnnotation(annotationId, noDefault);
|
|
159
166
|
const getBaseAnnotation = (node2) => {
|
|
160
167
|
const value = getAnnotationById(node2);
|
|
161
168
|
if (value !== void 0) {
|
|
162
|
-
if (options?.noDefault && value === defaultAnnotations[node2._tag]?.annotations[annotationId]) {
|
|
163
|
-
return void 0;
|
|
164
|
-
}
|
|
165
169
|
return value;
|
|
166
170
|
}
|
|
167
171
|
if (AST.isUnion(node2)) {
|
|
@@ -184,7 +188,7 @@ var isDiscriminatedUnion = (node) => {
|
|
|
184
188
|
var getDiscriminatingProps = (node) => {
|
|
185
189
|
invariant(AST.isUnion(node), void 0, {
|
|
186
190
|
F: __dxlog_file,
|
|
187
|
-
L:
|
|
191
|
+
L: 318,
|
|
188
192
|
S: void 0,
|
|
189
193
|
A: [
|
|
190
194
|
"AST.isUnion(node)",
|
|
@@ -202,7 +206,7 @@ var getDiscriminatingProps = (node) => {
|
|
|
202
206
|
var getDiscriminatedType = (node, value = {}) => {
|
|
203
207
|
invariant(AST.isUnion(node), void 0, {
|
|
204
208
|
F: __dxlog_file,
|
|
205
|
-
L:
|
|
209
|
+
L: 339,
|
|
206
210
|
S: void 0,
|
|
207
211
|
A: [
|
|
208
212
|
"AST.isUnion(node)",
|
|
@@ -211,7 +215,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
211
215
|
});
|
|
212
216
|
invariant(value, void 0, {
|
|
213
217
|
F: __dxlog_file,
|
|
214
|
-
L:
|
|
218
|
+
L: 340,
|
|
215
219
|
S: void 0,
|
|
216
220
|
A: [
|
|
217
221
|
"value",
|
|
@@ -226,7 +230,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
226
230
|
const match = AST.getPropertySignatures(type).filter((prop) => props?.includes(prop.name.toString())).every((prop) => {
|
|
227
231
|
invariant(AST.isLiteral(prop.type), void 0, {
|
|
228
232
|
F: __dxlog_file,
|
|
229
|
-
L:
|
|
233
|
+
L: 351,
|
|
230
234
|
S: void 0,
|
|
231
235
|
A: [
|
|
232
236
|
"AST.isLiteral(prop.type)",
|
|
@@ -244,7 +248,7 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
244
248
|
const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop);
|
|
245
249
|
invariant(AST.isLiteral(literal.type), void 0, {
|
|
246
250
|
F: __dxlog_file,
|
|
247
|
-
L:
|
|
251
|
+
L: 369,
|
|
248
252
|
S: void 0,
|
|
249
253
|
A: [
|
|
250
254
|
"AST.isLiteral(literal.type)",
|
|
@@ -261,6 +265,22 @@ var getDiscriminatedType = (node, value = {}) => {
|
|
|
261
265
|
const schema = S.Struct(fields);
|
|
262
266
|
return schema.ast;
|
|
263
267
|
};
|
|
268
|
+
var mapAst = (ast, f) => {
|
|
269
|
+
switch (ast._tag) {
|
|
270
|
+
case "TypeLiteral":
|
|
271
|
+
return new AST.TypeLiteral(ast.propertySignatures.map((prop) => new AST.PropertySignature(prop.name, f(prop.type), prop.isOptional, prop.isReadonly, prop.annotations)), ast.indexSignatures);
|
|
272
|
+
case "Union":
|
|
273
|
+
return AST.Union.make(ast.types.map(f), ast.annotations);
|
|
274
|
+
case "TupleType":
|
|
275
|
+
return new AST.TupleType(ast.elements.map((t) => new AST.OptionalType(f(t.type), t.isOptional, t.annotations)), ast.rest.map((t) => new AST.Type(f(t.type), t.annotations)), ast.isReadonly, ast.annotations);
|
|
276
|
+
case "Suspend": {
|
|
277
|
+
const newAst = f(ast.f());
|
|
278
|
+
return new AST.Suspend(() => newAst, ast.annotations);
|
|
279
|
+
}
|
|
280
|
+
default:
|
|
281
|
+
return ast;
|
|
282
|
+
}
|
|
283
|
+
};
|
|
264
284
|
|
|
265
285
|
// packages/common/effect/src/url.ts
|
|
266
286
|
import { AST as AST2 } from "@effect/schema";
|
|
@@ -337,6 +357,7 @@ export {
|
|
|
337
357
|
isLiteralUnion,
|
|
338
358
|
isOption,
|
|
339
359
|
isSimpleType,
|
|
360
|
+
mapAst,
|
|
340
361
|
visit
|
|
341
362
|
};
|
|
342
363
|
//# 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\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';\nimport { nonNullable } from '@dxos/util';\n\n//\n// Refs\n// https://effect.website/docs/schema/introduction\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\n/**\n * Get the base type; e.g., traverse through refinements.\n */\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node) || AST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {\n return 'object';\n }\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\n if (AST.isEnums(node)) {\n return 'enum';\n }\n\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST): boolean => !!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 (e.g., optional, discriminated unions).\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): Transforms?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Rewrite using visitNode?\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 // Tuple.\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 (e.g., optional, discriminated unions).\n else if (AST.isUnion(node)) {\n if (isOption(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\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\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\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n//\n// Annotations\n//\n\nconst defaultAnnotations: Record<string, AST.Annotated> = {\n ['ObjectKeyword' as const]: AST.objectKeyword,\n ['StringKeyword' as const]: AST.stringKeyword,\n ['NumberKeyword' as const]: AST.numberKeyword,\n ['BooleanKeyword' as const]: AST.booleanKeyword,\n};\n\n/**\n * Recursively descend into AST to find first matching annotations.\n * Optionally skips default annotations for basic types (e.g., 'a string').\n */\nexport const findAnnotation = <T>(\n node: AST.AST,\n annotationId: symbol,\n options?: { noDefault: boolean },\n): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId);\n\n const getBaseAnnotation = (node: AST.AST): T | undefined => {\n const value = getAnnotationById(node);\n if (value !== undefined) {\n if (options?.noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {\n return undefined;\n }\n\n return value as T;\n }\n\n if (AST.isUnion(node)) {\n if (isOption(node)) {\n return getAnnotationById(node.types[0]) as T;\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n\n//\n// Unions\n//\n\n/**\n * Effect S.optional creates a union type with undefined as the second type.\n */\nexport const isOption = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.length === 2 && AST.isUndefinedKeyword(node.types[1]);\n};\n\n/**\n * Determines if the node is a union of literal types.\n */\nexport const isLiteralUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.every(AST.isLiteral);\n};\n\n/**\n * Determines if the node is a discriminated union.\n */\nexport const isDiscriminatedUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && !!getDiscriminatingProps(node)?.length;\n};\n\n/**\n * Get the discriminating properties for the given union type.\n */\nexport const getDiscriminatingProps = (node: AST.AST): string[] | undefined => {\n invariant(AST.isUnion(node));\n if (isOption(node)) {\n return;\n }\n\n // Get common literals across all types.\n return node.types.reduce<string[]>((shared, type) => {\n const props = AST.getPropertySignatures(type)\n // TODO(burdon): Should check each literal is unique.\n .filter((p) => AST.isLiteral(p.type))\n .map((p) => p.name.toString());\n\n // Return common literals.\n return shared.length === 0 ? props : shared.filter((prop) => props.includes(prop));\n }, []);\n};\n\n/**\n * Get the discriminated type for the given value.\n */\nexport const getDiscriminatedType = (node: AST.AST, value: Record<string, any> = {}): AST.AST | undefined => {\n invariant(AST.isUnion(node));\n invariant(value);\n const props = getDiscriminatingProps(node);\n if (!props?.length) {\n return;\n }\n\n // Match provided values.\n for (const type of node.types) {\n const match = AST.getPropertySignatures(type)\n .filter((prop) => props?.includes(prop.name.toString()))\n .every((prop) => {\n invariant(AST.isLiteral(prop.type));\n return prop.type.literal === value[prop.name.toString()];\n });\n\n if (match) {\n return type;\n }\n }\n\n // Create union of discriminating properties.\n // NOTE: This may not work with non-overlapping variants.\n // TODO(burdon): Iterate through props and knock-out variants that don't match.\n const fields = Object.fromEntries(\n props\n .map((prop) => {\n const literals = node.types\n .map((type) => {\n const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop)!;\n invariant(AST.isLiteral(literal.type));\n return literal.type.literal;\n })\n .filter(nonNullable);\n\n return literals.length ? [prop, S.Literal(...literals)] : undefined;\n })\n .filter(nonNullable),\n );\n\n const schema = S.Struct(fields);\n return schema.ast;\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;AAC1B,SAASC,mBAAmB;;AAcrB,IAAMC,gBAAgB,CAACC,SAAAA;AAC5B,MAAIR,IAAIS,gBAAgBD,IAAAA,KAASR,IAAIU,cAAcF,IAAAA,KAASG,qBAAqBH,IAAAA,GAAO;AACtF,WAAO;EACT;AAEA,MAAIR,IAAIY,gBAAgBJ,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIR,IAAIa,gBAAgBL,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIR,IAAIc,iBAAiBN,IAAAA,GAAO;AAC9B,WAAO;EACT;AAEA,MAAIR,IAAIe,QAAQP,IAAAA,GAAO;AACrB,WAAO;EACT;AAEA,MAAIR,IAAIgB,UAAUR,IAAAA,GAAO;AACvB,WAAO;EACT;AACF;AAEO,IAAMS,eAAe,CAACT,SAA2B,CAAC,CAACD,cAAcC,IAAAA;AASxE,IAAMU,aAAa;AACnB,IAAMC,aAAa;AAKZ,IAAMC,WAAWlB,EAAEmB,eAAejB,KAAKF,EAAEoB,QAAQJ,UAAAA,CAAAA;AACjD,IAAMK,WAAWrB,EAAEmB,eAAejB,KAAKF,EAAEoB,QAAQH,UAAAA,CAAAA
|
|
6
|
-
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Schema", "S", "Option", "pipe", "invariant", "nonNullable", "getSimpleType", "node", "isObjectKeyword", "isTypeLiteral", "isDiscriminatedUnion", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "NonEmptyString", "pattern", "JsonProp", "
|
|
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';\nimport { nonNullable } from '@dxos/util';\n\n//\n// Refs\n// https://effect.website/docs/schema/introduction\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\n/**\n * Get the base type; e.g., traverse through refinements.\n */\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node) || AST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {\n return 'object';\n }\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\n if (AST.isEnums(node)) {\n return 'enum';\n }\n\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST): boolean => !!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\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 TestFn = (node: AST.AST, path: Path, depth: number) => VisitResult | boolean | undefined;\n\nexport type VisitorFn = (node: AST.AST, path: Path, depth: number) => void;\n\nconst defaultTest: TestFn = isSimpleType;\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: VisitorFn): void;\n (node: AST.AST, test: TestFn, visitor: VisitorFn): void;\n} = (node: AST.AST, testOrVisitor: TestFn | VisitorFn, visitor?: VisitorFn): void => {\n if (!visitor) {\n visitNode(node, defaultTest, testOrVisitor);\n } else {\n visitNode(node, testOrVisitor as TestFn, visitor);\n }\n};\n\nconst visitNode = (\n node: AST.AST,\n test: TestFn | undefined,\n visitor: VisitorFn,\n path: Path = [],\n depth = 0,\n): VisitResult | undefined => {\n const _result = test?.(node, path, depth);\n const result: VisitResult =\n _result === undefined\n ? VisitResult.CONTINUE\n : typeof _result === 'boolean'\n ? _result\n ? VisitResult.CONTINUE\n : VisitResult.SKIP\n : _result;\n\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 (e.g., optional, discriminated unions).\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): Transforms?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Rewrite using visitNode?\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 // Tuple.\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 (e.g., optional, discriminated unions).\n else if (AST.isUnion(node)) {\n if (isOption(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\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\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\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n//\n// Annotations\n//\n\nconst defaultAnnotations: Record<string, AST.Annotated> = {\n ['ObjectKeyword' as const]: AST.objectKeyword,\n ['StringKeyword' as const]: AST.stringKeyword,\n ['NumberKeyword' as const]: AST.numberKeyword,\n ['BooleanKeyword' as const]: AST.booleanKeyword,\n};\n\n/**\n * Get annotation or return undefined.\n * @param annotationId\n * @param noDefault If true, then return undefined for effect library defined values.\n */\nexport const getAnnotation =\n <T>(annotationId: symbol, noDefault = true) =>\n (node: AST.AST): T | undefined => {\n const value = pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n if (noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {\n return undefined;\n }\n\n return value;\n };\n\n/**\n * Recursively descend into AST to find first matching annotations.\n * Optionally skips default annotations for basic types (e.g., 'a string').\n */\n// TODO(burdon): Convert to effect pattern (i.e., return operator like getAnnotation).\nexport const findAnnotation = <T>(node: AST.AST, annotationId: symbol, noDefault = true): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId, noDefault);\n\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 if (isOption(node)) {\n return getAnnotationById(node.types[0]) as T;\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n\n//\n// Unions\n//\n\n/**\n * Effect S.optional creates a union type with undefined as the second type.\n */\nexport const isOption = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.length === 2 && AST.isUndefinedKeyword(node.types[1]);\n};\n\n/**\n * Determines if the node is a union of literal types.\n */\nexport const isLiteralUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && node.types.every(AST.isLiteral);\n};\n\n/**\n * Determines if the node is a discriminated union.\n */\nexport const isDiscriminatedUnion = (node: AST.AST): boolean => {\n return AST.isUnion(node) && !!getDiscriminatingProps(node)?.length;\n};\n\n/**\n * Get the discriminating properties for the given union type.\n */\nexport const getDiscriminatingProps = (node: AST.AST): string[] | undefined => {\n invariant(AST.isUnion(node));\n if (isOption(node)) {\n return;\n }\n\n // Get common literals across all types.\n return node.types.reduce<string[]>((shared, type) => {\n const props = AST.getPropertySignatures(type)\n // TODO(burdon): Should check each literal is unique.\n .filter((p) => AST.isLiteral(p.type))\n .map((p) => p.name.toString());\n\n // Return common literals.\n return shared.length === 0 ? props : shared.filter((prop) => props.includes(prop));\n }, []);\n};\n\n/**\n * Get the discriminated type for the given value.\n */\nexport const getDiscriminatedType = (node: AST.AST, value: Record<string, any> = {}): AST.AST | undefined => {\n invariant(AST.isUnion(node));\n invariant(value);\n const props = getDiscriminatingProps(node);\n if (!props?.length) {\n return;\n }\n\n // Match provided values.\n for (const type of node.types) {\n const match = AST.getPropertySignatures(type)\n .filter((prop) => props?.includes(prop.name.toString()))\n .every((prop) => {\n invariant(AST.isLiteral(prop.type));\n return prop.type.literal === value[prop.name.toString()];\n });\n\n if (match) {\n return type;\n }\n }\n\n // Create union of discriminating properties.\n // NOTE: This may not work with non-overlapping variants.\n // TODO(burdon): Iterate through props and knock-out variants that don't match.\n const fields = Object.fromEntries(\n props\n .map((prop) => {\n const literals = node.types\n .map((type) => {\n const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop)!;\n invariant(AST.isLiteral(literal.type));\n return literal.type.literal;\n })\n .filter(nonNullable);\n\n return literals.length ? [prop, S.Literal(...literals)] : undefined;\n })\n .filter(nonNullable),\n );\n\n const schema = S.Struct(fields);\n return schema.ast;\n};\n\n/**\n * Maps AST nodes.\n * The user is responsible for recursively calling {@link mapAst} on the AST.\n * NOTE: Will evaluate suspended ASTs.\n */\nexport const mapAst = (ast: AST.AST, f: (ast: AST.AST) => AST.AST): AST.AST => {\n switch (ast._tag) {\n case 'TypeLiteral':\n return new AST.TypeLiteral(\n ast.propertySignatures.map(\n (prop) =>\n new AST.PropertySignature(prop.name, f(prop.type), prop.isOptional, prop.isReadonly, prop.annotations),\n ),\n ast.indexSignatures,\n );\n case 'Union':\n return AST.Union.make(ast.types.map(f), ast.annotations);\n case 'TupleType':\n return new AST.TupleType(\n ast.elements.map((t) => new AST.OptionalType(f(t.type), t.isOptional, t.annotations)),\n ast.rest.map((t) => new AST.Type(f(t.type), t.annotations)),\n ast.isReadonly,\n ast.annotations,\n );\n case 'Suspend': {\n const newAst = f(ast.f());\n return new AST.Suspend(() => newAst, ast.annotations);\n }\n default:\n // TODO(dmaretskyi): Support more nodes.\n return ast;\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,UAAS;;;ACA7C,SAASC,KAAKC,UAAUC,SAAS;AACjC,SAASC,QAAQC,YAAY;AAE7B,SAASC,iBAAiB;AAC1B,SAASC,mBAAmB;;AAcrB,IAAMC,gBAAgB,CAACC,SAAAA;AAC5B,MAAIR,IAAIS,gBAAgBD,IAAAA,KAASR,IAAIU,cAAcF,IAAAA,KAASG,qBAAqBH,IAAAA,GAAO;AACtF,WAAO;EACT;AAEA,MAAIR,IAAIY,gBAAgBJ,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIR,IAAIa,gBAAgBL,IAAAA,GAAO;AAC7B,WAAO;EACT;AACA,MAAIR,IA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AAIJ,eAAeK,OAAOjC,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Schema", "S", "Option", "pipe", "invariant", "nonNullable", "getSimpleType", "node", "isObjectKeyword", "isTypeLiteral", "isDiscriminatedUnion", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "NonEmptyString", "pattern", "JsonProp", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "_result", "result", "undefined", "prop", "getPropertySignatures", "currentPath", "name", "toString", "type", "isTupleType", "i", "element", "elements", "entries", "isUnion", "types", "isRefinement", "from", "findNode", "child", "_", "isOption", "findProperty", "schema", "getProp", "rest", "typeNode", "length", "ast", "split", "defaultAnnotations", "objectKeyword", "stringKeyword", "numberKeyword", "booleanKeyword", "getAnnotation", "annotationId", "noDefault", "value", "getOrUndefined", "_tag", "annotations", "findAnnotation", "getAnnotationById", "getBaseAnnotation", "isUndefinedKeyword", "isLiteralUnion", "every", "getDiscriminatingProps", "reduce", "shared", "props", "filter", "p", "map", "includes", "getDiscriminatedType", "match", "literal", "fields", "Object", "fromEntries", "literals", "find", "Literal", "Struct", "mapAst", "f", "TypeLiteral", "propertySignatures", "PropertySignature", "isOptional", "isReadonly", "indexSignatures", "Union", "make", "TupleType", "t", "OptionalType", "Type", "newAst", "Suspend", "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":
|
|
1
|
+
{"inputs":{"packages/common/effect/src/ast.ts":{"bytes":38855,"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":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7711,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":1150,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"packages/common/effect/src/ast.ts","kind":"import-statement","original":"./ast"},{"path":"packages/common/effect/src/url.ts","kind":"import-statement","original":"./url"}],"format":"esm"}},"outputs":{"packages/common/effect/dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":24310},"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":"@dxos/util","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","getDiscriminatedType","getDiscriminatingProps","getParamKeyAnnotation","getSimpleType","isDiscriminatedUnion","isLiteralUnion","isOption","isSimpleType","mapAst","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":72},"packages/common/effect/src/ast.ts":{"bytesInOutput":8099},"packages/common/effect/src/url.ts":{"bytesInOutput":1624}},"bytes":10407}}}
|
package/dist/types/src/ast.d.ts
CHANGED
|
@@ -17,10 +17,6 @@ export type JsonPath = string & {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare const JsonPath: S.Schema<JsonPath>;
|
|
19
19
|
export declare const JsonProp: S.Schema<JsonProp>;
|
|
20
|
-
/**
|
|
21
|
-
* Get annotation or return undefined.
|
|
22
|
-
*/
|
|
23
|
-
export declare const getAnnotation: <T>(annotationId: symbol) => (node: AST.Annotated) => T | undefined;
|
|
24
20
|
export declare enum VisitResult {
|
|
25
21
|
CONTINUE = 0,
|
|
26
22
|
/**
|
|
@@ -33,8 +29,8 @@ export declare enum VisitResult {
|
|
|
33
29
|
EXIT = 2
|
|
34
30
|
}
|
|
35
31
|
export type Path = (string | number)[];
|
|
36
|
-
export type
|
|
37
|
-
export type
|
|
32
|
+
export type TestFn = (node: AST.AST, path: Path, depth: number) => VisitResult | boolean | undefined;
|
|
33
|
+
export type VisitorFn = (node: AST.AST, path: Path, depth: number) => void;
|
|
38
34
|
/**
|
|
39
35
|
* Visit leaf nodes.
|
|
40
36
|
* Refs:
|
|
@@ -42,8 +38,8 @@ export type Visitor = (node: AST.AST, path: Path, depth: number) => void;
|
|
|
42
38
|
* - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test
|
|
43
39
|
*/
|
|
44
40
|
export declare const visit: {
|
|
45
|
-
(node: AST.AST, visitor:
|
|
46
|
-
(node: AST.AST, test:
|
|
41
|
+
(node: AST.AST, visitor: VisitorFn): void;
|
|
42
|
+
(node: AST.AST, test: TestFn, visitor: VisitorFn): void;
|
|
47
43
|
};
|
|
48
44
|
/**
|
|
49
45
|
* Recursively descend into AST to find first node that passes the test.
|
|
@@ -53,13 +49,17 @@ export declare const findNode: (node: AST.AST, test: (node: AST.AST) => boolean)
|
|
|
53
49
|
* Get the AST node for the given property (dot-path).
|
|
54
50
|
*/
|
|
55
51
|
export declare const findProperty: (schema: S.Schema<any>, path: JsonPath | JsonProp) => AST.AST | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Get annotation or return undefined.
|
|
54
|
+
* @param annotationId
|
|
55
|
+
* @param noDefault If true, then return undefined for effect library defined values.
|
|
56
|
+
*/
|
|
57
|
+
export declare const getAnnotation: <T>(annotationId: symbol, noDefault?: boolean) => (node: AST.AST) => T | undefined;
|
|
56
58
|
/**
|
|
57
59
|
* Recursively descend into AST to find first matching annotations.
|
|
58
60
|
* Optionally skips default annotations for basic types (e.g., 'a string').
|
|
59
61
|
*/
|
|
60
|
-
export declare const findAnnotation: <T>(node: AST.AST, annotationId: symbol,
|
|
61
|
-
noDefault: boolean;
|
|
62
|
-
}) => T | undefined;
|
|
62
|
+
export declare const findAnnotation: <T>(node: AST.AST, annotationId: symbol, noDefault?: boolean) => T | undefined;
|
|
63
63
|
/**
|
|
64
64
|
* Effect S.optional creates a union type with undefined as the second type.
|
|
65
65
|
*/
|
|
@@ -80,4 +80,10 @@ export declare const getDiscriminatingProps: (node: AST.AST) => string[] | undef
|
|
|
80
80
|
* Get the discriminated type for the given value.
|
|
81
81
|
*/
|
|
82
82
|
export declare const getDiscriminatedType: (node: AST.AST, value?: Record<string, any>) => AST.AST | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Maps AST nodes.
|
|
85
|
+
* The user is responsible for recursively calling {@link mapAst} on the AST.
|
|
86
|
+
* NOTE: Will evaluate suspended ASTs.
|
|
87
|
+
*/
|
|
88
|
+
export declare const mapAst: (ast: AST.AST, f: (ast: AST.AST) => AST.AST) => AST.AST;
|
|
83
89
|
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../../src/ast.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,MAAM,IAAI,CAAC,EAAE,MAAM,gBAAgB,CAAC;AAalD,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,S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CAAC,GAAG,KAAG,CAAC,GAAG,SAOpB,CAAC;AAEJ;;;GAGG;AAEH,eAAO,MAAM,cAAc,GAAI,CAAC,QAAQ,GAAG,CAAC,GAAG,gBAAgB,MAAM,0BAAqB,CAAC,GAAG,SAiB7F,CAAC;AAMF;;GAEG;AACH,eAAO,MAAM,QAAQ,SAAU,GAAG,CAAC,GAAG,KAAG,OAExC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,SAAU,GAAG,CAAC,GAAG,KAAG,OAE9C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,SAAU,GAAG,CAAC,GAAG,KAAG,OAEpD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,sBAAsB,SAAU,GAAG,CAAC,GAAG,KAAG,MAAM,EAAE,GAAG,SAgBjE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,SAAU,GAAG,CAAC,GAAG,UAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAQ,GAAG,CAAC,GAAG,GAAG,SA2C/F,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,MAAM,QAAS,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,KAAG,GAAG,CAAC,GA2BvE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanity.test.d.ts","sourceRoot":"","sources":["../../../src/sanity.test.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2-staging.6d26b2a",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -22,13 +22,14 @@
|
|
|
22
22
|
"src"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@dxos/
|
|
26
|
-
"@dxos/
|
|
27
|
-
"@dxos/node-std": "0.7.
|
|
25
|
+
"@dxos/invariant": "0.7.2-staging.6d26b2a",
|
|
26
|
+
"@dxos/util": "0.7.2-staging.6d26b2a",
|
|
27
|
+
"@dxos/node-std": "0.7.2-staging.6d26b2a"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@effect/schema": "^0.75.5",
|
|
31
|
-
"effect": "^3.9.2"
|
|
31
|
+
"effect": "^3.9.2",
|
|
32
|
+
"@dxos/log": "0.7.2-staging.6d26b2a"
|
|
32
33
|
},
|
|
33
34
|
"peerDependencies": {
|
|
34
35
|
"@effect/schema": "^0.75.5",
|
package/src/ast.test.ts
CHANGED
|
@@ -114,7 +114,6 @@ describe('AST', () => {
|
|
|
114
114
|
const annotation = findAnnotation(
|
|
115
115
|
S.String.annotations({ [AST.TitleAnnotationId]: 'test' }).ast,
|
|
116
116
|
AST.TitleAnnotationId,
|
|
117
|
-
{ noDefault: true },
|
|
118
117
|
);
|
|
119
118
|
expect(annotation).to.eq('test');
|
|
120
119
|
|
|
@@ -122,7 +121,7 @@ describe('AST', () => {
|
|
|
122
121
|
const schemas = [S.Object, S.String, S.Number, S.Boolean];
|
|
123
122
|
for (const schema of schemas) {
|
|
124
123
|
for (const annotationId of annotationIds) {
|
|
125
|
-
const annotation = findAnnotation(schema.ast, annotationId
|
|
124
|
+
const annotation = findAnnotation(schema.ast, annotationId);
|
|
126
125
|
expect(annotation, schema.ast._tag).to.eq(undefined);
|
|
127
126
|
}
|
|
128
127
|
}
|
package/src/ast.ts
CHANGED
|
@@ -62,14 +62,6 @@ const PROP_REGEX = /\w+/;
|
|
|
62
62
|
export const JsonPath = S.NonEmptyString.pipe(S.pattern(PATH_REGEX)) as any as S.Schema<JsonPath>;
|
|
63
63
|
export const JsonProp = S.NonEmptyString.pipe(S.pattern(PROP_REGEX)) as any as S.Schema<JsonProp>;
|
|
64
64
|
|
|
65
|
-
/**
|
|
66
|
-
* Get annotation or return undefined.
|
|
67
|
-
*/
|
|
68
|
-
export const getAnnotation =
|
|
69
|
-
<T>(annotationId: symbol) =>
|
|
70
|
-
(node: AST.Annotated): T | undefined =>
|
|
71
|
-
pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
72
|
-
|
|
73
65
|
export enum VisitResult {
|
|
74
66
|
CONTINUE = 0,
|
|
75
67
|
/**
|
|
@@ -84,10 +76,11 @@ export enum VisitResult {
|
|
|
84
76
|
|
|
85
77
|
export type Path = (string | number)[];
|
|
86
78
|
|
|
87
|
-
export type
|
|
88
|
-
|
|
79
|
+
export type TestFn = (node: AST.AST, path: Path, depth: number) => VisitResult | boolean | undefined;
|
|
80
|
+
|
|
81
|
+
export type VisitorFn = (node: AST.AST, path: Path, depth: number) => void;
|
|
89
82
|
|
|
90
|
-
const defaultTest:
|
|
83
|
+
const defaultTest: TestFn = isSimpleType;
|
|
91
84
|
|
|
92
85
|
/**
|
|
93
86
|
* Visit leaf nodes.
|
|
@@ -96,24 +89,33 @@ const defaultTest: Tester = (node) => (isSimpleType(node) ? VisitResult.CONTINUE
|
|
|
96
89
|
* - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test
|
|
97
90
|
*/
|
|
98
91
|
export const visit: {
|
|
99
|
-
(node: AST.AST, visitor:
|
|
100
|
-
(node: AST.AST, test:
|
|
101
|
-
} = (node: AST.AST, testOrVisitor:
|
|
92
|
+
(node: AST.AST, visitor: VisitorFn): void;
|
|
93
|
+
(node: AST.AST, test: TestFn, visitor: VisitorFn): void;
|
|
94
|
+
} = (node: AST.AST, testOrVisitor: TestFn | VisitorFn, visitor?: VisitorFn): void => {
|
|
102
95
|
if (!visitor) {
|
|
103
96
|
visitNode(node, defaultTest, testOrVisitor);
|
|
104
97
|
} else {
|
|
105
|
-
visitNode(node, testOrVisitor as
|
|
98
|
+
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
106
99
|
}
|
|
107
100
|
};
|
|
108
101
|
|
|
109
102
|
const visitNode = (
|
|
110
103
|
node: AST.AST,
|
|
111
|
-
test:
|
|
112
|
-
visitor:
|
|
104
|
+
test: TestFn | undefined,
|
|
105
|
+
visitor: VisitorFn,
|
|
113
106
|
path: Path = [],
|
|
114
107
|
depth = 0,
|
|
115
108
|
): VisitResult | undefined => {
|
|
116
|
-
const
|
|
109
|
+
const _result = test?.(node, path, depth);
|
|
110
|
+
const result: VisitResult =
|
|
111
|
+
_result === undefined
|
|
112
|
+
? VisitResult.CONTINUE
|
|
113
|
+
: typeof _result === 'boolean'
|
|
114
|
+
? _result
|
|
115
|
+
? VisitResult.CONTINUE
|
|
116
|
+
: VisitResult.SKIP
|
|
117
|
+
: _result;
|
|
118
|
+
|
|
117
119
|
if (result === VisitResult.EXIT) {
|
|
118
120
|
return result;
|
|
119
121
|
}
|
|
@@ -244,24 +246,33 @@ const defaultAnnotations: Record<string, AST.Annotated> = {
|
|
|
244
246
|
['BooleanKeyword' as const]: AST.booleanKeyword,
|
|
245
247
|
};
|
|
246
248
|
|
|
249
|
+
/**
|
|
250
|
+
* Get annotation or return undefined.
|
|
251
|
+
* @param annotationId
|
|
252
|
+
* @param noDefault If true, then return undefined for effect library defined values.
|
|
253
|
+
*/
|
|
254
|
+
export const getAnnotation =
|
|
255
|
+
<T>(annotationId: symbol, noDefault = true) =>
|
|
256
|
+
(node: AST.AST): T | undefined => {
|
|
257
|
+
const value = pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
258
|
+
if (noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return value;
|
|
263
|
+
};
|
|
264
|
+
|
|
247
265
|
/**
|
|
248
266
|
* Recursively descend into AST to find first matching annotations.
|
|
249
267
|
* Optionally skips default annotations for basic types (e.g., 'a string').
|
|
250
268
|
*/
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
annotationId
|
|
254
|
-
options?: { noDefault: boolean },
|
|
255
|
-
): T | undefined => {
|
|
256
|
-
const getAnnotationById = getAnnotation(annotationId);
|
|
269
|
+
// TODO(burdon): Convert to effect pattern (i.e., return operator like getAnnotation).
|
|
270
|
+
export const findAnnotation = <T>(node: AST.AST, annotationId: symbol, noDefault = true): T | undefined => {
|
|
271
|
+
const getAnnotationById = getAnnotation(annotationId, noDefault);
|
|
257
272
|
|
|
258
273
|
const getBaseAnnotation = (node: AST.AST): T | undefined => {
|
|
259
274
|
const value = getAnnotationById(node);
|
|
260
275
|
if (value !== undefined) {
|
|
261
|
-
if (options?.noDefault && value === defaultAnnotations[node._tag]?.annotations[annotationId]) {
|
|
262
|
-
return undefined;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
276
|
return value as T;
|
|
266
277
|
}
|
|
267
278
|
|
|
@@ -368,3 +379,37 @@ export const getDiscriminatedType = (node: AST.AST, value: Record<string, any> =
|
|
|
368
379
|
const schema = S.Struct(fields);
|
|
369
380
|
return schema.ast;
|
|
370
381
|
};
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Maps AST nodes.
|
|
385
|
+
* The user is responsible for recursively calling {@link mapAst} on the AST.
|
|
386
|
+
* NOTE: Will evaluate suspended ASTs.
|
|
387
|
+
*/
|
|
388
|
+
export const mapAst = (ast: AST.AST, f: (ast: AST.AST) => AST.AST): AST.AST => {
|
|
389
|
+
switch (ast._tag) {
|
|
390
|
+
case 'TypeLiteral':
|
|
391
|
+
return new AST.TypeLiteral(
|
|
392
|
+
ast.propertySignatures.map(
|
|
393
|
+
(prop) =>
|
|
394
|
+
new AST.PropertySignature(prop.name, f(prop.type), prop.isOptional, prop.isReadonly, prop.annotations),
|
|
395
|
+
),
|
|
396
|
+
ast.indexSignatures,
|
|
397
|
+
);
|
|
398
|
+
case 'Union':
|
|
399
|
+
return AST.Union.make(ast.types.map(f), ast.annotations);
|
|
400
|
+
case 'TupleType':
|
|
401
|
+
return new AST.TupleType(
|
|
402
|
+
ast.elements.map((t) => new AST.OptionalType(f(t.type), t.isOptional, t.annotations)),
|
|
403
|
+
ast.rest.map((t) => new AST.Type(f(t.type), t.annotations)),
|
|
404
|
+
ast.isReadonly,
|
|
405
|
+
ast.annotations,
|
|
406
|
+
);
|
|
407
|
+
case 'Suspend': {
|
|
408
|
+
const newAst = f(ast.f());
|
|
409
|
+
return new AST.Suspend(() => newAst, ast.annotations);
|
|
410
|
+
}
|
|
411
|
+
default:
|
|
412
|
+
// TODO(dmaretskyi): Support more nodes.
|
|
413
|
+
return ast;
|
|
414
|
+
}
|
|
415
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect, pipe } from 'effect';
|
|
6
|
+
import { describe, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
|
|
10
|
+
describe('sanity tests', () => {
|
|
11
|
+
test('function pipeline', async ({ expect }) => {
|
|
12
|
+
const result = pipe(
|
|
13
|
+
10,
|
|
14
|
+
(value) => value + 3,
|
|
15
|
+
(value) => value * 2,
|
|
16
|
+
);
|
|
17
|
+
expect(result).to.eq(26);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('effect pipeline (mixing types)', async ({ expect }) => {
|
|
21
|
+
const result = await Effect.runPromise(
|
|
22
|
+
pipe(
|
|
23
|
+
Effect.promise(() => Promise.resolve(100)),
|
|
24
|
+
Effect.tap((value) => log('tap', { value })),
|
|
25
|
+
Effect.map((value: number) => String(value)),
|
|
26
|
+
Effect.tap((value) => log('tap', { value })),
|
|
27
|
+
Effect.map((value: string) => value.length),
|
|
28
|
+
Effect.tap((value) => log('tap', { value })),
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
expect(result).to.eq(3);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('effect pipeline (mixing sync/async)', async ({ expect }) => {
|
|
35
|
+
const result = await Effect.runPromise(
|
|
36
|
+
pipe(
|
|
37
|
+
Effect.succeed(100),
|
|
38
|
+
Effect.tap((value) => log('tap', { value })),
|
|
39
|
+
Effect.flatMap((value) => Effect.promise(() => Promise.resolve(String(value)))),
|
|
40
|
+
Effect.tap((value) => log('tap', { value })),
|
|
41
|
+
Effect.map((value) => value.length),
|
|
42
|
+
Effect.tap((value) => log('tap', { value })),
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
expect(result).to.eq(3);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('error handling', async ({ expect }) => {
|
|
49
|
+
Effect.runPromise(
|
|
50
|
+
pipe(
|
|
51
|
+
Effect.succeed(10),
|
|
52
|
+
Effect.map((value) => value * 2),
|
|
53
|
+
Effect.flatMap((value) =>
|
|
54
|
+
Effect.promise(() => {
|
|
55
|
+
if (value > 10) {
|
|
56
|
+
return Promise.reject(new Error('error message'));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return Promise.resolve(value);
|
|
60
|
+
}),
|
|
61
|
+
),
|
|
62
|
+
),
|
|
63
|
+
)
|
|
64
|
+
.then(() => expect.fail())
|
|
65
|
+
.catch((error) => {
|
|
66
|
+
expect(error).to.be.instanceOf(Error);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|