Skip to content

Commit 9f3ab53

Browse files
committed
fix: allow all hooks return { ok: false }
1 parent 18e8650 commit 9f3ab53

File tree

3 files changed

+36
-48
lines changed

3 files changed

+36
-48
lines changed

adminforth/index.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import SQLiteConnector from './dataConnectors/sqlite.js';
66
import CodeInjector from './modules/codeInjector.js';
77
import ExpressServer from './servers/express.js';
88
// import FastifyServer from './servers/fastify.js';
9-
import { ADMINFORTH_VERSION, listify, suggestIfTypo, RateLimiter, RAMLock, getClientIp, isProbablyUUIDColumn, convertPeriodToSeconds } from './modules/utils.js';
9+
import { ADMINFORTH_VERSION, listify, suggestIfTypo, RateLimiter, RAMLock, getClientIp, isProbablyUUIDColumn, convertPeriodToSeconds, hookResponseError } from './modules/utils.js';
1010
import {
1111
type AdminForthConfig,
1212
type IAdminForth,
@@ -643,12 +643,9 @@ class AdminForth implements IAdminForth {
643643
extra,
644644
});
645645

646-
if (!resp || (!resp.ok && !resp.error)) {
647-
throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
648-
}
649-
650-
if (resp.error) {
651-
return { error: resp.error };
646+
const hookRespError = hookResponseError(resp);
647+
if (hookRespError) {
648+
return hookRespError;
652649
}
653650
}
654651

@@ -695,14 +692,9 @@ class AdminForth implements IAdminForth {
695692
response,
696693
extra,
697694
});
698-
if (!resp || typeof resp.ok !== 'boolean') {
699-
throw new Error(`Hook beforeSave must return { ok: boolean, error?: string | null }`);
700-
}
701-
if (resp.ok === false && !resp.error) {
702-
return { error: resp.error ?? 'Operation aborted by hook' };
703-
}
704-
if (resp.error) {
705-
return { error: resp.error };
695+
const hookRespError = hookResponseError(resp);
696+
if (hookRespError) {
697+
return hookRespError;
706698
}
707699
}
708700

@@ -740,11 +732,9 @@ class AdminForth implements IAdminForth {
740732
response,
741733
extra,
742734
});
743-
if (!resp || (!resp.ok && !resp.error)) {
744-
throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
745-
}
746-
if (resp.error) {
747-
return { error: resp.error };
735+
const hookRespError = hookResponseError(resp);
736+
if (hookRespError) {
737+
return hookRespError;
748738
}
749739
}
750740

@@ -771,12 +761,9 @@ class AdminForth implements IAdminForth {
771761
response,
772762
extra,
773763
});
774-
if (!resp || (!resp.ok && !resp.error)) {
775-
throw new Error(`Hook beforeSave must return object with {ok: true} or { error: 'Error' } `);
776-
}
777-
778-
if (resp.error) {
779-
return { error: resp.error };
764+
const hookRespError = hookResponseError(resp);
765+
if (hookRespError) {
766+
return hookRespError;
780767
}
781768
}
782769

@@ -794,12 +781,9 @@ class AdminForth implements IAdminForth {
794781
response,
795782
extra,
796783
});
797-
if (!resp || (!resp.ok && !resp.error)) {
798-
throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
799-
}
800-
801-
if (resp.error) {
802-
return { error: resp.error };
784+
const hookRespError = hookResponseError(resp);
785+
if (hookRespError) {
786+
return hookRespError;
803787
}
804788
}
805789

adminforth/modules/restApi.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {cascadeChildrenDelete} from './utils.js'
1919

2020
import { afLogger } from "./logger.js";
2121

22-
import { ADMINFORTH_VERSION, listify, md5hash, getLoginPromptHTML } from './utils.js';
22+
import { ADMINFORTH_VERSION, listify, md5hash, getLoginPromptHTML, hookResponseError } from './utils.js';
2323

2424
import AdminForthAuth from "../auth.js";
2525
import { ActionCheckSource, AdminForthConfigMenuItem, AdminForthDataTypes, AdminForthFilterOperators, AdminForthResourceColumnInputCommon, AdminForthResourceCommon, AdminForthResourcePages,
@@ -748,14 +748,9 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
748748
},
749749
adminforth: this.adminforth,
750750
});
751-
if (!resp || typeof resp.ok !== 'boolean') {
752-
throw new Error(`Hook beforeSave must return { ok: boolean, error?: string | null }`);
753-
}
754-
if (resp.ok === false && !resp.error) {
755-
return { error: resp.error ?? 'Operation aborted by hook' };
756-
}
757-
if (resp.error) {
758-
return { error: resp.error };
751+
const hookRespError = hookResponseError(resp);
752+
if (hookRespError) {
753+
return hookRespError;
759754
}
760755
}
761756
const { limit, offset, filters, sort } = body;
@@ -954,13 +949,9 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
954949
},
955950
adminforth: this.adminforth,
956951
});
957-
958-
if (!resp || (!resp.ok && !resp.error)) {
959-
throw new Error(`Hook must return object with {ok: true} or { error: 'Error' } `);
960-
}
961-
962-
if (resp.error) {
963-
return { error: resp.error };
952+
const hookRespError = hookResponseError(resp);
953+
if (hookRespError) {
954+
return hookRespError;
964955
}
965956
}
966957

adminforth/modules/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,17 @@ export async function cascadeChildrenDelete(resource: AdminForthResource, primar
521521
}
522522

523523
return { error: null };
524+
}
525+
526+
export function hookResponseError(hookResponse: {ok: boolean, error?: string | null}) {
527+
if (!hookResponse || typeof hookResponse.ok !== 'boolean') {
528+
throw new Error(`Hook beforeSave must return { ok: boolean, error?: string | null }`);
529+
}
530+
if (hookResponse.ok === false && !hookResponse.error) {
531+
return { error: hookResponse.error ?? 'Operation aborted by hook' };
532+
}
533+
if (hookResponse.error) {
534+
return { error: hookResponse.error };
535+
}
536+
return null;
524537
}

0 commit comments

Comments
 (0)