Move the helpers out of the individual api versions, this way others can make usage of them without duplicating code

pull/9520/head
Bradley Hilton 8 years ago
parent 38ddce1d9d
commit 40967e2720
No known key found for this signature in database
GPG Key ID: 0666B2C24C43C358
  1. 17
      packages/rocketchat-api/package.js
  2. 8
      packages/rocketchat-api/server/api.js
  3. 12
      packages/rocketchat-api/server/default/helpers/getLoggedInUser.js
  4. 2
      packages/rocketchat-api/server/helpers/getLoggedInUser.js
  5. 2
      packages/rocketchat-api/server/helpers/getPaginationItems.js
  6. 2
      packages/rocketchat-api/server/helpers/getUserFromParams.js
  7. 2
      packages/rocketchat-api/server/helpers/isUserFromParams.js
  8. 2
      packages/rocketchat-api/server/helpers/parseJsonQuery.js
  9. 2
      packages/rocketchat-api/server/helpers/requestParams.js

@ -15,16 +15,13 @@ Package.onUse(function(api) {
api.addFiles('server/api.js', 'server');
api.addFiles('server/settings.js', 'server');
//Register v1 helpers
api.addFiles('server/v1/helpers/requestParams.js', 'server');
api.addFiles('server/v1/helpers/getPaginationItems.js', 'server');
api.addFiles('server/v1/helpers/getUserFromParams.js', 'server');
api.addFiles('server/v1/helpers/isUserFromParams.js', 'server');
api.addFiles('server/v1/helpers/parseJsonQuery.js', 'server');
api.addFiles('server/v1/helpers/getLoggedInUser.js', 'server');
//Register default helpers
api.addFiles('server/default/helpers/getLoggedInUser.js', 'server');
//Register helpers
api.addFiles('server/helpers/requestParams.js', 'server');
api.addFiles('server/helpers/getPaginationItems.js', 'server');
api.addFiles('server/helpers/getUserFromParams.js', 'server');
api.addFiles('server/helpers/isUserFromParams.js', 'server');
api.addFiles('server/helpers/parseJsonQuery.js', 'server');
api.addFiles('server/helpers/getLoggedInUser.js', 'server');
//Add default routes
api.addFiles('server/default/info.js', 'server');

@ -121,7 +121,7 @@ class API extends Restivus {
routes.forEach((route) => {
//Note: This is required due to Restivus calling `addRoute` in the constructor of itself
if (this.helperMethods) {
if (RocketChat.API.helperMethods) {
Object.keys(endpoints).forEach((method) => {
if (typeof endpoints[method] === 'function') {
endpoints[method] = {action: endpoints[method]};
@ -155,7 +155,7 @@ class API extends Restivus {
return result;
};
for (const [name, helperMethod] of this.helperMethods) {
for (const [name, helperMethod] of RocketChat.API.helperMethods) {
endpoints[method][name] = helperMethod;
}
@ -339,7 +339,9 @@ class API extends Restivus {
}
RocketChat.API = {};
RocketChat.API = {
helperMethods: new Map()
};
const getUserAuth = function _getUserAuth() {
const invalidResults = [undefined, null, false];

@ -1,12 +0,0 @@
RocketChat.API.default.helperMethods.set('getLoggedInUser', function _getLoggedInUser() {
let user;
if (this.request.headers['x-auth-token'] && this.request.headers['x-user-id']) {
user = RocketChat.models.Users.findOne({
'_id': this.request.headers['x-user-id'],
'services.resume.loginTokens.hashedToken': Accounts._hashLoginToken(this.request.headers['x-auth-token'])
});
}
return user;
});

@ -1,4 +1,4 @@
RocketChat.API.v1.helperMethods.set('getLoggedInUser', function _getLoggedInUser() {
RocketChat.API.helperMethods.set('getLoggedInUser', function _getLoggedInUser() {
let user;
if (this.request.headers['x-auth-token'] && this.request.headers['x-user-id']) {

@ -2,7 +2,7 @@
// If the count query param isn't defined, then we set it to the "API_Default_Count" setting
// If the count is zero, then that means unlimited and is only allowed if the setting "API_Allow_Infinite_Count" is true
RocketChat.API.v1.helperMethods.set('getPaginationItems', function _getPaginationItems() {
RocketChat.API.helperMethods.set('getPaginationItems', function _getPaginationItems() {
const hardUpperLimit = RocketChat.settings.get('API_Upper_Count_Limit') <= 0 ? 100 : RocketChat.settings.get('API_Upper_Count_Limit');
const defaultCount = RocketChat.settings.get('API_Default_Count') <= 0 ? 50 : RocketChat.settings.get('API_Default_Count');
const offset = this.queryParams.offset ? parseInt(this.queryParams.offset) : 0;

@ -1,5 +1,5 @@
//Convenience method, almost need to turn it into a middleware of sorts
RocketChat.API.v1.helperMethods.set('getUserFromParams', function _getUserFromParams() {
RocketChat.API.helperMethods.set('getUserFromParams', function _getUserFromParams() {
const doesntExist = { _doesntExist: true };
let user;
const params = this.requestParams();

@ -1,4 +1,4 @@
RocketChat.API.v1.helperMethods.set('isUserFromParams', function _isUserFromParams() {
RocketChat.API.helperMethods.set('isUserFromParams', function _isUserFromParams() {
const params = this.requestParams();
return (!params.userId && !params.username && !params.user) ||

@ -1,4 +1,4 @@
RocketChat.API.v1.helperMethods.set('parseJsonQuery', function _parseJsonQuery() {
RocketChat.API.helperMethods.set('parseJsonQuery', function _parseJsonQuery() {
let sort;
if (this.queryParams.sort) {
try {

@ -1,3 +1,3 @@
RocketChat.API.v1.helperMethods.set('requestParams', function _requestParams() {
RocketChat.API.helperMethods.set('requestParams', function _requestParams() {
return ['POST', 'PUT'].includes(this.request.method) ? this.bodyParams : this.queryParams;
});
Loading…
Cancel
Save