Skip to content

Instantly share code, notes, and snippets.

@jlaine
Last active May 7, 2018 12:45
Show Gist options
  • Save jlaine/1459871cc50daf98a789b139ca92d80d to your computer and use it in GitHub Desktop.
Save jlaine/1459871cc50daf98a789b139ca92d80d to your computer and use it in GitHub Desktop.
export function queryPaginated<T>(http: HttpClient, baseUrl: string, urlOrFilter?: string | object): Observable<Page<T>> {
let params = new HttpParams();
let url = baseUrl;
if (typeof urlOrFilter === 'string') {
// we were given a page URL, use it
url = urlOrFilter;
} else if (typeof urlOrFilter === 'object') {
// we were given filtering criteria, build the query string
Object.keys(urlOrFilter).sort().forEach(key => {
const value = urlOrFilter[key];
if (value !== null) {
params = params.set(key, value.toString());
}
});
}
return http.get<Page<T>>(url, {
params: params
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment