@Filter

export declare let Filter: (priority: number, route?: string) => Function;

@Filter decorator you use to declare controller filter each controller filter you have to explicitly bind to @Controller otherwise filter will not be invoked on desired controller.

You can bound more filters to same controller / route and that's why thy have priority number which is ordering filter execution.

Thy are ordered numerically descending.

Below we can see one example of full page caching implementation to avoid database peak and template compiling.

import {IFilter, Filter, Request, Inject, Logger} from "typeix";
import {InMemoryCache} from "../components/in-memory-cache";

/**
 * @constructor
 * @function
 * @name Cache
 *
 * @description
 * Cache filter example
 */
@Filter(100)
export class Cache implements IFilter {


  @Inject(Logger) logger: Logger;

  @Inject(Request) request: Request;

  @Inject(InMemoryCache) cacheProvider: InMemoryCache;


  before(data: string): string|Buffer|Promise<string|Buffer> {
    if (this.cacheProvider.has(this.request.getRoute())) {
      this.request.stopChain();
      return this.cacheProvider.get(this.request.getRoute());
    }
    return "Before cache controller filter <-" + data;
  }

  after(data: string): string|Buffer|Promise<string|Buffer> {
    this.cacheProvider.set(this.request.getRoute(), data, 10); // 10 seconds cache
    this.logger.warn("TRIGGER CACHE", data);
    return data;
  }

}

results matching ""

    No results matching ""