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';
|
2019-07-31 20:30:34 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
|
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-10-24 20:58:46 +08:00
|
|
|
export function makeCall(name, ...args) {
|
2019-07-13 02:59:56 +08:00
|
|
|
check(name, String);
|
2016-05-13 00:10:20 +08:00
|
|
|
|
2019-07-13 02:59:56 +08:00
|
|
|
const { credentials } = Auth;
|
2016-05-13 00:10:20 +08:00
|
|
|
|
2019-07-13 02:59:56 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (Meteor.status().connected) {
|
2019-06-03 22:15:52 +08:00
|
|
|
Meteor.call(name, credentials, ...args, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
2017-04-26 21:53:01 +08:00
|
|
|
|
2019-06-03 22:15:52 +08:00
|
|
|
resolve(result);
|
|
|
|
});
|
2019-07-13 02:59:56 +08:00
|
|
|
} else {
|
2019-08-14 01:52:25 +08:00
|
|
|
const failureString = `Call to ${name} failed because Meteor is not connected`;
|
|
|
|
// We don't want to send a log message if the call that failed wasa log message.
|
|
|
|
// Without this you can get into an endless loop of failed logging.
|
|
|
|
if (name !== 'logClient') {
|
|
|
|
logger.warn({
|
|
|
|
logCode: 'servicesapiindex_makeCall',
|
|
|
|
extraInfo: {
|
|
|
|
attemptForUserInfo: Auth.fullInfo,
|
|
|
|
name,
|
|
|
|
...args,
|
|
|
|
},
|
|
|
|
}, failureString);
|
|
|
|
}
|
|
|
|
reject(failureString);
|
2019-07-13 02:59:56 +08:00
|
|
|
}
|
|
|
|
});
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|