@sinclair/typebox-codegen
Advanced tools
| import { TypeBoxModel } from './model'; | ||
| export declare namespace ModelToEffect { | ||
| function Generate(model: TypeBoxModel): string; | ||
| } |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox-codegen | ||
| The MIT License (MIT) | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ModelToEffect = void 0; | ||
| const index_1 = require("../common/index"); | ||
| const Types = require("@sinclair/typebox"); | ||
| // -------------------------------------------------------------------------- | ||
| // ModelToEffect | ||
| // -------------------------------------------------------------------------- | ||
| var ModelToEffect; | ||
| (function (ModelToEffect) { | ||
| function IsDefined(value) { | ||
| return value !== undefined; | ||
| } | ||
| function Type(schema, type) { | ||
| return type; | ||
| } | ||
| function Any(schema) { | ||
| return Type(schema, `ES.Any`); | ||
| } | ||
| function Array(schema) { | ||
| const items = Visit(schema.items); | ||
| const buffer = []; | ||
| buffer.push(`ES.Array(${items})`); | ||
| if (IsDefined(schema.minItems)) | ||
| buffer.push(`.pipe(ES.minItems(${schema.minItems}))`); | ||
| if (IsDefined(schema.maxItems)) | ||
| buffer.push(`.pipe(ES.maxItems(${schema.maxItems}))`); | ||
| return Type(schema, buffer.join(``)); | ||
| } | ||
| function BigInt(schema) { | ||
| return Type(schema, `ES.BigInt`); | ||
| } | ||
| function Boolean(schema) { | ||
| return Type(schema, `ES.Boolean`); | ||
| } | ||
| function Date(schema) { | ||
| return Type(schema, `ES.Date`); | ||
| } | ||
| function Constructor(schema) { | ||
| return UnsupportedType(schema); | ||
| } | ||
| function Function(schema) { | ||
| return UnsupportedType(schema); | ||
| } | ||
| function Integer(schema) { | ||
| const buffer = []; | ||
| buffer.push(`ES.Int`); | ||
| if (IsDefined(schema.minimum)) | ||
| buffer.push(`.pipe(ES.greaterThanOrEqualTo(${schema.minimum}))`); | ||
| if (IsDefined(schema.maximum)) | ||
| buffer.push(`.pipe(ES.lessThanOrEqualTo(${schema.maximum}))`); | ||
| if (IsDefined(schema.exclusiveMinimum)) | ||
| buffer.push(`.pipe(ES.greaterThan(${schema.exclusiveMinimum}))`); | ||
| if (IsDefined(schema.exclusiveMaximum)) | ||
| buffer.push(`.pipe(ES.lessThan(${schema.exclusiveMaximum}))`); | ||
| if (IsDefined(schema.multipleOf)) | ||
| buffer.push(`.pipe(ES.multipleOf(${schema.multipleOf}))`); | ||
| return Type(schema, buffer.join(``)); | ||
| } | ||
| function Intersect(schema) { | ||
| return Object(Types.Type.Composite(schema.allOf)); // No Intersect Type? | ||
| } | ||
| function Literal(schema) { | ||
| const value = typeof schema.const === `string` ? `'${schema.const}'` : schema.const; | ||
| return Type(schema, `ES.Literal(${value})`); | ||
| } | ||
| function Never(schema) { | ||
| return Type(schema, `ES.Never`); | ||
| } | ||
| function Null(schema) { | ||
| return Type(schema, `ES.Null`); | ||
| } | ||
| function String(schema) { | ||
| const buffer = []; | ||
| buffer.push(`ES.String`); | ||
| if (IsDefined(schema.maxLength)) | ||
| buffer.push(`.pipe(ES.maxLength(${schema.maxLength}))`); | ||
| if (IsDefined(schema.minLength)) | ||
| buffer.push(`.pipe(ES.minLength(${schema.minLength}))`); | ||
| return Type(schema, buffer.join(``)); | ||
| } | ||
| function Number(schema) { | ||
| const buffer = []; | ||
| buffer.push(`ES.Number`); | ||
| if (IsDefined(schema.minimum)) | ||
| buffer.push(`.pipe(ES.greaterThanOrEqualTo(${schema.minimum}))`); | ||
| if (IsDefined(schema.maximum)) | ||
| buffer.push(`.pipe(ES.lessThanOrEqualTo(${schema.maximum}))`); | ||
| if (IsDefined(schema.exclusiveMinimum)) | ||
| buffer.push(`.pipe(ES.greaterThan(${schema.exclusiveMinimum}))`); | ||
| if (IsDefined(schema.exclusiveMaximum)) | ||
| buffer.push(`.pipe(ES.lessThan(${schema.exclusiveMaximum}))`); | ||
| if (IsDefined(schema.multipleOf)) | ||
| buffer.push(`.pipe(ES.multipleOf(${schema.multipleOf}))`); | ||
| return Type(schema, buffer.join(``)); | ||
| } | ||
| function Object(schema) { | ||
| // prettier-ignore | ||
| const properties = globalThis.Object.entries(schema.properties).map(([key, value]) => { | ||
| const optional = Types.TypeGuard.IsOptional(value); | ||
| const property = index_1.PropertyEncoder.Encode(key); | ||
| // prettier-ignore | ||
| return (optional | ||
| ? `${property}: ES.optional(${Visit(value)})` | ||
| : `${property}: ${Visit(value)}`); | ||
| }).join(`,`); | ||
| const buffer = []; | ||
| buffer.push(`ES.Struct({\n${properties}\n})`); | ||
| return Type(schema, buffer.join(``)); | ||
| } | ||
| function Promise(schema) { | ||
| return UnsupportedType(schema); | ||
| } | ||
| function Record(schema) { | ||
| for (const [key, value] of globalThis.Object.entries(schema.patternProperties)) { | ||
| const type = Visit(value); | ||
| return `ES.Record(${key === `^(0|[1-9][0-9]*)$` ? `ES.Number` : `ES.String`}, ${type})`; | ||
| } | ||
| return UnsupportedType(schema); | ||
| } | ||
| function Ref(schema) { | ||
| if (!reference_map.has(schema.$ref)) | ||
| return UnsupportedType(schema); // throw new ModelToZodNonReferentialType(schema.$ref!) | ||
| return schema.$ref; | ||
| } | ||
| function This(schema) { | ||
| return `${Type(schema, `ES.Any /* unsupported */`)}`; | ||
| } | ||
| function Tuple(schema) { | ||
| if (schema.items === undefined) | ||
| return `[]`; | ||
| const items = schema.items.map((schema) => Visit(schema)).join(`, `); | ||
| return Type(schema, `ES.Tuple(${items})`); | ||
| } | ||
| function TemplateLiteral(schema) { | ||
| return Type(schema, `ES.String.pipe(ES.pattern(/${schema.pattern}/))`); | ||
| } | ||
| function UInt8Array(schema) { | ||
| return Type(schema, `ES.Uint8Array`); | ||
| } | ||
| function Undefined(schema) { | ||
| return Type(schema, `ES.Undefined()`); | ||
| } | ||
| function Union(schema) { | ||
| return Type(schema, `ES.Union(${schema.anyOf.map((schema) => Visit(schema)).join(`, `)})`); | ||
| } | ||
| function Unknown(schema) { | ||
| return Type(schema, `ES.Unknown`); | ||
| } | ||
| function Void(schema) { | ||
| return Type(schema, `ES.Void`); | ||
| } | ||
| function UnsupportedType(schema) { | ||
| return `${Type(schema, `ES.Any /* unsupported */`)}`; | ||
| } | ||
| function Visit(schema) { | ||
| if (schema.$id !== undefined) | ||
| reference_map.set(schema.$id, schema); | ||
| if (schema.$id !== undefined && emitted_set.has(schema.$id)) | ||
| return schema.$id; | ||
| if (Types.TypeGuard.IsAny(schema)) | ||
| return Any(schema); | ||
| if (Types.TypeGuard.IsArray(schema)) | ||
| return Array(schema); | ||
| if (Types.TypeGuard.IsBigInt(schema)) | ||
| return BigInt(schema); | ||
| if (Types.TypeGuard.IsBoolean(schema)) | ||
| return Boolean(schema); | ||
| if (Types.TypeGuard.IsDate(schema)) | ||
| return Date(schema); | ||
| if (Types.TypeGuard.IsConstructor(schema)) | ||
| return Constructor(schema); | ||
| if (Types.TypeGuard.IsFunction(schema)) | ||
| return Function(schema); | ||
| if (Types.TypeGuard.IsInteger(schema)) | ||
| return Integer(schema); | ||
| if (Types.TypeGuard.IsIntersect(schema)) | ||
| return Intersect(schema); | ||
| if (Types.TypeGuard.IsLiteral(schema)) | ||
| return Literal(schema); | ||
| if (Types.TypeGuard.IsNever(schema)) | ||
| return Never(schema); | ||
| if (Types.TypeGuard.IsNull(schema)) | ||
| return Null(schema); | ||
| if (Types.TypeGuard.IsNumber(schema)) | ||
| return Number(schema); | ||
| if (Types.TypeGuard.IsObject(schema)) | ||
| return Object(schema); | ||
| if (Types.TypeGuard.IsPromise(schema)) | ||
| return Promise(schema); | ||
| if (Types.TypeGuard.IsRecord(schema)) | ||
| return Record(schema); | ||
| if (Types.TypeGuard.IsRef(schema)) | ||
| return Ref(schema); | ||
| if (Types.TypeGuard.IsString(schema)) | ||
| return String(schema); | ||
| if (Types.TypeGuard.IsTemplateLiteral(schema)) | ||
| return TemplateLiteral(schema); | ||
| if (Types.TypeGuard.IsThis(schema)) | ||
| return This(schema); | ||
| if (Types.TypeGuard.IsTuple(schema)) | ||
| return Tuple(schema); | ||
| if (Types.TypeGuard.IsUint8Array(schema)) | ||
| return UInt8Array(schema); | ||
| if (Types.TypeGuard.IsUndefined(schema)) | ||
| return Undefined(schema); | ||
| if (Types.TypeGuard.IsUnion(schema)) | ||
| return Union(schema); | ||
| if (Types.TypeGuard.IsUnknown(schema)) | ||
| return Unknown(schema); | ||
| if (Types.TypeGuard.IsVoid(schema)) | ||
| return Void(schema); | ||
| return UnsupportedType(schema); | ||
| } | ||
| function Collect(schema) { | ||
| return [...Visit(schema)].join(``); | ||
| } | ||
| function GenerateType(model, schema, references) { | ||
| const output = []; | ||
| for (const reference of references) { | ||
| if (reference.$id === undefined) | ||
| return UnsupportedType(schema); | ||
| reference_map.set(reference.$id, reference); | ||
| } | ||
| const type = Collect(schema); | ||
| output.push(`export type ${schema.$id} = ET.Type<typeof ${schema.$id}>`); | ||
| output.push(`export const ${schema.$id || `T`} = ${index_1.Formatter.Format(type)}`); | ||
| if (schema.$id) | ||
| emitted_set.add(schema.$id); | ||
| return output.join('\n'); | ||
| } | ||
| const reference_map = new Map(); | ||
| const recursive_set = new Set(); | ||
| const emitted_set = new Set(); | ||
| function Generate(model) { | ||
| reference_map.clear(); | ||
| recursive_set.clear(); | ||
| emitted_set.clear(); | ||
| const buffer = []; | ||
| buffer.push(`import { Schema as ET } from '@effect/schema/Schema'`); | ||
| buffer.push(`import { Schema as ES } from '@effect/schema'`); | ||
| buffer.push(``); | ||
| for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) { | ||
| buffer.push(GenerateType(model, type, model.types)); | ||
| } | ||
| return index_1.Formatter.Format(buffer.join('\n')); | ||
| } | ||
| ModelToEffect.Generate = Generate; | ||
| })(ModelToEffect || (exports.ModelToEffect = ModelToEffect = {})); |
+1
-0
| export * from './model-to-arktype'; | ||
| export * from './model-to-effect'; | ||
| export * from './model-to-io-ts'; | ||
@@ -3,0 +4,0 @@ export * from './model-to-javascript'; |
+1
-0
@@ -43,2 +43,3 @@ "use strict"; | ||
| __exportStar(require("./model-to-arktype"), exports); | ||
| __exportStar(require("./model-to-effect"), exports); | ||
| __exportStar(require("./model-to-io-ts"), exports); | ||
@@ -45,0 +46,0 @@ __exportStar(require("./model-to-javascript"), exports); |
+1
-1
| { | ||
| "name": "@sinclair/typebox-codegen", | ||
| "version": "0.10.1", | ||
| "version": "0.10.2", | ||
| "description": "Code Generation Tools for TypeBox", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+3
-2
@@ -49,5 +49,5 @@ <div align='center'> | ||
| TypeBox-Codegen is a code generation tool that converts TypeScript types into TypeBox types as well as several other schema and library representations. It works by extracting structural type information from the TypeScript compiler then remaps it into a TypeBox representation (referred to as a Model). This Model can then be passed on to various code generators which generate through TypeBox schema introspection. | ||
| TypeBox-Codegen is a code generation tool that transforms TypeScript types into TypeBox types as well as several other schema and library representations. It works by mapping structural type information from the TypeScript compiler into a TypeBox model. This model is then passed on to code generators which generate via TypeBox schema introspection. | ||
| The library contains code transformations for libraries such as [zod](https://github.com/colinhacks/zod), [io-ts](https://github.com/gcanti/io-ts), [arktype](https://github.com/arktypeio/arktype) and [valibot](https://github.com/fabian-hiller/valibot), assert function generators for JavaScript and TypeScript (derived from TypeBox's TypeCompiler) as well as JSON Schema derived from TypeBox's raw schematics. | ||
| The library contains code transformations for libraries such as [zod](https://github.com/colinhacks/zod), [effect](https://github.com/Effect-TS/effect), [arktype](https://github.com/arktypeio/arktype), [io-ts](https://github.com/gcanti/io-ts) and [valibot](https://github.com/fabian-hiller/valibot), assertion generators for JavaScript and TypeScript as well as Json Schema derived from TypeBox's raw schematics. | ||
@@ -110,2 +110,3 @@ [TypeBox Workbench Example](https://sinclairzx81.github.io/typebox-workbench/) | ||
| console.log('Model To ArkType', Codegen.ModelToArkType.Generate(model)) | ||
| console.log('Model To Effect', Codegen.ModelToEffect.Generate(model)) | ||
| ``` | ||
@@ -112,0 +113,0 @@ |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
161631
7.48%45
4.65%3544
8.38%129
0.78%22
10%