@lopan/filterchain 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/FilterChain.ts CHANGED
@@ -15,8 +15,8 @@ import type IResource from './IResource.ts';
15
15
  * <-- res -- | | <-- res -- | | <-- res -- |
16
16
  * ```
17
17
  */
18
- export default class FilterChain implements IFilterChain {
19
- private resource: IResource | null = null;
18
+ export default class FilterChain<T> implements IFilterChain<T> {
19
+ private resource: IResource<T> | null = null;
20
20
 
21
21
  /**
22
22
  * The current position of the filter chain
@@ -26,12 +26,12 @@ export default class FilterChain implements IFilterChain {
26
26
  /**
27
27
  * The filter collection
28
28
  */
29
- private filters: IFilter[] = [];
29
+ private filters: IFilter<T>[] = [];
30
30
 
31
31
  /**
32
32
  * @inheritdoc
33
33
  */
34
- public async doFilter(payload: unknown): Promise<unknown> {
34
+ public async doFilter(payload: T): Promise<unknown> {
35
35
  if (this.position >= this.filters.length) {
36
36
  const resource = this.resource;
37
37
  this.clearFilters();
@@ -51,7 +51,7 @@ export default class FilterChain implements IFilterChain {
51
51
  /**
52
52
  * Add a filter to the filter chain
53
53
  */
54
- public addFilter(filter: IFilter): void {
54
+ public addFilter(filter: IFilter<T>): void {
55
55
  this.filters.push(filter);
56
56
  }
57
57
 
@@ -67,7 +67,7 @@ export default class FilterChain implements IFilterChain {
67
67
  /**
68
68
  * Set the resource
69
69
  */
70
- public setResource(resource: IResource | null): void {
70
+ public setResource(resource: IResource<T> | null): void {
71
71
  this.resource = resource;
72
72
  }
73
73
  }
package/IFilter.ts CHANGED
@@ -9,11 +9,11 @@ import type IFilterChain from './IFilterChain.ts';
9
9
  *
10
10
  * The class that implements the `doFilter` method considers to be a filter
11
11
  */
12
- export default interface IFilter {
12
+ export default interface IFilter<T> {
13
13
  /**
14
14
  * The filter method
15
15
  *
16
16
  * This method needs to call `filterChain.doFilter(payload)` manually to execute the next filter
17
17
  */
18
- doFilter(payload: unknown, filterChain: IFilterChain): Promise<unknown>;
18
+ doFilter(payload: T, filterChain: IFilterChain<T>): Promise<unknown>;
19
19
  }
package/IFilterChain.ts CHANGED
@@ -6,9 +6,9 @@
6
6
  /**
7
7
  * Filter chain interface
8
8
  */
9
- export default interface IFilterChain {
9
+ export default interface IFilterChain<T> {
10
10
  /**
11
11
  * Invoked the next filter or the resource
12
12
  */
13
- doFilter(payload: unknown): Promise<unknown>;
13
+ doFilter(payload: T): Promise<unknown>;
14
14
  }
package/IResource.ts CHANGED
@@ -6,9 +6,9 @@
6
6
  /**
7
7
  * Class implementing this interface is a resource class
8
8
  */
9
- export default interface IResource {
9
+ export default interface IResource<T> {
10
10
  /**
11
11
  * Run the resource
12
12
  */
13
- run(payload: unknown): Promise<unknown>;
13
+ run(payload: T): Promise<unknown>;
14
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lopan/filterchain",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "main": "FilterChain.ts",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/test/index.ts CHANGED
@@ -1,28 +1,32 @@
1
1
  import FilterChain from '../FilterChain.ts';
2
2
  import type IFilter from '../IFilter.ts';
3
+ import IFilterChain from '../IFilterChain.ts';
3
4
 
5
+ type FilterData = {
6
+ data: string
7
+ } | null
4
8
 
5
- class ImgFilter1 implements IFilter {
6
- public async doFilter(payload: unknown, filterChain: FilterChain): Promise<unknown> {
7
- console.log('ImgFilter1 before');
8
- const res = await filterChain.doFilter(payload);
9
- console.log('ImgFilter1 after');
10
- return res;
11
- }
12
- }
13
- class ImgFilter2 implements IFilter {
14
- public async doFilter(payload: unknown, filterChain: FilterChain): Promise<unknown> {
15
- console.log('ImgFilter2 before');
16
- const res = await filterChain.doFilter(payload);
17
- console.log('ImgFilter2 after');
18
- return res;
19
- }
9
+ class ImgFilter1 implements IFilter<FilterData> {
10
+ async doFilter(payload: FilterData, filterChain: IFilterChain<FilterData>): Promise<unknown> {
11
+ console.log('ImgFilter1 before');
12
+ const res = await filterChain.doFilter(payload);
13
+ console.log('ImgFilter1 after');
14
+ return res;
15
+ }
20
16
  }
21
17
 
18
+ class ImgFilter2 implements IFilter<FilterData> {
19
+ async doFilter(payload: FilterData, filterChain: IFilterChain<FilterData>): Promise<unknown> {
20
+ console.log('ImgFilter2 before');
21
+ const res = await filterChain.doFilter(payload);
22
+ console.log('ImgFilter2 after');
23
+ return res;
24
+ }
25
+ }
22
26
 
23
27
  class Main {
24
28
  static async run() {
25
- const chain = new FilterChain()
29
+ const chain = new FilterChain<FilterData>()
26
30
  // chain.setResource(null)
27
31
  chain.addFilter(new ImgFilter1())
28
32
  chain.addFilter(new ImgFilter2())
@@ -32,4 +36,4 @@ class Main {
32
36
  }
33
37
  }
34
38
 
35
- Main.run()
39
+ Main.run()