UNPKG

@antmjs/rapper

Version:

rapper, use http api as a function

254 lines (253 loc) 9.63 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); var json_schema_to_typescript_1 = require("json-schema-to-typescript"); var JSON5 = __importStar(require("json5")); var lodash_1 = require("lodash"); function inferArraySchema(p, childProperties, common) { var rule = (p.rule && p.rule.trim()) || ''; if (Object.keys(childProperties).length !== 0) { return [ p.name, __assign(__assign({ type: 'array', items: { type: 'object', properties: childProperties, required: common['required'], additionalProperties: false, } }, common), { required: [] }), ]; } else if (['+1', '1'].includes(rule) && p.value) { try { var arr = JSON5.parse(p.value); if (Array.isArray(arr) && arr.length) { var type = (0, lodash_1.chain)(arr) .map(function (e) { return typeof e; }) .uniq() .value(); return [ p.name, __assign({ type: type }, common), ]; } else { return [ p.name, __assign({ type: ['string', 'number', 'boolean', 'object'] }, common), ]; } } catch (error) { return [ p.name, __assign({ type: ['string', 'number', 'boolean', 'object'] }, common), ]; } } else if (rule === '' && p.value) { try { var v = JSON5.parse(p.value); if (Array.isArray(v)) { if (!v.length) { return [ p.name, __assign({ type: 'array' }, common), ]; } var type = (0, lodash_1.chain)(v) .map(function (e) { return typeof e; }) .uniq() .value(); return [ p.name, __assign({ type: 'array', items: { type: type, } }, common), ]; } else { var type = typeof v; return [ p.name, __assign({ type: 'array', items: { type: type, } }, common), ]; } } catch (error) { return [ p.name, __assign({ type: 'array' }, common), ]; } } else { return [ p.name, __assign({ type: 'array' }, common), ]; } } var removeComment = function (str) { return str.replace(/\/\*|\*\//g, ''); }; function getRestfulPlaceHolders(itf) { var urlSplit = itf.url.split('/'); var restfulPlaceHolders = []; for (var i = 0; i < urlSplit.length; ++i) { var part = urlSplit[i]; var matchKeys = part.match(/(?:\{(.*)\}|\:(.*))/); if (!matchKeys) continue; var key = matchKeys[1] || matchKeys[2]; restfulPlaceHolders.push(key); } return restfulPlaceHolders; } function interfaceToJSONSchema(itf, scope) { var properties = itf.properties.filter(function (p) { return p.scope === scope; }); properties = __spreadArray(__spreadArray([], properties, true), [ { name: 'dummyroot', parentId: -2, id: -1, scope: scope, type: 'object', }, ], false); if (scope === 'request') { var placeHolders = getRestfulPlaceHolders(itf); properties = __spreadArray(__spreadArray([], properties, true), placeHolders.map(function (name, index) { return ({ name: name, parentId: -1, id: index + 100, scope: scope, type: 'string', }); }), true); } function findChildProperties(parentId) { return (0, lodash_1.chain)(properties) .filter(function (p) { return p.parentId === parentId; }) .map(function (p) { var type = p.type.toLowerCase().replace(/regexp|function/, 'string'); var childProperties = findChildProperties(p.id); var childItfs = properties.filter(function (x) { return x.parentId === p.id; }); var common = { additionalProperties: false, required: scope === 'request' ? childItfs.filter(function (e) { return e.required; }).map(function (e) { return e.name; }) : childItfs.map(function (e) { return e.name; }), }; common.description = ''; if ((p === null || p === void 0 ? void 0 : p.description) !== undefined && p.description !== '' && p.description !== null) { common.description = "".concat(removeComment(p.description)); } if ((p === null || p === void 0 ? void 0 : p.rule) !== undefined && p.rule !== '' && p.rule !== null) { common.description += "\n@rule ".concat(removeComment(p.rule)); } if ((p === null || p === void 0 ? void 0 : p.value) !== undefined && p.value !== '' && p.value !== null) { common.description += "\n@value ".concat(removeComment(p.value).replace(/^@/, '/@')); } if (['string', 'number'].includes(type) && p.value) { var enumArr = []; var regResult = /^@pick\(([\s\S]+)\)$/.exec(p.value); try { if (regResult) { var result = regResult[1]; enumArr = result && eval(result); } } catch (err) { } if (Array.isArray(enumArr) && enumArr.length) { common.enum = enumArr; } } if (['string', 'number', 'integer', 'boolean', 'null'].includes(type)) { return [ p.name, __assign({ type: type }, common), ]; } else if (type === 'object') { return [ p.name, __assign({ type: type, properties: childProperties }, common), ]; } else if (type === 'array') { return inferArraySchema(p, childProperties, common); } else { return [ p.name, __assign({ type: ['string', 'number', 'boolean', 'object'] }, common), ]; } }) .fromPairs() .value(); } var propertyChildren = findChildProperties(-2); var root = propertyChildren['dummyroot']; if (Object.keys(root.properties).length === 1 && (root.properties._root_ || root.properties.__root__)) { var _root_ = root.properties._root_ || root.properties.__root__; if (_root_.type === 'array') { return _root_; } } return root; } function convert(itf) { var reqJSONSchema = interfaceToJSONSchema(itf, 'request'); var resJSONSchema = interfaceToJSONSchema(itf, 'response'); var options = __assign(__assign({}, json_schema_to_typescript_1.DEFAULT_OPTIONS), { bannerComment: '' }); return Promise.all([ (0, json_schema_to_typescript_1.compile)(reqJSONSchema, 'Req', options), (0, json_schema_to_typescript_1.compile)(resJSONSchema, 'Res', options), ]); } exports.default = convert;