/
BrandFile.ts
80 lines (72 loc) · 1.65 KB
/
BrandFile.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
import { QlikSaaSClient, QlikFormData } from "qlik-rest-api";
export interface IBrandFile {
id: "logo" | "styles" | "favIcon";
eTag: string;
path: string;
contentType: string;
}
export const contentTypeMime = {
logo: "image/png",
styles: "text/css",
favIcon: "image/x-icon",
};
export class BrandFile {
#id: string;
private brandId: string;
#saasClient: QlikSaaSClient;
details: IBrandFile;
constructor(
saasClient: QlikSaaSClient,
brandId: string,
id: string,
details: IBrandFile
) {
this.#id = id;
this.brandId = brandId;
this.#saasClient = saasClient;
this.details = details;
}
/**
* Download the current brand file
*/
async download() {
const fileExtensions = {
logo: "png",
styles: "css",
favIcon: "ico",
};
return this.#saasClient
.Get<Buffer>(`brands/${this.brandId}/files/${this.#id}`)
.then((res) => ({
file: res.data,
id: `${this.#id}`,
name: `${this.#id}.${fileExtensions[this.#id]}`,
}));
}
/**
* Remove the current brand file from the brand
*/
async remove() {
return this.#saasClient
.Delete(`brands/${this.brandId}/files/${this.#id}`)
.then((res) => res.status);
}
/**
* Update the content of the current brand file
*/
async update(arg: { file: string }) {
const fd = new QlikFormData();
fd.append({
field: "file",
data: arg.file,
contentType: contentTypeMime[this.#id],
});
return this.#saasClient
.Put(
`brands/${this.brandId}/files/${this.#id}`,
fd.getData,
fd.getHeaders
)
.then((res) => res.status);
}
}