diff --git a/DEPRECATIONS.md b/DEPRECATIONS.md index e4ca0d41d9..82aa8144a0 100644 --- a/DEPRECATIONS.md +++ b/DEPRECATIONS.md @@ -10,6 +10,7 @@ The following is a list of deprecations, according to the [Deprecation Policy](h | DEPPS4 | Remove convenience method for http request `Parse.Cloud.httpRequest` | [#7589](https://github.com/parse-community/parse-server/pull/7589) | 5.0.0 (2022) | 6.0.0 (2023) | deprecated | - | | DEPPS5 | Config option `allowClientClassCreation` defaults to `false` | [#7925](https://github.com/parse-community/parse-server/pull/7925) | 5.3.0 (2022) | 7.0.0 (2024) | deprecated | - | | DEPPS6 | Auth providers disabled by default | [#7953](https://github.com/parse-community/parse-server/pull/7953) | 5.3.0 (2022) | 7.0.0 (2024) | deprecated | - | +| DEPPS7 | Remove file trigger syntax `Parse.Cloud.beforeSaveFile((request) => {})` | [#7966](https://github.com/parse-community/parse-server/pull/7966) | 5.3.0 (2022) | 7.0.0 (2024) | deprecated | - | [i_deprecation]: ## "The version and date of the deprecation." [i_removal]: ## "The version and date of the planned removal." diff --git a/spec/CloudCode.Validator.spec.js b/spec/CloudCode.Validator.spec.js index b5d5134533..196568f763 100644 --- a/spec/CloudCode.Validator.spec.js +++ b/spec/CloudCode.Validator.spec.js @@ -737,7 +737,8 @@ describe('cloud validator', () => { }); it('basic beforeSaveFile skipWithMasterKey', async done => { - Parse.Cloud.beforeSaveFile( + Parse.Cloud.beforeSave( + Parse.File, () => { throw 'beforeSaveFile should have resolved using master key.'; }, @@ -1431,7 +1432,7 @@ describe('cloud validator', () => { }); it('validate beforeSaveFile', async done => { - Parse.Cloud.beforeSaveFile(() => {}, validatorSuccess); + Parse.Cloud.beforeSave(Parse.File, () => {}, validatorSuccess); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); const result = await file.save({ useMasterKey: true }); @@ -1440,7 +1441,7 @@ describe('cloud validator', () => { }); it('validate beforeSaveFile fail', async done => { - Parse.Cloud.beforeSaveFile(() => {}, validatorFail); + Parse.Cloud.beforeSave(Parse.File, () => {}, validatorFail); try { const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); await file.save({ useMasterKey: true }); @@ -1452,7 +1453,7 @@ describe('cloud validator', () => { }); it('validate afterSaveFile', async done => { - Parse.Cloud.afterSaveFile(() => {}, validatorSuccess); + Parse.Cloud.afterSave(Parse.File, () => {}, validatorSuccess); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); const result = await file.save({ useMasterKey: true }); @@ -1461,7 +1462,7 @@ describe('cloud validator', () => { }); it('validate afterSaveFile fail', async done => { - Parse.Cloud.beforeSaveFile(() => {}, validatorFail); + Parse.Cloud.beforeSave(Parse.File, () => {}, validatorFail); try { const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); await file.save({ useMasterKey: true }); @@ -1473,7 +1474,7 @@ describe('cloud validator', () => { }); it('validate beforeDeleteFile', async done => { - Parse.Cloud.beforeDeleteFile(() => {}, validatorSuccess); + Parse.Cloud.beforeDelete(Parse.File, () => {}, validatorSuccess); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); await file.save(); @@ -1482,7 +1483,7 @@ describe('cloud validator', () => { }); it('validate beforeDeleteFile fail', async done => { - Parse.Cloud.beforeDeleteFile(() => {}, validatorFail); + Parse.Cloud.beforeDelete(Parse.File, () => {}, validatorFail); try { const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); await file.save(); @@ -1495,7 +1496,7 @@ describe('cloud validator', () => { }); it('validate afterDeleteFile', async done => { - Parse.Cloud.afterDeleteFile(() => {}, validatorSuccess); + Parse.Cloud.afterDelete(Parse.File, () => {}, validatorSuccess); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); await file.save(); @@ -1504,7 +1505,7 @@ describe('cloud validator', () => { }); it('validate afterDeleteFile fail', async done => { - Parse.Cloud.afterDeleteFile(() => {}, validatorFail); + Parse.Cloud.afterDelete(Parse.File, () => {}, validatorFail); try { const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); await file.save(); diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 4df3c90275..bf1a2c8c42 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -3405,7 +3405,7 @@ describe('saveFile hooks', () => { it('beforeSaveFile should return file that is already saved and not save anything to files adapter', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); const createFileSpy = spyOn(mockAdapter, 'createFile').and.callThrough(); - Parse.Cloud.beforeSaveFile(() => { + Parse.Cloud.beforeSave(Parse.File, () => { const newFile = new Parse.File('some-file.txt'); newFile._url = 'http://www.somewhere.com/parse/files/some-app-id/some-file.txt'; return newFile; @@ -3420,7 +3420,7 @@ describe('saveFile hooks', () => { it('beforeSaveFile should throw error', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); - Parse.Cloud.beforeSaveFile(() => { + Parse.Cloud.beforeSave(Parse.File, () => { throw new Parse.Error(400, 'some-error-message'); }); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); @@ -3434,8 +3434,8 @@ describe('saveFile hooks', () => { it('beforeSaveFile should change values of uploaded file by editing fileObject directly', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); const createFileSpy = spyOn(mockAdapter, 'createFile').and.callThrough(); - Parse.Cloud.beforeSaveFile(async req => { - expect(req.triggerName).toEqual('beforeSaveFile'); + Parse.Cloud.beforeSave(Parse.File, async req => { + expect(req.triggerName).toEqual('beforeSave'); expect(req.master).toBe(true); req.file.addMetadata('foo', 'bar'); req.file.addTag('tagA', 'some-tag'); @@ -3463,8 +3463,8 @@ describe('saveFile hooks', () => { it('beforeSaveFile should change values by returning new fileObject', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); const createFileSpy = spyOn(mockAdapter, 'createFile').and.callThrough(); - Parse.Cloud.beforeSaveFile(async req => { - expect(req.triggerName).toEqual('beforeSaveFile'); + Parse.Cloud.beforeSave(Parse.File, async req => { + expect(req.triggerName).toEqual('beforeSave'); expect(req.fileSize).toBe(3); const newFile = new Parse.File('donald_duck.pdf', [4, 5, 6], 'application/pdf'); newFile.setMetadata({ foo: 'bar' }); @@ -3497,8 +3497,8 @@ describe('saveFile hooks', () => { it('beforeSaveFile should contain metadata and tags saved from client', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); const createFileSpy = spyOn(mockAdapter, 'createFile').and.callThrough(); - Parse.Cloud.beforeSaveFile(async req => { - expect(req.triggerName).toEqual('beforeSaveFile'); + Parse.Cloud.beforeSave(Parse.File, async req => { + expect(req.triggerName).toEqual('beforeSave'); expect(req.fileSize).toBe(3); expect(req.file).toBeInstanceOf(Parse.File); expect(req.file.name()).toBe('popeye.txt'); @@ -3526,7 +3526,7 @@ describe('saveFile hooks', () => { await reconfigureServer({ filesAdapter: mockAdapter }); const config = Config.get('test'); config.filesController.options.preserveFileName = true; - Parse.Cloud.beforeSaveFile(async ({ file }) => { + Parse.Cloud.beforeSave(Parse.File, async ({ file }) => { expect(file.name()).toBe('popeye.txt'); const fileData = await file.getData(); const newFile = new Parse.File('2020-04-01.txt', { base64: fileData }); @@ -3540,13 +3540,13 @@ describe('saveFile hooks', () => { it('afterSaveFile should set fileSize to null if beforeSave returns an already saved file', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); const createFileSpy = spyOn(mockAdapter, 'createFile').and.callThrough(); - Parse.Cloud.beforeSaveFile(req => { + Parse.Cloud.beforeSave(Parse.File, req => { expect(req.fileSize).toBe(3); const newFile = new Parse.File('some-file.txt'); newFile._url = 'http://www.somewhere.com/parse/files/some-app-id/some-file.txt'; return newFile; }); - Parse.Cloud.afterSaveFile(req => { + Parse.Cloud.afterSave(Parse.File, req => { expect(req.fileSize).toBe(null); }); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); @@ -3559,7 +3559,7 @@ describe('saveFile hooks', () => { it('afterSaveFile should throw error', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); - Parse.Cloud.afterSaveFile(async () => { + Parse.Cloud.afterSave(Parse.File, async () => { throw new Parse.Error(400, 'some-error-message'); }); const filename = 'donald_duck.pdf'; @@ -3573,11 +3573,11 @@ describe('saveFile hooks', () => { it('afterSaveFile should call with fileObject', async done => { await reconfigureServer({ filesAdapter: mockAdapter }); - Parse.Cloud.beforeSaveFile(async req => { + Parse.Cloud.beforeSave(Parse.File, async req => { req.file.setTags({ tagA: 'some-tag' }); req.file.setMetadata({ foo: 'bar' }); }); - Parse.Cloud.afterSaveFile(async req => { + Parse.Cloud.afterSave(Parse.File, async req => { expect(req.master).toBe(true); expect(req.file._tags).toEqual({ tagA: 'some-tag' }); expect(req.file._metadata).toEqual({ foo: 'bar' }); @@ -3589,13 +3589,13 @@ describe('saveFile hooks', () => { it('afterSaveFile should change fileSize when file data changes', async done => { await reconfigureServer({ filesAdapter: mockAdapter }); - Parse.Cloud.beforeSaveFile(async req => { + Parse.Cloud.beforeSave(Parse.File, async req => { expect(req.fileSize).toBe(3); expect(req.master).toBe(true); const newFile = new Parse.File('donald_duck.pdf', [4, 5, 6, 7, 8, 9], 'application/pdf'); return newFile; }); - Parse.Cloud.afterSaveFile(async req => { + Parse.Cloud.afterSave(Parse.File, async req => { expect(req.fileSize).toBe(6); expect(req.master).toBe(true); done(); @@ -3606,7 +3606,7 @@ describe('saveFile hooks', () => { it('beforeDeleteFile should call with fileObject', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); - Parse.Cloud.beforeDeleteFile(req => { + Parse.Cloud.beforeDelete(Parse.File, req => { expect(req.file).toBeInstanceOf(Parse.File); expect(req.file._name).toEqual('popeye.txt'); expect(req.file._url).toEqual('http://www.somewhere.com/popeye.txt'); @@ -3618,7 +3618,7 @@ describe('saveFile hooks', () => { it('beforeDeleteFile should throw error', async done => { await reconfigureServer({ filesAdapter: mockAdapter }); - Parse.Cloud.beforeDeleteFile(() => { + Parse.Cloud.beforeDelete(Parse.File, () => { throw new Error('some error message'); }); const file = new Parse.File('popeye.txt'); @@ -3632,12 +3632,12 @@ describe('saveFile hooks', () => { it('afterDeleteFile should call with fileObject', async done => { await reconfigureServer({ filesAdapter: mockAdapter }); - Parse.Cloud.beforeDeleteFile(req => { + Parse.Cloud.beforeDelete(Parse.File, req => { expect(req.file).toBeInstanceOf(Parse.File); expect(req.file._name).toEqual('popeye.txt'); expect(req.file._url).toEqual('http://www.somewhere.com/popeye.txt'); }); - Parse.Cloud.afterDeleteFile(req => { + Parse.Cloud.afterDelete(Parse.File, req => { expect(req.file).toBeInstanceOf(Parse.File); expect(req.file._name).toEqual('popeye.txt'); expect(req.file._url).toEqual('http://www.somewhere.com/popeye.txt'); @@ -3649,7 +3649,7 @@ describe('saveFile hooks', () => { it('beforeSaveFile should not change file if nothing is returned', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); - Parse.Cloud.beforeSaveFile(() => { + Parse.Cloud.beforeSave(Parse.File, () => { return; }); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); @@ -3658,7 +3658,7 @@ describe('saveFile hooks', () => { }); it('throw custom error from beforeSaveFile', async done => { - Parse.Cloud.beforeSaveFile(() => { + Parse.Cloud.beforeSave(Parse.File, () => { throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'It should fail'); }); try { @@ -3672,7 +3672,7 @@ describe('saveFile hooks', () => { }); it('throw empty error from beforeSaveFile', async done => { - Parse.Cloud.beforeSaveFile(() => { + Parse.Cloud.beforeSave(Parse.File, () => { throw null; }); try { @@ -3684,6 +3684,55 @@ describe('saveFile hooks', () => { done(); } }); + + it('legacy hooks', async () => { + await reconfigureServer({ filesAdapter: mockAdapter }); + const logger = require('../lib/logger').logger; + const logSpy = spyOn(logger, 'warn').and.callFake(() => {}); + const triggers = { + beforeSaveFile(req) { + req.file.setTags({ tagA: 'some-tag' }); + req.file.setMetadata({ foo: 'bar' }); + expect(req.triggerName).toEqual('beforeSave'); + expect(req.master).toBe(true); + }, + afterSaveFile(req) { + expect(req.master).toBe(true); + expect(req.file._tags).toEqual({ tagA: 'some-tag' }); + expect(req.file._metadata).toEqual({ foo: 'bar' }); + }, + beforeDeleteFile(req) { + expect(req.file).toBeInstanceOf(Parse.File); + expect(req.file._name).toEqual('popeye.txt'); + expect(req.file._url).toEqual('http://www.somewhere.com/popeye.txt'); + expect(req.fileSize).toBe(null); + }, + afterDeleteFile(req) { + expect(req.file).toBeInstanceOf(Parse.File); + expect(req.file._name).toEqual('popeye.txt'); + expect(req.file._url).toEqual('http://www.somewhere.com/popeye.txt'); + }, + }; + + for (const key in triggers) { + spyOn(triggers, key).and.callThrough(); + Parse.Cloud[key](triggers[key]); + } + + const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); + await file.save({ useMasterKey: true }); + await new Parse.File('popeye.txt', [1, 2, 3], 'text/plain').destroy({ useMasterKey: true }); + await new Promise(resolve => setTimeout(resolve, 100)); + for (const key in triggers) { + expect(triggers[key]).toHaveBeenCalled(); + expect(logSpy).toHaveBeenCalledWith( + `DeprecationWarning: Parse.Cloud.${key} is deprecated and will be removed in a future version. Use Parse.Cloud.${key.replace( + 'File', + '' + )}(Parse.File, (request) => {})` + ); + } + }); }); describe('sendEmail', () => { diff --git a/src/Routers/FilesRouter.js b/src/Routers/FilesRouter.js index c0c7e00f13..cccf8e5504 100644 --- a/src/Routers/FilesRouter.js +++ b/src/Routers/FilesRouter.js @@ -141,7 +141,7 @@ export class FilesRouter { try { // run beforeSaveFile trigger const triggerResult = await triggers.maybeRunFileTrigger( - triggers.Types.beforeSaveFile, + triggers.Types.beforeSave, fileObject, config, req.auth @@ -194,12 +194,7 @@ export class FilesRouter { }; } // run afterSaveFile trigger - await triggers.maybeRunFileTrigger( - triggers.Types.afterSaveFile, - fileObject, - config, - req.auth - ); + await triggers.maybeRunFileTrigger(triggers.Types.afterSave, fileObject, config, req.auth); res.status(201); res.set('Location', saveResult.url); res.json(saveResult); @@ -222,7 +217,7 @@ export class FilesRouter { file._url = filesController.adapter.getFileLocation(req.config, filename); const fileObject = { file, fileSize: null }; await triggers.maybeRunFileTrigger( - triggers.Types.beforeDeleteFile, + triggers.Types.beforeDelete, fileObject, req.config, req.auth @@ -231,7 +226,7 @@ export class FilesRouter { await filesController.deleteFile(req.config, filename); // run afterDeleteFile trigger await triggers.maybeRunFileTrigger( - triggers.Types.afterDeleteFile, + triggers.Types.afterDelete, fileObject, req.config, req.auth diff --git a/src/cloud-code/Parse.Cloud.js b/src/cloud-code/Parse.Cloud.js index 9fb437cada..1ee02fdb60 100644 --- a/src/cloud-code/Parse.Cloud.js +++ b/src/cloud-code/Parse.Cloud.js @@ -134,7 +134,7 @@ ParseCloud.job = function (functionName, handler) { * * **Available in Cloud Code only.** * - * If you want to use beforeSave for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1. + * If you want to use beforeSave for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User} or {@link Parse.File}), you should pass the class itself and not the String for arg1. * * ``` * Parse.Cloud.beforeSave('MyCustomClass', (request) => { @@ -171,7 +171,7 @@ ParseCloud.beforeSave = function (parseClass, handler, validationHandler) { * * **Available in Cloud Code only.** * - * If you want to use beforeDelete for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1. + * If you want to use beforeDelete for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User} or {@link Parse.File}), you should pass the class itself and not the String for arg1. * ``` * Parse.Cloud.beforeDelete('MyCustomClass', (request) => { * // code here @@ -300,7 +300,7 @@ ParseCloud.afterLogout = function (handler) { * * **Available in Cloud Code only.** * - * If you want to use afterSave for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1. + * If you want to use afterSave for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User} or {@link Parse.File}), you should pass the class itself and not the String for arg1. * * ``` * Parse.Cloud.afterSave('MyCustomClass', async function(request) { @@ -337,7 +337,7 @@ ParseCloud.afterSave = function (parseClass, handler, validationHandler) { * * **Available in Cloud Code only.** * - * If you want to use afterDelete for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1. + * If you want to use afterDelete for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User} or {@link Parse.File}), you should pass the class itself and not the String for arg1. * ``` * Parse.Cloud.afterDelete('MyCustomClass', async (request) => { * // code here @@ -373,7 +373,7 @@ ParseCloud.afterDelete = function (parseClass, handler, validationHandler) { * * **Available in Cloud Code only.** * - * If you want to use beforeFind for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1. + * If you want to use beforeFind for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User} or {@link Parse.File}), you should pass the class itself and not the String for arg1. * ``` * Parse.Cloud.beforeFind('MyCustomClass', async (request) => { * // code here @@ -409,7 +409,7 @@ ParseCloud.beforeFind = function (parseClass, handler, validationHandler) { * * **Available in Cloud Code only.** * - * If you want to use afterFind for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1. + * If you want to use afterFind for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User} or {@link Parse.File}), you should pass the class itself and not the String for arg1. * ``` * Parse.Cloud.afterFind('MyCustomClass', async (request) => { * // code here @@ -458,18 +458,17 @@ ParseCloud.afterFind = function (parseClass, handler, validationHandler) { *``` * * @method beforeSaveFile + * @deprecated * @name Parse.Cloud.beforeSaveFile * @param {Function} func The function to run before saving a file. This function can be async and should take just one parameter, {@link Parse.Cloud.FileTriggerRequest}. * @param {(Object|Function)} validator An optional function to help validating cloud code. This function can be an async function and should take one parameter a {@link Parse.Cloud.FileTriggerRequest}, or a {@link Parse.Cloud.ValidatorObject}. */ ParseCloud.beforeSaveFile = function (handler, validationHandler) { - validateValidator(validationHandler); - triggers.addFileTrigger( - triggers.Types.beforeSaveFile, - handler, - Parse.applicationId, - validationHandler - ); + Deprecator.logRuntimeDeprecation({ + usage: 'Parse.Cloud.beforeSaveFile', + solution: 'Use Parse.Cloud.beforeSave(Parse.File, (request) => {})', + }); + ParseCloud.beforeSave(Parse.File, handler, validationHandler); }; /** @@ -490,18 +489,17 @@ ParseCloud.beforeSaveFile = function (handler, validationHandler) { *``` * * @method afterSaveFile + * @deprecated * @name Parse.Cloud.afterSaveFile * @param {Function} func The function to run after saving a file. This function can be async and should take just one parameter, {@link Parse.Cloud.FileTriggerRequest}. * @param {(Object|Function)} validator An optional function to help validating cloud code. This function can be an async function and should take one parameter a {@link Parse.Cloud.FileTriggerRequest}, or a {@link Parse.Cloud.ValidatorObject}. */ ParseCloud.afterSaveFile = function (handler, validationHandler) { - validateValidator(validationHandler); - triggers.addFileTrigger( - triggers.Types.afterSaveFile, - handler, - Parse.applicationId, - validationHandler - ); + Deprecator.logRuntimeDeprecation({ + usage: 'Parse.Cloud.afterSaveFile', + solution: 'Use Parse.Cloud.afterSave(Parse.File, (request) => {})', + }); + ParseCloud.afterSave(Parse.File, handler, validationHandler); }; /** @@ -522,18 +520,17 @@ ParseCloud.afterSaveFile = function (handler, validationHandler) { *``` * * @method beforeDeleteFile + * @deprecated * @name Parse.Cloud.beforeDeleteFile * @param {Function} func The function to run before deleting a file. This function can be async and should take just one parameter, {@link Parse.Cloud.FileTriggerRequest}. * @param {(Object|Function)} validator An optional function to help validating cloud code. This function can be an async function and should take one parameter a {@link Parse.Cloud.FileTriggerRequest}, or a {@link Parse.Cloud.ValidatorObject}. */ ParseCloud.beforeDeleteFile = function (handler, validationHandler) { - validateValidator(validationHandler); - triggers.addFileTrigger( - triggers.Types.beforeDeleteFile, - handler, - Parse.applicationId, - validationHandler - ); + Deprecator.logRuntimeDeprecation({ + usage: 'Parse.Cloud.beforeDeleteFile', + solution: 'Use Parse.Cloud.beforeDelete(Parse.File, (request) => {})', + }); + ParseCloud.beforeDelete(Parse.File, handler, validationHandler); }; /** @@ -554,18 +551,17 @@ ParseCloud.beforeDeleteFile = function (handler, validationHandler) { *``` * * @method afterDeleteFile + * @deprecated * @name Parse.Cloud.afterDeleteFile * @param {Function} func The function to after before deleting a file. This function can be async and should take just one parameter, {@link Parse.Cloud.FileTriggerRequest}. * @param {(Object|Function)} validator An optional function to help validating cloud code. This function can be an async function and should take one parameter a {@link Parse.Cloud.FileTriggerRequest}, or a {@link Parse.Cloud.ValidatorObject}. */ ParseCloud.afterDeleteFile = function (handler, validationHandler) { - validateValidator(validationHandler); - triggers.addFileTrigger( - triggers.Types.afterDeleteFile, - handler, - Parse.applicationId, - validationHandler - ); + Deprecator.logRuntimeDeprecation({ + usage: 'Parse.Cloud.afterDeleteFile', + solution: 'Use Parse.Cloud.afterDelete(Parse.File, (request) => {})', + }); + ParseCloud.afterDelete(Parse.File, handler, validationHandler); }; /** @@ -636,7 +632,7 @@ ParseCloud.sendEmail = function (data) { * * **Available in Cloud Code only.** * - * If you want to use beforeSubscribe for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1. + * If you want to use beforeSubscribe for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User} or {@link Parse.File}), you should pass the class itself and not the String for arg1. * ``` * Parse.Cloud.beforeSubscribe('MyCustomClass', (request) => { * // code here diff --git a/src/triggers.js b/src/triggers.js index 360166d0aa..4ba21b32ea 100644 --- a/src/triggers.js +++ b/src/triggers.js @@ -12,16 +12,11 @@ export const Types = { afterDelete: 'afterDelete', beforeFind: 'beforeFind', afterFind: 'afterFind', - beforeSaveFile: 'beforeSaveFile', - afterSaveFile: 'afterSaveFile', - beforeDeleteFile: 'beforeDeleteFile', - afterDeleteFile: 'afterDeleteFile', beforeConnect: 'beforeConnect', beforeSubscribe: 'beforeSubscribe', afterEvent: 'afterEvent', }; -const FileClassName = '@File'; const ConnectClassName = '@Connect'; const baseStore = function () { @@ -50,6 +45,9 @@ export function getClassName(parseClass) { if (parseClass && parseClass.className) { return parseClass.className; } + if (parseClass && parseClass.name) { + return parseClass.name.replace('Parse', '@'); + } return parseClass; } @@ -140,11 +138,6 @@ export function addTrigger(type, className, handler, applicationId, validationHa add(Category.Validators, `${type}.${className}`, validationHandler, applicationId); } -export function addFileTrigger(type, handler, applicationId, validationHandler) { - add(Category.Triggers, `${type}.${FileClassName}`, handler, applicationId); - add(Category.Validators, `${type}.${FileClassName}`, validationHandler, applicationId); -} - export function addConnectTrigger(type, handler, applicationId, validationHandler) { add(Category.Triggers, `${type}.${ConnectClassName}`, handler, applicationId); add(Category.Validators, `${type}.${ConnectClassName}`, validationHandler, applicationId); @@ -207,10 +200,6 @@ export async function runTrigger(trigger, name, request, auth) { return await trigger(request); } -export function getFileTrigger(type, applicationId) { - return getTrigger(FileClassName, type, applicationId); -} - export function triggerExists(className: string, type: string, applicationId: string): boolean { return getTrigger(className, type, applicationId) != undefined; } @@ -961,7 +950,8 @@ export function getRequestFileObject(triggerType, auth, fileObject, config) { } export async function maybeRunFileTrigger(triggerType, fileObject, config, auth) { - const fileTrigger = getFileTrigger(triggerType, config.applicationId); + const FileClassName = getClassName(Parse.File); + const fileTrigger = getTrigger(FileClassName, triggerType, config.applicationId); if (typeof fileTrigger === 'function') { try { const request = getRequestFileObject(triggerType, auth, fileObject, config);