Skip to content

Commit bec462b

Browse files
authored
Merge pull request #465 from devforth/feature/AdminForth/1203/https-github.-newrecordid-is-a
fix: rename newRecordId param in createResourceRecord to redirectToRe…
2 parents 9cc1419 + ad9ad90 commit bec462b

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

adminforth/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,17 +568,21 @@ class AdminForth implements IAdminForth {
568568
response,
569569
extra,
570570
});
571-
if (!resp || (typeof resp.ok !== 'boolean' && (!resp.error && !resp.newRecordId))) {
571+
if (resp.newRecordId) {
572+
afLogger.warn(`Deprecation warning: beforeSave hook returned 'newRecordId'. Since AdminForth v1.2.9 use 'redirectToRecordId' instead. 'newRecordId' will be removed in v2.0.0`);
573+
}
574+
if (!resp || (typeof resp.ok !== 'boolean' && (!resp.error && !resp.newRecordId && !resp.redirectToRecordId))) {
572575
throw new Error(
573-
`Invalid return value from beforeSave hook. Expected: { ok: boolean, error?: string | null, newRecordId?: any }.\n` +
574-
`Note: Return { ok: false, error: null, newRecordId } to stop creation and redirect to an existing record.`
576+
`Invalid return value from beforeSave hook. Expected: { ok: boolean, error?: string | null, newRecordId?: any, redirectToRecordId?: any }.\n` +
577+
`Note: Return { ok: false, error: null, redirectToRecordId } (preferred) or { ok: false, error: null, newRecordId } (deprecated) to stop creation and redirect to an existing record.`
575578
);
576579
}
577580
if (resp.ok === false && !resp.error) {
578-
const { error, ok, newRecordId } = resp;
581+
const { error, ok, newRecordId, redirectToRecordId } = resp;
579582
return {
580583
error: error ?? 'Operation aborted by hook',
581-
newRecordId: newRecordId
584+
newRecordId: redirectToRecordId ? redirectToRecordId : newRecordId,
585+
redirectToRecordId: redirectToRecordId
582586
};
583587
}
584588
if (resp.error) {

adminforth/modules/restApi.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,11 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
12801280
extra: { body, query, headers, cookies, requestUrl, response }
12811281
});
12821282
if (createRecordResponse.error) {
1283-
return { error: createRecordResponse.error, ok: false, newRecordId: createRecordResponse.newRecordId };
1283+
return {
1284+
error: createRecordResponse.error,
1285+
ok: false,
1286+
newRecordId: createRecordResponse.redirectToRecordId ? createRecordResponse.redirectToRecordId :createRecordResponse.newRecordId,
1287+
redirectToRecordId: createRecordResponse.redirectToRecordId };
12841288
}
12851289
const connector = this.adminforth.connectors[resource.dataSource];
12861290

adminforth/types/Back.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,15 @@ export type BeforeDataSourceRequestFunction = (params: {
505505
requestUrl: string,
506506
},
507507
adminforth: IAdminForth,
508-
}) => Promise<{ok: boolean, error?: string, newRecordId?: string}>;
508+
}) => Promise<{
509+
ok: boolean,
510+
error?: string | null,
511+
/**
512+
* @deprecated Since 1.2.9. Will be removed in 2.0.0. Use redirectToRecordId instead.
513+
*/
514+
newRecordId?: string,
515+
redirectToRecordId?: string
516+
}>;
509517

510518
/**
511519
* Modify response to change how data is returned after fetching from database.
@@ -549,8 +557,15 @@ export type CreateResourceRecordResult = {
549557
/**
550558
* Optional id of an existing record to redirect to
551559
* (used when a beforeSave hook aborts creation and supplies newRecordId, allows to implement programmatic creation via API).
560+
* @deprecated Since 1.2.9. Will be removed in 2.0.0. Use redirectToRecordId instead.
552561
*/
553562
newRecordId?: any;
563+
564+
/**
565+
* Optional id of an existing record to redirect to
566+
* (used when a beforeSave hook aborts creation and supplies redirectToRecordId, allows to implement programmatic creation via API).
567+
*/
568+
redirectToRecordId?: any;
554569
};
555570

556571
/**
@@ -838,7 +853,15 @@ export type BeforeCreateSaveFunction = (params: {
838853
response: IAdminForthHttpResponse,
839854

840855
extra?: HttpExtra,
841-
}) => Promise<{ok: boolean, error?: string | null, newRecordId?: string}>;
856+
}) => Promise<{
857+
ok: boolean,
858+
error?: string | null,
859+
/**
860+
* @deprecated Since 1.2.9. Will be removed in 2.0.0. Use redirectToRecordId instead.
861+
*/
862+
newRecordId?: string,
863+
redirectToRecordId?: string
864+
}>;
842865

843866
export type AfterCreateSaveFunction = (params: {
844867
/**

0 commit comments

Comments
 (0)