2016-02-09 20:45:02 -08:00
|
|
|
|
|
|
|
|
import PromiseRouter from '../PromiseRouter';
|
2016-04-25 12:52:21 -07:00
|
|
|
import rest from '../rest';
|
2016-09-24 13:53:15 -04:00
|
|
|
import _ from 'lodash';
|
2016-11-24 15:47:41 -05:00
|
|
|
import Parse from 'parse/node';
|
2016-02-17 02:52:32 +08:00
|
|
|
|
2016-04-01 11:51:42 -04:00
|
|
|
const ALLOWED_GET_QUERY_KEYS = ['keys', 'include'];
|
|
|
|
|
|
2016-02-19 23:47:44 -05:00
|
|
|
export class ClassesRouter extends PromiseRouter {
|
2016-04-25 12:52:21 -07:00
|
|
|
|
2017-09-05 17:51:11 -04:00
|
|
|
className(req) {
|
|
|
|
|
return req.params.className;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 20:45:02 -08:00
|
|
|
handleFind(req) {
|
2016-12-07 15:17:05 -08:00
|
|
|
const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
|
2017-09-05 17:51:11 -04:00
|
|
|
const options = ClassesRouter.optionsFromBody(body);
|
2017-10-02 06:23:09 -07:00
|
|
|
if (req.config.maxLimit && (body.limit > req.config.maxLimit)) {
|
|
|
|
|
// Silently replace the limit on the query with the max configured
|
|
|
|
|
options.limit = Number(req.config.maxLimit);
|
|
|
|
|
}
|
2016-02-09 20:45:02 -08:00
|
|
|
if (body.redirectClassNameForKey) {
|
|
|
|
|
options.redirectClassNameForKey = String(body.redirectClassNameForKey);
|
|
|
|
|
}
|
|
|
|
|
if (typeof body.where === 'string') {
|
|
|
|
|
body.where = JSON.parse(body.where);
|
|
|
|
|
}
|
2017-09-05 17:51:11 -04:00
|
|
|
return rest.find(req.config, req.auth, this.className(req), body.where, options, req.info.clientSDK)
|
2016-02-09 20:45:02 -08:00
|
|
|
.then((response) => {
|
|
|
|
|
return { response: response };
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a promise for a {response} object.
|
|
|
|
|
handleGet(req) {
|
2016-12-07 15:17:05 -08:00
|
|
|
const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
|
|
|
|
|
const options = {};
|
2016-03-30 13:50:36 -04:00
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
for (const key of Object.keys(body)) {
|
2016-04-01 11:51:42 -04:00
|
|
|
if (ALLOWED_GET_QUERY_KEYS.indexOf(key) === -1) {
|
2016-03-30 13:50:36 -04:00
|
|
|
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Improper encode of parameter');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof body.keys == 'string') {
|
|
|
|
|
options.keys = body.keys;
|
|
|
|
|
}
|
|
|
|
|
if (body.include) {
|
|
|
|
|
options.include = String(body.include);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 17:51:11 -04:00
|
|
|
return rest.get(req.config, req.auth, this.className(req), req.params.objectId, options, req.info.clientSDK)
|
2016-02-09 20:45:02 -08:00
|
|
|
.then((response) => {
|
|
|
|
|
if (!response.results || response.results.length == 0) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
|
|
|
|
|
}
|
2016-04-25 12:52:21 -07:00
|
|
|
|
2017-09-05 17:51:11 -04:00
|
|
|
if (this.className(req) === "_User") {
|
2016-04-25 12:52:21 -07:00
|
|
|
|
2016-02-11 20:32:31 -05:00
|
|
|
delete response.results[0].sessionToken;
|
2016-04-25 12:52:21 -07:00
|
|
|
|
2016-02-18 10:54:53 -05:00
|
|
|
const user = response.results[0];
|
2016-04-25 12:52:21 -07:00
|
|
|
|
2016-02-18 10:54:53 -05:00
|
|
|
if (req.auth.user && user.objectId == req.auth.user.id) {
|
|
|
|
|
// Force the session token
|
|
|
|
|
response.results[0].sessionToken = req.info.sessionToken;
|
|
|
|
|
}
|
2016-04-25 12:52:21 -07:00
|
|
|
}
|
2016-02-09 20:45:02 -08:00
|
|
|
return { response: response.results[0] };
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleCreate(req) {
|
2017-09-05 17:51:11 -04:00
|
|
|
return rest.create(req.config, req.auth, this.className(req), req.body, req.info.clientSDK);
|
2016-02-09 20:45:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleUpdate(req) {
|
2017-05-28 20:34:49 -04:00
|
|
|
const where = { objectId: req.params.objectId }
|
2017-09-05 17:51:11 -04:00
|
|
|
return rest.update(req.config, req.auth, this.className(req), where, req.body, req.info.clientSDK);
|
2016-02-09 20:45:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleDelete(req) {
|
2017-09-05 17:51:11 -04:00
|
|
|
return rest.del(req.config, req.auth, this.className(req), req.params.objectId, req.info.clientSDK)
|
2016-02-09 20:45:02 -08:00
|
|
|
.then(() => {
|
|
|
|
|
return {response: {}};
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-20 23:46:05 -08:00
|
|
|
|
|
|
|
|
static JSONFromQuery(query) {
|
2016-12-07 15:17:05 -08:00
|
|
|
const json = {};
|
|
|
|
|
for (const [key, value] of _.entries(query)) {
|
2016-02-20 23:46:05 -08:00
|
|
|
try {
|
|
|
|
|
json[key] = JSON.parse(value);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
json[key] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return json
|
|
|
|
|
}
|
2016-04-25 12:52:21 -07:00
|
|
|
|
2017-09-05 17:51:11 -04:00
|
|
|
static optionsFromBody(body) {
|
|
|
|
|
const allowConstraints = ['skip', 'limit', 'order', 'count', 'keys',
|
|
|
|
|
'include', 'redirectClassNameForKey', 'where'];
|
|
|
|
|
|
|
|
|
|
for (const key of Object.keys(body)) {
|
|
|
|
|
if (allowConstraints.indexOf(key) === -1) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.INVALID_QUERY, `Invalid parameter for query: ${key}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const options = {};
|
|
|
|
|
if (body.skip) {
|
|
|
|
|
options.skip = Number(body.skip);
|
|
|
|
|
}
|
|
|
|
|
if (body.limit || body.limit === 0) {
|
|
|
|
|
options.limit = Number(body.limit);
|
|
|
|
|
} else {
|
|
|
|
|
options.limit = Number(100);
|
|
|
|
|
}
|
|
|
|
|
if (body.order) {
|
|
|
|
|
options.order = String(body.order);
|
|
|
|
|
}
|
|
|
|
|
if (body.count) {
|
|
|
|
|
options.count = true;
|
|
|
|
|
}
|
|
|
|
|
if (typeof body.keys == 'string') {
|
|
|
|
|
options.keys = body.keys;
|
|
|
|
|
}
|
|
|
|
|
if (body.include) {
|
|
|
|
|
options.include = String(body.include);
|
|
|
|
|
}
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 23:47:44 -05:00
|
|
|
mountRoutes() {
|
|
|
|
|
this.route('GET', '/classes/:className', (req) => { return this.handleFind(req); });
|
|
|
|
|
this.route('GET', '/classes/:className/:objectId', (req) => { return this.handleGet(req); });
|
|
|
|
|
this.route('POST', '/classes/:className', (req) => { return this.handleCreate(req); });
|
|
|
|
|
this.route('PUT', '/classes/:className/:objectId', (req) => { return this.handleUpdate(req); });
|
|
|
|
|
this.route('DELETE', '/classes/:className/:objectId', (req) => { return this.handleDelete(req); });
|
2016-02-09 20:45:02 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ClassesRouter;
|