-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dependency of RC namespace in rc-api/helpers and api.js (#13273)
* Move RestAPI client to rc-api package * Remove dependency of RC namespace in rc-api/helpers and api.js
- Loading branch information
1 parent
bff0955
commit 767f35d
Showing
17 changed files
with
214 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { API } from './lib/RestApiClient'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
export const API = { | ||
delete(endpoint, params) { | ||
return API._jqueryCall('DELETE', endpoint, params); | ||
}, | ||
|
||
get(endpoint, params) { | ||
return API._jqueryCall('GET', endpoint, params); | ||
}, | ||
|
||
post(endpoint, params, body) { | ||
if (!body) { | ||
body = params; | ||
params = {}; | ||
} | ||
|
||
return API._jqueryCall('POST', endpoint, params, body); | ||
}, | ||
|
||
upload(endpoint, params, formData) { | ||
if (!formData) { | ||
formData = params; | ||
params = {}; | ||
} | ||
|
||
return API._jqueryFormDataCall(endpoint, params, formData); | ||
}, | ||
|
||
_generateQueryFromParams(params) { | ||
let query = ''; | ||
if (params && typeof params === 'object') { | ||
Object.keys(params).forEach((key) => { | ||
query += query === '' ? '?' : '&'; | ||
|
||
query += `${ key }=${ params[key] }`; | ||
}); | ||
} | ||
|
||
return query; | ||
}, | ||
|
||
_jqueryCall(method, endpoint, params, body) { | ||
const query = API._generateQueryFromParams(params); | ||
|
||
return new Promise(function _rlRestApiGet(resolve, reject) { | ||
jQuery.ajax({ | ||
method, | ||
url: `${ document.baseURI }api/${ endpoint }${ query }`, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-User-Id': localStorage['Meteor.userId'], | ||
'X-Auth-Token': localStorage['Meteor.loginToken'], | ||
}, | ||
data: JSON.stringify(body), | ||
success: function _rlGetSuccess(result) { | ||
resolve(result); | ||
}, | ||
error: function _rlGetFailure(xhr, status, errorThrown) { | ||
const error = new Error(errorThrown); | ||
error.xhr = xhr; | ||
reject(error); | ||
}, | ||
}); | ||
}); | ||
}, | ||
|
||
_jqueryFormDataCall(endpoint, params, formData) { | ||
const query = 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: `${ document.baseURI }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: { | ||
delete(endpoint, params) { | ||
return API.delete(`v1/${ endpoint }`, params); | ||
}, | ||
|
||
get(endpoint, params) { | ||
return API.get(`v1/${ endpoint }`, params); | ||
}, | ||
|
||
post(endpoint, params, body) { | ||
return API.post(`v1/${ endpoint }`, params, body); | ||
}, | ||
|
||
upload(endpoint, params, formData) { | ||
return API.upload(`v1/${ endpoint }`, params, formData); | ||
}, | ||
}, | ||
}; | ||
RocketChat.API = API; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import { RocketChat } from 'meteor/rocketchat:lib'; | ||
import { hasRole } from 'meteor/rocketchat:authorization'; | ||
import { Info } from 'meteor/rocketchat:utils'; | ||
import { API } from '../api'; | ||
|
||
RocketChat.API.default.addRoute('info', { authRequired: false }, { | ||
API.default.addRoute('info', { authRequired: false }, { | ||
get() { | ||
const user = this.getLoggedInUser(); | ||
|
||
if (user && RocketChat.authz.hasRole(user._id, 'admin')) { | ||
return RocketChat.API.v1.success({ | ||
info: RocketChat.Info, | ||
if (user && hasRole(user._id, 'admin')) { | ||
return API.v1.success({ | ||
info: Info, | ||
}); | ||
} | ||
|
||
return RocketChat.API.v1.success({ | ||
version: RocketChat.Info.version, | ||
return API.v1.success({ | ||
version: Info.version, | ||
}); | ||
}, | ||
}); |
7 changes: 4 additions & 3 deletions
7
packages/rocketchat-api/server/helpers/composeRoomWithLastMessage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { RocketChat } from 'meteor/rocketchat:lib'; | ||
import { composeMessageObjectWithUser } from 'meteor/rocketchat:utils'; | ||
import { API } from '../api'; | ||
|
||
RocketChat.API.helperMethods.set('composeRoomWithLastMessage', function _composeRoomWithLastMessage(room, userId) { | ||
API.helperMethods.set('composeRoomWithLastMessage', function _composeRoomWithLastMessage(room, userId) { | ||
if (room.lastMessage) { | ||
room.lastMessage = RocketChat.composeMessageObjectWithUser(room.lastMessage, userId); | ||
room.lastMessage = composeMessageObjectWithUser(room.lastMessage, userId); | ||
} | ||
return room; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.