@adminforth/crud-approve-plugin 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.ts +14 -4
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -182,14 +182,24 @@ export default class CRUDApprovePlugin extends AdminForthPlugin {
182
182
  updates,
183
183
  oldRecord,
184
184
  });
185
- if (!resp || (!resp.ok && !resp.error)) {
186
- throw new Error(`Hook beforeSave must return object with {ok: true} or { error: 'Error' } `);
185
+ if (!resp || (typeof resp.ok !== 'boolean' && (!resp.error && !resp.newRecordId))) {
186
+ throw new Error(
187
+ `Invalid return value from beforeSave hook. Expected: { ok: boolean, error?: string | null, newRecordId?: any }.\n` +
188
+ `Note: Return { ok: false, error: null, newRecordId } to stop creation and redirect to an existing record.`
189
+ );
190
+ }
191
+ if (resp.ok === false && !resp.error) {
192
+ const { error, ok, newRecordId } = resp;
193
+ return {
194
+ error: error ?? 'Operation aborted by hook',
195
+ newRecordId: newRecordId
196
+ };
187
197
  }
188
-
189
198
  if (resp.error) {
190
199
  return { error: resp.error };
191
200
  }
192
201
  }
202
+
193
203
  return { ok: true, error: null };
194
204
  }
195
205
 
@@ -342,7 +352,7 @@ export default class CRUDApprovePlugin extends AdminForthPlugin {
342
352
  const beforeSaveResp = await this.callBeforeSaveHooks(
343
353
  resource, action as AllowedActionsEnum, diffData['newRecord'],
344
354
  adminUser, diffRecord[this.options.resourceColumns.recordIdColumnName],
345
- undefined, diffData['oldRecord'], this.adminforth, extra
355
+ diffData['newRecord'], diffData['oldRecord'], this.adminforth, extra
346
356
  );
347
357
  if (beforeSaveResp.error) {
348
358
  response.status = 500;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/crud-approve-plugin",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },