/
util.ts
185 lines (175 loc) · 4.24 KB
/
util.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Policy } from "./TaskRouterCapability";
const EVENT_URL_BASE = "https://event-bridge.twilio.com/v1/wschannels";
const TASKROUTER_BASE_URL = "https://taskrouter.twilio.com";
const TASKROUTER_VERSION = "v1";
/**
* Build the default Policies for a worker
*
* @param version - TaskRouter version
* @param workspaceSid - workspace sid
* @param workerSid - worker sid
* @returns list of Policies
*/
export function defaultWorkerPolicies(
version: string,
workspaceSid: string,
workerSid: string
): Policy[] {
var activities = new Policy({
url: [
TASKROUTER_BASE_URL,
version,
"Workspaces",
workspaceSid,
"Activities",
].join("/"),
method: "GET",
allow: true,
});
var tasks = new Policy({
url: [
TASKROUTER_BASE_URL,
version,
"Workspaces",
workspaceSid,
"Tasks",
"**",
].join("/"),
method: "GET",
allow: true,
});
var reservations = new Policy({
url: [
TASKROUTER_BASE_URL,
version,
"Workspaces",
workspaceSid,
"Workers",
workerSid,
"Reservations",
"**",
].join("/"),
method: "GET",
allow: true,
});
var workerFetch = new Policy({
url: [
TASKROUTER_BASE_URL,
version,
"Workspaces",
workspaceSid,
"Workers",
workerSid,
].join("/"),
method: "GET",
allow: true,
});
return [activities, tasks, reservations, workerFetch];
}
/**
* Build the default Event Bridge Policies
*
* @param accountSid - account sid
* @param channelId - channel id
* @returns list of Policies
*/
export function defaultEventBridgePolicies(
accountSid: string,
channelId: string
): Policy[] {
var url = [EVENT_URL_BASE, accountSid, channelId].join("/");
return [
new Policy({
url: url,
method: "GET",
allow: true,
}),
new Policy({
url: url,
method: "POST",
allow: true,
}),
];
}
/**
* Generate TaskRouter workspace url
*
* @param workspaceSid - workspace sid or '**' for all workspaces
* @returns generated url
*/
export function workspacesUrl(workspaceSid?: string): string {
return [TASKROUTER_BASE_URL, TASKROUTER_VERSION, "Workspaces", workspaceSid]
.filter((item) => typeof item === "string")
.join("/");
}
/**
* Generate TaskRouter task queue url
*
* @param workspaceSid - workspace sid
* @param taskQueueSid - task queue sid or '**' for all task queues
* @returns generated url
*/
export function taskQueuesUrl(
workspaceSid: string,
taskQueueSid?: string
): string {
return [workspacesUrl(workspaceSid), "TaskQueues", taskQueueSid]
.filter((item) => typeof item === "string")
.join("/");
}
/**
* Generate TaskRouter task url
*
* @param workspaceSid - workspace sid
* @param taskSid - task sid or '**' for all tasks
* @returns generated url
*/
export function tasksUrl(workspaceSid: string, taskSid?: string): string {
return [workspacesUrl(workspaceSid), "Tasks", taskSid]
.filter((item) => typeof item === "string")
.join("/");
}
/**
* Generate TaskRouter activity url
*
* @param workspaceSid - workspace sid
* @param activitySid - activity sid or '**' for all activities
* @returns generated url
*/
export function activitiesUrl(
workspaceSid: string,
activitySid?: string
): string {
return [workspacesUrl(workspaceSid), "Activities", activitySid]
.filter((item) => typeof item === "string")
.join("/");
}
/**
* Generate TaskRouter worker url
*
* @param workspaceSid - workspace sid
* @param workerSid - worker sid or '**' for all workers
* @returns generated url
*/
export function workersUrl(workspaceSid: string, workerSid?: string): string {
return [workspacesUrl(workspaceSid), "Workers", workerSid]
.filter((item) => typeof item === "string")
.join("/");
}
/**
* Generate TaskRouter worker reservation url
*
* @param workspaceSid - workspace sid
* @param workerSid - worker sid
* @param reservationSid - reservation sid or '**' for all reservations
* @returns generated url
*/
export function reservationsUrl(
workspaceSid: string,
workerSid: string,
reservationSid?: string
): string {
return [workersUrl(workspaceSid, workerSid), "Reservations", reservationSid]
.filter((item) => typeof item === "string")
.join("/");
}