@@ -511,7 +511,7 @@ class AdminForth implements IAdminForth {
511511 async createResourceRecord (
512512 { resource, record, adminUser, extra } :
513513 { resource : AdminForthResource , record : any , adminUser : AdminUser , extra ?: HttpExtra }
514- ) : Promise < { error ?: string , createdRecord ?: any } > {
514+ ) : Promise < { error ?: string , createdRecord ?: any , newRecordId ?: any } > {
515515
516516 const err = this . validateRecordValues ( resource , record , 'create' ) ;
517517 if ( err ) {
@@ -528,8 +528,18 @@ class AdminForth implements IAdminForth {
528528 adminforth : this ,
529529 extra,
530530 } ) ;
531- if ( ! resp || ( ! resp . ok && ! resp . error ) ) {
532- throw new Error ( `Hook beforeSave must return object with {ok: true} or { error: 'Error' } ` ) ;
531+ if ( ! resp || ( typeof resp . ok !== 'boolean' && ( ! resp . error && ! resp . newRecordId ) ) ) {
532+ throw new Error (
533+ `Invalid return value from beforeSave hook. Expected: { ok: boolean, error?: string | null, newRecordId?: any }.\n` +
534+ `Note: Return { ok: false, error: null, newRecordId } to stop creation and redirect to an existing record.`
535+ ) ;
536+ }
537+ if ( resp . ok === false && ! resp . error ) {
538+ const { error, ok, newRecordId } = resp ;
539+ return {
540+ error : error ?? 'Operation aborted by hook' ,
541+ newRecordId : newRecordId
542+ } ;
533543 }
534544 if ( resp . error ) {
535545 return { error : resp . error } ;
@@ -605,8 +615,11 @@ class AdminForth implements IAdminForth {
605615 adminforth : this ,
606616 extra,
607617 } ) ;
608- if ( ! resp || ( ! resp . ok && ! resp . error ) ) {
609- throw new Error ( `Hook beforeSave must return object with {ok: true} or { error: 'Error' } ` ) ;
618+ if ( ! resp || typeof resp . ok !== 'boolean' ) {
619+ throw new Error ( `Hook beforeSave must return { ok: boolean, error?: string | null }` ) ;
620+ }
621+ if ( resp . ok === false && ! resp . error ) {
622+ return { error : resp . error ?? 'Operation aborted by hook' } ;
610623 }
611624 if ( resp . error ) {
612625 return { error : resp . error } ;
0 commit comments