@tsed/schema
Version:
JsonSchema module for Ts.ED Framework
168 lines (167 loc) • 6.01 kB
TypeScript
import { DecoratorTypes, Store, Type } from "@tsed/core";
import type { JsonClassStore } from "./JsonClassStore.js";
import type { JsonMethodStore } from "./JsonMethodStore.js";
import type { JsonParameterStore } from "./JsonParameterStore.js";
import type { JsonPropertyStore } from "./JsonPropertyStore.js";
import type { JsonSchema } from "./JsonSchema.js";
/**
* @ignore
*/
export declare const JsonEntitiesContainer: Map<DecoratorTypes, Type<JsonEntityStore>>;
/**
* Configuration options for creating a JsonEntityStore.
*
* @public
*/
export interface JsonEntityStoreOptions {
decoratorType: DecoratorTypes;
target: Type<any>;
propertyKey?: string | symbol;
index?: number;
descriptor?: any;
type?: Type<any>;
collectionType?: Type<any>;
beforeMiddlewares?: Function[];
middlewares?: Function[];
afterMiddlewares?: Function[];
[key: string]: any;
}
/**
* Base class for storing metadata about decorated entities (classes, properties, methods, parameters).
*
* JsonEntityStore is the foundation of Ts.ED's metadata system, managing schema information
* and type metadata for decorated entities. It serves as the parent class for specialized
* stores (JsonClassStore, JsonPropertyStore, JsonMethodStore, JsonParameterStore).
*
* ### Entity Types
*
* Different decorator types create different store subclasses:
* - **Class decorators**: Create JsonClassStore instances
* - **Property decorators**: Create JsonPropertyStore instances
* - **Method decorators**: Create JsonMethodStore instances
* - **Parameter decorators**: Create JsonParameterStore instances
*
* ### Key Responsibilities
*
* - **Schema Management**: Each store maintains a JsonSchema for the decorated entity
* - **Type Resolution**: Resolves TypeScript types, including collections and generics
* - **Metadata Storage**: Stores decorator metadata using Ts.ED's Store system
* - **Inheritance**: Manages parent-child relationships between entities
*
* ### Usage
*
* Stores are typically accessed via static `from()` methods:
*
* ```typescript
* // Get store for a class
* const classStore = JsonEntityStore.from(MyClass);
*
* // Get store for a property
* const propStore = JsonEntityStore.from(MyClass, "propertyName");
*
* // Get store for a method parameter
* const paramStore = JsonEntityStore.from(MyClass, "methodName", 0);
* ```
*
* ### Architecture
*
* The store system creates a hierarchical structure:
* - ClassStore contains PropertyStore and MethodStore children
* - MethodStore contains ParameterStore children
* - Each store has an associated JsonSchema
*
* @public
*/
export declare abstract class JsonEntityStore implements JsonEntityStoreOptions {
/**
* Original property key decorated by the decorator
*/
readonly propertyKey: string | symbol;
/**
* Alias of the property
*/
readonly propertyName: string;
/**
* Parameter index
*/
readonly index: number;
/**
* Method's descriptor
*/
readonly descriptor: PropertyDescriptor;
/**
* Decorator type used to declare the JsonSchemaStore.
*/
readonly decoratorType: DecoratorTypes;
token: Type<any>;
readonly store: Store;
readonly isStore = true;
readonly parent: JsonEntityStore;
readonly target: Type<any>;
[key: string]: any;
constructor(options: JsonEntityStoreOptions);
/**
* Type of the collection (Array, Map, Set, etc...)
*/
private _collectionType;
get collectionType(): Type<any> | undefined;
set collectionType(value: Type<any>);
/**
*
*/
protected _type: Type<any>;
get type(): Type<any> | any;
/**
* Get original type without transformation
* @param value
*/
set type(value: Type<any> | any);
/**
* Ref to JsonSchema
*/
protected _schema: JsonSchema;
/**
* Return the JsonSchema
*/
get schema(): JsonSchema;
/**
* Return the class name of the entity.
* @returns {string}
*/
get targetName(): string;
get isCollection(): boolean;
get isArray(): boolean;
get discriminatorAncestor(): JsonEntityStore | undefined;
get isPrimitive(): boolean;
get isDate(): boolean;
get isObject(): boolean;
get isClass(): boolean;
/**
* Return the itemSchema computed type. if the type is a function used for recursive model, the function will be called to
* get the right type.
*/
get computedType(): Type<any>;
get itemSchema(): JsonSchema;
get parentSchema(): JsonSchema;
get isDiscriminatorChild(): boolean;
get path(): string;
set path(path: string);
static from<T extends JsonClassStore = JsonClassStore>(target: Type<any>): T;
static from<T extends JsonPropertyStore = JsonPropertyStore>(target: Type<any> | any, propertyKey: string | symbol): T;
static from<T extends JsonParameterStore = JsonParameterStore>(target: Type<any> | any, propertyKey: string | symbol, index: number): T;
static from<T extends JsonMethodStore = JsonMethodStore>(target: Type<any> | any, propertyKey: string | symbol, descriptor: PropertyDescriptor): T;
static from<T extends JsonEntityStore = JsonEntityStore>(...args: any[]): T;
static fromMethod<T extends JsonMethodStore = JsonMethodStore>(target: any, propertyKey: string | symbol): T;
static get(target: Type<any>, propertyKey: string | symbol, descriptor?: any): JsonEntityStore;
isGetterOnly(): boolean | undefined;
get<T = any>(key: string, defaultValue?: any): T;
set(key: string, value?: any): Store;
toString(): string;
getBestType(): any;
is(input: DecoratorTypes.CLASS): this is JsonClassStore;
is(input: DecoratorTypes.PROP): this is JsonPropertyStore;
is(input: DecoratorTypes.METHOD): this is JsonMethodStore;
is(input: DecoratorTypes.PARAM): this is JsonParameterStore;
protected abstract build(): void;
protected buildType(type: any): void;
}