/
Collection.ts
114 lines (102 loc) · 2.6 KB
/
Collection.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { QlikSaaSClient } from "qlik-rest-api";
import { CollectionItem, ICollectionItem } from "./CollectionItem";
export interface ICollectionUpdate {
name: string;
description?: string;
}
export interface ICollection {
createdAt: string;
creatorId: string;
description: string;
id: string;
itemCount: number;
links: {
items: {
href: string;
};
self: {
href: string;
};
};
meta: {
items: {
data: ICollectionItem[];
links: {
collection: {
href: string;
};
next: {
href: string;
};
prev: {
href: string;
};
self: {
href: string;
};
};
};
};
name: string;
tenantId: string;
type: "public" | "private" | "favorite" | string;
updatedAt: string;
updaterId: string;
}
export class Collection {
#id: string;
#saasClient: QlikSaaSClient;
details: ICollection;
constructor(saasClient: QlikSaaSClient, id: string, details?: ICollection) {
if (!id) throw new Error(`collection.get: "id" parameter is required`);
this.details = details ?? ({} as ICollection);
this.#id = id;
this.#saasClient = saasClient;
}
async init(arg?: { force: boolean }) {
if (
!this.details ||
Object.keys(this.details).length == 0 ||
arg?.force == true
) {
this.details = await this.#saasClient
.Get<ICollection>(`collections/${this.#id}`)
.then((res) => res.data);
}
}
async remove() {
return await this.#saasClient
.Delete(`collections/${this.#id}`)
.then((res) => res.status);
}
async update(arg: ICollectionUpdate) {
if (!arg.name)
throw new Error(`collections.create: "name" parameter is required`);
let updateStatus = 0;
return this.#saasClient
.Put(`collections/${this.#id}`, arg)
.then((res) => {
updateStatus = res.status;
return this.init({ force: true });
})
.then(() => updateStatus);
}
async items() {
return await this.#saasClient
.Get<ICollectionItem[]>(`collections/${this.#id}/items`)
.then((res) => res.data)
.then((data) =>
data.map((t) => new CollectionItem(this.#saasClient, t.id, this.#id, t))
);
}
async addItem(arg: { id: string }) {
if (!arg.id)
throw new Error(`collection.addItem: "id" parameter is required`);
return await this.#saasClient
.Post<ICollectionItem>(`collections/${this.#id}/items`, { id: arg.id })
.then(
(res) =>
new CollectionItem(this.#saasClient, res.data.id, this.#id, res.data)
);
}
}