Skip to content

Commit

Permalink
optimized image hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamsi committed Jul 18, 2024
1 parent c7d3f8c commit 73b2403
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
57 changes: 34 additions & 23 deletions packages/core/src/fields/types/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ListTypeInfo extends BaseListTypeInfo> =
CommonFieldConfig<ListTypeInfo> & {
Expand Down Expand Up @@ -88,27 +87,24 @@ export function image <ListTypeInfo extends BaseListTypeInfo> (config: ImageFiel
throw Error("isIndexed: 'unique' is not a supported option for field type image")
}

const hooks: InternalFieldHooks<ListTypeInfo> = {}
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)
}
}
}
Expand All @@ -125,7 +121,22 @@ export function image <ListTypeInfo extends BaseListTypeInfo> (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,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/config/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export type FieldHooks<
/**
* Used to **validate** if a create, update or delete operation is OK
*/
validate?:{
validate?: {
create?: ValidateFieldHook<ListTypeInfo, 'create', FieldKey>
update?: ValidateFieldHook<ListTypeInfo, 'update', FieldKey>
delete?: ValidateFieldHook<ListTypeInfo, 'delete', FieldKey>
Expand Down

0 comments on commit 73b2403

Please sign in to comment.