UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

171 lines 7.32 kB
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } export class EnumType { constructor(value) { _defineProperty(this, "value", void 0); this.value = value; } } export class VariableType { constructor(value) { _defineProperty(this, "value", void 0); this.value = value; } toJSON() { return "$".concat(this.value); } } export var configFields = ['__args', '__alias', '__aliasFor', '__variables', '__directives', '__on', '__all_on', '__typeName', '__name']; export function stringifyArgs(obj_from_json) { if (obj_from_json instanceof EnumType) { return obj_from_json.value; } // variables should be prefixed with dollar sign and not quoted else if (obj_from_json instanceof VariableType) { return "$".concat(obj_from_json.value); } // Cheers to Derek: https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties else if (typeof obj_from_json !== 'object' || obj_from_json === null) { // not an object, stringify using native function return JSON.stringify(obj_from_json); } else if (Array.isArray(obj_from_json)) { return "[".concat(obj_from_json.map(item => stringifyArgs(item)).join(', '), "]"); } // Implements recursive object serialization according to JSON spec // but without quotes around the keys. var props = Object.keys(obj_from_json).map(key => "".concat(key, ": ").concat(stringifyArgs(obj_from_json[key]))).join(', '); return "{".concat(props, "}"); } export function buildArgs(argsObj) { var args = []; for (var argName in argsObj) { args.push("".concat(argName, ": ").concat(stringifyArgs(argsObj[argName]))); } return args.join(', '); } function buildVariables(varsObj) { var args = []; for (var varName in varsObj) { args.push("$".concat(varName, ": ").concat(varsObj[varName])); } return args.join(', '); } function buildDirectives(dirsObj) { var directiveName = Object.keys(dirsObj)[0]; var directiveValue = dirsObj[directiveName]; if (typeof directiveValue === 'boolean' || typeof directiveValue === 'object' && Object.keys(directiveValue).length === 0) { return directiveName; } else if (typeof directiveValue === 'object') { var args = []; for (var argName in directiveValue) { var argVal = stringifyArgs(directiveValue[argName]).replace(/"/g, ''); args.push("".concat(argName, ": ").concat(argVal)); } return "".concat(directiveName, "(").concat(args.join(', '), ")"); } else { throw new Error("Unsupported type for directive: ".concat(typeof directiveValue, ". Types allowed: object, boolean.\n") + "Offending object: ".concat(JSON.stringify(dirsObj))); } } function getIndent(level) { return Array(level * 2 + 1).join(' '); } function filterNonConfigFields(fieldName, ignoreFields) { // Returns true if fieldName is not a 'configField'. return configFields.indexOf(fieldName) == -1 && ignoreFields.indexOf(fieldName) == -1; } function convertQuery(node, level, output, options) { Object.keys(node).filter(key => filterNonConfigFields(key, options.ignoreFields)).forEach(key => { var value = node[key]; if (typeof value === 'object') { if (Array.isArray(value)) { value = value.find(item => item && typeof item === 'object'); if (!value) { output.push(["".concat(key), level]); return; } } var fieldCount = Object.keys(value).filter(keyCount => filterNonConfigFields(keyCount, options.ignoreFields)).length; var subFields = fieldCount > 0; var argsExist = typeof value.__args === 'object' && Object.keys(value.__args).length > 0; var directivesExist = typeof value.__directives === 'object'; var fullFragmentsExist = value.__all_on instanceof Array; var partialFragmentsExist = typeof value.__on === 'object'; var token = "".concat(key); if (typeof value.__name === 'string') { token = "".concat(token, " ").concat(value.__name); } if (typeof value.__aliasFor === 'string') { token = "".concat(token, ": ").concat(value.__aliasFor); } if (typeof value.__variables === 'object' && Object.keys(value.__variables).length > 0) { token = "".concat(token, " (").concat(buildVariables(value.__variables), ")"); } else if (argsExist || directivesExist) { var argsStr = ''; var dirsStr = ''; if (directivesExist) { dirsStr = Object.entries(value.__directives).map(item => "@".concat(buildDirectives({ [item[0]]: item[1] }))).join(' '); } if (argsExist) { argsStr = "(".concat(buildArgs(value.__args), ")"); } var spacer = directivesExist && argsExist ? ' ' : ''; token = "".concat(token, " ").concat(argsStr).concat(spacer).concat(dirsStr); } output.push([token + (subFields || partialFragmentsExist || fullFragmentsExist ? ' {' : ''), level]); convertQuery(value, level + 1, output, options); if (fullFragmentsExist) { value.__all_on.forEach(fullFragment => { output.push(["...".concat(fullFragment), level + 1]); }); } if (partialFragmentsExist) { var inlineFragments = value.__on instanceof Array ? value.__on : [value.__on]; inlineFragments.forEach(inlineFragment => { var name = inlineFragment.__typeName; output.push(["... on ".concat(name, " {"), level + 1]); convertQuery(inlineFragment, level + 2, output, options); output.push(['}', level + 1]); }); } if (subFields || partialFragmentsExist || fullFragmentsExist) { output.push(['}', level]); } } else if (options.includeFalsyKeys === true || value) { output.push(["".concat(key), level]); } }); } export function objectToQuery(query) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (!query || typeof query != 'object') { throw new Error('query object not specified'); } if (Object.keys(query).length == 0) { throw new Error('query object has no data'); } if (!(options.ignoreFields instanceof Array)) { options.ignoreFields = []; } var queryLines = []; convertQuery(query, 0, queryLines, options); var output = ''; queryLines.forEach(_ref => { var [line, level] = _ref; if (options.pretty) { if (output) { output += '\n'; } output += getIndent(level) + line; } else { if (output) { output += ' '; } output += line; } }); return output; } //# sourceMappingURL=objectToQuery.js.map