UNPKG

@tsed/schema

Version:
55 lines 1.6 kB
import "../components/index.js"; import { getValue } from "@tsed/core"; import { SpecTypes } from "../domain/SpecTypes.js"; import { execMapper } from "../registries/JsonSchemaMapperContainer.js"; import { getJsonEntityStore } from "./getJsonEntityStore.js"; /** * @ignore */ const CACHE_KEY = "$cache:schemes"; /** * @ignore */ function getKey(options) { return JSON.stringify(options); } /** * @ignore */ function get(entity, options) { const cache = entity.store.get(CACHE_KEY) || new Map(); const key = getKey(options); if (!cache.has(key)) { const schema = execMapper("schema", [entity.schema], options); if (Object.keys(getValue(options, "components.schemas", {})).length) { schema.definitions = options.components.schemas; } cache.set(key, schema); } entity.store.set(CACHE_KEY, cache); return cache.get(key); } export function getJsonSchema(model, options = {}) { const entity = getJsonEntityStore(model); const specType = options.specType || SpecTypes.JSON; options = { endpoint: true, groups: [], inlineEnums: specType === SpecTypes.JSON, ...options, specType, components: { schemas: {} } }; if (entity.decoratorType === "parameter") { options = { ...options, genericTypes: entity.nestedGenerics[0], nestedGenerics: entity.nestedGenerics, groups: entity.parameter?.groups }; } return get(entity, options); } //# sourceMappingURL=getJsonSchema.js.map