diff --git a/packages/core/src/fields/types/image/index.ts b/packages/core/src/fields/types/image/index.ts index 7877902c6d9..d3abe6c0635 100644 --- a/packages/core/src/fields/types/image/index.ts +++ b/packages/core/src/fields/types/image/index.ts @@ -9,7 +9,6 @@ import { } from '../../../types' import { graphql } from '../../..' import { SUPPORTED_IMAGE_EXTENSIONS } from './utils' -import { mergeFieldHooks, type InternalFieldHooks } from '../../resolve-hooks' export type ImageFieldConfig = CommonFieldConfig & { @@ -88,27 +87,24 @@ export function image (config: ImageFiel throw Error("isIndexed: 'unique' is not a supported option for field type image") } - const hooks: InternalFieldHooks = {} - if (!storage.preserve) { - hooks.beforeOperation = async (args) => { - if (args.operation === 'update' || args.operation === 'delete') { - const idKey = `${fieldKey}_id` - const id = args.item[idKey] - const extensionKey = `${fieldKey}_extension` - const extension = args.item[extensionKey] - - // This will occur on an update where an image already existed but has been - // changed, or on a delete, where there is no longer an item - if ( - (args.operation === 'delete' || - typeof args.resolvedData[fieldKey].id === 'string' || - args.resolvedData[fieldKey].id === null) && - typeof id === 'string' && - typeof extension === 'string' && - isValidImageExtension(extension) - ) { - await args.context.images(config.storage).deleteAtSource(id, extension) - } + async function beforeOperationFieldHook (args: any) { + if (args.operation === 'update' || args.operation === 'delete') { + const idKey = `${fieldKey}_id` + const id = args.item[idKey] + const extensionKey = `${fieldKey}_extension` + const extension = args.item[extensionKey] + + // This will occur on an update where an image already existed but has been + // changed, or on a delete, where there is no longer an item + if ( + (args.operation === 'delete' || + typeof args.resolvedData[fieldKey].id === 'string' || + args.resolvedData[fieldKey].id === null) && + typeof id === 'string' && + typeof extension === 'string' && + isValidImageExtension(extension) + ) { + await args.context.images(config.storage).deleteAtSource(id, extension) } } } @@ -125,7 +121,22 @@ export function image (config: ImageFiel }, })({ ...config, - hooks: mergeFieldHooks(hooks, config.hooks), + hooks: storage.preserve + ? config.hooks + : { + ...config.hooks, + beforeOperation: { + ...config.hooks?.beforeOperation, + async update (args) { + await config.hooks?.beforeOperation?.update?.(args) + await beforeOperationFieldHook(args) + }, + async delete (args) { + await config.hooks?.beforeOperation?.delete?.(args) + await beforeOperationFieldHook(args) + }, + }, + }, input: { create: { arg: inputArg, diff --git a/packages/core/src/types/config/hooks.ts b/packages/core/src/types/config/hooks.ts index a7fd3a89f9c..dd108fab1eb 100644 --- a/packages/core/src/types/config/hooks.ts +++ b/packages/core/src/types/config/hooks.ts @@ -121,7 +121,7 @@ export type FieldHooks< /** * Used to **validate** if a create, update or delete operation is OK */ - validate?:{ + validate?: { create?: ValidateFieldHook update?: ValidateFieldHook delete?: ValidateFieldHook