Commit c4c89202 authored by Andres Käver's avatar Andres Käver

universal query params parsing

parent 9e458b20
Pipeline #833 passed with stages
in 1 minute and 17 seconds
......@@ -2,6 +2,8 @@ import { autoinject } from 'aurelia-framework';
import { HttpClient, json } from 'aurelia-fetch-client';
import * as environment from '../../config/environment.json';
import { IFetchResponse } from 'types/IFetchResponse';
import { IQueryParams } from 'types/IQueryParams';
import { stringify } from 'querystring';
@autoinject
export class BaseService<TEntity> {
......@@ -14,10 +16,21 @@ export class BaseService<TEntity> {
constructor(protected apiEndpointUrl: string, protected httpClient: HttpClient) {
}
async getAll(): Promise<IFetchResponse<TEntity[]>> {
async getAll(queryParams?: IQueryParams ): Promise<IFetchResponse<TEntity[]>> {
let url = this.apiEndpointUrl;
if (queryParams !== undefined){
const params = [] as string[];
Object.keys(queryParams).forEach(key => {
params.push(key + '=' + queryParams[key]);
})
if (params.length > 0){
url = url + '?' + params.join('&');
}
}
try {
const response = await this.httpClient
.fetch(this.apiEndpointUrl, {
.fetch(url, {
cache: "no-store",
headers:this.authHeaders
});
......
......@@ -12,7 +12,7 @@ export class GpsSessionService extends BaseService<IGpsSession> {
}
/*
async getAllSessions(minLocations: number, minDistance: number, minDuration: number): Promise<IFetchResponse<IGpsSession[]>> {
let url = this.apiEndpointUrl;
url = url + '?minLocationsCount=' + minLocations.toString() + '&minDuration=' + minDuration.toString() + '&minDistance=' + minDistance.toString();
......@@ -44,5 +44,5 @@ export class GpsSessionService extends BaseService<IGpsSession> {
}
}
}
*/
}
export interface IQueryParams {
// indexer - allows to access unknown properties with foo['bar'].
[param: string]: any;
}
......@@ -118,7 +118,7 @@ export class HomeIndex {
L.imageOverlay(imageUrl, imageBounds).bringToFront();
*/
this.gpsSessionService.getAllSessions(this.minLocations, this.minDistance, this.minDuration).then(
this.gpsSessionService.getAll({minLocationsCount: this.minLocations, minDistance: this.minDistance, minDuration: this.minDuration}).then(
response => {
if (response.data) {
this.gpsSessions = response.data;
......@@ -161,7 +161,7 @@ export class HomeIndex {
// ================================= View ===============================
reloadSessions(): void {
this.gpsSessionService.getAllSessions(this.minLocations, this.minDistance, this.minDuration).then(
this.gpsSessionService.getAll({minLocationsCount: this.minLocations, minDistance: this.minDistance, minDuration: this.minDuration}).then(
response => {
if (response.data) {
this.gpsSessions = response.data;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment