Comparing version
@@ -5,4 +5,50 @@ var unirest = require('unirest') | ||
module.exports = function getStatus(target, cb) { | ||
function statusCategory(code) { | ||
if (code >= 100 && code < 200) { | ||
return "Informational" | ||
} else if (code >= 200 && code < 300) { | ||
return "Success" | ||
} else if (code >= 300 && code < 400) { | ||
return "Redirect" | ||
} else if (code >= 400 && code < 500) { | ||
return "Client Error" | ||
} else if (code >= 500 && code < 600) { | ||
return "Server Error" | ||
} else { | ||
return "Non-standard Category" | ||
} | ||
} | ||
function statusMessage(code) { | ||
return statusCodes[code] || "Non-standard Status" | ||
} | ||
function buildHandler(start) { | ||
return function(code) { | ||
var output = {} | ||
if (code && typeof code === 'number') { | ||
output.online = true | ||
output.latency = new Date().getTime() - start | ||
output.statusCode = code | ||
output.message = statusMessage(code) | ||
output.category = statusCategory(code) | ||
} else { | ||
output.online = false | ||
} | ||
return output | ||
} | ||
} | ||
function getStatus(target, callback) { | ||
// Build a new response hander with the current time | ||
// so we can calculate the latency when it's all over | ||
var buildResponse = buildHandler(new Date().getTime()) | ||
// Just in case the user doesn't care about the response | ||
// let's define a fallback callback right here | ||
if (!callback) { | ||
callback = function(){} | ||
} | ||
// We might have a HAR, so let's try to replay | ||
@@ -14,5 +60,9 @@ // all the entries and return an array with the | ||
harplayer.replayAll(target, function(err, res, body){ | ||
if (err) throw err | ||
if (err) { | ||
callback(null, err) | ||
} | ||
data.push(buildResponse(res.statusCode)) | ||
if (data.length === target.log.entries.length) cb(data) | ||
if (data.length === target.log.entries.length) { | ||
callback(data) | ||
} | ||
}) | ||
@@ -30,46 +80,10 @@ } else if (typeof target === 'string') { | ||
var data = buildResponse(res.status) | ||
cb(data) | ||
callback(data) | ||
}) | ||
} else { | ||
throw new Error("Missing target URL") | ||
throw new Error("Missing valid target URL") | ||
} | ||
} | ||
// Helper function to get the status type | ||
function statusType(code) { | ||
var statusType | ||
if (code >= 100 && code < 200) { | ||
statusType = "Informational" | ||
} else if (code >= 200 && code < 300) { | ||
statusType = "Success" | ||
} else if (code >= 300 && code < 400) { | ||
statusType = "Redirect" | ||
} else if (code >= 400 && code < 500) { | ||
statusType = "Client Error" | ||
} else if (code >= 500 && code < 600) { | ||
statusType = "Server Error" | ||
} else { | ||
statusType = "Non-standard Code" | ||
} | ||
return statusType | ||
} | ||
// Helper function to get the status code description | ||
function statusDescription(code) { | ||
return statusCodes[code] || "Non-standard Status" | ||
} | ||
// Helper function to format the response data | ||
function buildResponse(code) { | ||
var output = {} | ||
if (code && typeof code === 'number') { | ||
output["statusCode"] = code | ||
output["statusType"] = statusType(code) | ||
output["statusDescription"] = statusDescription(code) | ||
output["online"] = true | ||
} else { | ||
output["online"] = false | ||
} | ||
return output | ||
} | ||
module.exports = getStatus |
{ | ||
"name": "apistatus", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"description": "Returns the status of an HTTP request.", | ||
"main": "lib/apistatus.js", | ||
"scripts": { | ||
"test": "mocha --timeout 30000 tests/" | ||
"test": "mocha", | ||
"coverage": "istanbul cover _mocha -- -R spec", | ||
"codeclimate-report-coverage": "codeclimate < coverage/lcov.info" | ||
}, | ||
@@ -16,5 +18,7 @@ "author": "Mashape", | ||
"devDependencies": { | ||
"codeclimate-test-reporter": "0.0.4", | ||
"gulp": "^3.8.11", | ||
"gulp-jshint": "^1.9.4", | ||
"gulp-mocha": "^2.0.0", | ||
"istanbul": "^0.3.13", | ||
"mocha": "^2.0.1", | ||
@@ -28,7 +32,8 @@ "nock": "^0.50.0" | ||
"keywords": [ | ||
"url", | ||
"api", | ||
"status", | ||
"http", | ||
"get", | ||
"code" | ||
"har", | ||
"codes" | ||
], | ||
@@ -35,0 +40,0 @@ "bugs": { |
@@ -1,4 +0,4 @@ | ||
# API Status [](https://travis-ci.org/Mashape/apistatus/)  | ||
# API Status [](https://travis-ci.org/Mashape/apistatus/) [](https://codeclimate.com/github/Mashape/apistatus)  | ||
API status is a simple tool to send a request to an API and return response information. | ||
API status is a simple tool to send API requests and retrieve standard response data. | ||
@@ -18,3 +18,3 @@ ### Install | ||
console.log(status) | ||
// { statusCode: 200, statusType: 'Success', statusDescription: 'OK', online: true } | ||
// { online: true, statusCode: 200, category: 'Success', message: 'OK' } | ||
}) | ||
@@ -24,3 +24,3 @@ | ||
console.log(status) | ||
// { statusCode: 404, statusType: 'Client Error', statusDescription: 'Not Found', online: true } | ||
// { online: true, statusCode: 404, category: 'Client Error', message: 'Not Found' } | ||
}) | ||
@@ -32,2 +32,7 @@ | ||
}) | ||
apistatus(require("har.json"), function(statuses){ | ||
console.log(statuses) | ||
// [{ online: true, statusCode: 200, category: 'Success', message: 'OK' },... | ||
}) | ||
``` | ||
@@ -37,5 +42,3 @@ | ||
- optional HAR object for the requests to use for full API coverage beyond simple GET requests | ||
- optional HAR object for the response to check against, essentially automated API testing | ||
- a website with logs of status changes and support for periodic checking of saved APIs | ||
- optionally use the HAR response object to check against, essentially automated API testing | ||
@@ -42,0 +45,0 @@ ### Contributing |
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
48
6.67%18253
-7.65%7
40%497
-14.31%1
Infinity%