interceptors Blogs
Written by Kiprosh, team of passionate and disciplined craftsmen turning your ideas into reality.
Kiprosh is now part of LawLytics
Written by Kiprosh, team of passionate and disciplined craftsmen turning your ideas into reality.
There are often cross cutting concerns in your app, such as logging, security checkpoints and or maybe business validations. This is a very simple approach of implementing method interceptors in pure javascript. The crux: This decorates all the methods for the given object with before and after interceptions. intercept: function(o) { for (let p in o) { if (typeof o[p] === 'function') { let c = o[p]; o[p] = function() { MIFactory.beforeExecution(o, p); let r = c.call(o, arguments); MIFactory.afterExecution(o, p); return r; } } } return o; } method interceptor factory: MIFactory = { interceptors: [], register: function(interceptor) { MIFactory.interceptors.push(interceptor); }, unregister: function(