Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Apr 14, 2024
1 parent 644cf44 commit 5b14573
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 60 deletions.
42 changes: 42 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -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 };
62 changes: 2 additions & 60 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 5b14573

Please sign in to comment.