|
|
|
|
@ -12,7 +12,16 @@ RocketChat.API = { |
|
|
|
|
return RocketChat.API._jqueryCall('POST', endpoint, params, body); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_jqueryCall(method, endpoint, params, body) { |
|
|
|
|
upload(endpoint, params, formData) { |
|
|
|
|
if (!formData) { |
|
|
|
|
formData = params; |
|
|
|
|
params = {}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return RocketChat.API._jqueryFormDataCall(endpoint, params, formData); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_generateQueryFromParams(params) { |
|
|
|
|
let query = ''; |
|
|
|
|
if (params && typeof params === 'object') { |
|
|
|
|
Object.keys(params).forEach((key) => { |
|
|
|
|
@ -22,6 +31,12 @@ RocketChat.API = { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return query; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_jqueryCall(method, endpoint, params, body) { |
|
|
|
|
const query = RocketChat.API._generateQueryFromParams(params); |
|
|
|
|
|
|
|
|
|
return new Promise(function _rlRestApiGet(resolve, reject) { |
|
|
|
|
jQuery.ajax({ |
|
|
|
|
method, |
|
|
|
|
@ -42,6 +57,34 @@ RocketChat.API = { |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_jqueryFormDataCall(endpoint, params, formData) { |
|
|
|
|
const query = RocketChat.API._generateQueryFromParams(params); |
|
|
|
|
|
|
|
|
|
if (!(formData instanceof FormData)) { |
|
|
|
|
throw new Error('The formData parameter MUST be an instance of the FormData class.'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return new Promise(function _jqueryFormDataPromise(resolve, reject) { |
|
|
|
|
jQuery.ajax({ |
|
|
|
|
url: `${ Meteor.absoluteUrl() }api/${ endpoint }${ query }`, |
|
|
|
|
headers: { |
|
|
|
|
'X-User-Id': localStorage['Meteor.userId'], |
|
|
|
|
'X-Auth-Token': localStorage['Meteor.loginToken'] |
|
|
|
|
}, |
|
|
|
|
data: formData, |
|
|
|
|
processData: false, |
|
|
|
|
contentType: false, |
|
|
|
|
type: 'POST', |
|
|
|
|
success: function _jqueryFormDataSuccess(result) { |
|
|
|
|
resolve(result); |
|
|
|
|
}, |
|
|
|
|
error: function _jqueryFormDataError(xhr, status, errorThrown) { |
|
|
|
|
reject(new Error(errorThrown)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
v1: { |
|
|
|
|
get(endpoint, params) { |
|
|
|
|
return RocketChat.API.get(`v1/${ endpoint }`, params); |
|
|
|
|
@ -49,6 +92,10 @@ RocketChat.API = { |
|
|
|
|
|
|
|
|
|
post(endpoint, params, body) { |
|
|
|
|
return RocketChat.API.post(`v1/${ endpoint }`, params, body); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
upload(endpoint, params, formData) { |
|
|
|
|
return RocketChat.API.upload(`v1/${ endpoint }`, params, formData); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|