Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Rest API helpers only applying to v1 #9520

Merged
merged 7 commits into from
Feb 9, 2018
17 changes: 7 additions & 10 deletions packages/rocketchat-api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
21 changes: 15 additions & 6 deletions packages/rocketchat-api/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -121,15 +128,15 @@ 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]};
}

//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 {
Expand All @@ -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;
}

Expand Down Expand Up @@ -339,7 +346,9 @@ class API extends Restivus {
}


RocketChat.API = {};
RocketChat.API = {
helperMethods: new Map()
};

const getUserAuth = function _getUserAuth() {
const invalidResults = [undefined, null, false];
Expand Down Expand Up @@ -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',
Expand Down
12 changes: 0 additions & 12 deletions packages/rocketchat-api/server/default/helpers/getLoggedInUser.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/rocketchat-api/server/helpers/README.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
});