From 5b14573a5c7864f7eb2578d9fe3ed6a47c3fdd11 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Sat, 13 Apr 2024 23:19:02 -0700 Subject: [PATCH] cleanup --- src/context.ts | 42 ++++++++++++++++++++++++++++++++++ src/index.ts | 62 ++------------------------------------------------ src/types.ts | 18 +++++++++++++++ 3 files changed, 62 insertions(+), 60 deletions(-) create mode 100644 src/context.ts create mode 100644 src/types.ts diff --git a/src/context.ts b/src/context.ts new file mode 100644 index 0000000..f3712ac --- /dev/null +++ b/src/context.ts @@ -0,0 +1,42 @@ +import type { JSONSchema } from "./types"; + +export interface SchemaTSOptions { + useSingleQuotes: boolean; + useCamelCase: boolean; +} + +export interface SchemaTSContextI { + options: SchemaTSOptions; + root: JSONSchema; + schema: JSONSchema; + parents: JSONSchema[] +} + + +export class SchemaTSContext implements SchemaTSContextI { + options: SchemaTSOptions; + root: JSONSchema; + schema: JSONSchema; + parents: JSONSchema[]; + + constructor( + options: SchemaTSOptions, + root: JSONSchema, + schema: JSONSchema, + parents: JSONSchema[] = [] + ) { + this.options = options; + this.schema = schema; + this.root = root; + this.parents = parents; + } + + // Clone the context with the option to add a new parent + clone(newParent: JSONSchema): SchemaTSContext { + // Create a new array for parents to avoid mutation of the original array + const newParents = [...this.parents, newParent]; + return new SchemaTSContext(this.options, this.root, this.schema, newParents); + } +} + +export const defaultOptions: SchemaTSOptions = { useSingleQuotes: true, useCamelCase: false }; diff --git a/src/index.ts b/src/index.ts index 4992919..3bfa9d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,68 +1,10 @@ import generate from "@babel/generator"; import * as t from "@babel/types"; +import { defaultOptions, SchemaTSContext, type SchemaTSOptions } from "./context"; +import type { JSONSchema, JSONSchemaProperty } from "./types"; import { toCamelCase, toPascalCase } from "./utils"; -interface JSONSchema { - title: string; - properties?: { [key: string]: JSONSchemaProperty }; - required?: string[]; - type?: string; - items?: JSONSchema; - $defs?: { [key: string]: JSONSchema }; - additionalProperties?: boolean | JSONSchemaProperty; -} - -type JSONSchemaProperty = { - type?: string; - properties?: { [key: string]: JSONSchemaProperty }; - items?: JSONSchemaProperty; - required?: string[]; - $ref?: string; - additionalProperties?: boolean | JSONSchemaProperty; -}; - -interface SchemaTSOptions { - useSingleQuotes: boolean; - useCamelCase: boolean; -} - -interface SchemaTSContextI { - options: SchemaTSOptions; - root: JSONSchema; - schema: JSONSchema; - parents: JSONSchema[] -} - - -class SchemaTSContext implements SchemaTSContextI { - options: SchemaTSOptions; - root: JSONSchema; - schema: JSONSchema; - parents: JSONSchema[]; - - constructor( - options: SchemaTSOptions, - root: JSONSchema, - schema: JSONSchema, - parents: JSONSchema[] = [] - ) { - this.options = options; - this.schema = schema; - this.root = root; - this.parents = parents; - } - - // Clone the context with the option to add a new parent - clone(newParent: JSONSchema): SchemaTSContext { - // Create a new array for parents to avoid mutation of the original array - const newParents = [...this.parents, newParent]; - return new SchemaTSContext(this.options, this.root, this.schema, newParents); - } -} - -const defaultOptions: SchemaTSOptions = { useSingleQuotes: true, useCamelCase: false }; - export function generateTypeScript(schema: JSONSchema, options?: SchemaTSOptions): string { const interfaces = []; const opts = options || defaultOptions; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..4d97499 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,18 @@ +export interface JSONSchema { + title: string; + properties?: { [key: string]: JSONSchemaProperty }; + required?: string[]; + type?: string; + items?: JSONSchema; + $defs?: { [key: string]: JSONSchema }; + additionalProperties?: boolean | JSONSchemaProperty; +} + +export type JSONSchemaProperty = { + type?: string; + properties?: { [key: string]: JSONSchemaProperty }; + items?: JSONSchemaProperty; + required?: string[]; + $ref?: string; + additionalProperties?: boolean | JSONSchemaProperty; +};