Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions adminforth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ class AdminForth implements IAdminForth {
async createResourceRecord(
{ resource, record, adminUser, extra }:
{ resource: AdminForthResource, record: any, adminUser: AdminUser, extra?: HttpExtra }
): Promise<{ error?: string, createdRecord?: any }> {
): Promise<{ error?: string, createdRecord?: any, newRecordId?: any }> {

const err = this.validateRecordValues(resource, record, 'create');
if (err) {
Expand All @@ -528,8 +528,18 @@ class AdminForth implements IAdminForth {
adminforth: this,
extra,
});
if (!resp || (!resp.ok && !resp.error)) {
throw new Error(`Hook beforeSave must return object with {ok: true} or { error: 'Error' } `);
if (!resp || (typeof resp.ok !== 'boolean' && (!resp.error && !resp.newRecordId))) {
throw new Error(
`Invalid return value from beforeSave hook. Expected: { ok: boolean, error?: string | null, newRecordId?: any }.\n` +
`Note: Return { ok: false, error: null, newRecordId } to stop creation and redirect to an existing record.`
);
}
if (resp.ok === false && !resp.error) {
const { error, ok, newRecordId } = resp;
return {
error: error ?? 'Operation aborted by hook',
newRecordId: newRecordId
};
}
if (resp.error) {
return { error: resp.error };
Expand Down Expand Up @@ -605,8 +615,11 @@ class AdminForth implements IAdminForth {
adminforth: this,
extra,
});
if (!resp || (!resp.ok && !resp.error)) {
throw new Error(`Hook beforeSave must return object with {ok: true} or { error: 'Error' } `);
if (!resp || typeof resp.ok !== 'boolean') {
throw new Error(`Hook beforeSave must return { ok: boolean, error?: string | null }`);
}
if (resp.ok === false && !resp.error) {
return { error: resp.error ?? 'Operation aborted by hook' };
}
if (resp.error) {
return { error: resp.error };
Expand Down
2 changes: 1 addition & 1 deletion adminforth/modules/restApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {

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

Expand Down
2 changes: 1 addition & 1 deletion adminforth/spa/src/views/CreateView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async function saveRecord() {
record: record.value,
},
});
if (response?.error) {
if (response?.error == true) {
showErrorTost(response.error);
}
saving.value = false;
Expand Down
8 changes: 4 additions & 4 deletions adminforth/types/Back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export interface IAdminForth {

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

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

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



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

export type AfterCreateSaveFunction = (params: {
resource: AdminForthResource,
Expand Down