kodi-api
Version:
A complete implementation of Kodi JSON-RPC calls in an easy-to-use Javascript/TypeScript client.
61 lines (60 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KodiMethodNamespace = void 0;
const uuid_1 = require("uuid");
/** Method to return a Kodi JSON-RPC method as a function.
* @hidden
*/
function getMethod(nameSpace, methodName, camelCase, thisArg, init) {
const method = nameSpace + '.' + methodName.slice(0, 1).toUpperCase() + methodName.slice(1, methodName.length);
const methodFunc = async function methodFunc(...args) {
await thisArg.getIntrospectionCache();
// Run namespace intiializer.
if (typeof init === 'function')
init();
const methodDesc = thisArg.introspectionCache.describeMethod(method);
const params = {};
// Iterate over arguments, validating and constructing parameters
for (let i = 0; i < args.length; i += 1) {
const paramDesc = methodDesc.params[i];
thisArg.introspectionCache.validateSchema(args[i], paramDesc, thisArg.throwValidationError);
params[paramDesc.name] = args[i];
}
const response = await thisArg.jsonRpcClient.request(method, params, uuid_1.v4());
thisArg.introspectionCache.validateSchema(response, methodDesc.returns, thisArg.throwValidationError);
return response.result;
};
Object.defineProperty(methodFunc, 'name', { value: camelCase });
Object.defineProperty(methodFunc.constructor, 'name', { value: camelCase });
return methodFunc;
}
/** Dynamic class for constructing a namespace of Kodi method funcions. */
class KodiMethodNamespace {
constructor(nameSpace, initMethodName, thisArg) {
const setFunc = (name, method, thisArg, init) => {
const camelCase = method.slice(0, 1).toLowerCase() + method.slice(1, method.length);
const func = getMethod(name, method, camelCase, thisArg, init);
this[method] = func;
this[camelCase] = func;
};
const init = () => {
const groups = thisArg.introspectionCache.listMethods(true);
const methodNames = groups[nameSpace];
const listMethods = async function listMethods() {
return methodNames;
};
this.ListMethods = listMethods;
this.listMethods = listMethods;
for (const methodName of methodNames) {
setFunc(nameSpace, methodName, thisArg);
}
};
if (typeof thisArg.introspectionCache === 'undefined') {
setFunc(nameSpace, initMethodName, thisArg, init);
}
else {
init();
}
}
}
exports.KodiMethodNamespace = KodiMethodNamespace;