1.1.3 • Published 12 months ago

@kovalenko/base-filter v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

BaseFilter

Install

npm i @kovalenko/base-filter

Setup

add import to polyfills.ts

import 'reflect-metadata';

Usage

Class BaseFilter

Protected static properties

string

class MyFilter extends BaseFilter {
  protected static readonly key = 't';
}

number[]

Constructor

number

Observable

Public properties

Observable

Observable

number

number

number

Public methods

Protected methods

Decorators

class MyFilter extends BaseFilter {
  protected static readonly key = 't';
}

https://path/to?param=true becomes {param: true}

https://path/to?param=test becomes {param: ['test']}

https://path/to?date=2020-10-10 becomes {param: Moment}

Class QsHttpParams

Class extending @angular/common/http/HttpParams to pass filter into angular http client.

this.http.get('api/v1/test', {params: new QsHttpParams(filter.toJSON())});

Example

Filter

import {BaseFilter} from '@kovalenko/base-filter';

export class TestFilter extends BaseFilter {
  @FilterProperty()
  title?: string;

  @FilterProperty()
  @TransformBoolean()
  hasParticipants?: boolean;

  @Type(() => Number)
  @TransformArray()
  @FilterProperty()
  campaign?: number[];

  @TransformMoment()
  @TransformArray()
  @FilterProperty((v: moment.Moment[]) => v?.map(ts => ts.format('YYYY-MM')))
  submittedAtFrom?: moment.Moment[];
}

Service

import type {TestFilter} from './test-filter';
import {QsHttpParams} from '@kovalenko/base-filter';

@Injectable()
export class PersonService {

  constructor(
    private http: HttpClient,
  ) { }

  list(flt: TestFilter): Observable<any> {
    return this.http.get('api/v1/test', {
      params: new QsHttpParams(flt.toJSON()),
    });
  }
}

Component

import {ActivatedRoute} from '@angular/router';
import {PersonService} from './person.service';
import {TestFilter} from './test-filter';

@Component({
  selector: 'some-component',
  template: 'hi',
})
export class ApplicationListComponent implements OnInit, OnDestroy {
  loading = false;

  filter = new TestFilter(100, this.route.queryParams);

  constructor(
    private route: ActivatedRoute,
    private personService: PersonService,
  ) { }

  ngOnInit(): void {
    this.filter.updated$.pipe(
      skip(1),
    ).subscribe(f => {
      this.router.navigate([], {
        queryParams: f.toQueryParams(),
        relativeTo: this.route,
        queryParamsHandling: 'merge',
      });
    });

    this.filter.query$.pipe(
      tap(() => this.loading = true),
      switchMap(f => this.personService.list(f)),
    ).subscribe();
  }
}

License

MIT

1.1.3

12 months ago

1.1.2

12 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.0

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago