-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspaceServerTaskRepository.ts
61 lines (54 loc) Β· 2.49 KB
/
spaceServerTaskRepository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { chunk, flatMap } from "lodash";
import { Client } from "../../client";
import { ResourceCollection } from "../../resourceCollection";
import { spaceScopedRoutePrefix } from "../../spaceScopedRoutePrefix";
import { ServerTask } from "./serverTask";
import { ServerTaskDetails } from "./serverTaskDetails";
export class SpaceServerTaskRepository {
private baseApiPathTemplate = `${spaceScopedRoutePrefix}/tasks`;
private readonly client: Client;
private readonly spaceName: string;
constructor(client: Client, spaceName: string) {
this.client = client;
this.spaceName = spaceName;
}
async getById(serverTaskId: string): Promise<ServerTask> {
if (!serverTaskId) {
throw new Error("Server Task Id was not provided");
}
const response = await this.client.request<ServerTask>(`${this.baseApiPathTemplate}/${serverTaskId}`, { spaceName: this.spaceName });
return response;
}
async getByIds(serverTaskIds: string[]): Promise<ServerTask[]> {
const batchSize = 300;
const idArrays = chunk(serverTaskIds, batchSize);
const promises: Array<Promise<ResourceCollection<ServerTask>>> = [];
for (const [index, ids] of idArrays.entries()) {
promises.push(
this.client.request<ResourceCollection<ServerTask>>(`${this.baseApiPathTemplate}{?skip,take,ids,partialName}`, {
spaceName: this.spaceName,
ids: ids,
skip: index * batchSize,
take: batchSize,
})
);
}
return Promise.allSettled(promises).then((result) => flatMap(result, (c) => (c.status == "fulfilled" ? c.value.Items : [])));
}
async getDetails(serverTaskId: string): Promise<ServerTaskDetails> {
if (!serverTaskId) {
throw new Error("Server Task Id was not provided");
}
const response = await this.client.request<ServerTaskDetails>(`${this.baseApiPathTemplate}/${serverTaskId}/details`, {
spaceName: this.spaceName,
});
return response;
}
async getRaw(serverTaskId: string): Promise<string> {
if (!serverTaskId) {
throw new Error("Server Task Id was not provided");
}
const response = await this.client.request<string>(`${this.baseApiPathTemplate}/${serverTaskId}/raw`, { spaceName: this.spaceName });
return response;
}
}