UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

118 lines (117 loc) 3.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseTSFyValue = parseTSFyValue; var _utils = require("@backland/utils"); var _GraphType = require("../GraphType/GraphType"); var _ObjectType = require("../ObjectType"); var _fieldTypes = require("../fields/fieldTypes"); var _tsfy = require("./tsfy"); async function parseTSFyValue(rootValue, context) { rootValue = (0, _utils.proxyRealValue)(rootValue); // const typeDescription = (0, _utils.describeType)(rootValue); const identifier = (0, _tsfy.getTSFyIdentifier)(rootValue); const hash = typeDescription.hash(); const existing = context.refs[hash]; const currentRef = (0, _tsfy.createTSfyRef)(hash, identifier); if (existing !== undefined) { existing.count++; } else { context.refs[hash] = currentRef; } if (context.config.customParser) { const parsed = await context.config.customParser({ context, typeDescription, hash, identifier, existing, currentRef, value: rootValue }); if (parsed !== undefined) return parsed; } const { typename, native } = typeDescription; if (native && typename !== 'Object') { const body = typeDescription.toString(); currentRef.result = body; return currentRef; } if (_ObjectType.ObjectType.is(rootValue)) { const child = await parseTSFyValue(rootValue.definition, context); currentRef.parts = ['ObjectType<', ...(0, _utils.ensureArray)(child), '>']; return currentRef; } if (_GraphType.GraphType.is(rootValue)) { const child = await parseTSFyValue(rootValue.definition, context); currentRef.parts = ['GraphType<', ...(0, _utils.ensureArray)(child), '>']; return currentRef; } await (async () => { switch (typename) { case 'Function': { context.header[hash] = 'export type AnyFunction = (...args: any[]) => any; '; currentRef.result = 'AnyFunction'; return currentRef; } case 'Array': { if (!Array.isArray(rootValue)) throw _utils.noop; if (!rootValue.length) { currentRef.result = '[]'; return currentRef; } const lastIndex = rootValue.length - 1; const child = await (0, _utils.awaitSync)(rootValue.map(async (element, index) => { const part = await parseTSFyValue(element, context); const res = (0, _utils.ensureArray)(part); if (index !== lastIndex) return [...res, ', ']; return res; })); currentRef.parts = ['[', ...(0, _utils.ensureArray)(child), ']']; return currentRef; } case 'Object': { const pairs = Object.entries(rootValue); if (!pairs.length) { currentRef.result = '{}'; return currentRef; } currentRef.parts.push('{'); await (0, _utils.awaitSync)(pairs.map(async ([key, value]) => { if (key === '__dschm__') return; if ((value === null || value === void 0 ? void 0 : value.hidden) === true && (0, _fieldTypes.isFieldTypeName)(value.type)) { return; } const valueRes = await parseTSFyValue(value, context); currentRef.parts.push(`${JSON.stringify(key)}:`, valueRes, ','); })); currentRef.parts.push('}'); return currentRef; } default: { const described = (0, _utils.describeType)(rootValue); const { native, typename } = described; if (!native) { currentRef.result = `any /*${typename}*/`; } else { currentRef.result = typename; } return currentRef; } } })(); return currentRef; } //# sourceMappingURL=parseTSFyValue.js.map