api-doc-validator
Advanced tools
Comparing version 2.7.0 to 2.7.1
@@ -5,3 +5,3 @@ module.exports = ajvToJsDoc; | ||
function ajvToJsDoc(schema, params = {}) { | ||
const {schemas = {}, any = '*'} = params; | ||
const {schemas = {}, any = '*', jsDocNull = true} = params; | ||
const {type, title} = schema; | ||
@@ -11,3 +11,9 @@ | ||
if (type === 'string' || type === 'number' || type === 'boolean') { | ||
if (type === 'string' || type === 'number') { | ||
if (!schema.enum) return type; | ||
return !schema.enum ? type : schema.enum.map(v => JSON.stringify(v)).join('|'); | ||
} | ||
if (type === 'boolean' || (type === 'null' && !jsDocNull)) { | ||
return type; | ||
@@ -26,3 +32,3 @@ } | ||
if (!schema.items) { | ||
return 'Array'; | ||
return `Array<${any}>`; | ||
} | ||
@@ -51,3 +57,3 @@ | ||
if (anyOf && anyOf.length === 2 && anyOf.some(item => item.type === 'null')) { | ||
if (jsDocNull && anyOf && anyOf.length === 2 && anyOf.some(item => item.type === 'null')) { | ||
return '?' + ajvToJsDoc(anyOf.find(item => item.type !== 'null')); | ||
@@ -54,0 +60,0 @@ } |
@@ -7,2 +7,3 @@ const {readFileSync, createWriteStream} = require('fs'); | ||
const ajvToJsDoc = require('../ajvToJsDoc'); | ||
const {normalizeName} = ajvToJsDoc; | ||
@@ -68,4 +69,7 @@ module.exports = function ( | ||
if (jsdocTypedefs) { | ||
for (const name in schemas) { | ||
stream.write(`/** @typedef {${ajvToJsDoc(schemas[name])}} ${ajvToJsDoc.normalizeName(name)} */\n`); | ||
for (let name in schemas) { | ||
name = normalizeName(name); | ||
const schema = ajvToJsDoc(schemas[name]); | ||
stream.write(`/** @typedef {${schema}} ${name} */\n`); | ||
} | ||
@@ -153,3 +157,3 @@ } | ||
const methodJs = (method, tag, indent = 1) => createMethod({tpl: methodTpl, outputDir, endpoints, indent, method, tag, schemas}); | ||
const methodDts = (method, tag, indent = 1) => createMethod({tpl: `method = (params) => Promise<*>`, outputDir, endpoints, indent, method, tag, schemas}); | ||
const methodDts = (method, tag, indent = 1) => createMethod({tpl: `method = (params) => Promise<*>`, outputDir, endpoints, indent, method, tag, schemas, any: 'any', jsDocNull: false}); | ||
@@ -195,4 +199,9 @@ for (const prop in ns) { | ||
for (const name in schemas) { | ||
dtsStream.write(`\ninterface ${ajvToJsDoc.normalizeName(name)} ${ajvToJsDoc(schemas[name], {any: 'any'})}`); | ||
for (let name in schemas) { | ||
name = normalizeName(name); | ||
const schema = ajvToJsDoc(schemas[name], {any: 'any', jsDocNull: false}); | ||
const type = schema.charAt(0) === '{' && schema.charAt(schema.length - 1) === '}' ? 'interface' : 'type'; | ||
const eq = type === 'type' ? ' = ' : ' '; | ||
dtsStream.write(`\n${type} ${name}${eq}${schema}`); | ||
} | ||
@@ -247,6 +256,6 @@ | ||
function createMethod({tpl, endpoints, outputDir, indent, method, tag, schemas, any = '*'}) { | ||
function createMethod({tpl, endpoints, outputDir, indent, method, tag, schemas, any = '*', jsDocNull = true}) { | ||
const n = tag.match(/\d+$/)[0]; | ||
const e = endpoints[n]; | ||
const jsDoc = (schema) => ajvToJsDoc(schema, {schemas, any}); | ||
const jsDoc = (schema) => ajvToJsDoc(schema, {schemas, any, jsDocNull}); | ||
@@ -316,3 +325,3 @@ | ||
case 'Promise<*>': | ||
type = '*'; | ||
type = any; | ||
@@ -319,0 +328,0 @@ if (e.response) { |
{ | ||
"name": "api-doc-validator", | ||
"version": "2.7.0", | ||
"version": "2.7.1", | ||
"description": "api doc and validator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
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
57815
1612