hapi-rest-methods
Advanced tools
Comparing version 1.0.1 to 1.1.0
'use strict'; | ||
function decorateServerMethod(server, methodName) { | ||
server.decorate('server', methodName, function(path, handler) { | ||
server.route({ | ||
method: methodName, | ||
path: path, | ||
handler: handler | ||
}); | ||
server.decorate('server', methodName, function(path, options, handler) { | ||
if(typeof options == "function") { | ||
handler = options; | ||
options = { handler: handler }; | ||
} else { | ||
options.handler = handler; | ||
} | ||
options.path = path; | ||
options.method = methodName; | ||
if (options.method === 'any') options.method = '*'; | ||
server.route(options); | ||
}); | ||
@@ -14,3 +19,3 @@ } | ||
exports.register = function(server, options, next) { | ||
['get', 'post', 'put', 'patch', 'delete', 'options'].forEach(function(methodName) { | ||
['any', 'get', 'post', 'put', 'patch', 'delete', 'options'].forEach(function(methodName) { | ||
decorateServerMethod(server, methodName); | ||
@@ -17,0 +22,0 @@ }); |
{ | ||
"name": "hapi-rest-methods", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Add REST HTTP methods directly to server object of hapi.JS framework to easily add routes", | ||
@@ -10,3 +10,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"test": "jasmine" | ||
"test": "node spec/jasmine-runner.js" | ||
}, | ||
@@ -35,4 +35,6 @@ "repository": { | ||
"hapi": "^13.3.0", | ||
"jasmine": "^2.4.1" | ||
"jasmine": "^2.4.1", | ||
"jasmine-spec-reporter": "^2.4.0", | ||
"request": "^2.71.0" | ||
} | ||
} |
@@ -31,3 +31,3 @@ # hapi-rest-methods | ||
It also supports `post()`, `put()`, `patch()`, `delete()` and `options()`. | ||
It also supports `.post()`, `.put()`, `.patch()`, `.delete()` and `.options()`. Use `.any()` to match all of the (alias for `*` method). | ||
@@ -65,2 +65,18 @@ | ||
And if you need to [config routes](http://hapijs.com/api#route-configuration), just pass 3 parameters `(path, config, handler)`: | ||
``` | ||
var routeConfig = config: { | ||
description: 'Say hello!', | ||
notes: 'The user parameter defaults to \'stranger\' if unspecified', | ||
tags: ['api', 'greeting'], | ||
auth: { ... }, | ||
cache: { ... } | ||
// ... | ||
} | ||
server.delete('/the-internet', routeConfig, function(request, reply) { | ||
reply('...'); | ||
}); | ||
``` | ||
## Issues & Contributing | ||
@@ -67,0 +83,0 @@ |
describe("hapi-rest-methods", function() { | ||
var testMethods = ['any', 'get', 'post', 'put', 'patch', 'delete', 'options']; | ||
var hapi = require('hapi'); | ||
var restMethods = require('../') | ||
var server = new hapi.Server(); | ||
server.connection({ port: 8080 }); | ||
server.register(restMethods); | ||
var request = require('request'); | ||
var restMethods = require('../lib/index') | ||
['get', 'post', 'put', 'patch', 'delete', 'options'].forEach(function(methodName) { | ||
it("should decorate hapi.server and make it respond to ." + methodName + "()", function() { | ||
expect(typeof server[methodName]).toBe("function"); | ||
var server; | ||
beforeEach(function() { | ||
server = new hapi.Server(); | ||
server.connection({ port: 8080 }); | ||
server.register(restMethods); | ||
}); | ||
afterEach(function() { | ||
server.stop(); | ||
server = null; | ||
}); | ||
function it_for(specName, methodName, callback) { | ||
it(specName + " for " + methodName + "()", callback); | ||
} | ||
describe("when configuring server", function() { | ||
testMethods.forEach(function(methodName) { | ||
it_for("should decorate hapi.server and make it respond", methodName, function() { | ||
expect(typeof server[methodName]).toBe("function"); | ||
}); | ||
}); | ||
}); | ||
describe("when running server", function() { | ||
function testServer(methodName, path) { | ||
server.start(function(err) { | ||
if(err) throw err; | ||
if(methodName === 'any') methodName = 'patch'; // this can be anything, choosing patch for convenience | ||
request({ uri: server.info.uri + path, method: methodName }, function(error, response, body) { | ||
expect(response.statusCode).toBe(200); | ||
expect(body).toBe('test'); | ||
}); | ||
}); | ||
} | ||
function routeHandler(done) { | ||
return function(response, reply) { | ||
reply('test'); | ||
done(); | ||
} | ||
} | ||
testMethods.forEach(function(methodName) { | ||
it_for("should create route path and handler", methodName, function(done) { | ||
var path = '/test/' + methodName; | ||
server[methodName](path, routeHandler(done)); | ||
testServer(methodName, path); | ||
}); | ||
it_for("should create route path, config and handler", methodName, function(done) { | ||
var path = '/test/' + methodName; | ||
server[methodName](path, {}, routeHandler(done)); | ||
testServer(methodName, path); | ||
}); | ||
}); | ||
}); | ||
}); |
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
8001
9
96
86
4