Skip to content

Commit 3eafa04

Browse files
committed
Improve code structure
1 parent 4f099fe commit 3eafa04

File tree

4 files changed

+37
-54
lines changed

4 files changed

+37
-54
lines changed

Diff for: src/DirectRESTController.js

+21-34
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,11 @@ const Config = require('./Config');
22
const Auth = require('./Auth');
33
const RESTController = require('parse/lib/node/RESTController');
44
const URL = require('url');
5+
import { logger } from './logger';
56
import {
67
logRequest,
78
logResponse
8-
} from './SensitiveLogger';
9-
10-
export function routeRESTRequest(router, method, path, request, fallback) {
11-
12-
// Use the router to figure out what handler to use
13-
var match = router.match(method, path);
14-
if (!match) {
15-
if (fallback) {
16-
return fallback();
17-
}
18-
throw new Parse.Error(
19-
Parse.Error.INVALID_JSON,
20-
'cannot route ' + method + ' ' + path);
21-
}
22-
request.params = match.params;
23-
return new Promise((resolve, reject) => {
24-
match.handler(request).then(resolve, reject);
25-
});
26-
}
9+
} from './sensitiveLogger';
2710

2811
export function DirectRESTController(applicationId, router) {
2912
function handleRequest(method, path, data = {}, options = {}) {
@@ -86,11 +69,12 @@ export function DirectRESTController(applicationId, router) {
8669
if (method === 'GET') {
8770
query = data;
8871
}
89-
let forwardResponse = false;
90-
logRequest(config.serverURL+path, method, data, {});
72+
73+
logRequest("internal"+path, method, data, {});
74+
9175
return new Parse.Promise((resolve, reject) => {
9276
getAuth(options, config).then((auth) => {
93-
return routeRESTRequest(router, method, path, {
77+
let request = {
9478
body: data,
9579
config,
9680
auth,
@@ -99,19 +83,22 @@ export function DirectRESTController(applicationId, router) {
9983
sessionToken: options.sessionToken
10084
},
10185
query
102-
}, function() {
103-
forwardResponse = true;
104-
return RESTController.request.apply(null, args);
86+
};
87+
return Promise.resolve().then(() => {
88+
return router.tryRouteRequest(method, path, request);
89+
}).then((response) => {
90+
logResponse("internal"+path, method, response.response);
91+
resolve(response.response, response.status, response);
92+
}, (err) => {
93+
if (err instanceof Parse.Error &&
94+
err.code == Parse.Error.INVALID_JSON &&
95+
err.message == `cannot route ${method} ${path}`) {
96+
RESTController.request.apply(null, args).then(resolve, reject);
97+
} else {
98+
reject(err);
99+
}
105100
});
106-
}).then((response) => {
107-
if (forwardResponse) {
108-
return resolve(response);
109-
}
110-
logResponse(config.serverURL+path, method, response.response, {});
111-
return resolve(response.response, response.status, response);
112-
}, (error) => {
113-
return reject(error);
114-
})
101+
}, reject);
115102
})
116103

117104
};

Diff for: src/PromiseRouter.js

+15-19
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {inspect} from 'util';
1313
import {
1414
logRequest,
1515
logResponse
16-
} from './SensitiveLogger';
16+
} from './sensitiveLogger';
1717

1818
const Layer = require('express/lib/router/layer');
1919

@@ -136,26 +136,22 @@ export default class PromiseRouter {
136136

137137
expressApp() {
138138
var expressApp = express();
139-
for (var route of this.routes) {
140-
switch(route.method) {
141-
case 'POST':
142-
expressApp.post(route.path, makeExpressHandler(this.appId, route.handler));
143-
break;
144-
case 'GET':
145-
expressApp.get(route.path, makeExpressHandler(this.appId, route.handler));
146-
break;
147-
case 'PUT':
148-
expressApp.put(route.path, makeExpressHandler(this.appId, route.handler));
149-
break;
150-
case 'DELETE':
151-
expressApp.delete(route.path, makeExpressHandler(this.appId, route.handler));
152-
break;
153-
default:
154-
throw 'unexpected code branch';
155-
}
156-
}
139+
this.mountOnto(expressApp);
157140
return expressApp;
158141
}
142+
143+
tryRouteRequest(method, path, request) {
144+
var match = this.match(method, path);
145+
if (!match) {
146+
throw new Parse.Error(
147+
Parse.Error.INVALID_JSON,
148+
'cannot route ' + method + ' ' + path);
149+
}
150+
request.params = match.params;
151+
return new Promise((resolve, reject) => {
152+
match.handler(request).then(resolve, reject);
153+
});
154+
}
159155
}
160156

161157
// A helper function to make an express handler out of a a promise

Diff for: src/batch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function handleBatch(router, req) {
4343
auth: req.auth,
4444
info: req.info
4545
}
46-
return routeRESTRequest(router, restRequest.method, routablePath, request).then((response) => {
46+
return router.tryRouteRequest(restRequest.method, routablePath, request).then((response) => {
4747
return {success: response.response};
4848
}, (error) => {
4949
return {error: {code: error.code, error: error.message}};
File renamed without changes.

0 commit comments

Comments
 (0)