ma3route-sdk
Advanced tools
Comparing version 0.2.0 to 0.3.0
{ | ||
"url": "https://www.ma3route.com/findit-api/v2/json/" | ||
"url": "https://www.ma3route.com/findit-api/v2/json/", | ||
"enforce_params_filter": true | ||
} |
@@ -12,2 +12,3 @@ /** | ||
// own modules | ||
var paramsConfig = require("./params"); | ||
var generate = require("./generate"); | ||
@@ -22,3 +23,3 @@ | ||
*/ | ||
createOne: generate.newCreateOne("bannerAdverts"), | ||
createOne: generate.newCreateOne("bannerAdverts", paramsConfig.bannerAdverts.create), | ||
/** | ||
@@ -28,3 +29,3 @@ * Retrieve banner adverts | ||
*/ | ||
get: generate.newGet("bannerAdverts"), | ||
get: generate.newGet("bannerAdverts", paramsConfig.bannerAdverts.get), | ||
/** | ||
@@ -34,3 +35,3 @@ * Retrieve a single banner advert | ||
*/ | ||
getOne: generate.newGetOne("bannerAdverts"), | ||
getOne: generate.newGetOne("bannerAdverts", paramsConfig.bannerAdverts.get), | ||
}; |
@@ -12,2 +12,3 @@ /** | ||
// own modules | ||
var paramsConfig = require("./params"); | ||
var generate = require("./generate"); | ||
@@ -22,3 +23,3 @@ | ||
*/ | ||
createOne: generate.newCreateOne("drivingReports"), | ||
createOne: generate.newCreateOne("drivingReports", paramsConfig.drivingReports.create), | ||
/** | ||
@@ -28,3 +29,3 @@ * Retrieve driving reports | ||
*/ | ||
get: generate.newGet("drivingReports"), | ||
get: generate.newGet("drivingReports", paramsConfig.drivingReports.get), | ||
/** | ||
@@ -34,3 +35,3 @@ * Retrieve drive types | ||
*/ | ||
getDriveTypes: generate.newGet("driveTypes"), | ||
getDriveTypes: generate.newGet("driveTypes", []), | ||
/** | ||
@@ -40,3 +41,3 @@ * Retrieve a single driving report | ||
*/ | ||
getOne: generate.newGetOne("drivingReports"), | ||
getOne: generate.newGetOne("drivingReports", paramsConfig.drivingReports.get), | ||
}; |
@@ -12,2 +12,3 @@ /** | ||
// own modules | ||
var paramsConfig = require("./params"); | ||
var generate = require("./generate"); | ||
@@ -22,3 +23,3 @@ | ||
*/ | ||
get: generate.newGet("externalStream"), | ||
get: generate.newGet("externalStream", paramsConfig.externalStream.get), | ||
/** | ||
@@ -28,3 +29,3 @@ * Retrieve a single item from external stream | ||
*/ | ||
getOne: generate.newGetOne("externalStream"), | ||
getOne: generate.newGetOne("externalStream", paramsConfig.externalStream.get), | ||
}; |
@@ -35,6 +35,7 @@ /** | ||
* | ||
* @param {Endpoint} endpoint | ||
* @param {Endpoint} endpoint | ||
* @param {String[]} [allowable] | ||
* @return {itemsGetRequest} | ||
*/ | ||
function newGet(endpoint) { | ||
function newGet(endpoint, allowable) { | ||
return function(params, callback) { | ||
@@ -44,3 +45,3 @@ // allow options params | ||
// get and detach/remove the uri options | ||
var uriOptions = utils.getURIOptions(args.params); | ||
var uriOptions = utils.getURIOptions([utils.setup(), this, args.params]); | ||
utils.removeURIOptions(args.params); | ||
@@ -51,2 +52,5 @@ // create the URI | ||
var authOptions = utils.getAuthOptions([utils.setup(), this, args.params]); | ||
utils.removeAuthOptions(args.params); | ||
// remove params that are not allowed | ||
args.params = utils.pickParams(args.params, allowable); | ||
// add the params as querystring to the URI | ||
@@ -58,3 +62,3 @@ utils.addQueries(uri, args.params); | ||
} catch(err) { | ||
return callback(err); | ||
return args.callback(err); | ||
} | ||
@@ -72,3 +76,3 @@ | ||
* | ||
* @param {Endpoint} endpoint | ||
* @param {Endpoint} endpoint | ||
* @return {itemsGetOneRequest} | ||
@@ -94,9 +98,10 @@ */ | ||
* | ||
* @param {Endpoint} endpoint | ||
* @param {Endpoint} endpoint | ||
* @param {String[]} [allowable] | ||
* @return {itemsCreateOneRequest} | ||
*/ | ||
function newCreateOne(endpoint) { | ||
function newCreateOne(endpoint, allowable) { | ||
return function(body, callback) { | ||
// get and remove/detach URI options | ||
var uriOptions = utils.getURIOptions(body); | ||
var uriOptions = utils.getURIOptions([utils.setup(), this, body]); | ||
utils.removeURIOptions(body); | ||
@@ -107,2 +112,5 @@ // create a URI | ||
var authOptions = utils.getAuthOptions([utils.setup(), this, body]); | ||
utils.removeAuthOptions(body); | ||
// remove params that are not allowed | ||
body = utils.pickParams(body, allowable); | ||
// sign the URI (automatically removes the secret param from body) | ||
@@ -128,8 +136,9 @@ try { | ||
* | ||
* @param {Endpoint} endpoint | ||
* @param {Object} initParams - parameters used to indicate we are infact modifying | ||
* @param {Endpoint} endpoint | ||
* @param {Object} initParams - parameters used to indicate we are infact modifying | ||
* @param {String[]} [allowable] | ||
* @return {itemsModifyOneRequest} | ||
*/ | ||
function newModifyOne(endpoint, initParams) { | ||
var modify = newCreateOne(endpoint); | ||
function newModifyOne(endpoint, initParams, allowable) { | ||
var modify = newCreateOne(endpoint, allowable); | ||
return function(body, callback) { | ||
@@ -145,4 +154,5 @@ _.assign(body, initParams); | ||
* | ||
* @param {Endpoint} endpoint | ||
* @param {Endpoint} endpoint | ||
* @return {SSEClientFactory} | ||
* @throws Error | ||
*/ | ||
@@ -149,0 +159,0 @@ function newSSEClientFactory(endpoint) { |
@@ -12,2 +12,3 @@ /** | ||
// own modules | ||
var paramsConfig = require("./params"); | ||
var generate = require("./generate"); | ||
@@ -22,3 +23,3 @@ | ||
*/ | ||
createOne: generate.newCreateOne("listedAdverts"), | ||
createOne: generate.newCreateOne("listedAdverts", paramsConfig.listedAdverts.create), | ||
/** | ||
@@ -28,3 +29,3 @@ * Retrieve listed adverts | ||
*/ | ||
get: generate.newGet("listedAdverts"), | ||
get: generate.newGet("listedAdverts", paramsConfig.listedAdverts.get), | ||
/** | ||
@@ -34,3 +35,3 @@ * Retrieve a single listed advert | ||
*/ | ||
getOne: generate.newGetOne("listedAdverts"), | ||
getOne: generate.newGetOne("listedAdverts", paramsConfig.listedAdverts.get), | ||
}; |
@@ -12,2 +12,3 @@ /** | ||
// own modules | ||
var paramsConfig = require("./params"); | ||
var generate = require("./generate"); | ||
@@ -22,3 +23,3 @@ | ||
*/ | ||
contactUs: generate.newCreateOne("contactUs"), | ||
contactUs: generate.newCreateOne("contactUs", paramsConfig.contactUs.create), | ||
}; |
@@ -12,2 +12,3 @@ /** | ||
// own modules | ||
var paramsConfig = require("./params"); | ||
var generate = require("./generate"); | ||
@@ -22,3 +23,3 @@ | ||
*/ | ||
createOne: generate.newCreateOne("news"), | ||
createOne: generate.newCreateOne("news", paramsConfig.news.create), | ||
/** | ||
@@ -28,3 +29,3 @@ * Retrieve news posts | ||
*/ | ||
get: generate.newGet("news"), | ||
get: generate.newGet("news", paramsConfig.news.get), | ||
/** | ||
@@ -34,3 +35,3 @@ * Retrieve a single news post | ||
*/ | ||
getOne: generate.newGetOne("news"), | ||
getOne: generate.newGetOne("news", paramsConfig.news.get), | ||
}; |
@@ -22,3 +22,3 @@ /** | ||
*/ | ||
getTowns: generate.newGet("towns"), | ||
getTowns: generate.newGet("towns", []), | ||
}; |
@@ -23,7 +23,2 @@ /** | ||
/** | ||
* Create a new SSE Client for the external stream | ||
* @type {SSEClientFactory} | ||
*/ | ||
externalStream: generate.newSSEClientFactory("sseExternalStream"), | ||
/** | ||
* Create a new SSE Client for traffic updates | ||
@@ -30,0 +25,0 @@ * @type {SSEClientFactory} |
@@ -12,2 +12,3 @@ /** | ||
// own modules | ||
var paramsConfig = require("./params"); | ||
var generate = require("./generate"); | ||
@@ -22,3 +23,3 @@ | ||
*/ | ||
createOne: generate.newCreateOne("trafficUpdates"), | ||
createOne: generate.newCreateOne("trafficUpdates", paramsConfig.trafficUpdates.create), | ||
/** | ||
@@ -28,3 +29,3 @@ * Retrieving traffic updates | ||
*/ | ||
get: generate.newGet("trafficUpdates"), | ||
get: generate.newGet("trafficUpdates", paramsConfig.trafficUpdates.get), | ||
/** | ||
@@ -34,3 +35,3 @@ * Retrieve a single traffic update | ||
*/ | ||
getOne: generate.newGetOne("trafficUpdates"), | ||
getOne: generate.newGetOne("trafficUpdates", paramsConfig.trafficUpdates.get), | ||
/** | ||
@@ -40,3 +41,3 @@ * Retrieve severity levels | ||
*/ | ||
getSeverityLevels: generate.newGet("severity"), | ||
getSeverityLevels: generate.newGet("severity", []), | ||
}; |
@@ -12,2 +12,3 @@ /** | ||
// own modules | ||
var paramsConfig = require("./params"); | ||
var generate = require("./generate"); | ||
@@ -22,3 +23,3 @@ | ||
*/ | ||
createOne: generate.newCreateOne("users"), | ||
createOne: generate.newCreateOne("users", paramsConfig.users.create), | ||
/** | ||
@@ -30,3 +31,3 @@ * Editing user's profile | ||
editprofile: true, | ||
}), | ||
}, paramsConfig.users.edit), | ||
/** | ||
@@ -36,3 +37,3 @@ * Retrieve user information | ||
*/ | ||
getOne: generate.newGetOne("users"), | ||
getOne: generate.newGetOne("users", paramsConfig.users.login), | ||
}; |
@@ -22,3 +22,6 @@ /** | ||
passResponse: passResponse, | ||
pickParams: pickParams, | ||
request: request(), | ||
removeAuthOptions: removeAuthOptions, | ||
removeOptions: removeOptions, | ||
removeURIOptions: removeURIOptions, | ||
@@ -46,3 +49,7 @@ setProxy: setProxy, | ||
// module variables | ||
var SETTINGS = config; | ||
/** | ||
* @name SDKSettings | ||
* @param {Boolean} enforce_params_filter | ||
*/ | ||
var SETTINGS = _.cloneDeep(config); | ||
var uriKeys = ["proxy"]; | ||
@@ -132,4 +139,2 @@ var authKeys = ["key", "secret"]; | ||
return url(options).segment("ssedrivingreports"); | ||
case "sseExternalStream": | ||
return url(options).segment("externalstream"); | ||
case "sseTrafficUpdates": | ||
@@ -281,2 +286,9 @@ return url(options).segment("ssetrafficupdates"); | ||
// null bodies | ||
if (_.includes([null, undefined, ""], response.body)) { | ||
if (!response.error) { | ||
response.error = 5; // assume the server fucked up | ||
} | ||
} | ||
return response; | ||
@@ -302,3 +314,3 @@ } | ||
if (response.error) { | ||
var message = response.body ? response.body.message : undefined; | ||
var message = response.body ? response.body.message : "received an empty body"; | ||
var err = new errors.HttpStatusError(response.statusCode, message); | ||
@@ -328,3 +340,3 @@ return callback(err, body.data, body.meta, response); | ||
toExclude = _.isBoolean(toExclude) ? toExclude : true; | ||
var excludes = ["secret"]; | ||
var excludes = ["key", "secret", "proxy"]; | ||
for (var key in params) { | ||
@@ -409,11 +421,15 @@ // ignore excludes | ||
/** | ||
* Remmove all options applicable for creating a new URI instance | ||
* Delete keys from objects | ||
* | ||
* @param {...Object} objects - objects to modify | ||
* @param {Object[]} args | ||
* @param {String|String[]} keys | ||
*/ | ||
function removeURIOptions() { | ||
for (var index = 0; index < arguments.length; index++) { | ||
var arg = arguments[index]; | ||
function removeOptions(args, keys) { | ||
if (!_.isArray(keys)) { | ||
keys = [ keys ]; | ||
} | ||
for (var index = 0; index < args.length; index++) { | ||
var arg = args[index]; | ||
for (var key in arg) { | ||
if (_.includes(uriKeys, key)) { | ||
if (_.includes(keys, key)) { | ||
delete arg[key]; | ||
@@ -427,2 +443,22 @@ } | ||
/** | ||
* Remove all options applicable for creating a new URI instance | ||
* | ||
* @param {...Object} objects - objects to modify | ||
*/ | ||
function removeURIOptions() { | ||
return removeOptions(arguments, uriKeys); | ||
} | ||
/** | ||
* Remove all options applicable for signing requests | ||
* | ||
* @param {...Object} objects - objects to modify | ||
*/ | ||
function removeAuthOptions() { | ||
return removeOptions(arguments, authKeys); | ||
} | ||
/** | ||
* Return an object with authentication options from an object | ||
@@ -437,1 +473,15 @@ * | ||
} | ||
/** | ||
* Pick out parameters | ||
* | ||
* @param {Object} params | ||
* @return {Object} cleaner params | ||
*/ | ||
function pickParams(params, keys) { | ||
if (SETTINGS.enforce_params_filter) { | ||
return _.pick(params, keys); | ||
} | ||
return params; | ||
} |
{ | ||
"name": "ma3route-sdk", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Node.js SDK for developing with the Ma3Route REST API", | ||
@@ -5,0 +5,0 @@ "repository": { |
34234
20
1109