/
Actions.ts
97 lines (84 loc) · 2.59 KB
/
Actions.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
import { GenericID } from "../../common/common.types";
import TagoIOModule, { GenericModuleParams } from "../../common/TagoIOModule";
import dateParser from "../Utils/dateParser";
import { ActionCreateInfo, ActionInfo, ActionQuery } from "./actions.types";
class Actions extends TagoIOModule<GenericModuleParams> {
/**
* Retrieves a list with all actions from the account
* @default
* ```json
* queryObj: {
* page: 1,
* fields: ["id", "name"],
* filter: {},
* amount: 20,
* orderBy: "name,asc",
* }
* ```
* @param queryObj Search query params
*/
public async list(queryObj?: ActionQuery): Promise<ActionInfo[]> {
let result = await this.doRequest<ActionInfo[]>({
path: "/action",
method: "GET",
params: {
page: queryObj?.page || 1,
fields: queryObj?.fields || ["id", "name"],
filter: queryObj?.filter || {},
amount: queryObj?.amount || 20,
orderBy: queryObj?.orderBy ? `${queryObj.orderBy[0]},${queryObj.orderBy[1]}` : "name,asc",
},
});
result = result.map((data) => dateParser(data, ["created_at", "updated_at", "last_triggered"]));
return result;
}
/**
* Generates and retrieves a new action from the account
* @param actionObj Object data to create new TagoIO Action
*/
public async create(actionObj: ActionCreateInfo): Promise<{ action: string }> {
const result = await this.doRequest<{ action: string }>({
path: "/action",
method: "POST",
body: actionObj,
});
return result;
}
/**
* Modify any property of the action.
* @param actionID Action ID
* @param actionObj Action Object with data to be replaced
*/
public async edit(actionID: GenericID, actionObj: Partial<ActionCreateInfo>): Promise<string> {
const result = await this.doRequest<string>({
path: `/action/${actionID}`,
method: "PUT",
body: actionObj,
});
return result;
}
/**
* Deletes an action from the account
* @param actionID Action ID
*/
public async delete(actionID: GenericID): Promise<string> {
const result = await this.doRequest<string>({
path: `/action/${actionID}`,
method: "DELETE",
});
return result;
}
/**
* Gets information about the action
* @param actionID Action ID
*/
public async info(actionID: GenericID): Promise<ActionInfo> {
let result = await this.doRequest<ActionInfo>({
path: `/action/${actionID}`,
method: "GET",
});
result = dateParser(result, ["created_at", "updated_at", "last_triggered"]);
return result;
}
}
export default Actions;