Skip to content

Commit 34614e0

Browse files
authored
Pass context in beforeDelete, afterDelete, beforeFind and Parse.Cloud.run. (#6666)
* add context for following hooks. 1. beforeDelete 2. afterDelete 3. beforeFind 4. Cloud Function * revert un-necessary code change. * fix: failing test cases. * fix: failing test cases. * fix: failing test cases. * fix: failing test cases. * fix: failing test cases. * fix: failing test cases. * fix: failing test cases. * review changes * revert changes * revert changes * review changes * lint changes * review changes
1 parent 4437ea7 commit 34614e0

19 files changed

+130
-43
lines changed

spec/CloudCode.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,4 +2968,58 @@ describe('afterLogin hook', () => {
29682968
obj.set("obj2", obj2);
29692969
await obj.save(null, { context: { a: 'a' } });
29702970
});
2971+
2972+
it('should have access to context as saveAll argument', async () => {
2973+
Parse.Cloud.beforeSave('TestObject', (req) => {
2974+
expect(req.context.a).toEqual('a');
2975+
});
2976+
Parse.Cloud.afterSave('TestObject', (req) => {
2977+
expect(req.context.a).toEqual('a');
2978+
});
2979+
const obj1 = new TestObject();
2980+
const obj2 = new TestObject();
2981+
await Parse.Object.saveAll([obj1, obj2], { context: { a: 'a' }});
2982+
});
2983+
2984+
it('should have access to context as destroyAll argument', async () => {
2985+
Parse.Cloud.beforeDelete('TestObject', (req) => {
2986+
expect(req.context.a).toEqual('a');
2987+
});
2988+
Parse.Cloud.afterDelete('TestObject', (req) => {
2989+
expect(req.context.a).toEqual('a');
2990+
});
2991+
const obj1 = new TestObject();
2992+
const obj2 = new TestObject();
2993+
await Parse.Object.saveAll([obj1, obj2]);
2994+
await Parse.Object.destroyAll([obj1, obj2], { context: { a: 'a' } });
2995+
});
2996+
2997+
it('should have access to context as destroy a object', async () => {
2998+
Parse.Cloud.beforeDelete('TestObject', (req) => {
2999+
expect(req.context.a).toEqual('a');
3000+
});
3001+
Parse.Cloud.afterDelete('TestObject', (req) => {
3002+
expect(req.context.a).toEqual('a');
3003+
});
3004+
const obj = new TestObject();
3005+
await obj.save();
3006+
await obj.destroy({ context: { a: 'a' } });
3007+
});
3008+
3009+
it('should have access to context in beforeFind hook', async () => {
3010+
Parse.Cloud.beforeFind('TestObject', (req) => {
3011+
expect(req.context.a).toEqual('a');
3012+
});
3013+
const query = new Parse.Query('TestObject');
3014+
return query.find({ context: { a: 'a' } });
3015+
});
3016+
3017+
it('should have access to context when cloud function is called.', async () => {
3018+
Parse.Cloud.define('contextTest', async (req) => {
3019+
expect(req.context.a).toEqual('a');
3020+
return {};
3021+
});
3022+
3023+
await Parse.Cloud.run('contextTest', {}, { context: { a: 'a' } });
3024+
});
29713025
});

src/GraphQL/helpers/objectsMutations.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const createObject = async (className, fields, config, auth, info) => {
55
fields = {};
66
}
77

8-
return (await rest.create(config, auth, className, fields, info.clientSDK))
8+
return (await rest.create(config, auth, className, fields, info.clientSDK, info.context))
99
.response;
1010
};
1111

@@ -27,12 +27,13 @@ const updateObject = async (
2727
className,
2828
{ objectId },
2929
fields,
30-
info.clientSDK
30+
info.clientSDK,
31+
info.context
3132
)).response;
3233
};
3334

3435
const deleteObject = async (className, objectId, config, auth, info) => {
35-
await rest.del(config, auth, className, objectId, info.clientSDK);
36+
await rest.del(config, auth, className, objectId, info.context);
3637
return true;
3738
};
3839

src/GraphQL/helpers/objectsQueries.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ const getObject = async (
7474
className,
7575
objectId,
7676
options,
77-
info.clientSDK
77+
info.clientSDK,
78+
info.context
7879
);
7980

8081
if (!response.results || response.results.length == 0) {
@@ -142,7 +143,8 @@ const findObjects = async (
142143
className,
143144
where,
144145
preCountOptions,
145-
info.clientSDK
146+
info.clientSDK,
147+
info.context
146148
)
147149
).count;
148150
if ((skip || 0) + limit < preCount) {
@@ -222,7 +224,8 @@ const findObjects = async (
222224
className,
223225
where,
224226
options,
225-
info.clientSDK
227+
info.clientSDK,
228+
info.context
226229
);
227230
results = findResult.results;
228231
count = findResult.count;

src/GraphQL/loaders/usersQueries.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ const getUserFromSessionToken = async (
5555
'_Session',
5656
{ sessionToken },
5757
options,
58-
info.clientVersion
58+
info.clientVersion,
59+
info.context,
5960
);
6061
if (
6162
!response.results ||

src/ParseServerRESTController.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function ParseServerRESTController(applicationId, router) {
107107
info: {
108108
applicationId: applicationId,
109109
sessionToken: options.sessionToken,
110+
context: options.context || {}, // Add context
110111
},
111112
query,
112113
};

src/RestWrite.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function RestWrite(
3232
data,
3333
originalData,
3434
clientSDK,
35+
context,
3536
action
3637
) {
3738
if (auth.isReadOnly) {
@@ -46,18 +47,12 @@ function RestWrite(
4647
this.clientSDK = clientSDK;
4748
this.storage = {};
4849
this.runOptions = {};
49-
this.context = {};
50+
this.context = context || {};
5051

5152
if (action) {
5253
this.runOptions.action = action;
5354
}
5455

55-
// Parse context
56-
if (data._context && data._context instanceof Object) {
57-
this.context = data._context;
58-
delete data._context;
59-
}
60-
6156
if (!query) {
6257
if (this.config.allowCustomObjectId) {
6358
if (

src/Routers/AggregateRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export class AggregateRouter extends ClassesRouter {
6969
this.className(req),
7070
body.where,
7171
options,
72-
req.info.clientSDK
72+
req.info.clientSDK,
73+
req.info.context,
7374
)
7475
.then(response => {
7576
for (const result of response.results) {

src/Routers/AudiencesRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export class AudiencesRouter extends ClassesRouter {
2121
'_Audience',
2222
body.where,
2323
options,
24-
req.info.clientSDK
24+
req.info.clientSDK,
25+
req.info.context,
2526
)
2627
.then(response => {
2728
response.results.forEach(item => {

src/Routers/ClassesRouter.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export class ClassesRouter extends PromiseRouter {
4040
this.className(req),
4141
body.where,
4242
options,
43-
req.info.clientSDK
43+
req.info.clientSDK,
44+
req.info.context,
4445
)
4546
.then(response => {
4647
return { response: response };
@@ -120,7 +121,8 @@ export class ClassesRouter extends PromiseRouter {
120121
req.auth,
121122
this.className(req),
122123
req.body,
123-
req.info.clientSDK
124+
req.info.clientSDK,
125+
req.info.context
124126
);
125127
}
126128

@@ -132,7 +134,8 @@ export class ClassesRouter extends PromiseRouter {
132134
this.className(req),
133135
where,
134136
req.body,
135-
req.info.clientSDK
137+
req.info.clientSDK,
138+
req.info.context
136139
);
137140
}
138141

@@ -143,7 +146,7 @@ export class ClassesRouter extends PromiseRouter {
143146
req.auth,
144147
this.className(req),
145148
req.params.objectId,
146-
req.info.clientSDK
149+
req.info.context
147150
)
148151
.then(() => {
149152
return { response: {} };

src/Routers/CloudCodeRouter.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export class CloudCodeRouter extends PromiseRouter {
8888
req.auth,
8989
'_JobSchedule',
9090
formatJobSchedule(job_schedule),
91-
req.client
91+
req.client,
92+
req.info.context
9293
);
9394
}
9495

@@ -102,7 +103,9 @@ export class CloudCodeRouter extends PromiseRouter {
102103
req.auth,
103104
'_JobSchedule',
104105
{ objectId },
105-
formatJobSchedule(job_schedule)
106+
formatJobSchedule(job_schedule),
107+
undefined,
108+
req.info.context
106109
)
107110
.then(response => {
108111
return {
@@ -114,7 +117,7 @@ export class CloudCodeRouter extends PromiseRouter {
114117
static deleteJob(req) {
115118
const { objectId } = req.params;
116119
return rest
117-
.del(req.config, req.auth, '_JobSchedule', objectId)
120+
.del(req.config, req.auth, '_JobSchedule', objectId, req.info.context)
118121
.then(response => {
119122
return {
120123
response,

src/Routers/FunctionsRouter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export class FunctionsRouter extends PromiseRouter {
148148
headers: req.config.headers,
149149
ip: req.config.ip,
150150
functionName,
151+
context: req.info.context,
151152
};
152153

153154
if (theValidator && typeof theValidator === 'function') {

src/Routers/IAPValidationRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ function getFileForProductIdentifier(productIdentifier, req) {
5151
'_Product',
5252
{ productIdentifier: productIdentifier },
5353
undefined,
54-
req.info.clientSDK
54+
req.info.clientSDK,
55+
req.info.context
5556
)
5657
.then(function(result) {
5758
const products = result.results;

src/Routers/InstallationsRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export class InstallationsRouter extends ClassesRouter {
2121
'_Installation',
2222
body.where,
2323
options,
24-
req.info.clientSDK
24+
req.info.clientSDK,
25+
req.info.context
2526
)
2627
.then(response => {
2728
return { response: response };

src/Routers/SessionsRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export class SessionsRouter extends ClassesRouter {
2323
'_Session',
2424
{ sessionToken: req.info.sessionToken },
2525
undefined,
26-
req.info.clientSDK
26+
req.info.clientSDK,
27+
req.info.context
2728
)
2829
.then(response => {
2930
if (!response.results || response.results.length == 0) {

src/Routers/UsersRouter.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ export class UsersRouter extends ClassesRouter {
178178
'_Session',
179179
{ sessionToken },
180180
{ include: 'user' },
181-
req.info.clientSDK
181+
req.info.clientSDK,
182+
req.info.context
182183
)
183184
.then(response => {
184185
if (
@@ -302,7 +303,8 @@ export class UsersRouter extends ClassesRouter {
302303
'_Session',
303304
{ sessionToken: req.info.sessionToken },
304305
undefined,
305-
req.info.clientSDK
306+
req.info.clientSDK,
307+
req.info.context
306308
)
307309
.then(records => {
308310
if (records.results && records.results.length) {
@@ -311,7 +313,8 @@ export class UsersRouter extends ClassesRouter {
311313
req.config,
312314
Auth.master(req.config),
313315
'_Session',
314-
records.results[0].objectId
316+
records.results[0].objectId,
317+
req.info.context
315318
)
316319
.then(() => {
317320
this._runAfterLogoutTrigger(req, records.results[0]);

src/batch.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ function handleBatch(router, req) {
100100
info: req.info,
101101
};
102102

103-
// Add context to request body
104-
if (req.body._context) { request.body._context = req.body._context; }
105-
106103
return router
107104
.tryRouteRequest(restRequest.method, routablePath, request)
108105
.then(

src/middlewares.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function handleParseHeaders(req, res, next) {
3333
dotNetKey: req.get('X-Parse-Windows-Key'),
3434
restAPIKey: req.get('X-Parse-REST-API-Key'),
3535
clientVersion: req.get('X-Parse-Client-Version'),
36+
context: {},
3637
};
3738

3839
var basicAuth = httpAuth(req);
@@ -103,6 +104,10 @@ export function handleParseHeaders(req, res, next) {
103104
info.masterKey = req.body._MasterKey;
104105
delete req.body._MasterKey;
105106
}
107+
if (req.body._context && req.body._context instanceof Object) {
108+
info.context = req.body._context;
109+
delete req.body._context;
110+
}
106111
if (req.body._ContentType) {
107112
req.headers['content-type'] = req.body._ContentType;
108113
delete req.body._ContentType;

0 commit comments

Comments
 (0)