Skip to content

Commit ccd8763

Browse files
committed
refactor: Add option to convert Parse.Pointer in Cloud Function payload
1 parent 9674d4a commit ccd8763

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

spec/CloudCode.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,27 @@ describe('Cloud Code', () => {
13531353
});
13541354
});
13551355

1356+
it('should not enode Parse Objects', async () => {
1357+
const user = new Parse.User();
1358+
user.setUsername('username');
1359+
user.setPassword('password');
1360+
user.set('deleted', false);
1361+
await user.signUp();
1362+
Parse.Cloud.define(
1363+
'deleteAccount',
1364+
async req => {
1365+
expect(req.params.object instanceof Parse.Object).not.toBeTrue();
1366+
return 'Object deleted';
1367+
},
1368+
{
1369+
requireMaster: true,
1370+
}
1371+
);
1372+
await Parse.Cloud.run('deleteAccount', { object: user.toPointer() }, { useMasterKey: true });
1373+
});
1374+
13561375
it('allow cloud to encode Parse Objects', async () => {
1376+
await reconfigureServer({ encodeCloudPointers: true });
13571377
const user = new Parse.User();
13581378
user.setUsername('username');
13591379
user.setPassword('password');

src/Options/Definitions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ module.exports.ParseServerOptions = {
210210
action: parsers.booleanParser,
211211
default: false,
212212
},
213+
encodeCloudPointers: {
214+
env: 'PARSE_SERVER_ENCODE_CLOUD_POINTERS',
215+
help: 'Whether Parse Pointers should be encoded in Cloud Code.',
216+
action: parsers.booleanParser,
217+
default: false,
218+
},
213219
encryptionKey: {
214220
env: 'PARSE_SERVER_ENCRYPTION_KEY',
215221
help: 'Key for encrypting your files',

src/Options/docs.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Options/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ export interface ParseServerOptions {
196196
cacheAdapter: ?Adapter<CacheAdapter>;
197197
/* Adapter module for email sending */
198198
emailAdapter: ?Adapter<MailAdapter>;
199+
/* Whether Parse Pointers should be encoded in Cloud Code.
200+
:DEFAULT: false */
201+
encodeCloudPointers: ?boolean;
199202
/* Public URL to your parse server with http:// or https://.
200203
:ENV: PARSE_PUBLIC_SERVER_URL */
201204
publicServerURL: ?string;

src/Routers/FunctionsRouter.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { jobStatusHandler } from '../StatusHandler';
99
import _ from 'lodash';
1010
import { logger } from '../logger';
1111

12-
function parseObject(obj) {
12+
function parseObject(obj, config) {
1313
if (Array.isArray(obj)) {
1414
return obj.map(item => {
1515
return parseObject(item);
@@ -18,21 +18,21 @@ function parseObject(obj) {
1818
return Object.assign(new Date(obj.iso), obj);
1919
} else if (obj && obj.__type == 'File') {
2020
return Parse.File.fromJSON(obj);
21-
} else if (obj && obj.__type == 'Pointer') {
21+
} else if (obj && obj.__type == 'Pointer' && config.encodeCloudPointers) {
2222
return Parse.Object.fromJSON({
2323
__type: 'Pointer',
2424
className: obj.className,
2525
objectId: obj.objectId,
2626
});
2727
} else if (obj && typeof obj === 'object') {
28-
return parseParams(obj);
28+
return parseParams(obj, config);
2929
} else {
3030
return obj;
3131
}
3232
}
3333

34-
function parseParams(params) {
35-
return _.mapValues(params, parseObject);
34+
function parseParams(params, config) {
35+
return _.mapValues(params, item => parseObject(item, config));
3636
}
3737

3838
export class FunctionsRouter extends PromiseRouter {
@@ -66,7 +66,7 @@ export class FunctionsRouter extends PromiseRouter {
6666
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Invalid job.');
6767
}
6868
let params = Object.assign({}, req.body, req.query);
69-
params = parseParams(params);
69+
params = parseParams(params, req.config);
7070
const request = {
7171
params: params,
7272
log: req.config.loggerController,
@@ -126,7 +126,7 @@ export class FunctionsRouter extends PromiseRouter {
126126
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, `Invalid function: "${functionName}"`);
127127
}
128128
let params = Object.assign({}, req.body, req.query);
129-
params = parseParams(params);
129+
params = parseParams(params, req.config);
130130
const request = {
131131
params: params,
132132
master: req.auth && req.auth.isMaster,

0 commit comments

Comments
 (0)