Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add helper function to load APIv3 HAL collection pages

Oliver Günther19b74c9c033e
diff --git a/frontend/src/app/core/apiv3/helpers/get-paginated-results.ts b/frontend/src/app/core/apiv3/helpers/get-paginated-results.ts
new file mode 100644
index 00000000000..6449ebda259
--- /dev/null
+++ b/frontend/src/app/core/apiv3/helpers/get-paginated-results.ts
@@ -0,0 +1,77 @@
+import {
+ map,
+ mergeMap,
+} from 'rxjs/operators';
+import { CollectionResource } from 'core-app/features/hal/resources/collection-resource';
+import {
+ forkJoin,
+ Observable,
+ of,
+} from 'rxjs';
+import { ApiV3PaginationParameters } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
+import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
+import { HalResource } from 'core-app/features/hal/resources/hal-resource';
+
+/**
+ * The API will resolve pageSize=-1 to the maximum value
+ * we can request in one call. This is configurable under adminstration.
+ */
+export const MAGIC_PAGE_NUMBER = -1;
+
+/**
+ * Right now, we still support HAL-class based collections as well as interface-based responses.
+ */
+type ApiV3CollectionType<T> = CollectionResource<T>|IHALCollection<T>;
+
+/**
+ * Extract the elements of either a HAL class or an interface
+ */
+function extractCollectionElements<T>(collection:ApiV3CollectionType<T>):T[] {
+ if (collection instanceof HalResource) {
+ return collection.elements;
+ }
+
+ return collection._embedded.elements;
+}
+
+/**
+ * Get ALL pages of a potentially paginated APIv3 request.
+ *
+ * @param request The requesting callback to request specific pages
+ * @param pageSize The pageSize parameter to request, defaults to -1 (the maximum magic page number)
+ */
+export function getPaginatedResults<T>(
+ request:(params:ApiV3PaginationParameters) => Observable<ApiV3CollectionType<T>>,
+ pageSize = MAGIC_PAGE_NUMBER,
+):Observable<T[]> {
+ return request({ pageSize, offset: 1 })
+ .pipe(
+ mergeMap((collection:ApiV3CollectionType<T>) => {
+ const resolvedSize = collection.pageSize;
+
+ if (collection.total > collection.count) {
+ const remaining = collection.total - collection.count;
+ const pagesRemaining = Math.ceil(remaining / resolvedSize);
+ const calls = new Array(pagesRemaining)
+ .fill(null)
+ .map((_, i) => request({ pageSize: resolvedSize, offset: i + 2 }));
+
+ // Branch out and fetch all remaining pages in parallel.
+ // Afterwards, merge the resulting list
+ return forkJoin(...calls).pipe(
+ map(
+ (results:ApiV3CollectionType<T>[]) => results.reduce(
+ (acc, next) => acc.concat(extractCollectionElements(next)),
+ extractCollectionElements(collection),
+ ),
+ ),
+ );
+ }
+
+ // The current page is the only page, return the results.
+ return of(extractCollectionElements(collection));
+ }),
+ // Elements may incorrectly be undefined here due to the way the representer works
+ map((elements) => elements || []),
+ );
+}
diff --git a/frontend/src/app/core/apiv3/paths/apiv3-list-resource.interface.ts b/frontend/src/app/core/apiv3/paths/apiv3-list-resource.interface.ts
index a47a647eee6..b05f132c6fe 100644
--- a/frontend/src/app/core/apiv3/paths/apiv3-list-resource.interface.ts
+++ b/frontend/src/app/core/apiv3/paths/apiv3-list-resource.interface.ts
@@ -32,13 +32,16 @@ import { ApiV3FilterBuilder, FilterOperator } from 'core-app/shared/helpers/api-
export type ApiV3ListFilter = [string, FilterOperator, boolean|string[]];
-export interface ApiV3ListParameters {
+export interface ApiV3PaginationParameters {
+ pageSize:number;
+ offset:number;
+}
+
+export interface ApiV3ListParameters extends Partial<ApiV3PaginationParameters> {
filters?:ApiV3ListFilter[];
sortBy?:[string, string][];
groupBy?:string;
select?:string[];
- pageSize?:number;
- offset?:number;
}
export interface ApiV3ListResourceInterface<T> {