diff --git a/packages/rocketchat-api/package.js b/packages/rocketchat-api/package.js index 859ca35ffdf0..633765ab24d2 100644 --- a/packages/rocketchat-api/package.js +++ b/packages/rocketchat-api/package.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'); diff --git a/packages/rocketchat-api/server/api.js b/packages/rocketchat-api/server/api.js index df2e1a9dd72a..230ef520f1a4 100644 --- a/packages/rocketchat-api/server/api.js +++ b/packages/rocketchat-api/server/api.js @@ -6,7 +6,6 @@ class API extends Restivus { super(properties); this.logger = new Logger(`API ${ properties.version ? properties.version : 'default' } Logger`, {}); this.authMethods = []; - this.helperMethods = new Map(); this.fieldSeparator = '.'; this.defaultFieldsToExclude = { joinCode: 0, @@ -51,6 +50,14 @@ class API extends Restivus { }; } + hasHelperMethods() { + return RocketChat.API.helperMethods.size !== 0; + } + + getHelperMethods() { + return RocketChat.API.helperMethods; + } + addAuthMethod(method) { this.authMethods.push(method); } @@ -121,7 +128,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 (this.hasHelperMethods()) { Object.keys(endpoints).forEach((method) => { if (typeof endpoints[method] === 'function') { endpoints[method] = {action: endpoints[method]}; @@ -129,7 +136,7 @@ class API extends Restivus { //Add a try/catch for each endpoint const originalAction = endpoints[method].action; - endpoints[method].action = function() { + endpoints[method].action = function _internalRouteActionHandler() { this.logger.debug(`${ this.request.method.toUpperCase() }: ${ this.request.url }`); let result; try { @@ -155,7 +162,7 @@ class API extends Restivus { return result; }; - for (const [name, helperMethod] of this.helperMethods) { + for (const [name, helperMethod] of this.getHelperMethods()) { endpoints[method][name] = helperMethod; } @@ -339,7 +346,9 @@ class API extends Restivus { } -RocketChat.API = {}; +RocketChat.API = { + helperMethods: new Map() +}; const getUserAuth = function _getUserAuth() { const invalidResults = [undefined, null, false]; @@ -374,7 +383,7 @@ const getUserAuth = function _getUserAuth() { }; }; -const createApi = function(enableCors) { +const createApi = function _createApi(enableCors) { if (!RocketChat.API.v1 || RocketChat.API.v1._config.enableCors !== enableCors) { RocketChat.API.v1 = new API({ version: 'v1', diff --git a/packages/rocketchat-api/server/default/helpers/getLoggedInUser.js b/packages/rocketchat-api/server/default/helpers/getLoggedInUser.js deleted file mode 100644 index bfca9f0d11bf..000000000000 --- a/packages/rocketchat-api/server/default/helpers/getLoggedInUser.js +++ /dev/null @@ -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; -}); diff --git a/packages/rocketchat-api/server/helpers/README.md b/packages/rocketchat-api/server/helpers/README.md new file mode 100644 index 000000000000..8437f7558dcb --- /dev/null +++ b/packages/rocketchat-api/server/helpers/README.md @@ -0,0 +1,4 @@ +# REST API Helpers + +## What are they? +Helpers are functions which get injected into the context of `this` on each request the REST API recieves. This allows for commonly used code, such as retriving the target user, to be placed in one spot and used throughout the entire REST API code base. diff --git a/packages/rocketchat-api/server/v1/helpers/getLoggedInUser.js b/packages/rocketchat-api/server/helpers/getLoggedInUser.js similarity index 78% rename from packages/rocketchat-api/server/v1/helpers/getLoggedInUser.js rename to packages/rocketchat-api/server/helpers/getLoggedInUser.js index ab9d141e8130..4589e13dda2b 100644 --- a/packages/rocketchat-api/server/v1/helpers/getLoggedInUser.js +++ b/packages/rocketchat-api/server/helpers/getLoggedInUser.js @@ -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']) { diff --git a/packages/rocketchat-api/server/v1/helpers/getPaginationItems.js b/packages/rocketchat-api/server/helpers/getPaginationItems.js similarity index 92% rename from packages/rocketchat-api/server/v1/helpers/getPaginationItems.js rename to packages/rocketchat-api/server/helpers/getPaginationItems.js index dd1732df6c73..96052a56b848 100644 --- a/packages/rocketchat-api/server/v1/helpers/getPaginationItems.js +++ b/packages/rocketchat-api/server/helpers/getPaginationItems.js @@ -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; diff --git a/packages/rocketchat-api/server/v1/helpers/getUserFromParams.js b/packages/rocketchat-api/server/helpers/getUserFromParams.js similarity index 90% rename from packages/rocketchat-api/server/v1/helpers/getUserFromParams.js rename to packages/rocketchat-api/server/helpers/getUserFromParams.js index c52296f0fb7a..b00ba0173724 100644 --- a/packages/rocketchat-api/server/v1/helpers/getUserFromParams.js +++ b/packages/rocketchat-api/server/helpers/getUserFromParams.js @@ -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(); diff --git a/packages/rocketchat-api/server/v1/helpers/isUserFromParams.js b/packages/rocketchat-api/server/helpers/isUserFromParams.js similarity index 76% rename from packages/rocketchat-api/server/v1/helpers/isUserFromParams.js rename to packages/rocketchat-api/server/helpers/isUserFromParams.js index fab907bc96da..194011d93d91 100644 --- a/packages/rocketchat-api/server/v1/helpers/isUserFromParams.js +++ b/packages/rocketchat-api/server/helpers/isUserFromParams.js @@ -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) || diff --git a/packages/rocketchat-api/server/v1/helpers/parseJsonQuery.js b/packages/rocketchat-api/server/helpers/parseJsonQuery.js similarity index 97% rename from packages/rocketchat-api/server/v1/helpers/parseJsonQuery.js rename to packages/rocketchat-api/server/helpers/parseJsonQuery.js index 663a19f938d4..c01cc120e6a7 100644 --- a/packages/rocketchat-api/server/v1/helpers/parseJsonQuery.js +++ b/packages/rocketchat-api/server/helpers/parseJsonQuery.js @@ -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 { diff --git a/packages/rocketchat-api/server/v1/helpers/requestParams.js b/packages/rocketchat-api/server/helpers/requestParams.js similarity index 54% rename from packages/rocketchat-api/server/v1/helpers/requestParams.js rename to packages/rocketchat-api/server/helpers/requestParams.js index bc5718313913..aa4290c00b6d 100644 --- a/packages/rocketchat-api/server/v1/helpers/requestParams.js +++ b/packages/rocketchat-api/server/helpers/requestParams.js @@ -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; });