88 AfterDataSourceResponseFunction ,
99 BeforeDataSourceRequestFunction ,
1010 IAdminForthRestAPI ,
11+ IAdminForthSort ,
1112 HttpExtra ,
1213} from "../types/Back.js" ;
1314
@@ -531,7 +532,6 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
531532 method : 'POST' ,
532533 path : '/get_resource_data' ,
533534 handler : async ( { body, adminUser, headers, query, cookies, requestUrl } ) => {
534-
535535 const { resourceId, source } = body ;
536536 if ( [ 'show' , 'list' , 'edit' ] . includes ( source ) === false ) {
537537 return { error : 'Invalid source, should be list or show' } ;
@@ -546,15 +546,15 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
546546 if ( ! resource ) {
547547 return { error : `Resource ${ resourceId } not found` } ;
548548 }
549-
549+
550550 const meta = { requestBody : body , pk : undefined } ;
551551 if ( source === 'edit' || source === 'show' ) {
552552 meta . pk = body . filters . find ( ( f ) => f . field === resource . columns . find ( ( col ) => col . primaryKey ) . name ) ?. value ;
553553 }
554554
555555 const { allowedActions } = await interpretResource (
556- adminUser ,
557- resource ,
556+ adminUser ,
557+ resource ,
558558 meta ,
559559 {
560560 'show' : ActionCheckSource . ShowRequest ,
@@ -581,10 +581,10 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
581581 } [ source ] ;
582582
583583 for ( const hook of listify ( resource . hooks ?. [ hookSource ] ?. beforeDatasourceRequest ) ) {
584- const resp = await hook ( {
585- resource,
586- query : body ,
587- adminUser,
584+ const resp = await hook ( {
585+ resource,
586+ query : body ,
587+ adminUser,
588588 extra : {
589589 body, query, headers, cookies, requestUrl
590590 } ,
@@ -600,6 +600,12 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
600600 }
601601 const { limit, offset, filters, sort } = body ;
602602
603+ // remove virtual fields from sort if still presented after
604+ // beforeDatasourceRequest hook
605+ const sortFiltered = sort . filter ( ( sortItem : IAdminForthSort ) => {
606+ return ! resource . columns . find ( ( col ) => col . name === sortItem . field && col . virtual ) ;
607+ } ) ;
608+
603609 for ( const filter of ( filters || [ ] ) ) {
604610 if ( ! Object . values ( AdminForthFilterOperators ) . includes ( filter . operator ) ) {
605611 throw new Error ( `Operator '${ filter . operator } ' is not allowed` ) ;
@@ -614,18 +620,17 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
614620 throw new Error ( `Value for operator '${ filter . operator } ' should be an array` ) ;
615621 }
616622 }
617-
618-
619623 }
620624
621625 const data = await this . adminforth . connectors [ resource . dataSource ] . getData ( {
622626 resource,
623627 limit,
624628 offset,
625629 filters,
626- sort,
630+ sort : sortFiltered ,
627631 getTotals : true ,
628632 } ) ;
633+
629634 // for foreign keys, add references
630635 await Promise . all (
631636 resource . columns . filter ( ( col ) => col . foreignResource ) . map ( async ( col ) => {
@@ -681,12 +686,11 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
681686
682687 // only after adminforth made all post processing, give user ability to edit it
683688 for ( const hook of listify ( resource . hooks ?. [ hookSource ] ?. afterDatasourceResponse ) ) {
684- const resp = await hook ( {
685- resource,
689+ const resp = await hook ( {
690+ resource,
686691 query : body ,
687- response :
688- data . data ,
689- adminUser,
692+ response : data . data ,
693+ adminUser,
690694 extra : {
691695 body, query, headers, cookies, requestUrl
692696 } ,
@@ -703,7 +707,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
703707 }
704708
705709 return {
706- ...data ,
710+ ...data ,
707711 options : resource ?. options ,
708712 } ;
709713 } ,
0 commit comments