19231: Add smaller page sizes (10 and 20 items) to load faster
[arvados-workbench2.git] / src / services / groups-service / groups-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { snakeCase, camelCase } from "lodash";
6 import { CommonResourceService } from 'services/common-service/common-resource-service';
7 import { ListResults, ListArguments } from 'services/common-service/common-service';
8 import { AxiosInstance, AxiosRequestConfig } from "axios";
9 import { CollectionResource } from "models/collection";
10 import { ProjectResource } from "models/project";
11 import { ProcessResource } from "models/process";
12 import { WorkflowResource } from "models/workflow";
13 import { TrashableResourceService } from "services/common-service/trashable-resource-service";
14 import { ApiActions } from "services/api/api-actions";
15 import { GroupResource } from "models/group";
16 import { Session } from "models/session";
17
18 export interface ContentsArguments {
19     limit?: number;
20     offset?: number;
21     order?: string;
22     filters?: string;
23     recursive?: boolean;
24     includeTrash?: boolean;
25     excludeHomeProject?: boolean;
26 }
27
28 export interface SharedArguments extends ListArguments {
29     include?: string;
30 }
31
32 export type GroupContentsResource =
33     CollectionResource |
34     ProjectResource |
35     ProcessResource |
36     WorkflowResource;
37
38 export class GroupsService<T extends GroupResource = GroupResource> extends TrashableResourceService<T> {
39
40     constructor(serverApi: AxiosInstance, actions: ApiActions) {
41         super(serverApi, "groups", actions);
42     }
43
44     async contents(uuid: string, args: ContentsArguments = {}, session?: Session): Promise<ListResults<GroupContentsResource>> {
45         const { filters, order, ...other } = args;
46         const params = {
47             ...other,
48             filters: filters ? `[${filters}]` : undefined,
49             order: order ? order : undefined
50         };
51         const pathUrl = uuid ? `/${uuid}/contents` : '/contents';
52
53         const cfg: AxiosRequestConfig = { params: CommonResourceService.mapKeys(snakeCase)(params) };
54         if (session) {
55             cfg.baseURL = session.baseUrl;
56             cfg.headers = { 'Authorization': 'Bearer ' + session.token };
57         }
58
59         const response = await CommonResourceService.defaultResponse(
60             this.serverApi.get(this.resourceType + pathUrl, cfg), this.actions, false
61         );
62
63         return { ...TrashableResourceService.mapKeys(camelCase)(response), clusterId: session && session.clusterId };
64     }
65
66     shared(params: SharedArguments = {}): Promise<ListResults<GroupContentsResource>> {
67         return CommonResourceService.defaultResponse(
68             this.serverApi
69                 .get(this.resourceType + '/shared', { params }),
70             this.actions
71         );
72     }
73 }
74
75 export enum GroupContentsResourcePrefix {
76     COLLECTION = "collections",
77     PROJECT = "groups",
78     PROCESS = "container_requests",
79     WORKFLOW = "workflows"
80 }