Skip to content

Commit e1c11bd

Browse files
committed
feat: add possobolity to set cookies from the before/after save hooks
1 parent c48f422 commit e1c11bd

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

adminforth/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
HttpExtra,
2121
BeforeCreateSaveFunction,
2222
AdminForthInputConfig,
23+
IAdminForthHttpResponse,
2324
} from './types/Back.js';
2425

2526
import {
@@ -538,8 +539,8 @@ class AdminForth implements IAdminForth {
538539
}
539540

540541
async createResourceRecord(
541-
{ resource, record, adminUser, extra }:
542-
{ resource: AdminForthResource, record: any, adminUser: AdminUser, extra?: HttpExtra }
542+
{ resource, record, adminUser, extra, response }:
543+
{ resource: AdminForthResource, record: any, adminUser: AdminUser, extra?: HttpExtra, response: IAdminForthHttpResponse }
543544
): Promise<{ error?: string, createdRecord?: any, newRecordId?: any }> {
544545

545546
const err = this.validateRecordValues(resource, record, 'create');
@@ -557,6 +558,7 @@ class AdminForth implements IAdminForth {
557558
record,
558559
adminUser,
559560
adminforth: this,
561+
response,
560562
extra,
561563
});
562564
if (!resp || (typeof resp.ok !== 'boolean' && (!resp.error && !resp.newRecordId))) {
@@ -602,6 +604,7 @@ class AdminForth implements IAdminForth {
602604
adminUser,
603605
adminforth: this,
604606
recordWithVirtualColumns,
607+
response,
605608
extra,
606609
});
607610

@@ -621,9 +624,9 @@ class AdminForth implements IAdminForth {
621624
* record is partial record with only changed fields
622625
*/
623626
async updateResourceRecord(
624-
{ resource, recordId, record, oldRecord, adminUser, extra, updates }:
625-
| { resource: AdminForthResource, recordId: any, record: any, oldRecord: any, adminUser: AdminUser, extra?: HttpExtra, updates?: never }
626-
| { resource: AdminForthResource, recordId: any, record?: never, oldRecord: any, adminUser: AdminUser, extra?: HttpExtra, updates: any }
627+
{ resource, recordId, record, oldRecord, adminUser, response, extra, updates }:
628+
| { resource: AdminForthResource, recordId: any, record: any, oldRecord: any, adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: HttpExtra, updates?: never }
629+
| { resource: AdminForthResource, recordId: any, record?: never, oldRecord: any, adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: HttpExtra, updates: any }
627630
): Promise<{ error?: string }> {
628631
const dataToUse = updates || record;
629632
const err = this.validateRecordValues(resource, dataToUse, 'edit');
@@ -651,6 +654,7 @@ class AdminForth implements IAdminForth {
651654
oldRecord,
652655
adminUser,
653656
adminforth: this,
657+
response,
654658
extra,
655659
});
656660
if (!resp || typeof resp.ok !== 'boolean') {
@@ -695,6 +699,7 @@ class AdminForth implements IAdminForth {
695699
oldRecord,
696700
recordId,
697701
adminforth: this,
702+
response,
698703
extra,
699704
});
700705
if (!resp || (!resp.ok && !resp.error)) {
@@ -709,8 +714,8 @@ class AdminForth implements IAdminForth {
709714
}
710715

711716
async deleteResourceRecord(
712-
{ resource, recordId, adminUser, record, extra }:
713-
{ resource: AdminForthResource, recordId: any, adminUser: AdminUser, record: any, extra?: HttpExtra }
717+
{ resource, recordId, adminUser, record, response, extra }:
718+
{ resource: AdminForthResource, recordId: any, adminUser: AdminUser, record: any, response: IAdminForthHttpResponse, extra?: HttpExtra }
714719
): Promise<{ error?: string }> {
715720
// execute hook if needed
716721
for (const hook of listify(resource.hooks?.delete?.beforeSave)) {
@@ -720,6 +725,7 @@ class AdminForth implements IAdminForth {
720725
adminUser,
721726
recordId,
722727
adminforth: this,
728+
response,
723729
extra,
724730
});
725731
if (!resp || (!resp.ok && !resp.error)) {
@@ -742,6 +748,7 @@ class AdminForth implements IAdminForth {
742748
adminUser,
743749
recordId,
744750
adminforth: this,
751+
response,
745752
extra,
746753
});
747754
if (!resp || (!resp.ok && !resp.error)) {

adminforth/modules/restApi.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
11621162
server.endpoint({
11631163
method: 'POST',
11641164
path: '/create_record',
1165-
handler: async ({ body, adminUser, query, headers, cookies, requestUrl }) => {
1165+
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
11661166
const resource = this.adminforth.config.resources.find((res) => res.resourceId == body['resourceId']);
11671167
if (!resource) {
11681168
return { error: `Resource '${body['resourceId']}' not found` };
@@ -1276,22 +1276,22 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
12761276
}
12771277
}
12781278

1279-
const response = await this.adminforth.createResourceRecord({ resource, record, adminUser, extra: { body, query, headers, cookies, requestUrl } });
1280-
if (response.error) {
1281-
return { error: response.error, ok: false, newRecordId: response.newRecordId };
1279+
const createRecordResponse = await this.adminforth.createResourceRecord({ resource, record, adminUser, response, extra: { body, query, headers, cookies, requestUrl } });
1280+
if (createRecordResponse.error) {
1281+
return { error: createRecordResponse.error, ok: false, newRecordId: createRecordResponse.newRecordId };
12821282
}
12831283
const connector = this.adminforth.connectors[resource.dataSource];
12841284

12851285
return {
1286-
newRecordId: response.createdRecord[connector.getPrimaryKey(resource)],
1286+
newRecordId: createRecordResponse.createdRecord[connector.getPrimaryKey(resource)],
12871287
ok: true
12881288
}
12891289
}
12901290
});
12911291
server.endpoint({
12921292
method: 'POST',
12931293
path: '/update_record',
1294-
handler: async ({ body, adminUser, query, headers, cookies, requestUrl }) => {
1294+
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
12951295
const resource = this.adminforth.config.resources.find((res) => res.resourceId == body['resourceId']);
12961296
if (!resource) {
12971297
return { error: `Resource '${body['resourceId']}' not found` };
@@ -1396,7 +1396,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
13961396
}
13971397
}
13981398

1399-
const { error } = await this.adminforth.updateResourceRecord({ resource, record, adminUser, oldRecord, recordId, extra: { body, query, headers, cookies, requestUrl} });
1399+
const { error } = await this.adminforth.updateResourceRecord({ resource, record, adminUser, oldRecord, recordId, response, extra: { body, query, headers, cookies, requestUrl} });
14001400
if (error) {
14011401
return { error };
14021402
}
@@ -1409,7 +1409,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
14091409
server.endpoint({
14101410
method: 'POST',
14111411
path: '/delete_record',
1412-
handler: async ({ body, adminUser, query, headers, cookies, requestUrl }) => {
1412+
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
14131413
const resource = this.adminforth.config.resources.find((res) => res.resourceId == body['resourceId']);
14141414
const record = await this.adminforth.connectors[resource.dataSource].getRecordByPrimaryKey(resource, body['primaryKey']);
14151415
if (!resource) {
@@ -1435,7 +1435,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
14351435
return { error };
14361436
}
14371437

1438-
const { error: deleteError } = await this.adminforth.deleteResourceRecord({ resource, record, adminUser, recordId: body['primaryKey'], extra: { body, query, headers, cookies, requestUrl } });
1438+
const { error: deleteError } = await this.adminforth.deleteResourceRecord({ resource, record, adminUser, recordId: body['primaryKey'], response, extra: { body, query, headers, cookies, requestUrl } });
14391439
if (deleteError) {
14401440
return { error: deleteError };
14411441
}

adminforth/types/Back.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,16 @@ export interface IAdminForth {
360360
tr(msg: string, category: string, lang: string, params: any, pluralizationNumber?: number): Promise<string>;
361361

362362
createResourceRecord(
363-
params: { resource: AdminForthResource, record: any, adminUser: AdminUser, extra?: HttpExtra }
363+
params: { resource: AdminForthResource, record: any, response: IAdminForthHttpResponse, adminUser: AdminUser, extra?: HttpExtra }
364364
): Promise<{ error?: string, createdRecord?: any, newRecordId?: any }>;
365365

366366
updateResourceRecord(
367-
params: { resource: AdminForthResource, recordId: any, record: any, oldRecord: any, adminUser: AdminUser, extra?: HttpExtra, updates?: never }
368-
| { resource: AdminForthResource, recordId: any, record?: never, oldRecord: any, adminUser: AdminUser, extra?: HttpExtra, updates: any }
367+
params: { resource: AdminForthResource, recordId: any, record: any, oldRecord: any, adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: HttpExtra, updates?: never }
368+
| { resource: AdminForthResource, recordId: any, record?: never, oldRecord: any, adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: HttpExtra, updates: any }
369369
): Promise<{ error?: string }>;
370370

371371
deleteResourceRecord(
372-
params: { resource: AdminForthResource, recordId: string, adminUser: AdminUser, record: any, extra?: HttpExtra }
372+
params: { resource: AdminForthResource, recordId: string, adminUser: AdminUser, record: any, response: IAdminForthHttpResponse, extra?: HttpExtra }
373373
): Promise<{ error?: string }>;
374374

375375
auth: IAdminForthAuth;
@@ -539,6 +539,7 @@ export type BeforeDeleteSaveFunction = (params: {
539539
adminUser: AdminUser,
540540
record: any,
541541
adminforth: IAdminForth,
542+
response: IAdminForthHttpResponse,
542543
extra?: HttpExtra,
543544
}) => Promise<{ok: boolean, error?: string}>;
544545

@@ -551,6 +552,7 @@ export type BeforeEditSaveFunction = (params: {
551552
record: any, // legacy, 'updates' should be used instead
552553
oldRecord: any,
553554
adminforth: IAdminForth,
555+
response: IAdminForthHttpResponse,
554556
extra?: HttpExtra,
555557
}) => Promise<{ok: boolean, error?: string | null}>;
556558

@@ -561,6 +563,7 @@ export type BeforeCreateSaveFunction = (params: {
561563
adminUser: AdminUser,
562564
record: any,
563565
adminforth: IAdminForth,
566+
response: IAdminForthHttpResponse,
564567
extra?: HttpExtra,
565568
}) => Promise<{ok: boolean, error?: string | null, newRecordId?: string}>;
566569

@@ -571,6 +574,7 @@ export type AfterCreateSaveFunction = (params: {
571574
record: any,
572575
adminforth: IAdminForth,
573576
recordWithVirtualColumns?: any,
577+
response: IAdminForthHttpResponse,
574578
extra?: HttpExtra,
575579
}) => Promise<{ok: boolean, error?: string}>;
576580

@@ -584,6 +588,7 @@ export type AfterDeleteSaveFunction = (params: {
584588
adminUser: AdminUser,
585589
record: any,
586590
adminforth: IAdminForth,
591+
response: IAdminForthHttpResponse,
587592
extra?: HttpExtra,
588593
}) => Promise<{ok: boolean, error?: string}>;
589594

@@ -596,6 +601,7 @@ export type AfterEditSaveFunction = (params: {
596601
record: any, // legacy, 'updates' should be used instead
597602
oldRecord: any,
598603
adminforth: IAdminForth,
604+
response: IAdminForthHttpResponse,
599605
extra?: HttpExtra,
600606
}) => Promise<{ok: boolean, error?: string}>;
601607

0 commit comments

Comments
 (0)