export class QueryService { public executeMiddleware(middlewares: Function[], data: any, next: Function) { const composition = middlewares.reduceRight( (next, fn) => (v: any) => { // collect next data const info = data; fn(info, next); }, next ); composition(data); } }