@spec2ts/jsonschema
Version:
Utility to convert JSON Schemas to Typescript using TypeScript native compiler
69 lines (68 loc) • 3.54 kB
TypeScript
import * as ts from "typescript";
import $RefParser from "@apidevtools/json-schema-ref-parser";
import type { JSONSchema4, JSONSchema4TypeName, JSONSchema6, JSONSchema6TypeName, JSONSchema7, JSONSchema7TypeName } from "json-schema";
export { JSONSchema4, JSONSchema6, JSONSchema7 };
export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
export type JSONSchemaTypeName = JSONSchema4TypeName | JSONSchema6TypeName | JSONSchema7TypeName;
export type JSONSchemaDefinition = JSONSchema | boolean;
export type JSONSchemaTuple = JSONSchema & {
prefixItems: JSONSchemaDefinition[];
};
export type JSONReference = {
$ref: string;
};
export interface ParserOptions {
cwd?: string;
avoidAny?: boolean;
enableDate?: boolean | "strict" | "lax";
parseReference?: (ref: ParsedReference, context: ParserContext) => void;
}
export interface ParserContext {
schema: JSONSchema;
$refs: $RefParser["$refs"];
refs: Record<string, ParsedReference>;
parseReference: (ref: ParsedReference, context: ParserContext) => void;
imports: ts.ImportDeclaration[];
aliases: Array<ts.TypeAliasDeclaration | ts.InterfaceDeclaration>;
options: ParserOptions;
names: Record<string, number>;
refPrefix?: string;
}
export interface ParsedReference {
$ref: string;
name: string;
schema: JSONSchema;
node: ts.TypeReferenceNode;
isRemote: boolean;
isLocal: boolean;
path?: string;
}
export interface ReferenceDetails<T> {
id: string;
path: string;
schema: T;
}
/**
* Creates a type node from a given schema.
* Delegates to getBaseTypeFromSchema internally and optionally adds a union with null.
*/
export declare function getTypeFromSchema(schema: JSONSchema | JSONReference | undefined, context: ParserContext): ts.TypeNode;
/** This is the very core of the Schema to TS conversion - it takes a schema and returns the appropriate type. */
export declare function getBaseTypeFromSchema(schema: JSONSchema | JSONReference | undefined, context: ParserContext): ts.TypeNode;
/** Recursively creates a type literal with the given props. */
export declare function getTypeFromProperties(props: Record<string, JSONSchemaDefinition | JSONReference>, required: string[] | null | undefined, additionalProperties: boolean | JSONSchema | JSONReference | null | undefined, context: ParserContext): ts.TypeNode;
/** Creates types from definitions. */
export declare function parseDefinitions(schema: JSONSchema | JSONReference | undefined, context: ParserContext): void;
export declare function parseReference(obj: JSONReference, context: ParserContext): ParsedReference;
export declare function getReferenceType(obj: JSONReference, context: ParserContext): ts.TypeReferenceNode;
export declare function resolveReference<T>(obj: T | JSONReference, context: ParserContext): T;
export declare function resolveReferenceDetails<T>(obj: JSONReference, context: ParserContext): ReferenceDetails<T>;
export declare function isReference(obj: unknown): obj is JSONReference;
export declare function createContext(schema: JSONSchema, options: ParserOptions): Promise<ParserContext>;
export declare function createRefContext(ref: ParsedReference, context: ParserContext): ParserContext;
export declare function getSchemaName(schema: JSONSchema, path?: string): string;
export declare function getAnyType(context: ParserContext): ts.TypeNode;
export declare function isNullable(schema?: JSONSchema & {
nullable?: boolean;
}): boolean;
export declare function pascalCase(name: string): string;