http-methods
Advanced tools
Comparing version 0.1.0 to 1.0.0
48
index.js
@@ -1,44 +0,12 @@ | ||
var contentTypes = require("content-types") | ||
, body = require("body") | ||
, jsonBody = body.json | ||
, formBody = body.form | ||
'use strict'; | ||
module.exports = methods | ||
var FormRequestHandler = require('./form.js'); | ||
var MethodRequestHandler = require('./method.js'); | ||
module.exports = methods; | ||
function methods(routes, handleHttpForms) { | ||
return handleHttpForms ? formRequestHandler : requestHandler | ||
function requestHandler(req) { | ||
var method = req.method | ||
, f = routes[method] || routes.notFound || notFound | ||
return f.apply(this, arguments) | ||
} | ||
function formRequestHandler(req, res) { | ||
var args = arguments | ||
, self = this | ||
contentTypes(req, res, { | ||
"application/json": jsonBody | ||
, "application/x-www-form-urlencoded": formBody | ||
, "default": callRequestHandler | ||
})(req, res, extractMethod) | ||
function callRequestHandler() { | ||
requestHandler.apply(self, args) | ||
} | ||
function extractMethod(err, body) { | ||
var method = body._method | ||
, f = routes[method] || routes.notFound || notFound | ||
return f.apply(self, args) | ||
} | ||
} | ||
return handleHttpForms ? | ||
FormRequestHandler(routes) : | ||
MethodRequestHandler(routes); | ||
} | ||
function notFound(req, res) { | ||
res.statusCode = 405 | ||
res.end("405 Method Not Allowed " + req.url) | ||
} |
{ | ||
"name": "http-methods", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Handle multiple methods elegantly", | ||
@@ -20,7 +20,7 @@ "keywords": [], | ||
"dependencies": { | ||
"content-types": "~0.1.0", | ||
"body": "~0.1.0" | ||
"body": "~0.1.0", | ||
"content-types": "~0.1.0" | ||
}, | ||
"devDependencies": { | ||
"tap": "0.2.5", | ||
"tape": "^3.0.3", | ||
"test-server": "0.0.1" | ||
@@ -35,4 +35,4 @@ }, | ||
"scripts": { | ||
"test": "tap --stderr --tap ./test" | ||
"test": "node test/integration.js" | ||
} | ||
} |
@@ -1,27 +0,53 @@ | ||
var test = require("tap").test | ||
, methods = require("..") | ||
, testServer = require("test-server") | ||
'use strict'; | ||
var test = require('tape') | ||
var methods = require('..') | ||
var testServer = require('test-server') | ||
testServer(handleRequest, startTests) | ||
function handleRequest(req, res) { | ||
var get = endValue("get") | ||
, post = endValue("post") | ||
, put = endValue("put") | ||
, del = endValue("del") | ||
var get = endValue('get'); | ||
var post = endValue('post'); | ||
var put = endValue('put'); | ||
var del = endValue('del'); | ||
if (req.url === "/form") { | ||
if (req.url === '/form') { | ||
return methods({ | ||
"GET": get | ||
, "POST": post | ||
, "PUT": put | ||
, "DELETE": del | ||
}, true).apply(this, arguments) | ||
'GET': get, | ||
'POST': post, | ||
'PUT': put, | ||
'DELETE': del | ||
}, true).call(this, req, res, {}, sendError); | ||
} else if (req.url === '/no-callback') { | ||
try { | ||
methods({ | ||
'GET': get, | ||
'POST': post, | ||
'PUT': put, | ||
'DELETE': del | ||
}).call(this, req, res); | ||
} catch (err) { | ||
return sendError(err); | ||
} | ||
} else if (req.url === '/empty-arg') { | ||
try { | ||
methods().call(this, req, res); | ||
} catch (err) { | ||
return sendError(err); | ||
} | ||
} | ||
methods({ | ||
"PUT": put | ||
, "GET": get | ||
, "POST": post | ||
}).apply(this, arguments) | ||
'PUT': put, | ||
'GET': get, | ||
'POST': post | ||
}).call(this, req, res, {}, sendError); | ||
function sendError(err) { | ||
if (err) { | ||
res.statusCode = err.statusCode || 500; | ||
res.end(err.message); | ||
} | ||
} | ||
} | ||
@@ -34,58 +60,80 @@ | ||
function startTests(request, done) { | ||
test("get", function (t) { | ||
request("/", testMethod("get", t)) | ||
test('throws without cb', function (t) { | ||
request('/no-callback', function (err, res, body) { | ||
t.ifError(err); | ||
t.equal(res.statusCode, 500); | ||
t.equal(res.body, 'callback required'); | ||
t.end(); | ||
}) | ||
}) | ||
test("post", function (t) { | ||
test('throws without opts', function (t) { | ||
request('/empty-arg', function (err, res, body) { | ||
t.ifError(err); | ||
t.equal(res.statusCode, 500); | ||
t.equal(res.body, 'methods must be an object'); | ||
t.end(); | ||
}) | ||
}) | ||
test('get', function (t) { | ||
request('/', testMethod('get', t)) | ||
}) | ||
test('post', function (t) { | ||
request({ | ||
uri: "/" | ||
, method: "POST" | ||
}, testMethod("post", t)) | ||
uri: '/' | ||
, method: 'POST' | ||
}, testMethod('post', t)) | ||
}) | ||
test("put", function (t) { | ||
test('put', function (t) { | ||
request({ | ||
uri: "/" | ||
, method: "PUT" | ||
}, testMethod("put", t)) | ||
uri: '/' | ||
, method: 'PUT' | ||
}, testMethod('put', t)) | ||
}) | ||
test("form get", function (t) { | ||
request("/form", testMethod("get", t)) | ||
test('form get', function (t) { | ||
request('/form', testMethod('get', t)) | ||
}) | ||
test("form post", function (t) { | ||
test('form post', function (t) { | ||
request({ | ||
uri: "/form" | ||
, method: "POST" | ||
}, testMethod("post", t)) | ||
uri: '/form' | ||
, method: 'POST' | ||
}, testMethod('post', t)) | ||
}) | ||
test("form put", function (t) { | ||
test('form put', function (t) { | ||
request({ | ||
uri: "/form" | ||
, method: "POST" | ||
uri: '/form' | ||
, method: 'POST' | ||
, form: { | ||
_method: "PUT" | ||
_method: 'PUT' | ||
} | ||
}, testMethod("put", t)) | ||
}, testMethod('put', t)) | ||
}) | ||
test("form delete", function (t) { | ||
test('form delete', function (t) { | ||
request({ | ||
uri: "/form" | ||
, method: "POST" | ||
uri: '/form' | ||
, method: 'POST' | ||
, form: { | ||
_method: "DELETE" | ||
_method: 'DELETE' | ||
} | ||
}, testMethod("del", t)) | ||
}, testMethod('del', t)) | ||
}) | ||
test("error", function (t) { | ||
test('error', function (t) { | ||
request({ | ||
uri: "/" | ||
, method: "DELETE" | ||
uri: '/' | ||
, method: 'DELETE' | ||
}, function (err, res, body) { | ||
t.equal(res.statusCode, 405) | ||
t.equal(body, "405 Method Not Allowed /") | ||
t.equal(body, '405 Method Not Allowed /') | ||
@@ -96,3 +144,3 @@ t.end() | ||
.on("end", done) | ||
.on('end', done) | ||
} | ||
@@ -106,2 +154,2 @@ | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8668
11
207
0
1