2016-07-11 21:45:24 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2017-04-20 22:12:14 +08:00
|
|
|
import { check } from 'meteor/check';
|
2017-10-13 02:53:33 +08:00
|
|
|
import notify from '/imports/ui/components/toast/service';
|
2016-05-14 00:17:00 +08:00
|
|
|
|
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);
|
2016-05-13 00:10:20 +08:00
|
|
|
|
2017-03-17 22:23:00 +08:00
|
|
|
const credentials = Auth.credentials;
|
2016-05-13 00:10:20 +08:00
|
|
|
|
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
|
|
|
}
|
2016-05-13 00:10:20 +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) => {
|
2017-10-13 02:53:33 +08:00
|
|
|
notify(`Ops! Error while executing ${name}`, 'error');
|
2017-04-26 21:53:01 +08:00
|
|
|
throw e;
|
|
|
|
});
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|
2016-05-13 00:10:20 +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-10-13 02:53:33 +08:00
|
|
|
function logClient(...arggs) {
|
2017-04-28 21:47:07 +08:00
|
|
|
const credentials = Auth.credentials;
|
2017-10-13 02:53:33 +08:00
|
|
|
const args = Array.prototype.slice.call(arggs, 0);
|
2017-04-28 21:47:07 +08:00
|
|
|
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,
|
2016-05-13 00:10:20 +08:00
|
|
|
};
|
2017-04-20 22:12:14 +08:00
|
|
|
|
|
|
|
export default API;
|
|
|
|
|
2016-05-13 00:10:20 +08:00
|
|
|
export {
|
2017-05-04 01:19:21 +08:00
|
|
|
makeCall,
|
|
|
|
call,
|
|
|
|
logClient,
|
2016-05-13 00:10:20 +08:00
|
|
|
};
|