Skip to content

Commit 550aa1a

Browse files
committed
Merge branch 'next' of https://github.com/devforth/adminforth into next
2 parents 2a7c484 + 7d49dfa commit 550aa1a

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

adminforth/index.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 };

adminforth/modules/restApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
11191119

11201120
const response = await this.adminforth.createResourceRecord({ resource, record, adminUser, extra: { body, query, headers, cookies, requestUrl } });
11211121
if (response.error) {
1122-
return { error: response.error, ok: false };
1122+
return { error: response.error, ok: false, newRecordId: response.newRecordId };
11231123
}
11241124
const connector = this.adminforth.connectors[resource.dataSource];
11251125

adminforth/spa/src/views/CreateView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ async function saveRecord() {
154154
record: record.value,
155155
},
156156
});
157-
if (response?.error) {
157+
if (response?.error == true) {
158158
showErrorTost(response.error);
159159
}
160160
saving.value = false;

adminforth/types/Back.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export interface IAdminForth {
336336

337337
createResourceRecord(
338338
params: { resource: AdminForthResource, record: any, adminUser: AdminUser, extra?: HttpExtra }
339-
): Promise<{ error?: string, createdRecord?: any }>;
339+
): Promise<{ error?: string, createdRecord?: any, newRecordId?: any }>;
340340

341341
updateResourceRecord(
342342
params: { resource: AdminForthResource, recordId: any, record: any, oldRecord: any, adminUser: AdminUser, extra?: HttpExtra }
@@ -474,7 +474,7 @@ export type BeforeDataSourceRequestFunction = (params: {
474474
requestUrl: string,
475475
},
476476
adminforth: IAdminForth,
477-
}) => Promise<{ok: boolean, error?: string}>;
477+
}) => Promise<{ok: boolean, error?: string, newRecordId?: string}>;
478478

479479
/**
480480
* Modify response to change how data is returned after fetching from database.
@@ -525,7 +525,7 @@ export type BeforeEditSaveFunction = (params: {
525525
oldRecord: any,
526526
adminforth: IAdminForth,
527527
extra?: HttpExtra,
528-
}) => Promise<{ok: boolean, error?: string}>;
528+
}) => Promise<{ok: boolean, error?: string | null}>;
529529

530530

531531

@@ -535,7 +535,7 @@ export type BeforeCreateSaveFunction = (params: {
535535
record: any,
536536
adminforth: IAdminForth,
537537
extra?: HttpExtra,
538-
}) => Promise<{ok: boolean, error?: string}>;
538+
}) => Promise<{ok: boolean, error?: string | null, newRecordId?: string}>;
539539

540540
export type AfterCreateSaveFunction = (params: {
541541
resource: AdminForthResource,

0 commit comments

Comments
 (0)