HTTP Query module based on the @ngrx/store package.
yarn add @ngsm/http-query
or npm i @ngsm/http-query --save
Library requires @ngrx/store
module.
Import QueryModule in your app.module.ts:
import { QueryModule } from '@ngsm/query';
...,
@NgModule({
....,
imports: [
....,
QueryModule
]
}
...
import { QueryFacade } from '@ngsm/query';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-cars',
templateUrl: './cars.component.html',
})
export class CarsComponent implements OnInit {
// Available methods:
loader$ = this.queryFacade.isInProgress$('cars');
query$ = this.queryFacade.query$<YOUR_TYPE>('getCars');
response$ = this.queryFacade.response$<YOUR_TYPE>('getCars');
body$ = this.queryFacade.body$<YOUR_TYPE>('getCars');
error$ = this.queryFacade.error$('getCars');
status$ = this.queryFacade.status$('getCars');
constructor(
private httpClient: HttpClient,
private queryFacade: QueryFacade,
) {}
ngOnInit() {
this.getCars().subscribe();
}
getCars(): Observable<YOUR_TYPE> {
return this.http.get('API_URL', {
headers: { queryName: 'getCars', queryGroups: ['cars'] }
});
}
}
Sebastian Musiał |