Skip to content

Commit 16502b0

Browse files
authored
Merge pull request #441 from devforth/allow-actions-set-cookies
Allow actions set cookies
2 parents 57b2699 + 7cb340c commit 16502b0

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
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/configValidator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export default class ConfigValidator implements IConfigValidator {
249249
icon: 'flowbite:trash-bin-outline',
250250
confirm: 'Are you sure you want to delete selected items?',
251251
allowed: async ({ resource, adminUser, allowedActions }) => { return allowedActions.delete },
252-
action: async ({ selectedIds, adminUser }) => {
252+
action: async ({ selectedIds, adminUser, response }) => {
253253
const connector = this.adminforth.connectors[res.dataSource];
254254

255255
// for now if at least one error, stop and return error
@@ -267,6 +267,7 @@ export default class ConfigValidator implements IConfigValidator {
267267
resource: res as AdminForthResource,
268268
record,
269269
adminUser,
270+
response,
270271
adminforth: this.adminforth
271272
});
272273
if (!error && resp.error) {
@@ -290,6 +291,7 @@ export default class ConfigValidator implements IConfigValidator {
290291
record,
291292
adminUser,
292293
recordId: recordId,
294+
response,
293295
adminforth: this.adminforth,
294296
});
295297
}

adminforth/modules/restApi.ts

Lines changed: 12 additions & 12 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
}
@@ -1448,7 +1448,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
14481448
server.endpoint({
14491449
method: 'POST',
14501450
path: '/start_bulk_action',
1451-
handler: async ({ body, adminUser, tr }) => {
1451+
handler: async ({ body, adminUser, tr, response }) => {
14521452
const { resourceId, actionId, recordIds } = body;
14531453
const resource = this.adminforth.config.resources.find((res) => res.resourceId == resourceId);
14541454
if (!resource) {
@@ -1473,13 +1473,13 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
14731473
return { error: await tr(`Action "{actionId}" not allowed`, 'errors', { actionId: action.label }) };
14741474
}
14751475
}
1476-
const response = await action.action({selectedIds: recordIds, adminUser, resource, tr});
1476+
const bulkActionResponse = await action.action({selectedIds: recordIds, adminUser, resource, response, tr});
14771477

14781478
return {
14791479
actionId,
14801480
recordIds,
14811481
resourceId,
1482-
...response
1482+
...bulkActionResponse
14831483
}
14841484
}
14851485
})

adminforth/types/Back.ts

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

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

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

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

374374
auth: IAdminForthAuth;
@@ -545,6 +545,7 @@ export type BeforeDeleteSaveFunction = (params: {
545545
adminUser: AdminUser,
546546
record: any,
547547
adminforth: IAdminForth,
548+
response: IAdminForthHttpResponse,
548549
extra?: HttpExtra,
549550
}) => Promise<{ok: boolean, error?: string}>;
550551

@@ -557,6 +558,7 @@ export type BeforeEditSaveFunction = (params: {
557558
record: any, // legacy, 'updates' should be used instead
558559
oldRecord: any,
559560
adminforth: IAdminForth,
561+
response: IAdminForthHttpResponse,
560562
extra?: HttpExtra,
561563
}) => Promise<{ok: boolean, error?: string | null}>;
562564

@@ -567,6 +569,7 @@ export type BeforeCreateSaveFunction = (params: {
567569
adminUser: AdminUser,
568570
record: any,
569571
adminforth: IAdminForth,
572+
response: IAdminForthHttpResponse,
570573
extra?: HttpExtra,
571574
}) => Promise<{ok: boolean, error?: string | null, newRecordId?: string}>;
572575

@@ -577,6 +580,7 @@ export type AfterCreateSaveFunction = (params: {
577580
record: any,
578581
adminforth: IAdminForth,
579582
recordWithVirtualColumns?: any,
583+
response: IAdminForthHttpResponse,
580584
extra?: HttpExtra,
581585
}) => Promise<{ok: boolean, error?: string}>;
582586

@@ -590,6 +594,7 @@ export type AfterDeleteSaveFunction = (params: {
590594
adminUser: AdminUser,
591595
record: any,
592596
adminforth: IAdminForth,
597+
response: IAdminForthHttpResponse,
593598
extra?: HttpExtra,
594599
}) => Promise<{ok: boolean, error?: string}>;
595600

@@ -602,6 +607,7 @@ export type AfterEditSaveFunction = (params: {
602607
record: any, // legacy, 'updates' should be used instead
603608
oldRecord: any,
604609
adminforth: IAdminForth,
610+
response: IAdminForthHttpResponse,
605611
extra?: HttpExtra,
606612
}) => Promise<{ok: boolean, error?: string}>;
607613

@@ -1543,8 +1549,8 @@ export interface AdminForthBulkAction extends AdminForthBulkActionCommon {
15431549
* Callback which will be called on backend when user clicks on action button.
15441550
* It should return Promise which will be resolved when action is done.
15451551
*/
1546-
action: ({ resource, selectedIds, adminUser, tr }: {
1547-
resource: AdminForthResource, selectedIds: Array<any>, adminUser: AdminUser, tr: (key: string, category?: string, params?: any) => string
1552+
action: ({ resource, selectedIds, adminUser, response, tr }: {
1553+
resource: AdminForthResource, selectedIds: Array<any>, adminUser: AdminUser, response: IAdminForthHttpResponse, tr: (key: string, category?: string, params?: any) => string
15481554
}) => Promise<{ ok: boolean, error?: string, successMessage?: string }>,
15491555

15501556
/**

0 commit comments

Comments
 (0)