bigbluebutton-Github/bigbluebutton-html5/imports/ui/services/api/index.js

90 lines
2.1 KiB
JavaScript
Raw Normal View History

import Auth from '/imports/ui/services/auth';
2017-04-20 22:12:14 +08:00
import { check } from 'meteor/check';
import NotificationService from '/imports/ui/services/notification/notificationService';
2017-04-20 22:12:14 +08:00
/**
* Send the request to the server via Meteor.call and don't treat errors.
2017-05-04 01:19:21 +08:00
*
2017-04-26 21:53:01 +08:00
* @param {string} name
* @param {any} args
2017-04-20 22:12:14 +08:00
* @see https://docs.meteor.com/api/methods.html#Meteor-call
* @return {Promise}
*/
2017-04-26 21:47:44 +08:00
function makeCall(name, ...args) {
2017-04-20 22:12:14 +08:00
check(name, String);
const credentials = Auth.credentials;
2017-04-20 22:12:14 +08:00
return new Promise((resolve, reject) => {
Meteor.call(name, credentials, ...args, (error, result) => {
if (error) {
reject(error);
}
2017-04-26 21:53:01 +08:00
resolve(result);
2017-04-20 22:12:14 +08:00
});
});
2017-06-03 03:25:02 +08:00
}
2017-04-20 22:12:14 +08:00
/**
* Send the request to the server via Meteor.call and treat the error to a default callback.
2017-05-04 01:19:21 +08:00
*
2017-04-26 21:53:01 +08:00
* @param {string} name
* @param {any} args
2017-04-20 22:12:14 +08:00
* @see https://docs.meteor.com/api/methods.html#Meteor-call
* @return {Promise}
*/
2017-04-26 21:47:44 +08:00
function call(name, ...args) {
2017-04-26 21:53:01 +08:00
return makeCall(name, ...args).catch((e) => {
NotificationService.add({ notification: `Error while executing ${name}` });
throw e;
});
2017-06-03 03:25:02 +08:00
}
2017-05-04 00:40:57 +08:00
/**
* Log the error to the client and to the server.
2017-05-04 01:19:21 +08:00
*
* @example
2017-05-04 00:40:57 +08:00
* @code{ logClient({error:"Error caused by blabla"}) }
*/
2017-04-29 01:12:08 +08:00
function logClient() {
2017-04-28 21:47:07 +08:00
const credentials = Auth.credentials;
const args = Array.prototype.slice.call(arguments, 0);
const userInfo = window.navigator;
args.push({
systemProps: {
language: userInfo.language,
userAgent: userInfo.userAgent,
screenSize: { width: screen.width, height: screen.height },
windowSize: { width: window.innerWidth, height: window.innerHeight },
bbbVersion: Meteor.settings.public.app.bbbServerVersion,
location: window.location.href,
2017-05-04 01:19:21 +08:00
},
2017-04-28 21:47:07 +08:00
});
2017-04-29 01:12:08 +08:00
const logTypeInformed = arguments.length > 1;
const outputLog = logTypeInformed ? Array.prototype.slice.call(args, 1) : args;
2017-05-04 01:19:21 +08:00
console.warn('Client log:', outputLog);
2017-04-29 01:12:08 +08:00
Meteor.call('logClient',
2017-05-04 01:19:21 +08:00
logTypeInformed ? args[0] : 'info',
2017-04-29 01:12:08 +08:00
credentials,
2017-06-03 03:25:02 +08:00
outputLog,
2017-04-29 01:12:08 +08:00
);
2017-06-03 03:25:02 +08:00
}
2017-04-28 21:47:07 +08:00
2017-04-20 22:12:14 +08:00
const API = {
2017-05-04 01:09:33 +08:00
logClient,
2017-04-26 21:47:44 +08:00
makeCall,
2017-05-04 01:19:21 +08:00
call,
};
2017-04-20 22:12:14 +08:00
export default API;
export {
2017-05-04 01:19:21 +08:00
makeCall,
call,
logClient,
};