UNPKG

@tsed/schema

Version:
109 lines (108 loc) 3 kB
import { __decorate } from "tslib"; import { DecoratorTypes } from "@tsed/core"; import { JsonEntityComponent } from "../decorators/config/jsonEntityComponent.js"; import { JsonEntityStore } from "./JsonEntityStore.js"; import { JsonSchema } from "./JsonSchema.js"; /** * Store for class-level metadata and schema information. * * JsonClassStore manages metadata for classes decorated with schema decorators like * `@Model()`, `@Generics()`, `@DiscriminatorKey()`, etc. It serves as the root container * for all class-level schema information and coordinates child stores for properties * and methods. * * ### Responsibilities * * - **Schema Generation**: Creates and maintains the JsonSchema for the class * - **Children Coordination**: Manages child PropertyStore and MethodStore instances * - **Route Configuration**: Stores base path for controller classes * - **Inheritance**: Handles schema inheritance from parent classes * - **OpenAPI Integration**: Generates component schemas for OpenAPI specifications * * ### Usage * * ```typescript * // Get class store * const classStore = JsonEntityStore.from(MyClass); * * // Access class schema * const schema = classStore.schema; * * // Get all properties * const properties = classStore.children; * * // Check if it's a controller with a path * const path = classStore.path; * ``` * * ### Class Store Hierarchy * * The class store acts as the root of the store hierarchy: * ``` * JsonClassStore (User class) * ├─ JsonPropertyStore (name property) * ├─ JsonPropertyStore (email property) * └─ JsonMethodStore (validate method) * ├─ JsonParameterStore (param 0) * └─ JsonParameterStore (param 1) * ``` * * ### Schema Structure * * For a class like: * ```typescript * class User { * @Property() * name: string; * * @Property() * email: string; * } * ``` * * The class store generates: * ```json * { * "type": "object", * "properties": { * "name": {"type": "string"}, * "email": {"type": "string"} * } * } * ``` * * ### Controller Path * * For controller classes decorated with `@Controller("/users")`, the store * maintains the base path which is used for route generation. * * @public */ let JsonClassStore = class JsonClassStore extends JsonEntityStore { constructor() { super(...arguments); /** * List of children JsonEntityStore (properties or methods or params) */ this.children = new Map(); } get path() { return this.store.get("path", "/"); } set path(path) { this.store.set("path", path); } build() { if (!this._type) { this.buildType(this.target); } this._type = this._type || Object; this._schema = JsonSchema.from({ type: this.type }); } }; JsonClassStore = __decorate([ JsonEntityComponent(DecoratorTypes.CLASS) ], JsonClassStore); export { JsonClassStore };