Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make possible to alter response using the after save trigger #5814

Merged
merged 4 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/RestQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,28 @@ describe('RestQuery.each', () => {
expect(resultsOne.length).toBe(1);
expect(resultsTwo.length).toBe(1);
});

it('test afterSave response object is return', done => {
Parse.Cloud.afterSave('TestObject2', function(req) {
const jsonObject = req.object.toJSON();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not so straightforward for others users to use this capability. Can you test just using the code below?

req.object.unset('todelete');
req.object.set('toadd', true);
return req.object;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually an other issue I had. If you do that you will have :

{
  "tokeep": true,
  "createdAt": "2019-07-18T09:12:58.496Z",
  "todelete": {
    "__op": "Delete"
  },
  "updatedAt": "2019-07-18T09:12:58.496Z",
  "toadd": true,
  "objectId": "zC2AmMvGxM"
}

the __op is not really great response :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try like this?

req.object.unset('todelete');
req.object.set('toadd', true);
return req.object._toFullJSON();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that I have :

{ todelete: { __op: 'Delete' },
     tokeep: true,
     createdAt: '2019-07-25T13:49:32.552Z',
     updatedAt: '2019-07-25T13:49:32.552Z',
     toadd: true,
     objectId: '4lgUT2xWo5',
     __type: 'Object',
     className: 'TestObject2' }

with toJson :

{ todelete: { __op: 'Delete' },
     tokeep: true,
     createdAt: '2019-07-25T13:49:32.552Z',
     updatedAt: '2019-07-25T13:49:32.552Z',
     toadd: true,
     objectId: '4lgUT2xWo5' }

delete jsonObject.todelete;
jsonObject.toadd = true;

req.object = {
toJSON: () => jsonObject,
equals: () => false,
};

return req.object;
});

rest
.create(config, nobody, 'TestObject2', { todelete: true, tokeep: true })
.then(response => {
expect(response.response.toadd).toBeTruthy();
expect(response.response.tokeep).toBeTruthy();
expect(response.response.todelete).toBeUndefined();
done();
});
});
});
9 changes: 9 additions & 0 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,15 @@ RestWrite.prototype.runAfterSaveTrigger = function() {
this.config,
this.context
)
.then(result => {
if (
brunoMaurice marked this conversation as resolved.
Show resolved Hide resolved
result &&
result.constructor === Object &&
Object.keys(result).length !== 0
) {
this.response.response = result;
}
})
.catch(function(err) {
logger.warn('afterSave caught an error', err);
});
Expand Down
10 changes: 10 additions & 0 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ export function getResponseObject(request, resolve, reject) {
) {
return resolve(response);
}
if (
brunoMaurice marked this conversation as resolved.
Show resolved Hide resolved
response &&
typeof response.toJSON === 'function' &&
request.triggerName === Types.afterSave
) {
return resolve(response.toJSON());
}
if (request.triggerName === Types.afterSave) {
return resolve();
}
response = {};
if (request.triggerName === Types.beforeSave) {
response['object'] = request.object._getSaveJSON();
Expand Down