@dxos/effect 0.7.0 → 0.7.1-staging.7f6f91c
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 +115 -14
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +123 -17
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +115 -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 +28 -2
- package/dist/types/src/ast.d.ts.map +1 -1
- package/package.json +8 -8
- package/src/ast.test.ts +71 -9
- package/src/ast.ts +140 -23
|
@@ -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';\n\n//\n// Refs\n// https://effect.website/docs/guides/schema\n// https://www.npmjs.com/package/@effect/schema\n// https://effect-ts.github.io/effect/schema/AST.ts.html\n//\n\nexport type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';\n\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node)) {\n return 'object';\n }\n if (AST.isStringKeyword(node)) {\n return 'string';\n }\n if (AST.isNumberKeyword(node)) {\n return 'number';\n }\n if (AST.isBooleanKeyword(node)) {\n return 'boolean';\n }\n if (AST.isEnums(node)) {\n return 'enum';\n }\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST) => !!getSimpleType(node);\n\n//\n// Branded types\n//\n\nexport type JsonProp = string & { __JsonPath: true; __JsonProp: true };\nexport type JsonPath = string & { __JsonPath: true };\n\nconst PATH_REGEX = /[a-zA-Z_$][\\w$]*(?:\\.[a-zA-Z_$][\\w$]*)*/;\nconst PROP_REGEX = /\\w+/;\n\n/**\n * https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html\n */\nexport const JsonPath = S.NonEmptyString.pipe(S.pattern(PATH_REGEX)) as any as S.Schema<JsonPath>;\nexport const JsonProp = S.NonEmptyString.pipe(S.pattern(PROP_REGEX)) as any as S.Schema<JsonProp>;\n\n/**\n * Get annotation or return undefined.\n */\nexport const getAnnotation =\n <T>(annotationId: symbol) =>\n (node: AST.Annotated): T | undefined =>\n pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n\nexport enum VisitResult {\n CONTINUE = 0,\n /**\n * Skip visiting children.\n */\n SKIP = 1,\n /**\n * Stop traversing immediately.\n */\n EXIT = 2,\n}\n\nexport type Path = (string | number)[];\n\nexport type Tester = (node: AST.AST, path: Path, depth: number) => VisitResult | undefined;\nexport type Visitor = (node: AST.AST, path: Path, depth: number) => void;\n\nconst defaultTest: Tester = (node) => (isSimpleType(node) ? VisitResult.CONTINUE : VisitResult.SKIP);\n\n/**\n * Visit leaf nodes.\n * Refs:\n * - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor\n * - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test\n */\nexport const visit: {\n (node: AST.AST, visitor: Visitor): void;\n (node: AST.AST, test: Tester, visitor: Visitor): void;\n} = (node: AST.AST, testOrVisitor: Tester | Visitor, visitor?: Visitor): void => {\n if (!visitor) {\n visitNode(node, defaultTest, testOrVisitor);\n } else {\n visitNode(node, testOrVisitor as Tester, visitor);\n }\n};\n\nconst visitNode = (\n node: AST.AST,\n test: Tester | undefined,\n visitor: Visitor,\n path: Path = [],\n depth = 0,\n): VisitResult | undefined => {\n const result = test?.(node, path, depth) ?? VisitResult.CONTINUE;\n if (result === VisitResult.EXIT) {\n return result;\n }\n if (result !== VisitResult.SKIP) {\n visitor(node, path, depth);\n }\n\n // Object.\n if (AST.isTypeLiteral(node)) {\n for (const prop of AST.getPropertySignatures(node)) {\n const currentPath = [...path, prop.name.toString()];\n const result = visitNode(prop.type, test, visitor, currentPath, depth + 1);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Array.\n else if (AST.isTupleType(node)) {\n for (const [i, element] of node.elements.entries()) {\n const currentPath = [...path, i];\n const result = visitNode(element.type, test, visitor, currentPath, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Branching union.\n else if (AST.isUnion(node)) {\n for (const type of node.types) {\n const result = visitNode(type, test, visitor, path, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Refinement.\n else if (AST.isRefinement(node)) {\n const result = visitNode(node.from, test, visitor, path, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n\n // TODO(burdon): Transform?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Reuse visitor.\nexport const findNode = (node: AST.AST, test: (node: AST.AST) => boolean): AST.AST | undefined => {\n if (test(node)) {\n return node;\n }\n\n // Object.\n else if (AST.isTypeLiteral(node)) {\n for (const prop of AST.getPropertySignatures(node)) {\n const child = findNode(prop.type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Array.\n else if (AST.isTupleType(node)) {\n for (const [_, element] of node.elements.entries()) {\n const child = findNode(element.type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Branching union.\n else if (AST.isUnion(node)) {\n for (const type of node.types) {\n const child = findNode(type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\n\n return undefined;\n};\n\n/**\n * Get the AST node for the given property (dot-path).\n */\nexport const findProperty = (schema: S.Schema<any>, path: JsonPath | JsonProp): AST.AST | undefined => {\n const getProp = (node: AST.AST, path: JsonProp[]): AST.AST | undefined => {\n const [name, ...rest] = path;\n const typeNode = findNode(node, AST.isTypeLiteral);\n invariant(typeNode);\n for (const prop of AST.getPropertySignatures(typeNode)) {\n if (prop.name === name) {\n if (rest.length) {\n return getProp(prop.type, rest);\n } else {\n return prop.type;\n }\n }\n }\n\n return undefined;\n };\n\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n/**\n * Recursively descend into AST to find first matching annotations\n */\nexport const findAnnotation = <T>(node: AST.AST, annotationId: symbol): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId);\n const getBaseAnnotation = (node: AST.AST): T | undefined => {\n const value = getAnnotationById(node);\n if (value !== undefined) {\n return value as T;\n }\n\n if (AST.isUnion(node)) {\n for (const type of node.types) {\n const value = getBaseAnnotation(type);\n if (value !== undefined) {\n return value as T;\n }\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\n\nimport { decamelize } from '@dxos/util';\n\nconst ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');\n\ntype ParamKeyAnnotationValue = { key: string };\n\nexport const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =\n AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);\n\nexport const ParamKeyAnnotation =\n (value: ParamKeyAnnotationValue) =>\n <S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>\n self.annotations({ [ParamKeyAnnotationId]: value });\n\n/**\n * HTTP params parser.\n * Supports custom key serialization.\n */\nexport class UrlParser<T extends Record<string, any>> {\n constructor(private readonly _schema: S.Struct<T>) {}\n\n /**\n * Parse URL params.\n */\n parse(_url: string): T {\n const url = new URL(_url);\n return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {\n let value = url.searchParams.get(decamelize(key));\n if (value == null) {\n value = url.searchParams.get(key);\n }\n\n if (value != null) {\n if (AST.isNumberKeyword(type.ast)) {\n params[key] = parseInt(value);\n } else if (AST.isBooleanKeyword(type.ast)) {\n params[key] = value === 'true' || value === '1';\n } else {\n params[key] = value;\n }\n }\n\n return params;\n }, {}) as T;\n }\n\n /**\n * Return URL with encoded params.\n */\n create(_url: string, params: T): URL {\n const url = new URL(_url);\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined) {\n const field = this._schema.fields[key];\n if (field) {\n const { key: serializedKey } = pipe(\n getParamKeyAnnotation(field.ast),\n Option.getOrElse(() => ({\n key: decamelize(key),\n })),\n );\n\n url.searchParams.set(serializedKey, String(value));\n }\n }\n });\n\n return url;\n }\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["import_schema", "import_effect", "getSimpleType", "node", "AST", "isObjectKeyword", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "S", "NonEmptyString", "pipe", "pattern", "JsonProp", "getAnnotation", "annotationId", "Option", "getOrUndefined", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "result", "
|
|
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,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;AAKZ,IAAMC,WAAWC,eAAAA,OAAEC,eAAeC,KAAKF,eAAAA,OAAEG,QAAQN,UAAAA,CAAAA;AACjD,IAAMO,WAAWJ,eAAAA,OAAEC,eAAeC,KAAKF,eAAAA,OAAEG,QAAQL,UAAAA,CAAAA;AAKjD,IAAMO,gBACX,CAAIC,iBACJ,CAACpB,aACCgB,oBAAKf,mBAAIkB,cAAiBC,YAAAA,EAAcpB,IAAAA,GAAOqB,qBAAOC,cAAc;;UA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4B,CAAC+B,QAAQ,CAACC,KAAKzE,IAAAA,MAAK;AACzF,UAAIgC,QAAQsC,IAAII,aAAaC,QAAIC,yBAAWH,GAAAA,CAAAA;AAC5C,UAAIzC,SAAS,MAAM;AACjBA,gBAAQsC,IAAII,aAAaC,IAAIF,GAAAA;MAC/B;AAEA,UAAIzC,SAAS,MAAM;AACjB,YAAIrE,eAAAA,IAAIK,gBAAgBgC,KAAKqB,GAAG,GAAG;AACjCmD,iBAAOC,GAAAA,IAAOI,SAAS7C,KAAAA;QACzB,WAAWrE,eAAAA,IAAIM,iBAAiB+B,KAAKqB,GAAG,GAAG;AACzCmD,iBAAOC,GAAAA,IAAOzC,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACLwC,iBAAOC,GAAAA,IAAOzC;QAChB;MACF;AAEA,aAAOwC;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAM,OAAOT,MAAcG,QAAgB;AACnC,UAAMF,MAAM,IAAIC,IAAIF,IAAAA;AACpBjB,WAAO/C,QAAQmE,MAAAA,EAAQO,QAAQ,CAAC,CAACN,KAAKzC,KAAAA,MAAM;AAC1C,UAAIA,UAAUC,QAAW;AACvB,cAAM+C,QAAQ,KAAKb,QAAQhB,OAAOsB,GAAAA;AAClC,YAAIO,OAAO;AACT,gBAAM,EAAEP,KAAKQ,cAAa,QAAKvG,eAAAA,MAC7BoF,sBAAsBkB,MAAM3D,GAAG,GAC/BtC,eAAAA,OAAOmG,UAAU,OAAO;YACtBT,SAAKG,yBAAWH,GAAAA;UAClB,EAAA,CAAA;AAGFH,cAAII,aAAaS,IAAIF,eAAeG,OAAOpD,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOsC;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", "getAnnotation", "annotationId", "Option", "getOrUndefined", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "result", "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", "findAnnotation", "options", "getAnnotationById", "getBaseAnnotation", "value", "undefined", "noDefault", "_tag", "annotations", "isUndefinedKeyword", "isLiteralUnion", "every", "getDiscriminatingProps", "reduce", "shared", "props", "filter", "p", "map", "includes", "getDiscriminatedType", "match", "literal", "fields", "Object", "fromEntries", "literals", "find", "nonNullable", "Literal", "Struct", "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":33857,"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":21745},"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","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":72},"packages/common/effect/src/ast.ts":{"bytesInOutput":7227},"packages/common/effect/src/url.ts":{"bytesInOutput":1624}},"bytes":9432}}}
|
|
@@ -7,9 +7,10 @@ import { AST as AST3, JSONSchema, Schema as S2 } from "@effect/schema";
|
|
|
7
7
|
import { AST, Schema as S } from "@effect/schema";
|
|
8
8
|
import { Option, pipe } from "effect";
|
|
9
9
|
import { invariant } from "@dxos/invariant";
|
|
10
|
+
import { nonNullable } from "@dxos/util";
|
|
10
11
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/effect/src/ast.ts";
|
|
11
12
|
var getSimpleType = (node) => {
|
|
12
|
-
if (AST.isObjectKeyword(node)) {
|
|
13
|
+
if (AST.isObjectKeyword(node) || AST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {
|
|
13
14
|
return "object";
|
|
14
15
|
}
|
|
15
16
|
if (AST.isStringKeyword(node)) {
|
|
@@ -110,16 +111,17 @@ var findNode = (node, test) => {
|
|
|
110
111
|
}
|
|
111
112
|
}
|
|
112
113
|
} else if (AST.isUnion(node)) {
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
if (isOption(node)) {
|
|
115
|
+
for (const type of node.types) {
|
|
116
|
+
const child = findNode(type, test);
|
|
117
|
+
if (child) {
|
|
118
|
+
return child;
|
|
119
|
+
}
|
|
117
120
|
}
|
|
118
121
|
}
|
|
119
122
|
} else if (AST.isRefinement(node)) {
|
|
120
123
|
return findNode(node.from, test);
|
|
121
124
|
}
|
|
122
|
-
return void 0;
|
|
123
125
|
};
|
|
124
126
|
var findProperty = (schema, path) => {
|
|
125
127
|
const getProp = (node, path2) => {
|
|
@@ -127,7 +129,7 @@ var findProperty = (schema, path) => {
|
|
|
127
129
|
const typeNode = findNode(node, AST.isTypeLiteral);
|
|
128
130
|
invariant(typeNode, void 0, {
|
|
129
131
|
F: __dxlog_file,
|
|
130
|
-
L:
|
|
132
|
+
L: 221,
|
|
131
133
|
S: void 0,
|
|
132
134
|
A: [
|
|
133
135
|
"typeNode",
|
|
@@ -143,28 +145,122 @@ var findProperty = (schema, path) => {
|
|
|
143
145
|
}
|
|
144
146
|
}
|
|
145
147
|
}
|
|
146
|
-
return void 0;
|
|
147
148
|
};
|
|
148
149
|
return getProp(schema.ast, path.split("."));
|
|
149
150
|
};
|
|
150
|
-
var
|
|
151
|
+
var defaultAnnotations = {
|
|
152
|
+
["ObjectKeyword"]: AST.objectKeyword,
|
|
153
|
+
["StringKeyword"]: AST.stringKeyword,
|
|
154
|
+
["NumberKeyword"]: AST.numberKeyword,
|
|
155
|
+
["BooleanKeyword"]: AST.booleanKeyword
|
|
156
|
+
};
|
|
157
|
+
var findAnnotation = (node, annotationId, options) => {
|
|
151
158
|
const getAnnotationById = getAnnotation(annotationId);
|
|
152
159
|
const getBaseAnnotation = (node2) => {
|
|
153
160
|
const value = getAnnotationById(node2);
|
|
154
161
|
if (value !== void 0) {
|
|
162
|
+
if (options?.noDefault && value === defaultAnnotations[node2._tag]?.annotations[annotationId]) {
|
|
163
|
+
return void 0;
|
|
164
|
+
}
|
|
155
165
|
return value;
|
|
156
166
|
}
|
|
157
167
|
if (AST.isUnion(node2)) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (value2 !== void 0) {
|
|
161
|
-
return value2;
|
|
162
|
-
}
|
|
168
|
+
if (isOption(node2)) {
|
|
169
|
+
return getAnnotationById(node2.types[0]);
|
|
163
170
|
}
|
|
164
171
|
}
|
|
165
172
|
};
|
|
166
173
|
return getBaseAnnotation(node);
|
|
167
174
|
};
|
|
175
|
+
var isOption = (node) => {
|
|
176
|
+
return AST.isUnion(node) && node.types.length === 2 && AST.isUndefinedKeyword(node.types[1]);
|
|
177
|
+
};
|
|
178
|
+
var isLiteralUnion = (node) => {
|
|
179
|
+
return AST.isUnion(node) && node.types.every(AST.isLiteral);
|
|
180
|
+
};
|
|
181
|
+
var isDiscriminatedUnion = (node) => {
|
|
182
|
+
return AST.isUnion(node) && !!getDiscriminatingProps(node)?.length;
|
|
183
|
+
};
|
|
184
|
+
var getDiscriminatingProps = (node) => {
|
|
185
|
+
invariant(AST.isUnion(node), void 0, {
|
|
186
|
+
F: __dxlog_file,
|
|
187
|
+
L: 307,
|
|
188
|
+
S: void 0,
|
|
189
|
+
A: [
|
|
190
|
+
"AST.isUnion(node)",
|
|
191
|
+
""
|
|
192
|
+
]
|
|
193
|
+
});
|
|
194
|
+
if (isOption(node)) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
return node.types.reduce((shared, type) => {
|
|
198
|
+
const props = AST.getPropertySignatures(type).filter((p) => AST.isLiteral(p.type)).map((p) => p.name.toString());
|
|
199
|
+
return shared.length === 0 ? props : shared.filter((prop) => props.includes(prop));
|
|
200
|
+
}, []);
|
|
201
|
+
};
|
|
202
|
+
var getDiscriminatedType = (node, value = {}) => {
|
|
203
|
+
invariant(AST.isUnion(node), void 0, {
|
|
204
|
+
F: __dxlog_file,
|
|
205
|
+
L: 328,
|
|
206
|
+
S: void 0,
|
|
207
|
+
A: [
|
|
208
|
+
"AST.isUnion(node)",
|
|
209
|
+
""
|
|
210
|
+
]
|
|
211
|
+
});
|
|
212
|
+
invariant(value, void 0, {
|
|
213
|
+
F: __dxlog_file,
|
|
214
|
+
L: 329,
|
|
215
|
+
S: void 0,
|
|
216
|
+
A: [
|
|
217
|
+
"value",
|
|
218
|
+
""
|
|
219
|
+
]
|
|
220
|
+
});
|
|
221
|
+
const props = getDiscriminatingProps(node);
|
|
222
|
+
if (!props?.length) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
for (const type of node.types) {
|
|
226
|
+
const match = AST.getPropertySignatures(type).filter((prop) => props?.includes(prop.name.toString())).every((prop) => {
|
|
227
|
+
invariant(AST.isLiteral(prop.type), void 0, {
|
|
228
|
+
F: __dxlog_file,
|
|
229
|
+
L: 340,
|
|
230
|
+
S: void 0,
|
|
231
|
+
A: [
|
|
232
|
+
"AST.isLiteral(prop.type)",
|
|
233
|
+
""
|
|
234
|
+
]
|
|
235
|
+
});
|
|
236
|
+
return prop.type.literal === value[prop.name.toString()];
|
|
237
|
+
});
|
|
238
|
+
if (match) {
|
|
239
|
+
return type;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const fields = Object.fromEntries(props.map((prop) => {
|
|
243
|
+
const literals = node.types.map((type) => {
|
|
244
|
+
const literal = AST.getPropertySignatures(type).find((p) => p.name.toString() === prop);
|
|
245
|
+
invariant(AST.isLiteral(literal.type), void 0, {
|
|
246
|
+
F: __dxlog_file,
|
|
247
|
+
L: 358,
|
|
248
|
+
S: void 0,
|
|
249
|
+
A: [
|
|
250
|
+
"AST.isLiteral(literal.type)",
|
|
251
|
+
""
|
|
252
|
+
]
|
|
253
|
+
});
|
|
254
|
+
return literal.type.literal;
|
|
255
|
+
}).filter(nonNullable);
|
|
256
|
+
return literals.length ? [
|
|
257
|
+
prop,
|
|
258
|
+
S.Literal(...literals)
|
|
259
|
+
] : void 0;
|
|
260
|
+
}).filter(nonNullable));
|
|
261
|
+
const schema = S.Struct(fields);
|
|
262
|
+
return schema.ast;
|
|
263
|
+
};
|
|
168
264
|
|
|
169
265
|
// packages/common/effect/src/url.ts
|
|
170
266
|
import { AST as AST2 } from "@effect/schema";
|
|
@@ -233,8 +329,13 @@ export {
|
|
|
233
329
|
findNode,
|
|
234
330
|
findProperty,
|
|
235
331
|
getAnnotation,
|
|
332
|
+
getDiscriminatedType,
|
|
333
|
+
getDiscriminatingProps,
|
|
236
334
|
getParamKeyAnnotation,
|
|
237
335
|
getSimpleType,
|
|
336
|
+
isDiscriminatedUnion,
|
|
337
|
+
isLiteralUnion,
|
|
338
|
+
isOption,
|
|
238
339
|
isSimpleType,
|
|
239
340
|
visit
|
|
240
341
|
};
|
|
@@ -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';\n\n//\n// Refs\n// https://effect.website/docs/guides/schema\n// https://www.npmjs.com/package/@effect/schema\n// https://effect-ts.github.io/effect/schema/AST.ts.html\n//\n\nexport type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';\n\nexport const getSimpleType = (node: AST.AST): SimpleType | undefined => {\n if (AST.isObjectKeyword(node)) {\n return 'object';\n }\n if (AST.isStringKeyword(node)) {\n return 'string';\n }\n if (AST.isNumberKeyword(node)) {\n return 'number';\n }\n if (AST.isBooleanKeyword(node)) {\n return 'boolean';\n }\n if (AST.isEnums(node)) {\n return 'enum';\n }\n if (AST.isLiteral(node)) {\n return 'literal';\n }\n};\n\nexport const isSimpleType = (node: AST.AST) => !!getSimpleType(node);\n\n//\n// Branded types\n//\n\nexport type JsonProp = string & { __JsonPath: true; __JsonProp: true };\nexport type JsonPath = string & { __JsonPath: true };\n\nconst PATH_REGEX = /[a-zA-Z_$][\\w$]*(?:\\.[a-zA-Z_$][\\w$]*)*/;\nconst PROP_REGEX = /\\w+/;\n\n/**\n * https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html\n */\nexport const JsonPath = S.NonEmptyString.pipe(S.pattern(PATH_REGEX)) as any as S.Schema<JsonPath>;\nexport const JsonProp = S.NonEmptyString.pipe(S.pattern(PROP_REGEX)) as any as S.Schema<JsonProp>;\n\n/**\n * Get annotation or return undefined.\n */\nexport const getAnnotation =\n <T>(annotationId: symbol) =>\n (node: AST.Annotated): T | undefined =>\n pipe(AST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);\n\nexport enum VisitResult {\n CONTINUE = 0,\n /**\n * Skip visiting children.\n */\n SKIP = 1,\n /**\n * Stop traversing immediately.\n */\n EXIT = 2,\n}\n\nexport type Path = (string | number)[];\n\nexport type Tester = (node: AST.AST, path: Path, depth: number) => VisitResult | undefined;\nexport type Visitor = (node: AST.AST, path: Path, depth: number) => void;\n\nconst defaultTest: Tester = (node) => (isSimpleType(node) ? VisitResult.CONTINUE : VisitResult.SKIP);\n\n/**\n * Visit leaf nodes.\n * Refs:\n * - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor\n * - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test\n */\nexport const visit: {\n (node: AST.AST, visitor: Visitor): void;\n (node: AST.AST, test: Tester, visitor: Visitor): void;\n} = (node: AST.AST, testOrVisitor: Tester | Visitor, visitor?: Visitor): void => {\n if (!visitor) {\n visitNode(node, defaultTest, testOrVisitor);\n } else {\n visitNode(node, testOrVisitor as Tester, visitor);\n }\n};\n\nconst visitNode = (\n node: AST.AST,\n test: Tester | undefined,\n visitor: Visitor,\n path: Path = [],\n depth = 0,\n): VisitResult | undefined => {\n const result = test?.(node, path, depth) ?? VisitResult.CONTINUE;\n if (result === VisitResult.EXIT) {\n return result;\n }\n if (result !== VisitResult.SKIP) {\n visitor(node, path, depth);\n }\n\n // Object.\n if (AST.isTypeLiteral(node)) {\n for (const prop of AST.getPropertySignatures(node)) {\n const currentPath = [...path, prop.name.toString()];\n const result = visitNode(prop.type, test, visitor, currentPath, depth + 1);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Array.\n else if (AST.isTupleType(node)) {\n for (const [i, element] of node.elements.entries()) {\n const currentPath = [...path, i];\n const result = visitNode(element.type, test, visitor, currentPath, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Branching union.\n else if (AST.isUnion(node)) {\n for (const type of node.types) {\n const result = visitNode(type, test, visitor, path, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n }\n\n // Refinement.\n else if (AST.isRefinement(node)) {\n const result = visitNode(node.from, test, visitor, path, depth);\n if (result === VisitResult.EXIT) {\n return result;\n }\n }\n\n // TODO(burdon): Transform?\n};\n\n/**\n * Recursively descend into AST to find first node that passes the test.\n */\n// TODO(burdon): Reuse visitor.\nexport const findNode = (node: AST.AST, test: (node: AST.AST) => boolean): AST.AST | undefined => {\n if (test(node)) {\n return node;\n }\n\n // Object.\n else if (AST.isTypeLiteral(node)) {\n for (const prop of AST.getPropertySignatures(node)) {\n const child = findNode(prop.type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Array.\n else if (AST.isTupleType(node)) {\n for (const [_, element] of node.elements.entries()) {\n const child = findNode(element.type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Branching union.\n else if (AST.isUnion(node)) {\n for (const type of node.types) {\n const child = findNode(type, test);\n if (child) {\n return child;\n }\n }\n }\n\n // Refinement.\n else if (AST.isRefinement(node)) {\n return findNode(node.from, test);\n }\n\n return undefined;\n};\n\n/**\n * Get the AST node for the given property (dot-path).\n */\nexport const findProperty = (schema: S.Schema<any>, path: JsonPath | JsonProp): AST.AST | undefined => {\n const getProp = (node: AST.AST, path: JsonProp[]): AST.AST | undefined => {\n const [name, ...rest] = path;\n const typeNode = findNode(node, AST.isTypeLiteral);\n invariant(typeNode);\n for (const prop of AST.getPropertySignatures(typeNode)) {\n if (prop.name === name) {\n if (rest.length) {\n return getProp(prop.type, rest);\n } else {\n return prop.type;\n }\n }\n }\n\n return undefined;\n };\n\n return getProp(schema.ast, path.split('.') as JsonProp[]);\n};\n\n/**\n * Recursively descend into AST to find first matching annotations\n */\nexport const findAnnotation = <T>(node: AST.AST, annotationId: symbol): T | undefined => {\n const getAnnotationById = getAnnotation(annotationId);\n const getBaseAnnotation = (node: AST.AST): T | undefined => {\n const value = getAnnotationById(node);\n if (value !== undefined) {\n return value as T;\n }\n\n if (AST.isUnion(node)) {\n for (const type of node.types) {\n const value = getBaseAnnotation(type);\n if (value !== undefined) {\n return value as T;\n }\n }\n }\n };\n\n return getBaseAnnotation(node);\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\n\nimport { decamelize } from '@dxos/util';\n\nconst ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');\n\ntype ParamKeyAnnotationValue = { key: string };\n\nexport const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =\n AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);\n\nexport const ParamKeyAnnotation =\n (value: ParamKeyAnnotationValue) =>\n <S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>\n self.annotations({ [ParamKeyAnnotationId]: value });\n\n/**\n * HTTP params parser.\n * Supports custom key serialization.\n */\nexport class UrlParser<T extends Record<string, any>> {\n constructor(private readonly _schema: S.Struct<T>) {}\n\n /**\n * Parse URL params.\n */\n parse(_url: string): T {\n const url = new URL(_url);\n return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {\n let value = url.searchParams.get(decamelize(key));\n if (value == null) {\n value = url.searchParams.get(key);\n }\n\n if (value != null) {\n if (AST.isNumberKeyword(type.ast)) {\n params[key] = parseInt(value);\n } else if (AST.isBooleanKeyword(type.ast)) {\n params[key] = value === 'true' || value === '1';\n } else {\n params[key] = value;\n }\n }\n\n return params;\n }, {}) as T;\n }\n\n /**\n * Return URL with encoded params.\n */\n create(_url: string, params: T): URL {\n const url = new URL(_url);\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined) {\n const field = this._schema.fields[key];\n if (field) {\n const { key: serializedKey } = pipe(\n getParamKeyAnnotation(field.ast),\n Option.getOrElse(() => ({\n key: decamelize(key),\n })),\n );\n\n url.searchParams.set(serializedKey, String(value));\n }\n }\n });\n\n return url;\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,UAAS;;;ACA7C,SAASC,KAAKC,UAAUC,SAAS;AACjC,SAASC,QAAQC,YAAY;AAE7B,SAASC,iBAAiB;;
|
|
6
|
-
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Schema", "S", "Option", "pipe", "invariant", "getSimpleType", "node", "isObjectKeyword", "isStringKeyword", "isNumberKeyword", "isBooleanKeyword", "isEnums", "isLiteral", "isSimpleType", "PATH_REGEX", "PROP_REGEX", "JsonPath", "NonEmptyString", "pattern", "JsonProp", "getAnnotation", "annotationId", "getOrUndefined", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "result", "
|
|
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,EAA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"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", "getAnnotation", "annotationId", "getOrUndefined", "VisitResult", "defaultTest", "visit", "testOrVisitor", "visitor", "visitNode", "test", "path", "depth", "result", "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", "findAnnotation", "options", "getAnnotationById", "getBaseAnnotation", "value", "undefined", "noDefault", "_tag", "annotations", "isUndefinedKeyword", "isLiteralUnion", "every", "getDiscriminatingProps", "reduce", "shared", "props", "filter", "p", "map", "includes", "getDiscriminatedType", "match", "literal", "fields", "Object", "fromEntries", "literals", "find", "Literal", "Struct", "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":33857,"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":21747},"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","visit"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":72},"packages/common/effect/src/ast.ts":{"bytesInOutput":7227},"packages/common/effect/src/url.ts":{"bytesInOutput":1624}},"bytes":9525}}}
|
package/dist/types/src/ast.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { AST, Schema as S } from '@effect/schema';
|
|
2
2
|
export type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';
|
|
3
|
+
/**
|
|
4
|
+
* Get the base type; e.g., traverse through refinements.
|
|
5
|
+
*/
|
|
3
6
|
export declare const getSimpleType: (node: AST.AST) => SimpleType | undefined;
|
|
4
7
|
export declare const isSimpleType: (node: AST.AST) => boolean;
|
|
5
8
|
export type JsonProp = string & {
|
|
@@ -51,7 +54,30 @@ export declare const findNode: (node: AST.AST, test: (node: AST.AST) => boolean)
|
|
|
51
54
|
*/
|
|
52
55
|
export declare const findProperty: (schema: S.Schema<any>, path: JsonPath | JsonProp) => AST.AST | undefined;
|
|
53
56
|
/**
|
|
54
|
-
* Recursively descend into AST to find first matching annotations
|
|
57
|
+
* Recursively descend into AST to find first matching annotations.
|
|
58
|
+
* Optionally skips default annotations for basic types (e.g., 'a string').
|
|
59
|
+
*/
|
|
60
|
+
export declare const findAnnotation: <T>(node: AST.AST, annotationId: symbol, options?: {
|
|
61
|
+
noDefault: boolean;
|
|
62
|
+
}) => T | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Effect S.optional creates a union type with undefined as the second type.
|
|
65
|
+
*/
|
|
66
|
+
export declare const isOption: (node: AST.AST) => boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Determines if the node is a union of literal types.
|
|
69
|
+
*/
|
|
70
|
+
export declare const isLiteralUnion: (node: AST.AST) => boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Determines if the node is a discriminated union.
|
|
73
|
+
*/
|
|
74
|
+
export declare const isDiscriminatedUnion: (node: AST.AST) => boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Get the discriminating properties for the given union type.
|
|
77
|
+
*/
|
|
78
|
+
export declare const getDiscriminatingProps: (node: AST.AST) => string[] | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Get the discriminated type for the given value.
|
|
55
81
|
*/
|
|
56
|
-
export declare const
|
|
82
|
+
export declare const getDiscriminatedType: (node: AST.AST, value?: Record<string, any>) => AST.AST | undefined;
|
|
57
83
|
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../../src/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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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1-staging.7f6f91c",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -22,17 +22,17 @@
|
|
|
22
22
|
"src"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@dxos/util": "0.7.
|
|
26
|
-
"@dxos/node-std": "0.7.
|
|
27
|
-
"@dxos/invariant": "0.7.
|
|
25
|
+
"@dxos/util": "0.7.1-staging.7f6f91c",
|
|
26
|
+
"@dxos/node-std": "0.7.1-staging.7f6f91c",
|
|
27
|
+
"@dxos/invariant": "0.7.1-staging.7f6f91c"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@effect/schema": "^0.75.
|
|
31
|
-
"effect": "^3.9.
|
|
30
|
+
"@effect/schema": "^0.75.5",
|
|
31
|
+
"effect": "^3.9.2"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@effect/schema": "^0.
|
|
35
|
-
"effect": "^3.9.
|
|
34
|
+
"@effect/schema": "^0.75.5",
|
|
35
|
+
"effect": "^3.9.2"
|
|
36
36
|
},
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"access": "public"
|