/
Brands.ts
128 lines (112 loc) · 3.17 KB
/
Brands.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { QlikFormData, QlikSaaSClient } from "qlik-rest-api";
import { Brand, IBrand } from "./Brand";
import { contentTypeMime } from "./BrandFile";
import { parseFilter } from "../util/filter";
export interface IBrandCreate {
/**
* The path and name of a JPG or PNG file that will be adjusted to fit in a 'box'
* measuring 109px in width and 62 px in height while maintaining aspect ratio.
* Maximum size of 300 KB, but smaller is recommended.
*/
logo?: string;
/**
* Name of the brand.
*/
name: string;
/**
* @NOT_OPERATIONAL
*
*
* The path and name of a JSON file to define brand style settings.
* Maximum size is 100 KB. This property is not currently operational.
*
*/
styles?: string;
/**
* The path and name of a properly formatted ICO file. Maximum size is 100 KB.
*/
favIcon?: string;
/**
* Description of the brand.
*/
description?: string;
}
export class Brands {
#saasClient: QlikSaaSClient;
constructor(saasClient: QlikSaaSClient) {
this.#saasClient = saasClient;
}
/**
* Get specific brand (by brand ID)
*/
async get(arg: { id: string }) {
if (!arg.id) throw new Error(`brands.get: "id" parameter is required`);
const brand: Brand = new Brand(this.#saasClient, arg.id);
await brand.init();
return brand;
}
/**
* Get all existing brands
*/
async getAll() {
return await this.#saasClient
.Get<{ data: IBrand[] }>(`brands?limit=50`)
.then((res) => res.data.data ?? res.data ?? [])
.then((data) => data.map((t) => new Brand(this.#saasClient, t.id, t)));
}
async getFilter(arg: { filter: string }) {
if (!arg.filter)
throw new Error(`brands.getFilter: "filter" parameter is required`);
return await this.getAll().then((entities) => {
const anonFunction = Function(
"entities",
`return entities.filter(f => ${parseFilter(arg.filter, "f.details")})`
);
return anonFunction(entities) as Brand[];
});
}
async removeFilter(arg: { filter: string }) {
if (!arg.filter)
throw new Error(`brands.removeFilter: "filter" parameter is required`);
return await this.getFilter(arg).then((entities) =>
Promise.all(
entities.map((entity) =>
entity.remove().then((s) => ({ id: entity.details.id, status: s }))
)
)
);
}
/**
* Create new brand
*/
async create(arg: IBrandCreate) {
if (!arg.name)
throw new Error(`brands.create: "name" parameter is required`);
const fd = new QlikFormData();
fd.append({
data: arg.name,
field: "name",
});
if (arg.favIcon)
fd.append({
data: arg.favIcon,
field: "favIcon",
contentType: contentTypeMime.favIcon,
});
if (arg.logo)
fd.append({
data: arg.logo,
field: "logo",
contentType: contentTypeMime.logo,
});
if (arg.styles)
fd.append({
data: arg.name,
field: "styles",
// contentType: contentTypeMime.styles,
});
return await this.#saasClient
.Post<IBrand>(`brands`, fd.getData, fd.getHeaders)
.then((res) => new Brand(this.#saasClient, res.data.id, res.data));
}
}