@@ -1,21 +1,22 @@
|
||||
|
||||
import PromiseRouter from '../PromiseRouter';
|
||||
import rest from '../rest';
|
||||
import _ from 'lodash';
|
||||
import Parse from 'parse/node';
|
||||
import rest from '../rest';
|
||||
import _ from 'lodash';
|
||||
import Parse from 'parse/node';
|
||||
|
||||
const ALLOWED_GET_QUERY_KEYS = ['keys', 'include'];
|
||||
|
||||
export class ClassesRouter extends PromiseRouter {
|
||||
|
||||
className(req) {
|
||||
return req.params.className;
|
||||
}
|
||||
|
||||
handleFind(req) {
|
||||
const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
|
||||
const body = Object.assign(
|
||||
req.body,
|
||||
ClassesRouter.JSONFromQuery(req.query)
|
||||
);
|
||||
const options = ClassesRouter.optionsFromBody(body);
|
||||
if (req.config.maxLimit && (body.limit > req.config.maxLimit)) {
|
||||
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);
|
||||
}
|
||||
@@ -25,20 +26,34 @@ export class ClassesRouter extends PromiseRouter {
|
||||
if (typeof body.where === 'string') {
|
||||
body.where = JSON.parse(body.where);
|
||||
}
|
||||
return rest.find(req.config, req.auth, this.className(req), body.where, options, req.info.clientSDK)
|
||||
.then((response) => {
|
||||
return rest
|
||||
.find(
|
||||
req.config,
|
||||
req.auth,
|
||||
this.className(req),
|
||||
body.where,
|
||||
options,
|
||||
req.info.clientSDK
|
||||
)
|
||||
.then(response => {
|
||||
return { response: response };
|
||||
});
|
||||
}
|
||||
|
||||
// Returns a promise for a {response} object.
|
||||
handleGet(req) {
|
||||
const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
|
||||
const body = Object.assign(
|
||||
req.body,
|
||||
ClassesRouter.JSONFromQuery(req.query)
|
||||
);
|
||||
const options = {};
|
||||
|
||||
for (const key of Object.keys(body)) {
|
||||
if (ALLOWED_GET_QUERY_KEYS.indexOf(key) === -1) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Improper encode of parameter');
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_QUERY,
|
||||
'Improper encode of parameter'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,17 +64,27 @@ export class ClassesRouter extends PromiseRouter {
|
||||
options.include = String(body.include);
|
||||
}
|
||||
|
||||
return rest.get(req.config, req.auth, this.className(req), req.params.objectId, options, req.info.clientSDK)
|
||||
.then((response) => {
|
||||
return rest
|
||||
.get(
|
||||
req.config,
|
||||
req.auth,
|
||||
this.className(req),
|
||||
req.params.objectId,
|
||||
options,
|
||||
req.info.clientSDK
|
||||
)
|
||||
.then(response => {
|
||||
if (!response.results || response.results.length == 0) {
|
||||
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
|
||||
throw new Parse.Error(
|
||||
Parse.Error.OBJECT_NOT_FOUND,
|
||||
'Object not found.'
|
||||
);
|
||||
}
|
||||
|
||||
if (this.className(req) === "_User") {
|
||||
|
||||
if (this.className(req) === '_User') {
|
||||
delete response.results[0].sessionToken;
|
||||
|
||||
const user = response.results[0];
|
||||
const user = response.results[0];
|
||||
|
||||
if (req.auth.user && user.objectId == req.auth.user.id) {
|
||||
// Force the session token
|
||||
@@ -71,18 +96,38 @@ export class ClassesRouter extends PromiseRouter {
|
||||
}
|
||||
|
||||
handleCreate(req) {
|
||||
return rest.create(req.config, req.auth, this.className(req), req.body, req.info.clientSDK);
|
||||
return rest.create(
|
||||
req.config,
|
||||
req.auth,
|
||||
this.className(req),
|
||||
req.body,
|
||||
req.info.clientSDK
|
||||
);
|
||||
}
|
||||
|
||||
handleUpdate(req) {
|
||||
const where = { objectId: req.params.objectId }
|
||||
return rest.update(req.config, req.auth, this.className(req), where, req.body, req.info.clientSDK);
|
||||
const where = { objectId: req.params.objectId };
|
||||
return rest.update(
|
||||
req.config,
|
||||
req.auth,
|
||||
this.className(req),
|
||||
where,
|
||||
req.body,
|
||||
req.info.clientSDK
|
||||
);
|
||||
}
|
||||
|
||||
handleDelete(req) {
|
||||
return rest.del(req.config, req.auth, this.className(req), req.params.objectId, req.info.clientSDK)
|
||||
return rest
|
||||
.del(
|
||||
req.config,
|
||||
req.auth,
|
||||
this.className(req),
|
||||
req.params.objectId,
|
||||
req.info.clientSDK
|
||||
)
|
||||
.then(() => {
|
||||
return {response: {}};
|
||||
return { response: {} };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -95,16 +140,28 @@ export class ClassesRouter extends PromiseRouter {
|
||||
json[key] = value;
|
||||
}
|
||||
}
|
||||
return json
|
||||
return json;
|
||||
}
|
||||
|
||||
static optionsFromBody(body) {
|
||||
const allowConstraints = ['skip', 'limit', 'order', 'count', 'keys',
|
||||
'include', 'includeAll', 'redirectClassNameForKey', 'where'];
|
||||
const allowConstraints = [
|
||||
'skip',
|
||||
'limit',
|
||||
'order',
|
||||
'count',
|
||||
'keys',
|
||||
'include',
|
||||
'includeAll',
|
||||
'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}`);
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_QUERY,
|
||||
`Invalid parameter for query: ${key}`
|
||||
);
|
||||
}
|
||||
}
|
||||
const options = {};
|
||||
@@ -135,11 +192,21 @@ export class ClassesRouter extends PromiseRouter {
|
||||
}
|
||||
|
||||
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); });
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user