hapi-swagger
Advanced tools
Comparing version 14.5.4 to 14.5.5
@@ -86,9 +86,10 @@ const Hoek = require('@hapi/hoek'); | ||
} else { | ||
//console.log('b', JSON.stringify(schemaObj) + '\n'); | ||
// object to array | ||
const keys = Object.keys(schemaObj.properties); | ||
keys.forEach((element, index) => { | ||
const key = keys[index]; | ||
Object.keys(schemaObj.properties).forEach((key) => { | ||
let item = schemaObj.properties[key]; | ||
if (typeof item === 'undefined' ) { | ||
return; | ||
} | ||
item.name = key; | ||
@@ -99,7 +100,7 @@ item.in = parameterType; | ||
delete item.required; | ||
if (schemaObj.required && (schemaObj.properties[key].required || schemaObj.required.indexOf(key) > -1)) { | ||
if (schemaObj.required && (item.required || schemaObj.required.includes(key))) { | ||
item.required = true; | ||
} | ||
if (schemaObj.optional && schemaObj.optional.indexOf(key) > -1) { | ||
if (schemaObj.optional && schemaObj.optional.includes(key)) { | ||
item.required = false; | ||
@@ -106,0 +107,0 @@ } |
145
lib/paths.js
@@ -44,4 +44,3 @@ const Hoek = require('@hapi/hoek'); | ||
* | ||
* @param {Object} setting | ||
* @param {Object} routes | ||
* @param {Array} routes | ||
* @return {Object} | ||
@@ -57,24 +56,3 @@ */ | ||
const routeOptions = Hoek.reach(route, 'settings.plugins.hapi-swagger') || {}; | ||
const routeData = { | ||
path: route.path, | ||
method: route.method.toUpperCase(), | ||
description: route.settings.description, | ||
notes: route.settings.notes, | ||
tags: Hoek.reach(route, 'settings.tags'), | ||
queryParams: Hoek.reach(route, 'settings.validate.query'), | ||
pathParams: Hoek.reach(route, 'settings.validate.params'), | ||
payloadParams: Hoek.reach(route, 'settings.validate.payload'), | ||
responseSchema: Hoek.reach(route, 'settings.response.schema'), | ||
responseStatus: Hoek.reach(route, 'settings.response.status'), | ||
headerParams: Hoek.reach(route, 'settings.validate.headers'), | ||
consumes: Hoek.reach(routeOptions, 'consumes') || null, | ||
produces: Hoek.reach(routeOptions, 'produces') || null, | ||
responses: Hoek.reach(routeOptions, 'responses') || null, | ||
payloadType: Hoek.reach(routeOptions, 'payloadType') || null, | ||
security: Hoek.reach(routeOptions, 'security') || null, | ||
order: Hoek.reach(routeOptions, 'order') || null, | ||
deprecated: Hoek.reach(routeOptions, 'deprecated') || null, | ||
id: Hoek.reach(routeOptions, 'id') || null, | ||
groups: route.group | ||
}; | ||
const routeData = internals.createRouteMetaData(route, routeOptions); | ||
@@ -115,7 +93,7 @@ Utilities.assignVendorExtensions(routeData, routeOptions); | ||
); | ||
if (property === 'payloadParams') { | ||
routeData[property] = Joi.object().label('Hidden Model'); | ||
} else { | ||
routeData[property] = Joi.object({ 'Hidden Model': Joi.string() }); | ||
} | ||
routeData[property] = property === 'payloadParams' | ||
? Joi.object().label('Hidden Model') | ||
: Joi.object({ 'Hidden Model': Joi.string() }); | ||
} else { | ||
@@ -131,13 +109,3 @@ self.settings.log( | ||
// hapi wildcard method support | ||
if (routeData.method === '*') { | ||
// OPTIONS not supported by Swagger and HEAD not support by Hapi | ||
['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].forEach(method => { | ||
const newRoute = Hoek.clone(routeData); | ||
newRoute.method = method.toUpperCase(); | ||
routesData.push(newRoute); | ||
}); | ||
} else { | ||
routesData.push(routeData); | ||
} | ||
routesData.push(...internals.getMultiMethodRoutes(routeData)); | ||
}); | ||
@@ -149,6 +117,53 @@ | ||
/** | ||
* Create route meta data | ||
* @param {Object} route | ||
* @param {Object} routeOptions | ||
* @returns {Object} | ||
*/ | ||
internals.createRouteMetaData = function(route, routeOptions) { | ||
return { | ||
path: route.path, | ||
method: route.method.toUpperCase(), | ||
description: route.settings.description, | ||
notes: route.settings.notes, | ||
tags: Hoek.reach(route, 'settings.tags'), | ||
queryParams: Hoek.reach(route, 'settings.validate.query'), | ||
pathParams: Hoek.reach(route, 'settings.validate.params'), | ||
payloadParams: Hoek.reach(route, 'settings.validate.payload'), | ||
responseSchema: Hoek.reach(route, 'settings.response.schema'), | ||
responseStatus: Hoek.reach(route, 'settings.response.status'), | ||
headerParams: Hoek.reach(route, 'settings.validate.headers'), | ||
consumes: Hoek.reach(routeOptions, 'consumes') || null, | ||
produces: Hoek.reach(routeOptions, 'produces') || null, | ||
responses: Hoek.reach(routeOptions, 'responses') || null, | ||
payloadType: Hoek.reach(routeOptions, 'payloadType') || null, | ||
security: Hoek.reach(routeOptions, 'security') || null, | ||
order: Hoek.reach(routeOptions, 'order') || null, | ||
deprecated: Hoek.reach(routeOptions, 'deprecated') || null, | ||
id: Hoek.reach(routeOptions, 'id') || null, | ||
groups: route.group | ||
}; | ||
} | ||
/** | ||
* Handle the case when route's method is declared with wildcard syntax | ||
* @param {Object} route | ||
* @returns {*[]} | ||
*/ | ||
internals.getMultiMethodRoutes = function(route) { | ||
return route.method !== '*' | ||
? [Hoek.clone(route)] | ||
: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].map((method) => { | ||
// OPTIONS not supported by Swagger and HEAD not support by Hapi | ||
return { | ||
...Hoek.clone(route), | ||
method | ||
}; | ||
}); | ||
} | ||
/** | ||
* build the swagger path section from hapi routes data | ||
* | ||
* @param {Object} setting | ||
* @param {Object} routes | ||
* @param {Array} routes | ||
* @return {Object} | ||
@@ -208,3 +223,3 @@ */ | ||
let payloadStructures = this.getDefaultStructures(); | ||
const payloadJoi = internals.getJOIObj(route, 'payloadParams'); | ||
const payloadJoi = route.payloadParams; | ||
if (payloadType.toLowerCase() === 'json') { | ||
@@ -224,7 +239,5 @@ // set as json | ||
// change form mimetype based on meta property 'swaggerType' | ||
if (internals.hasFileType(route)) { | ||
out.consumes = ['multipart/form-data']; | ||
} else { | ||
out.consumes = ['application/x-www-form-urlencoded']; | ||
} | ||
out.consumes = internals.hasFileType(route) | ||
? ['multipart/form-data'] | ||
: ['application/x-www-form-urlencoded']; | ||
} | ||
@@ -234,5 +247,4 @@ } | ||
// set required true/false for each path params | ||
//const pathParams = this.properties.toParameters (internals.getJOIObj(route, 'pathParams'), 'path', false); | ||
let pathStructures = this.getDefaultStructures(); | ||
const pathJoi = internals.getJOIObj(route, 'pathParams'); | ||
const pathJoi = route.pathParams; | ||
if (Utilities.hasJoiChildren(pathJoi)) { | ||
@@ -274,5 +286,4 @@ pathStructures = this.getSwaggerStructures(pathJoi, 'path', false, false); | ||
//const headerParams = this.properties.toParameters (internals.getJOIObj(route, 'headerParams'), 'header', false); | ||
let headerStructures = this.getDefaultStructures(); | ||
const headerJoi = internals.getJOIObj(route, 'headerParams'); | ||
const headerJoi = route.headerParams; | ||
if (Utilities.hasJoiChildren(headerJoi)) { | ||
@@ -299,3 +310,3 @@ headerStructures = this.getSwaggerStructures(headerJoi, 'header', false, false); | ||
let queryStructures = this.getDefaultStructures(); | ||
const queryJoi = internals.getJOIObj(route, 'queryParams'); | ||
const queryJoi = route.queryParams; | ||
if (Utilities.hasJoiChildren(queryJoi)) { | ||
@@ -345,17 +356,2 @@ queryStructures = this.getSwaggerStructures(queryJoi, 'query', false, false); | ||
/** | ||
* gets the JOI object from route object | ||
* | ||
* @param {Object} route | ||
* @param {string} name | ||
* @return {Object} | ||
*/ | ||
internals.getJOIObj = function(route, name) { | ||
const prama = route[name]; | ||
// if (Utilities.hasJoiChildren(route[name])) { | ||
// prama = route[name]._inner.children; | ||
// } | ||
return prama; | ||
}; | ||
/** | ||
* overload one object with another | ||
@@ -382,3 +378,3 @@ * | ||
}); | ||
return payloadParamsString.indexOf('swaggerType') > -1; | ||
return payloadParamsString.includes('swaggerType'); | ||
}; | ||
@@ -420,9 +416,3 @@ | ||
internals.hasContentTypeHeader = function(path) { | ||
let out = false; | ||
path.parameters.forEach((prama) => { | ||
if (prama.in === 'header' && prama.name.toLowerCase() === 'content-type') { | ||
out = true; | ||
} | ||
}); | ||
return out; | ||
return path.parameters.some((parameter) => parameter.in === 'header' && parameter.name.toLowerCase() === 'content-type'); | ||
}; | ||
@@ -449,7 +439,6 @@ | ||
const out = { | ||
return { | ||
properties: outProperties || {}, | ||
parameters: outParameters || [] | ||
}; | ||
return out; | ||
}; | ||
@@ -456,0 +445,0 @@ |
@@ -403,10 +403,11 @@ const Hoek = require('@hapi/hoek'); | ||
* | ||
* @param {Object} property | ||
* @param {Object} input | ||
* @param {Object} joiObj | ||
* @return {Object} | ||
*/ | ||
internals.properties.prototype.parseDate = function (property, joiObj) { | ||
internals.properties.prototype.parseDate = function (input, joiObj) { | ||
const dateFormat = Hoek.reach(joiObj, '_flags.format'); | ||
const property = Hoek.clone(input); | ||
if (dateFormat === 'timestamp' || dateFormat === 'javascript') { | ||
if (['timestamp', 'javascript'].includes(dateFormat)) { | ||
// Seems like the exact name of the format differs for different versions of Joi | ||
@@ -595,6 +596,2 @@ // Javascript is what is set by Joi.date().timestamp() | ||
//if (!property.$ref && Utilities.getJoiLabel(joiObj)) { | ||
// property.name = Utilities.getJoiLabel(joiObj); | ||
//} | ||
return property; | ||
@@ -693,2 +690,3 @@ }; | ||
// TODO check why we return true if we must return a string or undefined | ||
return true; | ||
@@ -703,3 +701,3 @@ } | ||
/** | ||
* return existance of an item in array of - structure [ { name: 'value' } ] | ||
* return existence of an item in array of - structure [ { name: 'value' } ] | ||
* | ||
@@ -711,8 +709,3 @@ * @param {Array} array | ||
internals.hasPropertyByName = function (array, name) { | ||
return ( | ||
array && | ||
array.some((obj) => { | ||
return obj.name === name; | ||
}) | ||
); | ||
return Array.isArray(array) && array.some((obj) => obj.name === name); | ||
}; |
@@ -97,3 +97,3 @@ const HTTPStatus = require('http-status'); | ||
// test for any JOI objects | ||
if (Hoek.reach(userDefindedSchemas[key], 'schema') && Utilities.isJoi(userDefindedSchemas[key].schema) === true) { | ||
if (Hoek.reach(userDefindedSchemas[key], 'schema') && Utilities.isJoi(userDefindedSchemas[key].schema)) { | ||
out = this.getResponse(key, userDefindedSchemas[key].schema, useDefinitions, isAlt); | ||
@@ -109,3 +109,3 @@ out.description = userDefindedSchemas[key].description; | ||
// overwrite discovery with user definded | ||
// overwrite discovery with user defined | ||
if (!discoveredSchemas[key] && out) { | ||
@@ -141,12 +141,9 @@ // if it does not exist create it | ||
internals.responses.prototype.getResponse = function(statusCode, joiObj, useDefinitions) { | ||
let out; | ||
//name, joiObj, parent, parameterType, useDefinitions, isAlt | ||
const outProperties = this.properties.parseProperty(null, joiObj, null, 'body', useDefinitions, false); | ||
out = { | ||
let out = { | ||
description: Hoek.reach(joiObj, '_flags.description'), | ||
schema: outProperties | ||
schema: this.properties.parseProperty(null, joiObj,null,'body', useDefinitions,false), | ||
headers: Utilities.getJoiMetaProperty(joiObj, 'headers'), | ||
examples: Utilities.getJoiMetaProperty(joiObj, 'examples') | ||
}; | ||
out.headers = Utilities.getJoiMetaProperty(joiObj, 'headers'); | ||
out.examples = Utilities.getJoiMetaProperty(joiObj, 'examples'); | ||
if (out.schema !== undefined) { | ||
@@ -153,0 +150,0 @@ delete out.schema['x-meta']; |
{ | ||
"name": "hapi-swagger", | ||
"description": "A swagger documentation UI generator plugin for hapi", | ||
"version": "14.5.4", | ||
"version": "14.5.5", | ||
"author": "Glenn Jones", | ||
@@ -6,0 +6,0 @@ "repository": { |
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
112675
3069