@fluidframework/tree-agent 2.74.0-365691 → 2.74.0-370705
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/methodBinding.d.ts +6 -1
- package/dist/methodBinding.d.ts.map +1 -1
- package/dist/methodBinding.js +14 -3
- package/dist/methodBinding.js.map +1 -1
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +2 -12
- package/dist/prompt.js.map +1 -1
- package/dist/propertyBinding.js +2 -2
- package/dist/propertyBinding.js.map +1 -1
- package/dist/renderSchemaTypeScript.d.ts +18 -0
- package/dist/renderSchemaTypeScript.d.ts.map +1 -0
- package/dist/renderSchemaTypeScript.js +399 -0
- package/dist/renderSchemaTypeScript.js.map +1 -0
- package/dist/renderZodTypeScript.d.ts +21 -0
- package/dist/renderZodTypeScript.d.ts.map +1 -0
- package/dist/renderZodTypeScript.js +272 -0
- package/dist/renderZodTypeScript.js.map +1 -0
- package/dist/typeGeneration.d.ts +3 -5
- package/dist/typeGeneration.d.ts.map +1 -1
- package/dist/typeGeneration.js +21 -254
- package/dist/typeGeneration.js.map +1 -1
- package/dist/utils.d.ts +10 -27
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +18 -362
- package/dist/utils.js.map +1 -1
- package/lib/methodBinding.d.ts +6 -1
- package/lib/methodBinding.d.ts.map +1 -1
- package/lib/methodBinding.js +11 -1
- package/lib/methodBinding.js.map +1 -1
- package/lib/prompt.d.ts.map +1 -1
- package/lib/prompt.js +3 -13
- package/lib/prompt.js.map +1 -1
- package/lib/propertyBinding.js +1 -1
- package/lib/propertyBinding.js.map +1 -1
- package/lib/renderSchemaTypeScript.d.ts +18 -0
- package/lib/renderSchemaTypeScript.d.ts.map +1 -0
- package/lib/renderSchemaTypeScript.js +395 -0
- package/lib/renderSchemaTypeScript.js.map +1 -0
- package/lib/renderZodTypeScript.d.ts +21 -0
- package/lib/renderZodTypeScript.d.ts.map +1 -0
- package/lib/renderZodTypeScript.js +267 -0
- package/lib/renderZodTypeScript.js.map +1 -0
- package/lib/typeGeneration.d.ts +3 -5
- package/lib/typeGeneration.d.ts.map +1 -1
- package/lib/typeGeneration.js +24 -257
- package/lib/typeGeneration.js.map +1 -1
- package/lib/utils.d.ts +10 -27
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +18 -360
- package/lib/utils.js.map +1 -1
- package/package.json +10 -10
- package/src/methodBinding.ts +15 -3
- package/src/prompt.ts +6 -22
- package/src/propertyBinding.ts +1 -1
- package/src/renderSchemaTypeScript.ts +523 -0
- package/src/renderZodTypeScript.ts +314 -0
- package/src/typeGeneration.ts +32 -414
- package/src/utils.ts +18 -412
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AACtE,OAAO,EACN,eAAe,EACf,SAAS,EACT,eAAe,EACf,aAAa,EACb,QAAQ,EACR,gBAAgB,EAChB,gBAAgB,EAChB,WAAW,EACX,eAAe,GACf,MAAM,+BAA+B,CAAC;AAQvC,OAAO,EAAE,CAAC,EAAmB,MAAM,KAAK,CAAC;AAGzC,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EACN,IAAI,EACJ,WAAW,EACX,aAAa,EACb,UAAU,EACV,WAAW,EACX,eAAe,GAEf,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAGlC,CAAC;AAEJ;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACzC,UAA+B,EAC/B,MAAwB;IAIxB,O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Aa,EAAE,MAAM,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC;IAC7E,CAAC;AACF,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert, unreachableCase } from \"@fluidframework/core-utils/internal\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tArrayNodeSchema,\n\tFieldKind,\n\tgetSimpleSchema,\n\tMapNodeSchema,\n\tNodeKind,\n\tObjectNodeSchema,\n\tRecordNodeSchema,\n\tValueSchema,\n\twalkFieldSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tImplicitFieldSchema,\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNodeSchema,\n} from \"@fluidframework/tree/internal\";\nimport { z, type ZodTypeAny } from \"zod\";\n\nimport type { BindableSchema } from \"./methodBinding.js\";\nimport { FunctionWrapper, getExposedMethods } from \"./methodBinding.js\";\nimport { getExposedProperties } from \"./propertyBinding.js\";\nimport {\n\tfail,\n\tgetOrCreate,\n\thasAtLeastTwo,\n\tllmDefault,\n\tmapIterable,\n\ttryGetSingleton,\n\ttype MapGetSet,\n} from \"./utils.js\";\n\n/**\n *\n * TODO: Add a prompt suggestion API!\n *\n * TODO: Handle rate limit errors.\n *\n * TODO: Pass descriptions from schema metadata to the generated TS types that we put in the prompt\n *\n * TODO make the Ids be \"Vector-2\" instead of \"Vector2\" (or else it gets weird when you have a type called \"Vector2\")\n */\n\n/**\n * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM\n */\nconst promptSchemaCache = new WeakMap<\n\tSimpleTreeSchema,\n\tReturnType<typeof generateEditTypes>\n>();\n\n/**\n * TODO\n */\nexport function generateEditTypesForPrompt(\n\trootSchema: ImplicitFieldSchema,\n\tschema: SimpleTreeSchema,\n): {\n\tdomainTypes: Record<string, ZodTypeAny>;\n} {\n\treturn getOrCreate(promptSchemaCache, schema, () => {\n\t\tconst allSchemas = new Set<TreeNodeSchema>();\n\t\tconst objectTypeSchemas = new Set<TreeNodeSchema>();\n\t\twalkFieldSchema(rootSchema, {\n\t\t\tnode: (n) => {\n\t\t\t\tallSchemas.add(n);\n\t\t\t\tif (\n\t\t\t\t\tn instanceof ObjectNodeSchema ||\n\t\t\t\t\tn instanceof MapNodeSchema ||\n\t\t\t\t\tn instanceof ArrayNodeSchema ||\n\t\t\t\t\tn instanceof RecordNodeSchema\n\t\t\t\t) {\n\t\t\t\t\tobjectTypeSchemas.add(n);\n\t\t\t\t\tconst exposedMethods = getExposedMethods(n);\n\t\t\t\t\tfor (const t of exposedMethods.referencedTypes) {\n\t\t\t\t\t\tallSchemas.add(t);\n\t\t\t\t\t\tobjectTypeSchemas.add(t);\n\t\t\t\t\t}\n\t\t\t\t\tconst exposedProperties = getExposedProperties(n);\n\t\t\t\t\tfor (const t of exposedProperties.referencedTypes) {\n\t\t\t\t\t\tallSchemas.add(t);\n\t\t\t\t\t\tobjectTypeSchemas.add(t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tconst nodeSchemas = [...objectTypeSchemas.values()] as BindableSchema[];\n\t\tconst bindableSchemas = new Map<string, BindableSchema>(\n\t\t\tnodeSchemas.map((nodeSchema) => [nodeSchema.identifier, nodeSchema]),\n\t\t);\n\t\treturn generateEditTypes(\n\t\t\t[...allSchemas.values()].map((s) => getSimpleSchema(s)),\n\t\t\tnew Map(),\n\t\t\tbindableSchemas,\n\t\t);\n\t});\n}\n\n/**\n * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}\n * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.\n *\n * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.\n *\n * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.\n */\nfunction generateEditTypes(\n\tschemas: Iterable<SimpleTreeSchema>,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): {\n\tdomainTypes: Record<string, ZodTypeAny>;\n} {\n\tconst domainTypes: Record<string, ZodTypeAny> = {};\n\tfor (const schema of schemas) {\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\t// If this does overwrite anything in domainTypes, it is guaranteed to be overwritten with an identical value due to the getOrCreate\n\t\t\tdomainTypes[name] = getOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\tname,\n\t\t\t\tobjectCache,\n\t\t\t\tbindableSchemas,\n\t\t\t);\n\t\t}\n\t}\n\n\treturn {\n\t\tdomainTypes,\n\t};\n}\n\nfunction getBoundMembersForBindable(bindableSchema: BindableSchema): {\n\treferencedTypes: Set<TreeNodeSchema>;\n\tmembers: [string, ZodTypeAny][];\n} {\n\tconst memberTypes: [string, ZodTypeAny][] = [];\n\n\tconst methods = getExposedMethods(bindableSchema);\n\tfor (const [name, method] of Object.entries(methods.methods)) {\n\t\tconst zodFunction = z.instanceof(FunctionWrapper);\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any\n\t\t(zodFunction as any).method = method;\n\t\tmemberTypes.push([name, zodFunction]);\n\t}\n\tconst props = getExposedProperties(bindableSchema);\n\tfor (const [name, prop] of Object.entries(props.properties)) {\n\t\tconst schema = prop.schema;\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any\n\t\t(schema as any).property = prop;\n\t\tmemberTypes.push([name, prop.schema]);\n\t}\n\n\treturn {\n\t\tmembers: memberTypes,\n\t\treferencedTypes: new Set([...props.referencedTypes, ...methods.referencedTypes]),\n\t};\n}\n\nfunction getBoundMembers(\n\tdefinition: string,\n\tbindableSchemas: Map<string, BindableSchema>,\n): [string, ZodTypeAny][] {\n\tconst bindableSchema = bindableSchemas.get(definition) ?? fail(\"unknown definition\");\n\treturn getBoundMembersForBindable(bindableSchema).members;\n}\n\nfunction addBindingIntersectionIfNeeded(\n\ttypeString: \"array\" | \"map\" | \"record\",\n\tzodTypeBound: ZodTypeAny,\n\tdefinition: string,\n\tsimpleNodeSchema: SimpleNodeSchema,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tlet zodType = zodTypeBound;\n\tlet description = simpleNodeSchema.metadata?.description ?? \"\";\n\n\tconst boundMembers = getBoundMembers(definition, bindableSchemas);\n\tconst methods: Record<string, z.ZodTypeAny> = {};\n\tconst properties: Record<string, z.ZodTypeAny> = {};\n\n\tlet hasProperties: boolean = false;\n\tlet hasMethods: boolean = false;\n\n\tif (boundMembers.length > 0) {\n\t\tfor (const [name, zodMember] of boundMembers) {\n\t\t\tconst kind =\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t(zodMember as any).method === undefined\n\t\t\t\t\t? // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(zodMember as any).property === undefined\n\t\t\t\t\t\t? \"Unknown\"\n\t\t\t\t\t\t: \"Property\"\n\t\t\t\t\t: \"Method\";\n\n\t\t\tif (kind === \"Unknown\") {\n\t\t\t\tthrow new UsageError(`Unrecognized bound member ${name} in schema ${definition}`);\n\t\t\t}\n\t\t\tif (kind === \"Method\") {\n\t\t\t\tif (methods[name] !== undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tmethods[name] = zodMember;\n\t\t\t}\n\t\t\tif (kind === \"Property\") {\n\t\t\t\tif (properties[name] !== undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tproperties[name] = zodMember;\n\t\t\t}\n\t\t}\n\t\thasMethods = Object.keys(methods).length > 0 ? true : false;\n\t\thasProperties = Object.keys(properties).length > 0 ? true : false;\n\n\t\tif (hasMethods) {\n\t\t\tzodType = z.intersection(zodType, z.object(methods));\n\t\t}\n\t\tif (hasProperties) {\n\t\t\tzodType = z.intersection(zodType, z.object(properties));\n\t\t}\n\t}\n\n\tlet note = \"\";\n\tif (hasMethods && hasProperties) {\n\t\tnote = `Note: this ${typeString} has custom user-defined methods and properties directly on it.`;\n\t} else if (hasMethods) {\n\t\tnote = `Note: this ${typeString} has custom user-defined methods directly on it.`;\n\t} else if (hasProperties) {\n\t\tnote = `Note: this ${typeString} has custom user-defined properties directly on it.`;\n\t}\n\tif (note !== \"\") {\n\t\tdescription = description === \"\" ? note : `${description} - ${note}`;\n\t}\n\n\treturn zodType.describe(description);\n}\n\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tdefinition: string,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst simpleNodeSchema = definitionMap.get(definition) ?? fail(\"Unexpected definition\");\n\treturn getOrCreate(objectCache, simpleNodeSchema, () => {\n\t\t// Handle recursive types: temporarily create a zod \"lazy\" type that can be referenced by a recursive call to getOrCreateType.\n\t\tlet type: ZodTypeAny | undefined;\n\t\tobjectCache.set(\n\t\t\tsimpleNodeSchema,\n\t\t\tz.lazy(() => type ?? fail(\"Recursive type used before creation\")),\n\t\t);\n\t\tswitch (simpleNodeSchema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tconst properties: Record<string, z.ZodTypeAny> = Object.fromEntries(\n\t\t\t\t\t[...simpleNodeSchema.fields]\n\t\t\t\t\t\t.map(([key, field]) => {\n\t\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\tgetOrCreateTypeForField(definitionMap, field, objectCache, bindableSchemas),\n\t\t\t\t\t\t\t];\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.filter(([, value]) => value !== undefined),\n\t\t\t\t);\n\n\t\t\t\tfor (const [name, zodMember] of getBoundMembers(definition, bindableSchemas)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\tconst kind = (zodMember as any).method === undefined ? \"Property\" : \"Method\";\n\t\t\t\t\tif (properties[name] !== undefined) {\n\t\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tproperties[name] = zodMember;\n\t\t\t\t}\n\n\t\t\t\treturn (type = z\n\t\t\t\t\t.object(properties)\n\t\t\t\t\t.describe(simpleNodeSchema.metadata?.description ?? \"\"));\n\t\t\t}\n\t\t\tcase NodeKind.Map: {\n\t\t\t\tconst zodType = z.map(\n\t\t\t\t\tz.string(),\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"map\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Record: {\n\t\t\t\tconst zodType = z.record(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"record\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Array: {\n\t\t\t\tconst zodType = z.array(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"array\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Leaf: {\n\t\t\t\tswitch (simpleNodeSchema.leafKind) {\n\t\t\t\t\tcase ValueSchema.Boolean: {\n\t\t\t\t\t\treturn (type = z.boolean());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Number: {\n\t\t\t\t\t\treturn (type = z.number());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.String: {\n\t\t\t\t\t\treturn (type = z.string());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Null: {\n\t\t\t\t\t\treturn (type = z.null());\n\t\t\t\t\t}\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tthrow new Error(`Unsupported leaf kind ${NodeKind[simpleNodeSchema.leafKind]}.`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn unreachableCase(simpleNodeSchema, \"Unknown node kind\");\n\t\t\t}\n\t\t}\n\t});\n}\n\nfunction getOrCreateTypeForField(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tfieldSchema: SimpleFieldSchema,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst customMetadata = fieldSchema.metadata.custom as\n\t\t| Record<string | symbol, unknown>\n\t\t| undefined;\n\tconst getDefault = customMetadata?.[llmDefault];\n\tif (getDefault !== undefined) {\n\t\tif (typeof getDefault !== \"function\") {\n\t\t\tthrow new UsageError(\n\t\t\t\t`Expected value of ${llmDefault.description} property to be a function, but got ${typeof getDefault}`,\n\t\t\t);\n\t\t}\n\n\t\tif (fieldSchema.kind !== FieldKind.Optional) {\n\t\t\tthrow new UsageError(\n\t\t\t\t`The ${llmDefault.description} property is only permitted on optional fields.`,\n\t\t\t);\n\t\t}\n\t}\n\n\tconst field = getTypeForAllowedTypes(\n\t\tdefinitionMap,\n\t\tnew Set(fieldSchema.simpleAllowedTypes.keys()),\n\t\tobjectCache,\n\t\tbindableSchemas,\n\t).describe(\n\t\tgetDefault === undefined\n\t\t\t? (fieldSchema.metadata?.description ?? \"\")\n\t\t\t: \"Do not populate this field. It will be automatically supplied by the system after insertion.\",\n\t);\n\n\tswitch (fieldSchema.kind) {\n\t\tcase FieldKind.Required: {\n\t\t\treturn field;\n\t\t}\n\t\tcase FieldKind.Optional: {\n\t\t\treturn field.optional();\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn field\n\t\t\t\t.optional()\n\t\t\t\t.describe(\n\t\t\t\t\t\"This is an ID automatically generated by the system. Do not supply it when constructing a new object.\",\n\t\t\t\t);\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\n\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tallowedTypes: ReadonlySet<string>,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst single = tryGetSingleton(allowedTypes);\n\tif (single === undefined) {\n\t\tconst types = [\n\t\t\t...mapIterable(allowedTypes, (name) => {\n\t\t\t\treturn getOrCreateType(definitionMap, name, objectCache, bindableSchemas);\n\t\t\t}),\n\t\t];\n\t\tassert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(definitionMap, single, objectCache, bindableSchemas);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAIjF,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EACN,sBAAsB,GAEtB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAAkD,CAAC;AAExF;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACzC,UAA+B,EAC/B,MAAwB;IAExB,OAAO,WAAW,CAAC,iBAAiB,EAAE,MAAM,EAAE,GAAG,EAAE,CAClD,4BAA4B,CAAC,UAAU,CAAC,CACxC,CAAC;AACH,CAAC;AAED,SAAS,4BAA4B,CACpC,UAA+B;IAE/B,MAAM,eAAe,GAAG,IAAI,GAAG,EAA0B,CAAC;IAE1D,eAAe,CAAC,UAAU,EAAE;QAC3B,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE;YACd,IAAI,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC5B,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;gBAE3C,MAAM,cAAc,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;gBAC/C,KAAK,MAAM,UAAU,IAAI,cAAc,CAAC,eAAe,EAAE,CAAC;oBACzD,IAAI,gBAAgB,CAAC,UAAU,CAAC,EAAE,CAAC;wBAClC,eAAe,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;oBACxD,CAAC;gBACF,CAAC;gBAED,MAAM,iBAAiB,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAC;gBACrD,KAAK,MAAM,UAAU,IAAI,iBAAiB,CAAC,eAAe,EAAE,CAAC;oBAC5D,IAAI,gBAAgB,CAAC,UAAU,CAAC,EAAE,CAAC;wBAClC,eAAe,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;oBACxD,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;KACD,CAAC,CAAC;IAEH,MAAM,gBAAgB,GAAG,eAAe,CAAC,UAAU,CAAC,CAAC;IACrD,OAAO,sBAAsB,CAAC,gBAAgB,CAAC,WAAW,EAAE,eAAe,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { getSimpleSchema, walkFieldSchema } from \"@fluidframework/tree/internal\";\nimport type { ImplicitFieldSchema, SimpleTreeSchema } from \"@fluidframework/tree/internal\";\n\nimport type { BindableSchema } from \"./methodBinding.js\";\nimport { getExposedMethods, isBindableSchema } from \"./methodBinding.js\";\nimport { getExposedProperties } from \"./propertyBinding.js\";\nimport {\n\trenderSchemaTypeScript,\n\ttype SchemaTypeScriptRenderResult,\n} from \"./renderSchemaTypeScript.js\";\nimport { getOrCreate } from \"./utils.js\";\n\nconst promptSchemaCache = new WeakMap<SimpleTreeSchema, SchemaTypeScriptRenderResult>();\n\n/**\n * Generates TypeScript declarations for the schemas reachable from the provided root field schema.\n */\nexport function generateEditTypesForPrompt(\n\trootSchema: ImplicitFieldSchema,\n\tschema: SimpleTreeSchema,\n): SchemaTypeScriptRenderResult {\n\treturn getOrCreate(promptSchemaCache, schema, () =>\n\t\tbuildPromptSchemaDescription(rootSchema),\n\t);\n}\n\nfunction buildPromptSchemaDescription(\n\trootSchema: ImplicitFieldSchema,\n): SchemaTypeScriptRenderResult {\n\tconst bindableSchemas = new Map<string, BindableSchema>();\n\n\twalkFieldSchema(rootSchema, {\n\t\tnode: (node) => {\n\t\t\tif (isBindableSchema(node)) {\n\t\t\t\tbindableSchemas.set(node.identifier, node);\n\n\t\t\t\tconst exposedMethods = getExposedMethods(node);\n\t\t\t\tfor (const referenced of exposedMethods.referencedTypes) {\n\t\t\t\t\tif (isBindableSchema(referenced)) {\n\t\t\t\t\t\tbindableSchemas.set(referenced.identifier, referenced);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst exposedProperties = getExposedProperties(node);\n\t\t\t\tfor (const referenced of exposedProperties.referencedTypes) {\n\t\t\t\t\tif (isBindableSchema(referenced)) {\n\t\t\t\t\t\tbindableSchemas.set(referenced.identifier, referenced);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t});\n\n\tconst simpleTreeSchema = getSimpleSchema(rootSchema);\n\treturn renderSchemaTypeScript(simpleTreeSchema.definitions, bindableSchemas);\n}\n"]}
|
package/lib/utils.d.ts
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
import type { ImplicitFieldSchema
|
|
5
|
+
import type { ImplicitFieldSchema } from "@fluidframework/tree";
|
|
6
6
|
import type { InsertableContent, TreeNode, TreeNodeSchema } from "@fluidframework/tree/alpha";
|
|
7
|
-
import { z } from "zod";
|
|
8
7
|
/**
|
|
9
8
|
* Subset of Map interface.
|
|
10
9
|
*
|
|
@@ -80,33 +79,17 @@ export declare function getFriendlyName(schema: TreeNodeSchema): string;
|
|
|
80
79
|
*/
|
|
81
80
|
export declare function isNamedSchema(schemaIdentifier: string): boolean;
|
|
82
81
|
/**
|
|
83
|
-
* Returns the unqualified name of a schema
|
|
84
|
-
*
|
|
85
|
-
*
|
|
82
|
+
* Returns the unqualified, sanitized Typescript-safe name of a schema
|
|
83
|
+
* Examples:
|
|
84
|
+
* - `"my.scope.MyNode"` returns `"MyNode"`
|
|
85
|
+
* - `"my.scope.MyNode-2"` returns `"MyNode_2"`
|
|
86
|
+
* - `"my.scope.MyNode!"` returns `"MyNode_"`
|
|
87
|
+
* @remarks
|
|
88
|
+
* - Removes all characters before the last dot in the schema name.
|
|
89
|
+
* - Sanitizes the remainder into a valid Typescript identifier
|
|
90
|
+
* - If there is a dot in a user's schema name, this might produce unexpected results.
|
|
86
91
|
*/
|
|
87
92
|
export declare function unqualifySchema(schemaIdentifier: string): string;
|
|
88
|
-
/**
|
|
89
|
-
* Details about the properties of a TypeScript schema represented as Zod.
|
|
90
|
-
*/
|
|
91
|
-
export interface SchemaDetails {
|
|
92
|
-
hasHelperMethods: boolean;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Returns the TypeScript source code corresponding to a Zod schema. The schema is supplied as an object where each
|
|
96
|
-
* property provides a name for an associated Zod type. The return value is a string containing the TypeScript source
|
|
97
|
-
* code corresponding to the schema. Each property of the schema object is emitted as a named `interface` or `type`
|
|
98
|
-
* declaration for the associated type and is referenced by that name in the emitted type declarations. Other types
|
|
99
|
-
* referenced in the schema are emitted in their structural form.
|
|
100
|
-
* @param schema - A schema object where each property provides a name for an associated Zod type.
|
|
101
|
-
* @param details - Optional details about the schema. The fields will be set according to the details in the given schema.
|
|
102
|
-
* @returns The TypeScript source code corresponding to the schema.
|
|
103
|
-
*/
|
|
104
|
-
export declare function getZodSchemaAsTypeScript(schema: Record<string, z.ZodType>, details?: SchemaDetails): string;
|
|
105
|
-
/**
|
|
106
|
-
* Create a Zod schema for a SharedTree schema class.
|
|
107
|
-
* @alpha
|
|
108
|
-
*/
|
|
109
|
-
export declare function instanceOf<T extends TreeNodeSchemaClass>(schema: T): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>>;
|
|
110
93
|
/**
|
|
111
94
|
* Adds all (optionally filtered) schemas reachable from the given schema to the given set.
|
|
112
95
|
* @returns The set of schemas added (same as the `schemas` parameter, if supplied).
|
package/lib/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAChE,OAAO,KAAK,EACX,iBAAiB,EACjB,QAAQ,EACR,cAAc,EAEd,MAAM,4BAA4B,CAAC;AASpC;;;;GAIG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC,EAAE,CAAC;IAC9B,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;IAC3B,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;CAC5B;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,CAE3C;AAED;;;;;;;GAOG;AACH,wBAAiB,WAAW,CAAC,CAAC,EAAE,CAAC,EAChC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GACd,gBAAgB,CAAC,CAAC,CAAC,CAIrB;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,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AAG,MAAM,CASpD"}
|
package/lib/utils.js
CHANGED
|
@@ -2,16 +2,11 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
6
|
-
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
7
5
|
import { assert } from "@fluidframework/core-utils/internal";
|
|
8
6
|
import { isFluidHandle } from "@fluidframework/runtime-utils";
|
|
9
7
|
import { UsageError } from "@fluidframework/telemetry-utils/internal";
|
|
10
|
-
import { ArrayNodeSchema, MapNodeSchema,
|
|
8
|
+
import { ArrayNodeSchema, MapNodeSchema, RecordNodeSchema, TreeAlpha, } from "@fluidframework/tree/alpha";
|
|
11
9
|
import { NodeKind, normalizeFieldSchema } from "@fluidframework/tree/internal";
|
|
12
|
-
import { z } from "zod";
|
|
13
|
-
import { FunctionWrapper } from "./methodBinding.js";
|
|
14
|
-
import { PropertyDef } from "./propertyBinding.js";
|
|
15
10
|
/**
|
|
16
11
|
* TBD
|
|
17
12
|
*/
|
|
@@ -131,366 +126,29 @@ export function isNamedSchema(schemaIdentifier) {
|
|
|
131
126
|
return /(?:Array|Map|Record)<\["(.*)"]>/.exec(schemaIdentifier) === null;
|
|
132
127
|
}
|
|
133
128
|
/**
|
|
134
|
-
* Returns the unqualified name of a schema
|
|
135
|
-
*
|
|
136
|
-
*
|
|
129
|
+
* Returns the unqualified, sanitized Typescript-safe name of a schema
|
|
130
|
+
* Examples:
|
|
131
|
+
* - `"my.scope.MyNode"` returns `"MyNode"`
|
|
132
|
+
* - `"my.scope.MyNode-2"` returns `"MyNode_2"`
|
|
133
|
+
* - `"my.scope.MyNode!"` returns `"MyNode_"`
|
|
134
|
+
* @remarks
|
|
135
|
+
* - Removes all characters before the last dot in the schema name.
|
|
136
|
+
* - Sanitizes the remainder into a valid Typescript identifier
|
|
137
|
+
* - If there is a dot in a user's schema name, this might produce unexpected results.
|
|
137
138
|
*/
|
|
138
139
|
export function unqualifySchema(schemaIdentifier) {
|
|
139
140
|
// Get the unqualified name by removing the scope (everything before the last dot).
|
|
140
141
|
const matches = /[^.]+$/.exec(schemaIdentifier);
|
|
141
|
-
|
|
142
|
-
|
|
142
|
+
const unqualifiedName = matches === null ? schemaIdentifier : matches[0];
|
|
143
|
+
let sanitizedName = unqualifiedName;
|
|
144
|
+
// Replace invalid characters with "_".
|
|
145
|
+
sanitizedName = sanitizedName.replace(/[^\w$]/g, "_");
|
|
146
|
+
// If the first character is a number, prefix it with "_".
|
|
147
|
+
if (!/^[$A-Z_a-z]/.test(sanitizedName)) {
|
|
148
|
+
sanitizedName = `_${sanitizedName}`;
|
|
143
149
|
}
|
|
144
|
-
return
|
|
150
|
+
return sanitizedName;
|
|
145
151
|
}
|
|
146
|
-
// TODO: yuck, this entire file has too many statics. we should rewrite it as a generic zod schema walk.
|
|
147
|
-
let detailsI = {
|
|
148
|
-
hasHelperMethods: false,
|
|
149
|
-
};
|
|
150
|
-
/**
|
|
151
|
-
* Returns the TypeScript source code corresponding to a Zod schema. The schema is supplied as an object where each
|
|
152
|
-
* property provides a name for an associated Zod type. The return value is a string containing the TypeScript source
|
|
153
|
-
* code corresponding to the schema. Each property of the schema object is emitted as a named `interface` or `type`
|
|
154
|
-
* declaration for the associated type and is referenced by that name in the emitted type declarations. Other types
|
|
155
|
-
* referenced in the schema are emitted in their structural form.
|
|
156
|
-
* @param schema - A schema object where each property provides a name for an associated Zod type.
|
|
157
|
-
* @param details - Optional details about the schema. The fields will be set according to the details in the given schema.
|
|
158
|
-
* @returns The TypeScript source code corresponding to the schema.
|
|
159
|
-
*/
|
|
160
|
-
export function getZodSchemaAsTypeScript(schema, details) {
|
|
161
|
-
detailsI = details ?? { hasHelperMethods: false };
|
|
162
|
-
let result = "";
|
|
163
|
-
let startOfLine = true;
|
|
164
|
-
let indent = 0;
|
|
165
|
-
const entries = [...Object.entries(schema)];
|
|
166
|
-
const namedTypes = new Map(entries.map(([name, type]) => [getTypeIdentity(type), name]));
|
|
167
|
-
for (const [name, type] of entries) {
|
|
168
|
-
if (result) {
|
|
169
|
-
appendNewLine();
|
|
170
|
-
}
|
|
171
|
-
const description = type._def.description;
|
|
172
|
-
if (description !== undefined && description !== "") {
|
|
173
|
-
for (const comment of description.split("\n")) {
|
|
174
|
-
append(`// ${comment}`);
|
|
175
|
-
appendNewLine();
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (getTypeKind(type) === z.ZodFirstPartyTypeKind.ZodObject) {
|
|
179
|
-
append(`interface ${name} `);
|
|
180
|
-
appendObjectType(type);
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
append(`type ${name} = `);
|
|
184
|
-
appendTypeDefinition(type);
|
|
185
|
-
append(";");
|
|
186
|
-
}
|
|
187
|
-
appendNewLine();
|
|
188
|
-
}
|
|
189
|
-
return result;
|
|
190
|
-
function append(s) {
|
|
191
|
-
if (startOfLine) {
|
|
192
|
-
result += " ".repeat(indent);
|
|
193
|
-
startOfLine = false;
|
|
194
|
-
}
|
|
195
|
-
result += s;
|
|
196
|
-
}
|
|
197
|
-
function appendNewLine() {
|
|
198
|
-
append("\n");
|
|
199
|
-
startOfLine = true;
|
|
200
|
-
}
|
|
201
|
-
function appendType(type, minPrecedence = 2 /* TypePrecedence.Object */) {
|
|
202
|
-
const name = namedTypes.get(getTypeIdentity(type));
|
|
203
|
-
if (name === undefined) {
|
|
204
|
-
const parenthesize = getTypePrecendece(type) < minPrecedence;
|
|
205
|
-
if (parenthesize)
|
|
206
|
-
append("(");
|
|
207
|
-
appendTypeDefinition(type);
|
|
208
|
-
if (parenthesize)
|
|
209
|
-
append(")");
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
append(name);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
function appendTypeDefinition(type) {
|
|
216
|
-
switch (getTypeKind(type)) {
|
|
217
|
-
case z.ZodFirstPartyTypeKind.ZodString: {
|
|
218
|
-
return append("string");
|
|
219
|
-
}
|
|
220
|
-
case z.ZodFirstPartyTypeKind.ZodNumber: {
|
|
221
|
-
return append("number");
|
|
222
|
-
}
|
|
223
|
-
case z.ZodFirstPartyTypeKind.ZodBoolean: {
|
|
224
|
-
return append("boolean");
|
|
225
|
-
}
|
|
226
|
-
case z.ZodFirstPartyTypeKind.ZodDate: {
|
|
227
|
-
return append("Date");
|
|
228
|
-
}
|
|
229
|
-
case z.ZodFirstPartyTypeKind.ZodUndefined: {
|
|
230
|
-
return append("undefined");
|
|
231
|
-
}
|
|
232
|
-
case z.ZodFirstPartyTypeKind.ZodNull: {
|
|
233
|
-
return append("null");
|
|
234
|
-
}
|
|
235
|
-
case z.ZodFirstPartyTypeKind.ZodUnknown: {
|
|
236
|
-
return append("unknown");
|
|
237
|
-
}
|
|
238
|
-
case z.ZodFirstPartyTypeKind.ZodArray: {
|
|
239
|
-
return appendArrayType(type);
|
|
240
|
-
}
|
|
241
|
-
case z.ZodFirstPartyTypeKind.ZodObject: {
|
|
242
|
-
return appendObjectType(type);
|
|
243
|
-
}
|
|
244
|
-
case z.ZodFirstPartyTypeKind.ZodUnion: {
|
|
245
|
-
return appendUnionOrIntersectionTypes(type._def.options, 0 /* TypePrecedence.Union */);
|
|
246
|
-
}
|
|
247
|
-
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
|
|
248
|
-
return appendUnionOrIntersectionTypes([...type._def.options.values()], 0 /* TypePrecedence.Union */);
|
|
249
|
-
}
|
|
250
|
-
case z.ZodFirstPartyTypeKind.ZodIntersection: {
|
|
251
|
-
return appendUnionOrIntersectionTypes([
|
|
252
|
-
type._def.left,
|
|
253
|
-
type._def.right,
|
|
254
|
-
], 1 /* TypePrecedence.Intersection */);
|
|
255
|
-
}
|
|
256
|
-
case z.ZodFirstPartyTypeKind.ZodTuple: {
|
|
257
|
-
return appendTupleType(type);
|
|
258
|
-
}
|
|
259
|
-
case z.ZodFirstPartyTypeKind.ZodRecord: {
|
|
260
|
-
return appendRecordType(type);
|
|
261
|
-
}
|
|
262
|
-
case z.ZodFirstPartyTypeKind.ZodMap: {
|
|
263
|
-
return appendMapType(type);
|
|
264
|
-
}
|
|
265
|
-
case z.ZodFirstPartyTypeKind.ZodLiteral: {
|
|
266
|
-
return appendLiteral(type._def.value);
|
|
267
|
-
}
|
|
268
|
-
case z.ZodFirstPartyTypeKind.ZodEnum: {
|
|
269
|
-
return append(type._def.values.map((value) => JSON.stringify(value)).join(" | "));
|
|
270
|
-
}
|
|
271
|
-
case z.ZodFirstPartyTypeKind.ZodOptional: {
|
|
272
|
-
return appendUnionOrIntersectionTypes([type._def.innerType, z.undefined()], 0 /* TypePrecedence.Union */);
|
|
273
|
-
}
|
|
274
|
-
case z.ZodFirstPartyTypeKind.ZodReadonly: {
|
|
275
|
-
return appendReadonlyType(type);
|
|
276
|
-
}
|
|
277
|
-
case z.ZodFirstPartyTypeKind.ZodEffects: {
|
|
278
|
-
// Currently, this only handles schema class instances, but there are other cases in which a ZodEffects could theoretically be used.
|
|
279
|
-
if (instanceOfs.has(type)) {
|
|
280
|
-
const objectNodeSchema = instanceOfs.get(type);
|
|
281
|
-
if (objectNodeSchema === undefined) {
|
|
282
|
-
throw new UsageError(`Unsupported zod effects type when transforming class method: ${getTypeKind(type)}`);
|
|
283
|
-
}
|
|
284
|
-
return append(getFriendlyName(objectNodeSchema));
|
|
285
|
-
}
|
|
286
|
-
throw new Error("Unsupported zod effects type. Did you use z.instanceOf? Use ExposedMethods.instanceOf function to reference schema classes in methods.");
|
|
287
|
-
}
|
|
288
|
-
case z.ZodFirstPartyTypeKind.ZodVoid: {
|
|
289
|
-
return append("void");
|
|
290
|
-
}
|
|
291
|
-
case z.ZodFirstPartyTypeKind.ZodLazy: {
|
|
292
|
-
return appendType(type._def.getter());
|
|
293
|
-
}
|
|
294
|
-
default: {
|
|
295
|
-
throw new UsageError(`Unsupported type when transforming class method: ${getTypeKind(type)}`);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
function appendBoundMethods(boundType) {
|
|
300
|
-
// eslint-disable-next-line prefer-const
|
|
301
|
-
for (let [name, type] of Object.entries(boundType._def.shape())) {
|
|
302
|
-
// Special handling of methods on objects
|
|
303
|
-
const method = type.method;
|
|
304
|
-
if (method !== undefined && method instanceof FunctionWrapper) {
|
|
305
|
-
detailsI.hasHelperMethods = true;
|
|
306
|
-
append(name);
|
|
307
|
-
append("(");
|
|
308
|
-
let first = true;
|
|
309
|
-
for (const [argName, argType] of method.args) {
|
|
310
|
-
if (!first)
|
|
311
|
-
append(", ");
|
|
312
|
-
if (getTypeKind(argType) === z.ZodFirstPartyTypeKind.ZodOptional) {
|
|
313
|
-
append(`${argName}?: `);
|
|
314
|
-
appendType(argType._def.innerType, 2 /* TypePrecedence.Object */);
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
append(`${argName}: `);
|
|
318
|
-
appendType(argType);
|
|
319
|
-
}
|
|
320
|
-
first = false;
|
|
321
|
-
}
|
|
322
|
-
if (method.rest !== null) {
|
|
323
|
-
if (!first)
|
|
324
|
-
append(", ");
|
|
325
|
-
append("...rest: ");
|
|
326
|
-
appendType(method.rest, 2 /* TypePrecedence.Object */);
|
|
327
|
-
append("[]");
|
|
328
|
-
}
|
|
329
|
-
append(`): `);
|
|
330
|
-
appendType(method.returns, 2 /* TypePrecedence.Object */);
|
|
331
|
-
append(";");
|
|
332
|
-
if (method.description !== undefined) {
|
|
333
|
-
append(` // ${method.description}`);
|
|
334
|
-
}
|
|
335
|
-
appendNewLine();
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
function appendBoundProperties(type) {
|
|
340
|
-
const property = type.property;
|
|
341
|
-
if (!(property instanceof PropertyDef)) {
|
|
342
|
-
if (type.description !== undefined && type.description !== "") {
|
|
343
|
-
append(` // ${type.description}`);
|
|
344
|
-
}
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
if (property.readOnly === true) {
|
|
348
|
-
append(" // readonly");
|
|
349
|
-
}
|
|
350
|
-
if (property.description !== undefined && property.description !== "") {
|
|
351
|
-
append(` - ${property.description}`);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
function appendArrayType(arrayType) {
|
|
355
|
-
appendType(arrayType._def.type, 2 /* TypePrecedence.Object */);
|
|
356
|
-
append("[]");
|
|
357
|
-
}
|
|
358
|
-
function appendObjectType(objectType) {
|
|
359
|
-
append("{");
|
|
360
|
-
appendNewLine();
|
|
361
|
-
indent++;
|
|
362
|
-
// eslint-disable-next-line prefer-const
|
|
363
|
-
for (let [name, type] of Object.entries(objectType._def.shape())) {
|
|
364
|
-
const method = type.method;
|
|
365
|
-
if (method === undefined || !(method instanceof FunctionWrapper)) {
|
|
366
|
-
append(name);
|
|
367
|
-
if (getTypeKind(type) === z.ZodFirstPartyTypeKind.ZodOptional) {
|
|
368
|
-
append("?");
|
|
369
|
-
type = type._def.innerType;
|
|
370
|
-
}
|
|
371
|
-
append(": ");
|
|
372
|
-
appendType(type);
|
|
373
|
-
append(";");
|
|
374
|
-
appendBoundProperties(type);
|
|
375
|
-
appendNewLine();
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
appendBoundMethods(objectType);
|
|
379
|
-
indent--;
|
|
380
|
-
append("}");
|
|
381
|
-
}
|
|
382
|
-
function appendUnionOrIntersectionTypes(types, minPrecedence) {
|
|
383
|
-
let first = true;
|
|
384
|
-
for (const type of types) {
|
|
385
|
-
if (!first)
|
|
386
|
-
append(minPrecedence === 1 /* TypePrecedence.Intersection */ ? " & " : " | ");
|
|
387
|
-
appendType(type, minPrecedence);
|
|
388
|
-
first = false;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
function appendTupleType(tupleType) {
|
|
392
|
-
append("[");
|
|
393
|
-
let first = true;
|
|
394
|
-
for (const type of tupleType._def.items) {
|
|
395
|
-
if (!first)
|
|
396
|
-
append(", ");
|
|
397
|
-
if (getTypeKind(type) === z.ZodFirstPartyTypeKind.ZodOptional) {
|
|
398
|
-
appendType(type._def.innerType, 2 /* TypePrecedence.Object */);
|
|
399
|
-
append("?");
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
appendType(type);
|
|
403
|
-
}
|
|
404
|
-
first = false;
|
|
405
|
-
}
|
|
406
|
-
const rest = tupleType._def.rest;
|
|
407
|
-
if (rest !== null) {
|
|
408
|
-
if (!first)
|
|
409
|
-
append(", ");
|
|
410
|
-
append("...");
|
|
411
|
-
appendType(rest, 2 /* TypePrecedence.Object */);
|
|
412
|
-
append("[]");
|
|
413
|
-
}
|
|
414
|
-
append("]");
|
|
415
|
-
}
|
|
416
|
-
function appendRecordType(recordType) {
|
|
417
|
-
append("Record<");
|
|
418
|
-
appendType(recordType._def.keyType);
|
|
419
|
-
append(", ");
|
|
420
|
-
appendType(recordType._def.valueType);
|
|
421
|
-
append(">");
|
|
422
|
-
}
|
|
423
|
-
function appendMapType(mapType) {
|
|
424
|
-
append("Map<");
|
|
425
|
-
appendType(mapType._def.keyType);
|
|
426
|
-
append(", ");
|
|
427
|
-
appendType(mapType._def.valueType);
|
|
428
|
-
append(">");
|
|
429
|
-
}
|
|
430
|
-
function appendLiteral(value) {
|
|
431
|
-
append(typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
|
432
|
-
? JSON.stringify(value)
|
|
433
|
-
: "any");
|
|
434
|
-
}
|
|
435
|
-
function appendReadonlyType(readonlyType) {
|
|
436
|
-
append("Readonly<");
|
|
437
|
-
appendType(readonlyType._def.innerType);
|
|
438
|
-
append(">");
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
function getTypeKind(type) {
|
|
442
|
-
return type._def.typeName;
|
|
443
|
-
}
|
|
444
|
-
function getTypeIdentity(type) {
|
|
445
|
-
switch (getTypeKind(type)) {
|
|
446
|
-
case z.ZodFirstPartyTypeKind.ZodObject: {
|
|
447
|
-
return type._def.shape();
|
|
448
|
-
}
|
|
449
|
-
case z.ZodFirstPartyTypeKind.ZodEnum: {
|
|
450
|
-
return type._def.values;
|
|
451
|
-
}
|
|
452
|
-
case z.ZodFirstPartyTypeKind.ZodUnion: {
|
|
453
|
-
return type._def.options;
|
|
454
|
-
}
|
|
455
|
-
default: {
|
|
456
|
-
return type;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
var TypePrecedence;
|
|
461
|
-
(function (TypePrecedence) {
|
|
462
|
-
TypePrecedence[TypePrecedence["Union"] = 0] = "Union";
|
|
463
|
-
TypePrecedence[TypePrecedence["Intersection"] = 1] = "Intersection";
|
|
464
|
-
TypePrecedence[TypePrecedence["Object"] = 2] = "Object";
|
|
465
|
-
})(TypePrecedence || (TypePrecedence = {}));
|
|
466
|
-
function getTypePrecendece(type) {
|
|
467
|
-
switch (getTypeKind(type)) {
|
|
468
|
-
case z.ZodFirstPartyTypeKind.ZodEnum:
|
|
469
|
-
case z.ZodFirstPartyTypeKind.ZodUnion:
|
|
470
|
-
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
|
|
471
|
-
return 0 /* TypePrecedence.Union */;
|
|
472
|
-
}
|
|
473
|
-
case z.ZodFirstPartyTypeKind.ZodIntersection: {
|
|
474
|
-
return 1 /* TypePrecedence.Intersection */;
|
|
475
|
-
}
|
|
476
|
-
default: {
|
|
477
|
-
return 2 /* TypePrecedence.Object */;
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* Create a Zod schema for a SharedTree schema class.
|
|
483
|
-
* @alpha
|
|
484
|
-
*/
|
|
485
|
-
export function instanceOf(schema) {
|
|
486
|
-
if (!(schema instanceof ObjectNodeSchema)) {
|
|
487
|
-
throw new UsageError(`${schema.identifier} must be an instance of ObjectNodeSchema.`);
|
|
488
|
-
}
|
|
489
|
-
const effect = z.instanceof(schema);
|
|
490
|
-
instanceOfs.set(effect, schema);
|
|
491
|
-
return effect;
|
|
492
|
-
}
|
|
493
|
-
const instanceOfs = new WeakMap();
|
|
494
152
|
/**
|
|
495
153
|
* Adds all (optionally filtered) schemas reachable from the given schema to the given set.
|
|
496
154
|
* @returns The set of schemas added (same as the `schemas` parameter, if supplied).
|