UNPKG

@tsed/schema

Version:
62 lines (61 loc) 1.79 kB
import "../components/index.js"; import { getValue, isClass, isPlainObject, nameOf } from "@tsed/core"; import { JsonParameterStore } from "../domain/JsonParameterStore.js"; import { SpecTypes } from "../domain/SpecTypes.js"; import { execMapper } from "../registries/JsonSchemaMapperContainer.js"; import { getJsonEntityStore } from "./getJsonEntityStore.js"; /** * @ignore */ const CACHES = new Map(); /** * @ignore */ function getKey(options) { return JSON.stringify(options, (key, value) => { if (value && !isPlainObject(value) && isClass(value)) { return nameOf(value); } return value; }); } /** * @ignore */ function get(model, options) { const cache = CACHES.get(model) || new Map(); CACHES.set(model, cache); const key = getKey(options); if (!cache.has(key)) { const entity = getJsonEntityStore(model); let mapper = "schema"; if (entity instanceof JsonParameterStore) { options = { ...options, root: true, groups: entity.schema.getGroups() }; mapper = "item"; } const schema = execMapper(mapper, [entity.schema], options); if (Object.keys(getValue(options, "components.schemas", {})).length) { schema.definitions = options.components.schemas; } cache.set(key, schema); } return cache.get(key); } export function getJsonSchema(model, options = {}) { const specType = options.specType || SpecTypes.JSON; options = { endpoint: true, groups: [], inlineEnums: specType === SpecTypes.JSON, ...options, specType, components: { schemas: {} } }; return get(model, options); }