1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-18 05:42:00 +03:00

Make MatrixHttpApi a generic API interface; move specifics to MatrixClient.

Add a utils file to store common operations like map() and encodeParams().
Modify MatrixClient and MatrixHttpApi to accepts opts as constructors.
This commit is contained in:
Kegan Dougal
2015-06-04 15:39:36 +01:00
parent d1e51de7ec
commit a95f9d3717
5 changed files with 1204 additions and 1111 deletions

52
lib/utils.js Normal file
View File

@@ -0,0 +1,52 @@
"use strict";
// avoiding deps on jquery and co
module.exports.encodeParams = function(params) {
var qs = "";
for (var key in params) {
if (!params.hasOwnProperty(key)) { continue; }
qs += "&" + encodeURIComponent(key) + "=" +
encodeURIComponent(params[key]);
}
return qs.substring(1);
};
module.exports.encodeUri = function(pathTemplate, variables) {
for (var key in variables) {
if (!variables.hasOwnProperty(key)) { continue; }
pathTemplate = pathTemplate.replace(
key, encodeURIComponent(variables[key])
);
}
return pathTemplate;
};
module.exports.map = function(array, fn) {
var results = new Array(array.length);
for (var i = 0; i < array.length; i++) {
results[i] = fn(array[i]);
}
return results;
};
module.exports.isFunction = function(value) {
return Object.prototype.toString.call(value) == "[object Function]";
};
module.exports.checkObjectHasKeys = function(obj, keys) {
for (var i = 0; i < keys.length; i++) {
if (!obj.hasOwnProperty(keys[i])) {
throw new Error("Missing required key: "+keys[i]);
}
}
};
module.exports.checkObjectHasNoAdditionalKeys = function(obj, allowedKeys) {
for (var key in obj) {
if (!obj.hasOwnProperty(key)) { continue; }
if (allowedKeys.indexOf(key) === -1) {
throw new Error("Unknown key: "+key);
}
}
};