leaf-converter
Advanced tools
Comparing version 1.10.0 to 1.11.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="1.11.0"></a> | ||
# [1.11.0](https://github.com/forsigner/leaf-converter/compare/v1.10.0...v1.11.0) (2018-08-13) | ||
### Features | ||
* use ts ([da2126d](https://github.com/forsigner/leaf-converter/commit/da2126d)) | ||
<a name="1.10.0"></a> | ||
@@ -7,0 +17,0 @@ # [1.10.0](https://github.com/forsigner/leaf-converter/compare/v1.9.5...v1.10.0) (2018-03-27) |
@@ -1,7 +0,27 @@ | ||
'use strict'; | ||
exports.keywords = ['maxLength', 'minLength', 'pattern', 'enum', 'minimum', 'maximum', | ||
// 'required', | ||
'maxProperties', 'minProperties', 'additionalProperties', 'minItems', 'maxItems', 'uniqueItems']; | ||
exports.numKeywords = ['maxLength', 'minLength', 'minimum', 'maximum', 'maxProperties', 'minProperties', 'minItems', 'maxItems']; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.keywords = [ | ||
'maxLength', | ||
'minLength', | ||
'pattern', | ||
'enum', | ||
'minimum', | ||
'maximum', | ||
// 'required', | ||
'maxProperties', | ||
'minProperties', | ||
'additionalProperties', | ||
'minItems', | ||
'maxItems', | ||
'uniqueItems', | ||
]; | ||
exports.numKeywords = [ | ||
'maxLength', | ||
'minLength', | ||
'minimum', | ||
'maximum', | ||
'maxProperties', | ||
'minProperties', | ||
'minItems', | ||
'maxItems', | ||
]; |
'use strict'; | ||
var schemaToObject = require('./schema-to-object'); | ||
var schemaToStruct = require('./schema-to-struct'); | ||
var objectToSchema = require('./object-to-schema'); | ||
var structToSchema = require('./struct-to-schema'); | ||
var jsonCommentParser = require('./json-comment-parser'); | ||
var mdToSchema = require('./md-to-schema'); | ||
var objectToMd = require('./object-to-md'); | ||
module.exports = { | ||
schemaToObject: schemaToObject, | ||
objectToSchema: objectToSchema, | ||
structToSchema: structToSchema, | ||
jsonCommentParser: jsonCommentParser, | ||
mdToSchema: mdToSchema, | ||
objectToMd: objectToMd, | ||
schemaToStruct: schemaToStruct | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var schema_to_object_1 = require("./schema-to-object"); | ||
exports.schemaToObject = schema_to_object_1.default; | ||
var schema_to_struct_1 = require("./schema-to-struct"); | ||
exports.schemaToStruct = schema_to_struct_1.default; | ||
var object_to_schema_1 = require("./object-to-schema"); | ||
exports.objectToSchema = object_to_schema_1.default; | ||
var struct_to_schema_1 = require("./struct-to-schema"); | ||
exports.structToSchema = struct_to_schema_1.default; | ||
var json_comment_parser_1 = require("./json-comment-parser"); | ||
exports.jsonCommentParser = json_comment_parser_1.default; | ||
var md_to_schema_1 = require("./md-to-schema"); | ||
exports.mdToSchema = md_to_schema_1.default; | ||
var object_to_md_1 = require("./object-to-md"); | ||
exports.objectToMd = object_to_md_1.default; | ||
exports.default = { | ||
schemaToObject: schema_to_object_1.default, | ||
objectToSchema: object_to_schema_1.default, | ||
structToSchema: struct_to_schema_1.default, | ||
jsonCommentParser: json_comment_parser_1.default, | ||
mdToSchema: md_to_schema_1.default, | ||
objectToMd: object_to_md_1.default, | ||
schemaToStruct: schema_to_struct_1.default, | ||
}; |
'use strict'; | ||
var stringify = require('./stringify'); | ||
var parse = require('./parse'); | ||
module.exports = { | ||
stringify: stringify, | ||
parse: parse | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var stringify_1 = require("./stringify"); | ||
exports.stringify = stringify_1.default; | ||
var parse_1 = require("./parse"); | ||
exports.parse = parse_1.default; | ||
exports.default = { | ||
stringify: stringify_1.default, | ||
parse: parse_1.default, | ||
}; |
'use strict'; | ||
var acorn = require('acorn'); | ||
var estraverse = require('estraverse'); | ||
module.exports = parse; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var acorn = require("acorn"); | ||
var estraverse = require("estraverse"); | ||
exports.default = parse; | ||
function parse(str) { | ||
var comments = getComments(str); | ||
var nodes = []; | ||
var ast = acorn.parse('(' + str + ')', { | ||
locations: true | ||
}); | ||
estraverse.traverse(ast, { | ||
enter: function enter(node) { | ||
if (node.type === 'Property') { | ||
nodes.push(node); | ||
} | ||
} | ||
}); | ||
nodes.reverse().forEach(function (node) { | ||
var prop = node.key.name || node.key.value; | ||
var _node$value = node.value, | ||
start = _node$value.start, | ||
end = _node$value.end, | ||
value = _node$value.value; | ||
var line = node.loc.start.line; | ||
var find = comments.find(function (item) { | ||
return item.key === '// ' + prop && item.commentLine === line; | ||
var comments = getComments(str); | ||
var nodes = []; | ||
var ast = acorn.parse("(" + str + ")", { | ||
locations: true, | ||
}); | ||
// TODO | ||
if (find) { | ||
var realValue = typeof value === 'string' ? '"' + value + '"' : value; | ||
// 兼容 node-json-comment | ||
var commentProperty = '"' + find.key + '": [null, ["// ' + find.value + '"]]'; | ||
var valueWithComment = realValue + ', ' + commentProperty; | ||
str = str.slice(0, start - 1) + valueWithComment + str.slice(end - 1); | ||
} | ||
}); | ||
var result = str.split('\n').map(function (item) { | ||
var reg = /\/\/.*,$/; | ||
if (reg.test(item)) { | ||
return item.replace(/,$/, ''); | ||
} | ||
return item; | ||
}).join('\n'); | ||
/* eslint-disable no-new-func */ | ||
var obj = new Function('return (' + result + ')')(); | ||
return obj; | ||
estraverse.traverse(ast, { | ||
enter: function (node) { | ||
if (node.type === 'Property') { | ||
nodes.push(node); | ||
} | ||
}, | ||
}); | ||
nodes.reverse().forEach(function (node) { | ||
var prop = node.key.name || node.key.value; | ||
var _a = node.value, start = _a.start, end = _a.end, value = _a.value; | ||
var line = node.loc.start.line; | ||
var find = comments.find(function (item) { | ||
return item.key === "// " + prop && item.commentLine === line; | ||
}); | ||
// TODO | ||
if (find) { | ||
var realValue = typeof value === 'string' ? "\"" + value + "\"" : value; | ||
// 兼容 node-json-comment | ||
var commentProperty = "\"" + find.key + "\": [null, [\"// " + find.value + "\"]]"; | ||
var valueWithComment = realValue + ", " + commentProperty; | ||
str = str.slice(0, start - 1) + valueWithComment + str.slice(end - 1); | ||
} | ||
}); | ||
var result = str | ||
.split('\n') | ||
.map(function (item) { | ||
var reg = /\/\/.*,$/; | ||
if (reg.test(item)) { | ||
return item.replace(/,$/, ''); | ||
} | ||
return item; | ||
}) | ||
.join('\n'); | ||
/* eslint-disable no-new-func */ | ||
var obj = new Function("return (" + result + ")")(); | ||
return obj; | ||
} | ||
exports.parse = parse; | ||
function getComments(source) { | ||
var comments = []; | ||
var nodes = []; | ||
var ast = acorn.parse('(' + source + ')', { | ||
locations: true, | ||
onComment: comments | ||
}); | ||
estraverse.replace(ast, { | ||
enter: function enter(node) { | ||
if (node.type === 'Property') { | ||
nodes.push(node); | ||
} | ||
} | ||
}); | ||
return comments.map(function (item) { | ||
var commentLine = item.loc.start.line; | ||
var value = item.value.trim(); | ||
var find = nodes.find(function (node) { | ||
return commentLine === node.loc.start.line; | ||
var comments = []; | ||
var nodes = []; | ||
var ast = acorn.parse("(" + source + ")", { | ||
locations: true, | ||
onComment: comments, | ||
}); | ||
if (!find) return {}; | ||
return { | ||
commentLine: commentLine, | ||
key: '// ' + (find.key.name || find.key.value), | ||
value: value | ||
}; | ||
}); | ||
} | ||
estraverse.replace(ast, { | ||
enter: function (node) { | ||
if (node.type === 'Property') { | ||
nodes.push(node); | ||
} | ||
}, | ||
}); | ||
return comments.map(function (item) { | ||
var commentLine = item.loc.start.line; | ||
var value = item.value.trim(); | ||
var find = nodes.find(function (node) { | ||
return commentLine === node.loc.start.line; | ||
}); | ||
if (!find) | ||
return {}; | ||
return { | ||
commentLine: commentLine, | ||
key: "// " + (find.key.name || find.key.value), | ||
value: value, | ||
}; | ||
}); | ||
} |
'use strict'; | ||
var json = require('comment-json'); | ||
var acorn = require('acorn'); | ||
var estraverse = require('estraverse'); | ||
module.exports = stringify; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var json = require("comment-json"); | ||
var acorn = require("acorn"); | ||
var estraverse = require("estraverse"); | ||
exports.default = stringify; | ||
function stringify(source) { | ||
var str = json.stringify(source, null, 2); | ||
return removeQuote(str); | ||
var str = json.stringify(source, null, 2); | ||
return removeQuote(str); | ||
} | ||
exports.stringify = stringify; | ||
function removeQuote(source) { | ||
var nodes = []; | ||
var ast = acorn.parse('(' + source + ')', { | ||
ranges: true | ||
}); | ||
estraverse.replace(ast, { | ||
enter: function enter(node) { | ||
if (node.type === 'Property' && node.key) { | ||
nodes.push(node); | ||
} | ||
} | ||
}); | ||
nodes.reverse().forEach(function (node) { | ||
var _node$key = node.key, | ||
start = _node$key.start, | ||
end = _node$key.end, | ||
value = _node$key.value; | ||
var readValue = value.split('|').length > 1 ? '"' + value + '"' : value; | ||
source = source.slice(0, start - 1) + readValue + source.slice(end - 1); | ||
}); | ||
return source; | ||
} | ||
var nodes = []; | ||
var ast = acorn.parse("(" + source + ")", { | ||
ranges: true, | ||
}); | ||
estraverse.replace(ast, { | ||
enter: function (node) { | ||
if (node.type === 'Property' && node.key) { | ||
nodes.push(node); | ||
} | ||
}, | ||
}); | ||
nodes.reverse().forEach(function (node) { | ||
var _a = node.key, start = _a.start, end = _a.end, value = _a.value; | ||
var readValue = value.split('|').length > 1 ? "\"" + value + "\"" : value; | ||
source = source.slice(0, start - 1) + readValue + source.slice(end - 1); | ||
}); | ||
return source; | ||
} |
@@ -1,20 +0,18 @@ | ||
'use strict'; | ||
var SimpleMarkdown = require('simple-markdown'); | ||
var yaml = require('js-yaml'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var SimpleMarkdown = require("simple-markdown"); | ||
var yaml = require("js-yaml"); | ||
var mdParse = SimpleMarkdown.defaultBlockParse; | ||
module.exports = function (str) { | ||
return getBasic(str); | ||
}; | ||
exports.default = stringify; | ||
function stringify(str) { | ||
return getBasic(str); | ||
} | ||
exports.stringify = stringify; | ||
function getBasic(basic) { | ||
var syntaxTree = mdParse(basic); | ||
var find = syntaxTree.find(function (item) { | ||
return item.type === 'codeBlock' && item.lang === 'yaml'; | ||
}); | ||
if (!find) return {}; | ||
var obj = yaml.safeLoad(find.content); | ||
return obj; | ||
} | ||
var syntaxTree = mdParse(basic); | ||
var find = syntaxTree.find(function (item) { return item.type === 'codeBlock' && item.lang === 'yaml'; }); | ||
if (!find) | ||
return {}; | ||
var obj = yaml.safeLoad(find.content); | ||
return obj; | ||
} |
@@ -1,37 +0,29 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// TODO 都没校验 | ||
var toBasic = require('./basic'); | ||
var toRequest = require('./request'); | ||
var toResponses = require('./responses'); | ||
module.exports = function (str) { | ||
var _splitDocsStr = splitDocsStr(str), | ||
basic = _splitDocsStr.basic, | ||
request = _splitDocsStr.request, | ||
responses = _splitDocsStr.responses; | ||
basic = toBasic(basic); | ||
var _basic = basic, | ||
_basic$method = _basic.method, | ||
method = _basic$method === undefined ? 'GET' : _basic$method; | ||
request = toRequest(request, method); | ||
responses = toResponses(responses); | ||
return { | ||
basic: basic, | ||
request: request, | ||
responses: responses | ||
}; | ||
}; | ||
var basic_1 = require("./basic"); | ||
var request_1 = require("./request"); | ||
var responses_1 = require("./responses"); | ||
exports.default = full; | ||
function full(str) { | ||
var _a = splitDocsStr(str), basic = _a.basic, request = _a.request, responses = _a.responses; | ||
basic = basic_1.default(basic); | ||
var _b = basic.method, method = _b === void 0 ? 'GET' : _b; | ||
request = request_1.default(request, method); | ||
responses = responses_1.default(responses); | ||
return { | ||
basic: basic, | ||
request: request, | ||
responses: responses, | ||
}; | ||
} | ||
exports.full = full; | ||
// TODO 不够严格 | ||
function splitDocsStr(str) { | ||
var splitRequest = str.split('# Request'); | ||
var splitResponses = splitRequest[1].split('# Responses'); | ||
var basic = splitRequest[0]; | ||
var request = splitResponses[0]; | ||
var responses = splitResponses[1]; | ||
return { basic: basic, request: request, responses: responses }; | ||
} | ||
var splitRequest = str.split('# Request'); | ||
var splitResponses = splitRequest[1].split('# Responses'); | ||
var basic = splitRequest[0]; | ||
var request = splitResponses[0]; | ||
var responses = splitResponses[1]; | ||
return { basic: basic, request: request, responses: responses }; | ||
} |
@@ -1,13 +0,11 @@ | ||
'use strict'; | ||
var full = require('./full'); | ||
var basic = require('./basic'); | ||
var request = require('./request'); | ||
var responses = require('./responses'); | ||
module.exports = { | ||
full: full, | ||
basic: basic, | ||
request: request, | ||
responses: responses | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var full_1 = require("./full"); | ||
exports.full = full_1.default; | ||
var basic_1 = require("./basic"); | ||
exports.basic = basic_1.default; | ||
var request_1 = require("./request"); | ||
exports.request = request_1.default; | ||
var responses_1 = require("./responses"); | ||
exports.responses = responses_1.default; | ||
exports.default = { full: full_1.default, basic: basic_1.default, request: request_1.default, responses: responses_1.default }; |
@@ -1,34 +0,28 @@ | ||
'use strict'; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var SimpleMarkdown = require('simple-markdown'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var SimpleMarkdown = require("simple-markdown"); | ||
var mdParse = SimpleMarkdown.defaultBlockParse; | ||
var _require = require('../object-to-schema'), | ||
json = _require.json; | ||
var _require2 = require('../json-comment-parser'), | ||
parse = _require2.parse; | ||
module.exports = function (str, method) { | ||
var request = getRequest(str); | ||
return requestToSchema(request, method); | ||
}; | ||
var object_to_schema_1 = require("../object-to-schema"); | ||
var json_comment_parser_1 = require("../json-comment-parser"); | ||
exports.default = toRequest; | ||
function toRequest(str, method) { | ||
var request = getRequest(str); | ||
return requestToSchema(request, method); | ||
} | ||
exports.toRequest = toRequest; | ||
function requestToSchema(request, method) { | ||
// TODO | ||
var type = method === 'GET' || method === 'DELETE' ? 'query' : 'body'; | ||
return _defineProperty({}, type, json(request)); | ||
var _a; | ||
// TODO | ||
var type = method === 'GET' || method === 'DELETE' ? 'query' : 'body'; | ||
return _a = {}, | ||
_a[type] = object_to_schema_1.json(request), | ||
_a; | ||
} | ||
function getRequest(request) { | ||
var syntaxTree = mdParse(request); | ||
var find = syntaxTree.find(function (item) { | ||
return item.type === 'codeBlock' && item.lang === 'js'; | ||
}); | ||
if (!find) return {}; | ||
var object = parse(find.content); | ||
return object; | ||
} | ||
var syntaxTree = mdParse(request); | ||
var find = syntaxTree.find(function (item) { return item.type === 'codeBlock' && item.lang === 'js'; }); | ||
if (!find) | ||
return {}; | ||
var object = json_comment_parser_1.parse(find.content); | ||
return object; | ||
} |
@@ -1,68 +0,55 @@ | ||
'use strict'; | ||
var SimpleMarkdown = require('simple-markdown'); | ||
var yaml = require('js-yaml'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var SimpleMarkdown = require("simple-markdown"); | ||
var yaml = require("js-yaml"); | ||
var object_to_schema_1 = require("../object-to-schema"); | ||
var json_comment_parser_1 = require("../json-comment-parser"); | ||
var mdParse = SimpleMarkdown.defaultBlockParse; | ||
var _require = require('../object-to-schema'), | ||
json = _require.json; | ||
var _require2 = require('../json-comment-parser'), | ||
parse = _require2.parse; | ||
module.exports = function (str) { | ||
var responses = []; | ||
var arr = str.split('##'); | ||
arr.forEach(function (item) { | ||
var reg = /Scene/; | ||
if (reg.test(item)) { | ||
responses.push(item); | ||
} | ||
}); | ||
var responsesObj = getResponses(responses); | ||
return responsesToSchema(responsesObj); | ||
}; | ||
exports.default = responses; | ||
function responses(str) { | ||
var responses = []; | ||
var arr = str.split('##'); | ||
arr.forEach(function (item) { | ||
var reg = /Scene/; | ||
if (reg.test(item)) { | ||
responses.push(item); | ||
} | ||
}); | ||
var responsesObj = getResponses(responses); | ||
return responsesToSchema(responsesObj); | ||
} | ||
exports.responses = responses; | ||
; | ||
function responsesToSchema(responses) { | ||
return responses.map(function (item) { | ||
var _item$body = item.body, | ||
body = _item$body === undefined ? {} : _item$body; | ||
item.body = json(body); | ||
return item; | ||
}); | ||
return responses.map(function (item) { | ||
var _a = item.body, body = _a === void 0 ? {} : _a; | ||
item.body = object_to_schema_1.json(body); | ||
return item; | ||
}); | ||
} | ||
function getResponses(responses) { | ||
var result = []; | ||
responses.forEach(function (item) { | ||
var syntaxTree = mdParse(item); | ||
var js = syntaxTree.find(function (item) { | ||
return item.type === 'codeBlock' && item.lang === 'js'; | ||
var result = []; | ||
responses.forEach(function (item) { | ||
var syntaxTree = mdParse(item); | ||
var js = syntaxTree.find(function (item) { return item.type === 'codeBlock' && item.lang === 'js'; }); | ||
var yml = syntaxTree.find(function (item) { return item.type === 'codeBlock' && item.lang === 'yaml'; }); | ||
var ymlObj = yaml.safeLoad(yml.content); | ||
if (!js) | ||
return {}; | ||
var body = json_comment_parser_1.parse(js.content); | ||
result.push({ | ||
body: body, | ||
mock: { | ||
options: { | ||
delay: ymlObj.delay, | ||
enable: ymlObj.mock, | ||
}, | ||
data: {}, | ||
}, | ||
scene: {}, | ||
description: ymlObj.desc, | ||
}); | ||
return null; | ||
}); | ||
var yml = syntaxTree.find(function (item) { | ||
return item.type === 'codeBlock' && item.lang === 'yaml'; | ||
}); | ||
var ymlObj = yaml.safeLoad(yml.content); | ||
if (!js) return {}; | ||
var body = parse(js.content); | ||
result.push({ | ||
body: body, | ||
mock: { | ||
options: { | ||
delay: ymlObj.delay, | ||
enable: ymlObj.mock | ||
}, | ||
data: {} | ||
}, | ||
scene: {}, | ||
description: ymlObj.desc | ||
}); | ||
}); | ||
return result; | ||
} | ||
return result; | ||
} |
@@ -1,17 +0,15 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var _require = require('./constant'), | ||
BASIC = _require.BASIC; | ||
module.exports = function () { | ||
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
if (_.isEmpty(obj)) return ''; | ||
return compiledBasic(obj); | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var constant_1 = require("./constant"); | ||
exports.default = basic; | ||
function basic(obj) { | ||
if (obj === void 0) { obj = {}; } | ||
if (_.isEmpty(obj)) | ||
return ''; | ||
return compiledBasic(obj); | ||
} | ||
exports.basic = basic; | ||
function compiledBasic(basic) { | ||
return _.template(BASIC)(basic); | ||
} | ||
return _.template(constant_1.BASIC)(basic); | ||
} |
@@ -1,26 +0,21 @@ | ||
'use strict'; | ||
var BASIC = '\n# Basic\n\n```yaml\ntitle: <%= title %>\ndescription: <%= description %>\npath: <%= path %>\nmethod: <%= method %>\n```\n'; | ||
var REQUEST = '\n# Request\n\n```js\n// <%= type %>\n<%= request %>\n```\n'; | ||
var RESPONSE = '\n\n## Scene <%= scene %>\n\n```yaml\ndesc: <%= desc %>\nmock: <%= enableMock %>\ndelay: <%= delay %>\n```\n\n```js\n<%= body %>\n```\n'; | ||
var EXTRA = '\n# Extra\n\n<%= extra %>\n'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var BASIC = "\n# Basic\n\n```yaml\ntitle: <%= title %>\ndescription: <%= description %>\npath: <%= path %>\nmethod: <%= method %>\n```\n"; | ||
exports.BASIC = BASIC; | ||
var REQUEST = "\n# Request\n\n```js\n// <%= type %>\n<%= request %>\n```\n"; | ||
exports.REQUEST = REQUEST; | ||
var RESPONSE = "\n\n## Scene <%= scene %>\n\n```yaml\ndesc: <%= desc %>\nmock: <%= enableMock %>\ndelay: <%= delay %>\n```\n\n```js\n<%= body %>\n```\n"; | ||
exports.RESPONSE = RESPONSE; | ||
var EXTRA = "\n# Extra\n\n<%= extra %>\n"; | ||
exports.EXTRA = EXTRA; | ||
// 需优化 | ||
function toJS(mobxObj) { | ||
try { | ||
return JSON.parse(JSON.stringify(mobxObj)); | ||
} catch (err) { | ||
return {}; | ||
} | ||
try { | ||
return JSON.parse(JSON.stringify(mobxObj)); | ||
} | ||
catch (err) { | ||
return {}; | ||
} | ||
} | ||
module.exports = { | ||
BASIC: BASIC, | ||
REQUEST: REQUEST, | ||
RESPONSE: RESPONSE, | ||
EXTRA: EXTRA, | ||
toJS: toJS | ||
}; | ||
exports.toJS = toJS; | ||
exports.default = { BASIC: BASIC, REQUEST: REQUEST, RESPONSE: RESPONSE, EXTRA: EXTRA, toJS: toJS }; |
@@ -1,15 +0,11 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var _require = require('./constant'), | ||
EXTRA = _require.EXTRA; | ||
module.exports = function (extra) { | ||
console.log('extra..................'); | ||
console.log(extra); | ||
console.log('EXTRA:', EXTRA); | ||
if (!extra) return ''; | ||
console.log('==-=-=-=-=-=-=-=-'); | ||
return _.template(EXTRA)({ extra: extra }); | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var constant_1 = require("./constant"); | ||
exports.default = extra; | ||
function extra(extra) { | ||
if (!extra) | ||
return ''; | ||
return _.template(constant_1.EXTRA)({ extra: extra }); | ||
} | ||
exports.extra = extra; |
@@ -1,25 +0,23 @@ | ||
'use strict'; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var _ = require('lodash'); | ||
var toBasic = require('./basic'); | ||
var toRequest = require('./request'); | ||
var toResponses = require('./responses'); | ||
var toExtra = require('./extra'); | ||
module.exports = function () { | ||
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
if (_.isEmpty(obj)) return ''; | ||
var method = obj.basic.method; | ||
var _obj$basic = obj.basic, | ||
basic = _obj$basic === undefined ? {} : _obj$basic, | ||
request = obj.request, | ||
responses = obj.responses; | ||
var _basic$extra = basic.extra, | ||
extra = _basic$extra === undefined ? '' : _basic$extra; | ||
var arr = [toBasic(basic), toRequest(request, method)].concat(_toConsumableArray(toResponses(responses)), [toExtra(extra)]); | ||
return arr.join(''); | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var basic_1 = require("./basic"); | ||
var request_1 = require("./request"); | ||
var responses_1 = require("./responses"); | ||
var extra_1 = require("./extra"); | ||
exports.default = full; | ||
function full(obj) { | ||
if (_.isEmpty(obj)) | ||
return ''; | ||
var method = obj.basic.method; | ||
var _a = obj.basic, basic = _a === void 0 ? {} : _a, request = obj.request, responses = obj.responses; | ||
var _b = basic.extra, extra = _b === void 0 ? '' : _b; | ||
var arr = [ | ||
basic_1.default(basic), | ||
request_1.default(request, method), | ||
responses_1.default(responses), | ||
extra_1.default(extra), | ||
]; | ||
return arr.join(''); | ||
} | ||
exports.full = full; |
@@ -1,17 +0,11 @@ | ||
/** | ||
* trans api data to markdown | ||
*/ | ||
'use strict'; | ||
var full = require('./full'); | ||
var basic = require('./basic'); | ||
var request = require('./request'); | ||
var responses = require('./responses'); | ||
module.exports = { | ||
full: full, | ||
basic: basic, | ||
request: request, | ||
responses: responses | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var full_1 = require("./full"); | ||
exports.full = full_1.default; | ||
var basic_1 = require("./basic"); | ||
exports.basic = basic_1.default; | ||
var request_1 = require("./request"); | ||
exports.request = request_1.default; | ||
var responses_1 = require("./responses"); | ||
exports.responses = responses_1.default; | ||
exports.default = { full: full_1.default, basic: basic_1.default, request: request_1.default, responses: responses_1.default }; |
@@ -1,28 +0,25 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var schemeToObject = require('../schema-to-object'); | ||
var _require = require('../json-comment-parser'), | ||
stringify = _require.stringify; | ||
var _require2 = require('./constant'), | ||
REQUEST = _require2.REQUEST, | ||
toJS = _require2.toJS; | ||
module.exports = function () { | ||
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var method = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'GET'; | ||
if (_.isEmpty(obj)) return ''; | ||
return compileRequest(obj, method); | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var schema_to_object_1 = require("../schema-to-object"); | ||
var json_comment_parser_1 = require("../json-comment-parser"); | ||
var constant_1 = require("./constant"); | ||
exports.default = request; | ||
function request(obj, method) { | ||
if (obj === void 0) { obj = {}; } | ||
if (method === void 0) { method = 'GET'; } | ||
if (_.isEmpty(obj)) | ||
return ''; | ||
return compileRequest(obj, method); | ||
} | ||
exports.request = request; | ||
function compileRequest(request, method) { | ||
var requestType = method === 'GET' ? 'query' : 'body'; | ||
var requestObj = schemeToObject(toJS(request[requestType] || {}), { comment: true }); | ||
var requestContent = stringify(requestObj); | ||
var compiledRequest = _.template(REQUEST); | ||
var type = method === 'GET' ? 'query object' : 'request body'; | ||
return compiledRequest({ request: requestContent, type: type }); | ||
} | ||
var requestType = method === 'GET' ? 'query' : 'body'; | ||
var requestObj = schema_to_object_1.default(constant_1.toJS(request[requestType] || {}), { | ||
comment: true, | ||
}); | ||
var requestContent = json_comment_parser_1.stringify(requestObj); | ||
var compiledRequest = _.template(constant_1.REQUEST); | ||
var type = method === 'GET' ? 'query object' : 'request body'; | ||
return compiledRequest({ request: requestContent, type: type }); | ||
} |
@@ -1,46 +0,31 @@ | ||
'use strict'; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var _ = require('lodash'); | ||
var schemeToObject = require('../schema-to-object'); | ||
var _require = require('../json-comment-parser'), | ||
stringify = _require.stringify; | ||
var _require2 = require('./constant'), | ||
RESPONSE = _require2.RESPONSE, | ||
toJS = _require2.toJS; | ||
module.exports = function () { | ||
var responses = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
if (_.isEmpty(responses)) return ''; | ||
return ['\n# Responses'].concat(_toConsumableArray(compileResponses(responses))).join(''); | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var schema_to_object_1 = require("../schema-to-object"); | ||
var json_comment_parser_1 = require("../json-comment-parser"); | ||
var constant_1 = require("./constant"); | ||
exports.default = responses; | ||
function responses(responses) { | ||
if (responses === void 0) { responses = []; } | ||
if (_.isEmpty(responses)) | ||
return ''; | ||
return ['\n# Responses'].concat(compileResponses(responses)).join(''); | ||
} | ||
exports.responses = responses; | ||
function compileResponses(responses) { | ||
return _.map(responses, function (item, key) { | ||
var description = item.description, | ||
_item$mock = item.mock, | ||
mock = _item$mock === undefined ? {} : _item$mock, | ||
_item$body = item.body, | ||
body = _item$body === undefined ? {} : _item$body; | ||
var responseObj = schemeToObject(toJS(body), { comment: true }); | ||
var responseContent = stringify(responseObj, null, 2); | ||
var compiledResponse = _.template(RESPONSE); | ||
var _mock$options = mock.options, | ||
options = _mock$options === undefined ? {} : _mock$options; | ||
var delay = options.delay, | ||
enable = options.enable; | ||
return compiledResponse({ | ||
scene: key + 1, | ||
desc: description, | ||
delay: delay, | ||
enableMock: enable, | ||
body: responseContent | ||
return _.map(responses, function (item, key) { | ||
var description = item.description, _a = item.mock, mock = _a === void 0 ? {} : _a, _b = item.body, body = _b === void 0 ? {} : _b; | ||
var responseObj = schema_to_object_1.default(constant_1.toJS(body), { comment: true }); | ||
var responseContent = json_comment_parser_1.stringify(responseObj); | ||
var compiledResponse = _.template(constant_1.RESPONSE); | ||
var _c = mock.options, options = _c === void 0 ? {} : _c; | ||
var delay = options.delay, enable = options.enable; | ||
return compiledResponse({ | ||
scene: key + 1, | ||
desc: description, | ||
delay: delay, | ||
enableMock: enable, | ||
body: responseContent, | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
@@ -1,10 +0,11 @@ | ||
'use strict'; | ||
var json = require('./json'); | ||
var tpl = require('./tpl'); | ||
module.exports = function (data) { | ||
var schema = json(data); | ||
tpl.responses[0].body = schema; | ||
return tpl; | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var json_1 = require("./json"); | ||
var tpl_1 = require("./tpl"); | ||
exports.default = api; | ||
function api(data) { | ||
var schema = json_1.default(data); | ||
tpl_1.default.responses[0].body = schema; | ||
return tpl_1.default; | ||
} | ||
exports.api = api; |
@@ -1,6 +0,7 @@ | ||
'use strict'; | ||
var json = require('./json'); | ||
var api = require('./api'); | ||
module.exports = { json: json, api: api }; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var json_1 = require("./json"); | ||
exports.json = json_1.default; | ||
var api_1 = require("./api"); | ||
exports.api = api_1.default; | ||
exports.default = { json: json_1.default, api: api_1.default }; |
@@ -1,284 +0,166 @@ | ||
'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _ = require('lodash'); | ||
var _require = require('../constant'), | ||
keywords = _require.keywords, | ||
numKeywords = _require.numKeywords; | ||
module.exports = objectToSchema; | ||
var rules = [[_.isNull, function (value) { | ||
return value; | ||
}], [_.isNumber, function (value) { | ||
return value; | ||
}], [_.isBoolean, function (value) { | ||
return value; | ||
}], [_.isString, function (value) { | ||
return value; | ||
}], [_.isRegExp, function (pattern) { | ||
return { type: 'string', pattern: pattern }; | ||
}], | ||
// Empty array -> array of any items | ||
[function (data) { | ||
return _.isArray(data) && !data.length; | ||
}, function (value) { | ||
return value; | ||
}], [_.isArray, function (items) { | ||
return { type: 'array', items: objectToSchema(items[0]) }; | ||
}], [_.isPlainObject, function (object) { | ||
return handleOjbect(object); | ||
}]]; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var constant_1 = require("../constant"); | ||
var rules = [ | ||
[_.isNull, function (value) { return value; }], | ||
[_.isNumber, function (value) { return value; }], | ||
[_.isBoolean, function (value) { return value; }], | ||
[_.isString, function (value) { return value; }], | ||
[_.isRegExp, function (pattern) { return ({ type: 'string', pattern: pattern }); }], | ||
// Empty array -> array of any items | ||
[function (data) { return _.isArray(data) && !data.length; }, function (value) { return value; }], | ||
[_.isArray, function (items) { return ({ type: 'array', items: objectToSchema(items[0]) }); }], | ||
[_.isPlainObject, function (object) { return handleOjbect(object); }], | ||
]; | ||
function handleOjbect(object) { | ||
var required = _.keys(object).filter(function (item) { | ||
return item.indexOf('//') < 0; | ||
}); | ||
return { | ||
type: 'object', | ||
properties: handleProps(object), | ||
required: required | ||
}; | ||
var required = _.keys(object).filter(function (item) { return item.indexOf('//') < 0; }); | ||
return { | ||
type: 'object', | ||
properties: handleProps(object), | ||
required: required, | ||
}; | ||
} | ||
function handleProps(object) { | ||
var obj = {}; | ||
_.keys(object).forEach(function (item) { | ||
// TODO | ||
if (item.indexOf('//') > -1) return; | ||
var _loop = function _loop(isMatch, makeSchema) { | ||
if (!isMatch(object[item])) { | ||
return 'continue'; | ||
} | ||
if (_.isArray(object[item]) && !object[item].length) { | ||
obj[item] = { | ||
type: 'array' | ||
var obj = {}; | ||
_.keys(object).forEach(function (item) { | ||
// TODO | ||
if (item.indexOf('//') > -1) | ||
return; | ||
var _loop_1 = function (isMatch, makeSchema) { | ||
if (!isMatch(object[item])) { | ||
return "continue"; | ||
} | ||
if (_.isArray(object[item]) && !object[item].length) { | ||
obj[item] = { | ||
type: 'array', | ||
}; | ||
return "continue"; | ||
} | ||
if (_.isPlainObject(object[item]) || _.isArray(object[item])) { | ||
obj[item] = makeSchema(object[item]); | ||
return "continue"; | ||
} | ||
var type = typeOf(object[item]); | ||
var commentValue = object["// " + item]; | ||
var value = { | ||
default: object[item], | ||
type: type, | ||
}; | ||
// handle comment | ||
if (commentValue) { | ||
var props_1 = getProps(commentValue); | ||
Object.keys(props_1).forEach(function (key) { | ||
value[key] = props_1[key]; | ||
}); | ||
} | ||
obj[item] = makeSchema(value); | ||
return "break"; | ||
}; | ||
return 'continue'; | ||
} | ||
if (_.isPlainObject(object[item]) || _.isArray(object[item])) { | ||
obj[item] = makeSchema(object[item]); | ||
return 'continue'; | ||
} | ||
var type = typeOf(object[item]); | ||
var commentValue = object['// ' + item]; | ||
var value = { | ||
default: object[item], | ||
type: type | ||
}; | ||
// handle comment | ||
if (commentValue) { | ||
var props = getProps(commentValue); | ||
Object.keys(props).forEach(function (key) { | ||
value[key] = props[key]; | ||
}); | ||
} | ||
obj[item] = makeSchema(value); | ||
return 'break'; | ||
}; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
_loop2: for (var _iterator = rules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref = _step.value; | ||
var _ref2 = _slicedToArray(_ref, 2); | ||
var isMatch = _ref2[0]; | ||
var makeSchema = _ref2[1]; | ||
var _ret = _loop(isMatch, makeSchema); | ||
switch (_ret) { | ||
case 'continue': | ||
continue; | ||
case 'break': | ||
break _loop2;} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
for (var _i = 0, rules_1 = rules; _i < rules_1.length; _i++) { | ||
var _a = rules_1[_i], isMatch = _a[0], makeSchema = _a[1]; | ||
var state_1 = _loop_1(isMatch, makeSchema); | ||
if (state_1 === "break") | ||
break; | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
}); | ||
return obj; | ||
}); | ||
return obj; | ||
} | ||
function getProps(commentValue) { | ||
var props = { | ||
description: getDesc(commentValue) | ||
}; | ||
var blockComment = getBlockComment(commentValue); | ||
if (!blockComment) return props; | ||
// block comment | ||
var commentLines = blockComment.split('\n'); | ||
commentLines.forEach(function (commentLine) { | ||
var matchKeywod = getMatchKeyword(commentLine); | ||
if (!matchKeywod) return; | ||
var value = getValueByKeyword(commentLine, matchKeywod); | ||
props[matchKeywod] = handleValueByKeywordType(value, matchKeywod); | ||
}); | ||
return props; | ||
var props = { | ||
description: getDesc(commentValue), | ||
}; | ||
var blockComment = getBlockComment(commentValue); | ||
if (!blockComment) | ||
return props; | ||
// block comment | ||
var commentLines = blockComment.split('\n'); | ||
commentLines.forEach(function (commentLine) { | ||
var matchKeywod = getMatchKeyword(commentLine); | ||
if (!matchKeywod) | ||
return; | ||
var value = getValueByKeyword(commentLine, matchKeywod); | ||
props[matchKeywod] = handleValueByKeywordType(value, matchKeywod); | ||
}); | ||
return props; | ||
} | ||
function getValueByKeyword(commentLine, matchKeywod) { | ||
var arr = commentLine.split(matchKeywod); | ||
return (_.last(arr) || '').trim(); | ||
var arr = commentLine.split(matchKeywod); | ||
var str = _.last(arr) || ''; | ||
return str.trim(); | ||
} | ||
function handleValueByKeywordType(value, matchKeywod) { | ||
if (matchKeywod === 'enum') { | ||
/* eslint-disable no-new-func */ | ||
return new Function('return (' + value + ')')(); | ||
} | ||
return isNumKeyword(matchKeywod) ? parseInt(value, 10) || 0 : value; | ||
if (matchKeywod === 'enum') { | ||
/* eslint-disable no-new-func */ | ||
return new Function("return (" + value + ")")(); | ||
} | ||
return isNumKeyword(matchKeywod) ? parseInt(value, 10) || 0 : value; | ||
} | ||
function getMatchKeyword(commentLine) { | ||
return keywords.find(function (word) { | ||
return commentLine.indexOf(word) > -1; | ||
}); | ||
return constant_1.keywords.find(function (word) { return commentLine.indexOf(word) > -1; }); | ||
} | ||
function isBlockComment() { | ||
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
return str.indexOf('/**') > -1; | ||
function isBlockComment(str) { | ||
if (str === void 0) { str = ''; } | ||
return str.indexOf('/**') > -1; | ||
} | ||
function getBlockComment(commentValue) { | ||
if (!commentValue[0] || !_.isArray(commentValue[0])) return null; | ||
return commentValue[0].find(function (item) { | ||
return isBlockComment(item); | ||
}); | ||
if (!commentValue[0] || !_.isArray(commentValue[0])) | ||
return null; | ||
return commentValue[0].find(function (item) { return isBlockComment(item); }); | ||
} | ||
function getDesc(commentValue) { | ||
var desc = ''; | ||
// 兼容旧代码 | ||
if (_.isString(commentValue)) { | ||
desc = commentValue; | ||
} | ||
// get description from comment | ||
if (_.isArray(commentValue) && commentValue.length === 2) { | ||
desc = commentValue[1][0].replace(/^\/\//, '').trim(); | ||
} | ||
return desc; | ||
var desc = ''; | ||
// 兼容旧代码 | ||
if (_.isString(commentValue)) { | ||
desc = commentValue; | ||
} | ||
// get description from comment | ||
if (_.isArray(commentValue) && commentValue.length === 2) { | ||
desc = commentValue[1][0].replace(/^\/\//, '').trim(); | ||
} | ||
return desc; | ||
} | ||
function objectToSchema(data) { | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = rules[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var _ref3 = _step2.value; | ||
var _ref4 = _slicedToArray(_ref3, 2); | ||
var isMatch = _ref4[0]; | ||
var makeSchema = _ref4[1]; | ||
if (!isMatch(data)) { | ||
continue; | ||
} | ||
if (_.isArray(data) && !data.length) { | ||
return { | ||
type: 'array' | ||
for (var _i = 0, rules_2 = rules; _i < rules_2.length; _i++) { | ||
var _a = rules_2[_i], isMatch = _a[0], makeSchema = _a[1]; | ||
if (!isMatch(data)) { | ||
continue; | ||
} | ||
if (_.isArray(data) && !data.length) { | ||
return { | ||
type: 'array', | ||
}; | ||
} | ||
if (_.isPlainObject(data) || _.isArray(data)) { | ||
return makeSchema(data); | ||
} | ||
var type = typeOf(data); | ||
var value = { | ||
default: data, | ||
type: type, | ||
}; | ||
} | ||
if (_.isPlainObject(data) || _.isArray(data)) { | ||
return makeSchema(data); | ||
} | ||
var type = typeOf(data); | ||
var _value = { | ||
default: data, | ||
type: type | ||
}; | ||
return makeSchema(_value); | ||
return makeSchema(value); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
} | ||
function typeOf(data) { | ||
var rules = [[_.isNull, 'null'], [_.isNumber, 'number'], [_.isBoolean, 'boolean'], [_.isString, 'string'], [_.isRegExp, 'string']]; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = rules[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var _ref5 = _step3.value; | ||
var _ref6 = _slicedToArray(_ref5, 2); | ||
var isMatch = _ref6[0]; | ||
var _value2 = _ref6[1]; | ||
if (isMatch(data)) { | ||
return _value2; | ||
} | ||
var rules = [ | ||
[_.isNull, 'null'], | ||
[_.isNumber, 'number'], | ||
[_.isBoolean, 'boolean'], | ||
[_.isString, 'string'], | ||
[_.isRegExp, 'string'], | ||
]; | ||
for (var _i = 0, rules_3 = rules; _i < rules_3.length; _i++) { | ||
var rule = rules_3[_i]; | ||
var isMatch = rule[0]; | ||
var value = rule[1]; | ||
if (isMatch(data)) { | ||
return value; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
function isNumKeyword(keyword) { | ||
return numKeywords.indexOf(keyword) > -1; | ||
} | ||
return constant_1.numKeywords.indexOf(keyword) > -1; | ||
} | ||
exports.default = objectToSchema; |
@@ -1,23 +0,25 @@ | ||
'use strict'; | ||
module.exports = { | ||
basic: { | ||
title: 'Title for API', | ||
description: 'Description for API', | ||
method: 'API method', | ||
path: 'API path' | ||
}, | ||
request: { | ||
headers: {}, | ||
query: {}, | ||
body: {} | ||
}, | ||
responses: [{ | ||
description: '成功返回', | ||
body: {}, | ||
mock: { | ||
delay: 100, | ||
enable: false | ||
} | ||
}] | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = { | ||
basic: { | ||
title: 'Title for API', | ||
description: 'Description for API', | ||
method: 'API method', | ||
path: 'API path', | ||
}, | ||
request: { | ||
headers: {}, | ||
query: {}, | ||
body: {}, | ||
}, | ||
responses: [ | ||
{ | ||
description: '成功返回', | ||
body: {}, | ||
mock: { | ||
delay: 100, | ||
enable: false, | ||
}, | ||
}, | ||
], | ||
}; |
@@ -1,35 +0,29 @@ | ||
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
module.exports = { | ||
removeSplitLine: removeSplitLine | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function getValue(value) { | ||
var arr = value.split('|'); | ||
if (arr.length < 2) return value; | ||
var maps = { | ||
string: arr[0], | ||
number: Number(arr[0]), | ||
boolean: arr[0] === 'true' | ||
}; | ||
return maps[arr[1]] ? maps[arr[1]] : arr[0]; | ||
var arr = value.split('|'); | ||
if (arr.length < 2) | ||
return value; | ||
var maps = { | ||
string: arr[0], | ||
number: Number(arr[0]), | ||
boolean: arr[0] === 'true', | ||
}; | ||
return maps[arr[1]] ? maps[arr[1]] : arr[0]; | ||
} | ||
function removeSplitLine(obj) { | ||
if (obj === null || (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') { | ||
return obj; | ||
} | ||
var temp = obj.constructor(); | ||
for (var key in obj) { | ||
if (typeof obj[key] === 'string') { | ||
temp[key] = getValue(obj[key]); | ||
} else { | ||
temp[key] = removeSplitLine(obj[key]); | ||
if (obj === null || typeof obj !== 'object') { | ||
return obj; | ||
} | ||
} | ||
return temp; | ||
} | ||
var temp = obj.constructor(); | ||
for (var key in obj) { | ||
if (typeof obj[key] === 'string') { | ||
temp[key] = getValue(obj[key]); | ||
} | ||
else { | ||
temp[key] = removeSplitLine(obj[key]); | ||
} | ||
} | ||
return temp; | ||
} | ||
exports.removeSplitLine = removeSplitLine; |
@@ -1,110 +0,82 @@ | ||
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _require = require('./constant'), | ||
keywords = _require.keywords; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var constant_1 = require("./constant"); | ||
function clone(source) { | ||
return JSON.parse(JSON.stringify(source)); | ||
return JSON.parse(JSON.stringify(source)); | ||
} | ||
var convert = function convert(schema, comment) { | ||
if (typeof schema.default !== 'undefined') { | ||
return schema.default; | ||
} else if (schema.type === 'object') { | ||
var _ret = function () { | ||
var properties = schema.properties; | ||
if (!properties) { | ||
return { | ||
v: {} | ||
var convert = function (schema, comment) { | ||
if (typeof schema.default !== 'undefined') { | ||
return schema.default; | ||
} | ||
else if (schema.type === 'object') { | ||
var properties_1 = schema.properties; | ||
if (!properties_1) { | ||
return {}; | ||
} | ||
var _loop_1 = function (key) { | ||
if (!properties_1.hasOwnProperty(key)) | ||
return "continue"; | ||
if (comment) { | ||
var comment_1 = [null, null]; | ||
if (properties_1[key].description) { | ||
comment_1[1] = ["// " + properties_1[key].description]; | ||
} | ||
var tmp_1 = ['/**']; | ||
constant_1.keywords.forEach(function (word) { | ||
if (!properties_1[key][word]) | ||
return; | ||
tmp_1.push("* @" + word + " " + properties_1[key][word]); | ||
}); | ||
tmp_1.push('*/'); | ||
if (tmp_1.length > 2) { | ||
comment_1[0] = [tmp_1.join('\n')]; | ||
} | ||
properties_1["// " + key] = comment_1; | ||
} | ||
properties_1[key] = convert(properties_1[key], comment); | ||
if (typeof properties_1[key] === 'undefined') { | ||
delete properties_1[key]; | ||
} | ||
}; | ||
} | ||
var _loop = function _loop(key) { | ||
if (!properties.hasOwnProperty(key)) return 'continue'; | ||
if (comment) { | ||
var _comment = [null, null]; | ||
if (properties[key].description) { | ||
_comment[1] = ['// ' + properties[key].description]; | ||
} | ||
var tmp = ['/**']; | ||
keywords.forEach(function (word) { | ||
if (!properties[key][word]) return; | ||
tmp.push('* @' + word + ' ' + properties[key][word]); | ||
}); | ||
tmp.push('*/'); | ||
if (tmp.length > 2) { | ||
_comment[0] = [tmp.join('\n')]; | ||
} | ||
properties['// ' + key] = _comment; | ||
for (var key in properties_1) { | ||
_loop_1(key); | ||
} | ||
properties[key] = convert(properties[key], comment); | ||
if (typeof properties[key] === 'undefined') { | ||
delete properties[key]; | ||
} | ||
}; | ||
for (var key in properties) { | ||
var _ret2 = _loop(key); | ||
if (_ret2 === 'continue') continue; | ||
} | ||
return { | ||
v: properties | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
} else if (schema.type === 'array') { | ||
if (!schema.items) { | ||
return []; | ||
return properties_1; | ||
} | ||
// minimum item count | ||
var ct = schema.minItems || 0; | ||
// tuple-typed arrays | ||
if (schema.items.constructor === Array) { | ||
var _values = schema.items.map(function (item) { | ||
return convert(item, comment); | ||
}); | ||
// remove undefined items at the end (unless required by minItems) | ||
for (var i = _values.length - 1; i >= 0; i--) { | ||
if (typeof _values[i] !== 'undefined') { | ||
break; | ||
else if (schema.type === 'array') { | ||
if (!schema.items) { | ||
return []; | ||
} | ||
if (i + 1 > ct) { | ||
_values.pop(); | ||
// minimum item count | ||
var ct = schema.minItems || 0; | ||
// tuple-typed arrays | ||
if (schema.items.constructor === Array) { | ||
var values_1 = schema.items.map(function (item) { return convert(item, comment); }); | ||
// remove undefined items at the end (unless required by minItems) | ||
for (var i = values_1.length - 1; i >= 0; i--) { | ||
if (typeof values_1[i] !== 'undefined') { | ||
break; | ||
} | ||
if (i + 1 > ct) { | ||
values_1.pop(); | ||
} | ||
} | ||
return values_1; | ||
} | ||
} | ||
return _values; | ||
// object-typed arrays | ||
var value = convert(schema.items, comment); | ||
if (typeof value === 'undefined') { | ||
return []; | ||
} | ||
var values = []; | ||
for (var i = 0; i < Math.max(1, ct); i++) { | ||
values.push(clone(value)); | ||
} | ||
return values; | ||
} | ||
// object-typed arrays | ||
var value = convert(schema.items, comment); | ||
if (typeof value === 'undefined') { | ||
return []; | ||
} | ||
var values = []; | ||
for (var _i = 0; _i < Math.max(1, ct); _i++) { | ||
values.push(clone(value)); | ||
} | ||
return values; | ||
} | ||
}; | ||
function main(schema) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var _options$comment = options.comment, | ||
comment = _options$comment === undefined ? false : _options$comment; | ||
return convert(clone(schema), comment); | ||
function main(schema, _a) { | ||
var comment = _a.comment; | ||
return convert(clone(schema), comment); | ||
} | ||
module.exports = main; | ||
exports.default = main; |
@@ -1,72 +0,64 @@ | ||
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function clone(source) { | ||
return JSON.parse(JSON.stringify(source)); | ||
return JSON.parse(JSON.stringify(source)); | ||
} | ||
var convert = function convert(schema) { | ||
if (['object', 'array'].indexOf(schema.type) < 0) { | ||
return schema.type || {}; | ||
} else if (schema.type === 'object') { | ||
var properties = schema.properties; | ||
if (!properties) { | ||
return {}; | ||
var convert = function (schema) { | ||
if (['object', 'array'].indexOf(schema.type) < 0) { | ||
return schema.type || {}; | ||
} | ||
for (var key in properties) { | ||
if (!properties.hasOwnProperty(key)) continue; | ||
properties[key] = convert(properties[key]); | ||
if (typeof properties[key] === 'undefined') { | ||
delete properties[key]; | ||
} | ||
else if (schema.type === 'object') { | ||
var properties = schema.properties; | ||
if (!properties) { | ||
return {}; | ||
} | ||
for (var key in properties) { | ||
if (!properties.hasOwnProperty(key)) | ||
continue; | ||
properties[key] = convert(properties[key]); | ||
if (typeof properties[key] === 'undefined') { | ||
delete properties[key]; | ||
} | ||
} | ||
return properties; | ||
} | ||
return properties; | ||
} else if (schema.type === 'array') { | ||
if (!schema.items) { | ||
return []; | ||
} | ||
// minimum item count | ||
var ct = schema.minItems || 0; | ||
// tuple-typed arrays | ||
if (schema.items.constructor === Array) { | ||
var _values = schema.items.map(function (item) { | ||
return convert(item); | ||
}); | ||
// remove undefined items at the end (unless required by minItems) | ||
for (var i = _values.length - 1; i >= 0; i--) { | ||
if (typeof _values[i] !== 'undefined') { | ||
break; | ||
else if (schema.type === 'array') { | ||
if (!schema.items) { | ||
return []; | ||
} | ||
if (i + 1 > ct) { | ||
_values.pop(); | ||
// minimum item count | ||
var ct = schema.minItems || 0; | ||
// tuple-typed arrays | ||
if (schema.items.constructor === Array) { | ||
var values_1 = schema.items.map(function (item) { return convert(item); }); | ||
// remove undefined items at the end (unless required by minItems) | ||
for (var i = values_1.length - 1; i >= 0; i--) { | ||
if (typeof values_1[i] !== 'undefined') { | ||
break; | ||
} | ||
if (i + 1 > ct) { | ||
values_1.pop(); | ||
} | ||
} | ||
return values_1; | ||
} | ||
} | ||
return _values; | ||
// object-typed arrays | ||
var value = convert(schema.items); | ||
if (typeof value === 'undefined') { | ||
return []; | ||
} | ||
var values = []; | ||
for (var i = 0; i < Math.max(1, ct); i++) { | ||
values.push(clone(value)); | ||
} | ||
return values; | ||
} | ||
// object-typed arrays | ||
var value = convert(schema.items); | ||
if (typeof value === 'undefined') { | ||
return []; | ||
} | ||
var values = []; | ||
for (var _i = 0; _i < Math.max(1, ct); _i++) { | ||
values.push(clone(value)); | ||
} | ||
return values; | ||
} | ||
// 不符合条件 | ||
return {}; | ||
// 不符合条件 | ||
return {}; | ||
}; | ||
function main(schema) { | ||
if ((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) !== 'object') return {}; | ||
return convert(clone(schema)); | ||
if (typeof schema !== 'object') | ||
return {}; | ||
return convert(clone(schema)); | ||
} | ||
module.exports = main; | ||
exports.default = main; |
@@ -1,69 +0,37 @@ | ||
'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _ = require('lodash'); | ||
module.exports = structToSchema; | ||
var rules = [[_.isString, function (value) { | ||
return handle(value); | ||
}], | ||
// Empty array -> array of any items | ||
[function (data) { | ||
return _.isArray(data) && !data.length; | ||
}, function () { | ||
return { type: 'array' }; | ||
}], [_.isArray, function (items) { | ||
return { type: 'array', items: structToSchema(items[0]) }; | ||
}], [_.isPlainObject, function (object) { | ||
return { | ||
type: 'object', | ||
required: _.keys(object), | ||
properties: _.mapValues(object, structToSchema) | ||
}; | ||
}]]; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
exports.default = structToSchema; | ||
var rules = [ | ||
[_.isString, function (value) { return handle(value); }], | ||
// Empty array -> array of any items | ||
[function (data) { return _.isArray(data) && !data.length; }, function () { return ({ type: 'array' }); }], | ||
[ | ||
_.isArray, | ||
function (items) { return ({ type: 'array', items: structToSchema(items[0]) }); }, | ||
], | ||
[ | ||
_.isPlainObject, | ||
function (object) { return ({ | ||
type: 'object', | ||
required: _.keys(object), | ||
properties: _.mapValues(object, structToSchema), | ||
}); }, | ||
], | ||
]; | ||
function handle(value) { | ||
return { | ||
type: value, | ||
default: '', | ||
description: '' | ||
}; | ||
return { | ||
type: value, | ||
default: '', | ||
description: '', | ||
}; | ||
} | ||
function structToSchema(data) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = rules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref = _step.value; | ||
var _ref2 = _slicedToArray(_ref, 2); | ||
var isMatch = _ref2[0]; | ||
var makeSchema = _ref2[1]; | ||
if (isMatch(data)) { | ||
return makeSchema(data); | ||
} | ||
for (var _i = 0, rules_1 = rules; _i < rules_1.length; _i++) { | ||
var _a = rules_1[_i], isMatch = _a[0], makeSchema = _a[1]; | ||
if (isMatch(data)) { | ||
return makeSchema(data); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
throw new TypeError(data); | ||
} | ||
throw new TypeError(data); | ||
} |
{ | ||
"name": "leaf-converter", | ||
"version": "1.11.0", | ||
"description": "", | ||
"version": "1.10.0", | ||
"main": "lib/index.js", | ||
"author": { | ||
"name": "forsigner", | ||
"email": "forsigner@gmail.com" | ||
"scripts": { | ||
"build": "rollup -c", | ||
"build:watch": "rollup -c -w", | ||
"test": "npm run compile && node example/json-comment-parser", | ||
"compile": "tsc -b", | ||
"compile:watch": "tsc -b -w", | ||
"release": "standard-version" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -16,27 +19,13 @@ "express", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/forsigner/leaf-converter.git" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-stage-1": "^6.24.1", | ||
"eslint": "^4.3.0", | ||
"eslint-config-egg": "^5.0.0", | ||
"standard-version": "^4.2.0" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"dist/**/*", | ||
"lib/**/*" | ||
], | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/forsigner/leaf-converter.git" | ||
}, | ||
"scripts": { | ||
"test": "npm run compile && node example/json-comment-parser", | ||
"compile": "babel src -d lib", | ||
"release": "standard-version" | ||
}, | ||
"author": "forsigner", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -46,2 +35,19 @@ "url": "https://github.com/forsigner/leaf-converter/issues" | ||
"homepage": "https://github.com/forsigner/leaf-converter", | ||
"devDependencies": { | ||
"@types/acorn": "^4.0.3", | ||
"@types/comment-json": "^1.1.1", | ||
"@types/estraverse": "^0.0.6", | ||
"@types/lodash": "^4.14.116", | ||
"rollup": "^0.64.1", | ||
"rollup-plugin-typescript": "^0.8.1", | ||
"standard-version": "^4.4.0", | ||
"typescript": "^3.0.1" | ||
}, | ||
"dependencies": { | ||
"acorn": "^5.7.1", | ||
"comment-json": "^1.1.3", | ||
"estraverse": "^4.2.0", | ||
"lodash": "^4.17.10", | ||
"simple-markdown": "^0.4.1" | ||
}, | ||
"_npmUser": { | ||
@@ -56,12 +62,3 @@ "name": "forsigner", | ||
} | ||
], | ||
"dependencies": { | ||
"acorn": "^5.3.0", | ||
"comment-json": "^1.1.3", | ||
"estraverse": "^4.2.0", | ||
"js-yaml": "^3.10.0", | ||
"json5": "^0.5.1", | ||
"lodash": "^4.17.4", | ||
"simple-markdown": "^0.3.1" | ||
} | ||
] | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
86231
5
55
2429
8
7
+ Addedsimple-markdown@0.4.4(transitive)
- Removedjs-yaml@^3.10.0
- Removedjson5@^0.5.1
- Removedargparse@1.0.10(transitive)
- Removedesprima@4.0.1(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedjson5@0.5.1(transitive)
- Removedsimple-markdown@0.3.3(transitive)
- Removedsprintf-js@1.0.3(transitive)
Updatedacorn@^5.7.1
Updatedlodash@^4.17.10
Updatedsimple-markdown@^0.4.1