New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hapi-swagger

Package Overview
Dependencies
Maintainers
1
Versions
163
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-swagger - npm Package Compare versions

Comparing version 0.4.2 to 0.5.0

test/response-message-test.js

70

lib/index.js

@@ -7,3 +7,3 @@

Joi = require('joi'),
Path = require('path');
Path = require('path'),
Crypto = require('crypto');

@@ -102,7 +102,7 @@

validate: {
query: {
query: Joi.object().keys({
path: Joi.string(),
tags: Joi.string(),
api_key: Joi.string()
}
})
},

@@ -238,2 +238,3 @@ handler: function (request, reply) {

var routeOptions = route.settings.plugins ? route.settings.plugins['hapi-swagger'] : {};
var routeData = {

@@ -248,3 +249,5 @@ path: route.path,

payloadParams: route.settings.validate && route.settings.validate.payload,
responseSchema: route.settings.response && route.settings.response.schema
responseSchema: route.settings.response && route.settings.response.schema,
headerParams: route.settings.validate && route.settings.validate.headers,
responseMessages: routeOptions && routeOptions.responseMessages || []
};

@@ -357,10 +360,8 @@

var notesStatus = internals.getResponseMessages(route.notes);
if (notesStatus.responseMessages.length > -1) {
op.responseMessages = notesStatus.responseMessages;
op.notes = notesStatus.notes;
}
op.notes = Array.isArray(route.notes) ? route.notes.join('<br/><br/>') : route.notes;
op.responseMessages = route.responseMessages;
var pathParam = internals.getParams(route, 'pathParams')
var queryParam = internals.getParams(route, 'queryParams')
var headerParam = internals.getParams(route, 'headerParams')

@@ -370,2 +371,3 @@ // build up swagger properties for route validation

var queryProperties = internals.validatorsToProperties(queryParam, swagger.models);
var headerProperties = internals.validatorsToProperties(headerParam, swagger.models);
var payloadApiParams;

@@ -392,2 +394,3 @@

op.parameters = op.parameters.concat(
internals.propertiesToAPIParams(headerProperties, 'header'),
internals.propertiesToAPIParams(pathProperties, 'path'),

@@ -499,3 +502,3 @@ internals.propertiesToAPIParams(queryProperties, 'query'),

}
}
}

@@ -553,3 +556,3 @@ return properties;

if (property.type === 'object' && param._inner) {
var className = undefined;
var className = undefined;
var param = (param._inner.children) ? param._inner.children : param._inner

@@ -604,48 +607,3 @@ property.type = internals.validatorsToModelName(

internals.getResponseMessages = function (notes) {
var out = {
notes: [],
responseMessages: []
};
if (Array.isArray(notes)) {
var i = notes.length,
x = 0,
hasCodes = false;
while (x < i) {
if (notes[x].toLowerCase().trim().indexOf('error status codes') === 0) {
hasCodes = true;
}
if (hasCodes === true) {
var items = [],
num,
note = notes[x];
if (note.indexOf(',') > -1) {
items = note.split(',');
if (isNaN(items[0]) === false) {
num = parseInt(items[0]);
if (num > 99 && num < 600) {
out.responseMessages.push({
'code': num,
'message': items[1].trim()
});
}
}
}
} else {
out.notes.push(notes[x]);
}
x++;
}
out.notes = out.notes.join('<br/><br/>');
} else {
out.notes = notes;
}
return out;
};
// create a model from an object of Joi validators. Return the model name

@@ -652,0 +610,0 @@ internals.validatorsToModelName = function (name, params, models) {

2

package.json
{
"name": "hapi-swagger",
"description": "A swagger documentation UI generator plugin for hapi",
"version": "0.4.2",
"version": "0.5.0",
"author": "Glenn Jones",

@@ -6,0 +6,0 @@ "repository": {

@@ -192,3 +192,3 @@ # hapi-swagger

### Error Status Codes
You can add HTTP error status codes to each of the endpoints. As HAPI routes don not directly have a property for error status codes so you need to add them to the notes. The status codes need to be added to the end of the notes array starting with array item with the value "Error status codes". Each error code should be added as string with code first follow by its description:
You can add HTTP error status codes to each of the endpoints. As HAPI routes don not directly have a property for error status codes so you need to add them the plugin configuration. The status codes need to be added as an array of objects with an error code and description:

@@ -200,7 +200,11 @@ config: {

jsonp: 'callback',
notes: [
'Adds together two numbers and return the result',
'Error status codes',
'400, bad request'
],
notes: ['Adds together two numbers and return the result'],
plugins: {
'hapi-swagger': {
responseMessages: [
{ code: 400, message: 'Bad Request' },
{ code: 500, message: 'Internal Server Error'}
]
}
},
validate: {

@@ -246,2 +250,3 @@ params: {

* John Brett (https://github.com/johnbrett)
* Felipe Leusin (https://github.com/felipeleusin)

@@ -248,0 +253,0 @@

@@ -7,6 +7,9 @@ /*

var chai = require('chai'),
Hapi = require('hapi'),
assert = chai.assert,
swagger = require('../lib/index.js');
var internals = swagger._internals;
var defaultHandler = function(request, response) {
reply('ok');
};

@@ -16,5 +19,2 @@ var testNotes1 = [

'This is more stuff about this API endpoint',
'Error Status Codes',
'400, Bad Request',
'404, Sum not found'
],

@@ -24,96 +24,78 @@ testNotes2 = [

],
testNotes3 = 'This is a note about this API endpoint',
testNotes4 = [
'This is a note about this API endpoint',
'This is more stuff about this API endpoint',
'Error status codes:',
'400, Bad Request',
'404, Sum not found'
]
testNotes3 = 'This is a note about this API endpoint';
describe('notes test', function() {
var server;
describe('notes complex array test', function() {
var obj = internals.getResponseMessages( testNotes1 ),
responseMessages = obj.responseMessages,
notes = obj.notes;
it('notes has <br/><br/> injected', function() {
assert.equal( notes, 'This is a note about this API endpoint<br/><br/>This is more stuff about this API endpoint' );
beforeEach(function(done) {
server = new Hapi.Server({debug: false});
server.pack.register(swagger, function(err) {
assert.ifError(err);
done();
});
});
it('responseMessages has 2 objects', function() {
assert.equal( responseMessages.length, 2 );
afterEach(function(done) {
server.stop(function() {
server = null;
done();
});
});
it('responseMessages has code', function() {
assert.equal( responseMessages[0].code, 400 );
});
describe('if notes array', function() {
it('responseMessages has message', function() {
assert.equal( responseMessages[0].message, 'Bad Request' );
});
it('when array length > 1 notes has <br/><br/> injected', function(done) {
server.route({
method: 'GET',
path: '/test',
handler: defaultHandler,
config: {
tags: ['api'],
notes: testNotes1
}
});
server.inject({ method: 'GET', url: '/docs?path=test '}, function (response) {
assert.equal(response.result.apis[0].operations[0].notes, 'This is a note about this API endpoint<br/><br/>This is more stuff about this API endpoint' );
done();
});
});
it('responseMessages has code', function() {
assert.equal( responseMessages[1].code, 404 );
it('array length equal 1 notes has no <br/>', function(done) {
server.route({
method: 'GET',
path: '/test',
handler: defaultHandler,
config: {
tags: ['api'],
notes: testNotes2
}
});
server.inject({ method: 'GET', url: '/docs?path=test '}, function (response) {
assert.equal(response.result.apis[0].operations[0].notes, 'This is a note about this API endpoint' );
done();
});
});
});
it('responseMessages has message', function() {
assert.equal( responseMessages[1].message, 'Sum not found' );
});
describe('if notes is string', function() {
});
describe('notes simple array test', function() {
var obj = internals.getResponseMessages( testNotes2 ),
responseMessages = obj.responseMessages,
notes = obj.notes;
it('notes has no <br/>', function() {
assert.equal( notes, 'This is a note about this API endpoint' );
beforeEach(function() {
server.route({
method: 'GET',
path: '/test',
handler: defaultHandler,
config: {
tags: ['api'],
notes: testNotes3
}
});
});
it('returns the string', function(done) {
server.inject({ method: 'GET', url: '/docs?path=test '}, function (response) {
assert.equal(response.result.apis[0].operations[0].notes, testNotes3 );
done();
});
});
});
it('responseMessages is an empty array', function() {
assert.equal( responseMessages.length, 0 );
});
});
describe('notes string test', function() {
var obj = internals.getResponseMessages( testNotes3 ),
responseMessages = obj.responseMessages,
notes = obj.notes;
it('notes contains string', function() {
assert.equal( notes, 'This is a note about this API endpoint' );
});
it('responseMessages is an empty array', function() {
assert.equal( responseMessages.length, 0 );
});
});
describe('notes mixed case detection test', function() {
var obj = internals.getResponseMessages( testNotes4 ),
responseMessages = obj.responseMessages,
notes = obj.notes;
it('responseMessages has 2 objects', function() {
assert.equal( responseMessages.length, 2 );
});
});
});

@@ -12,3 +12,3 @@ /*

describe('isPrefix test', function() {
describe('prefix test', function() {

@@ -15,0 +15,0 @@ it('isPrefix default', function(){

@@ -13,3 +13,3 @@ /*

// Based on pull request by David Waterston - http://jsfiddle.net/davidwaterston/cC4v8/
describe('isResourceRoute parsing test', function() {
describe('route parsing test', function() {

@@ -16,0 +16,0 @@ var routes = [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc