@@ -2,29 +2,30 @@ import Entity from '@js-entity-repos/core/dist/types/Entity';
22import { Request , Response } from 'express' ;
33import { OK } from 'http-status-codes' ;
44import FacadeConfig from '../FacadeConfig' ;
5- import catchErrors from '../utils/catchErrors' ;
65import getJsonQueryParam from '../utils/getJsonQueryParam' ;
76import getNumberQueryParam from '../utils/getNumberQueryParam' ;
87
98export default < E extends Entity > ( config : FacadeConfig < E > ) => {
10- return catchErrors ( async ( req : Request , res : Response ) => {
11- const limit = getNumberQueryParam ( req . query , 'limit' , config . defaultPaginationLimit ) ;
12- const result = await config . service . getEntities ( {
13- filter : config . constructFilter ( getJsonQueryParam ( req . query , 'filter' ) ) ,
14- pagination : {
15- cursor : req . query . cursor ,
16- forward : req . query . forward === 'true' ,
17- limit,
18- } ,
19- sort : getJsonQueryParam ( req . query , 'sort' ) ,
9+ return async ( req : Request , res : Response ) => {
10+ await config . handleTransaction ( { req, res } , async ( ) => {
11+ const limit = getNumberQueryParam ( req . query , 'limit' , config . defaultPaginationLimit ) ;
12+ const result = await config . service . getEntities ( {
13+ filter : config . constructFilter ( getJsonQueryParam ( req . query , 'filter' ) ) ,
14+ pagination : {
15+ cursor : req . query . cursor ,
16+ forward : req . query . forward === 'true' ,
17+ limit,
18+ } ,
19+ sort : getJsonQueryParam ( req . query , 'sort' ) ,
20+ } ) ;
21+ res . status ( OK ) ;
22+ if ( result . nextCursor !== undefined ) {
23+ res . setHeader ( 'x-entities-next-cursor' , result . nextCursor ) ;
24+ }
25+ if ( result . previousCursor !== undefined ) {
26+ res . setHeader ( 'x-entities-previous-cursor' , result . previousCursor ) ;
27+ }
28+ res . json ( result . entities ) ;
2029 } ) ;
21- res . status ( OK ) ;
22- if ( result . nextCursor !== undefined ) {
23- res . setHeader ( 'x-entities-next-cursor' , result . nextCursor ) ;
24- }
25- if ( result . previousCursor !== undefined ) {
26- res . setHeader ( 'x-entities-previous-cursor' , result . previousCursor ) ;
27- }
28- res . json ( result . entities ) ;
29- } ) ;
30+ } ;
3031} ;
0 commit comments