@zyratalk1/zyra-twilio-wrapper 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,322 @@
1
+ import type { AxiosInstance } from "axios";
2
+
3
+ export interface Config {
4
+ serverUrl: string,
5
+ identity: string,
6
+ waitUrl ?: string,
7
+ sdkToken: string,
8
+ accessToken?: string,
9
+ requestTimeoutMs?: number,
10
+ retry?: {
11
+ maxRetries?: number;
12
+ baseDelayMs?: number;
13
+ maxDelayMs?: number;
14
+ jitterRatio?: number;
15
+ circuitBreakerThreshold?: number;
16
+ circuitBreakerCooldownMs?: number;
17
+ }
18
+ }
19
+
20
+ export interface TwilioConferenceParticipant {
21
+ accountSid: string;
22
+ callSid: string;
23
+ callSidToCoach: string | null;
24
+ coaching: boolean;
25
+ conferenceSid: string;
26
+ dateCreated: string; // ISO 8601 date string
27
+ dateUpdated: string; // ISO 8601 date string
28
+ endConferenceOnExit: boolean;
29
+ hold: boolean;
30
+ label: string | null;
31
+ muted: boolean;
32
+ queueTime: string | null; // Twilio may return seconds as a string or null
33
+ startConferenceOnEnter: boolean;
34
+ status: 'queued' | 'ringing' | 'in-progress' | 'connected' | 'completed' | string;
35
+ uri: string;
36
+ }
37
+
38
+ export interface TwilioConference {
39
+ conferenceSid: string; // Your own wrapper ID
40
+ conferences: TwilioConferenceDetails;
41
+ }
42
+
43
+ export interface TwilioConferenceDetails {
44
+ accountSid: string;
45
+ apiVersion: string;
46
+ callSidEndingConference: string | null;
47
+ dateCreated: string; // ISO 8601 date
48
+ dateUpdated: string; // ISO 8601 date
49
+ friendlyName: string;
50
+ reasonConferenceEnded: string | null;
51
+ region: string;
52
+ sid: string; // Conference SID (same as conferenceSid)
53
+ status: 'init' | 'in-progress' | 'completed' | 'terminated' | string;
54
+ subresourceUris: {
55
+ participants: string;
56
+ recordings: string;
57
+ [key: string]: string;
58
+ };
59
+ uri: string;
60
+ }
61
+
62
+ export interface TokenRequestPayload {
63
+ identity: string;
64
+ ttl: number;
65
+ incomingAllow: boolean;
66
+ }
67
+
68
+ export interface TokenResponse {
69
+ result: {
70
+ data: {
71
+ token: string;
72
+ };
73
+ };
74
+ }
75
+
76
+ export interface CallResult {
77
+ message: string;
78
+ status: boolean;
79
+ }
80
+
81
+ export interface HoldCallPayload {
82
+ callSid: string;
83
+ conferenceName: string;
84
+ waitUrl: string;
85
+ }
86
+
87
+ export interface ResumeCallPayload {
88
+ callSid: string;
89
+ conferenceName: string;
90
+ }
91
+
92
+
93
+ export interface MergeCallPayload {
94
+ callSid: string;
95
+ conferenceName: string;
96
+ }
97
+
98
+ export interface HoldPayload {
99
+ callSid: string;
100
+ conferenceName: string;
101
+ waitUrl: string;
102
+ }
103
+
104
+ export interface AddParticipantPayload {
105
+ conferenceName: string;
106
+ newParticipantNo: string;
107
+ }
108
+
109
+ export interface AddParticipantPayloadIncoming {
110
+ callSid: string;
111
+ newParticipantNo: string;
112
+ }
113
+ export interface HoldAndAddResult extends CallResult {
114
+ holdResponse?: any;
115
+ addParticipantResponse?: any;
116
+ }
117
+
118
+ export interface GetConferencePayload {
119
+ conferenceName: string;
120
+ }
121
+
122
+ export interface GetConferenceResult extends CallResult {
123
+ conferenceDetail?: TwilioConference;
124
+ }
125
+
126
+ export interface GetParticipantsPayload {
127
+ conferenceSid: string;
128
+ }
129
+
130
+ export interface GetParticipantsResult extends CallResult {
131
+ participants?: TwilioConferenceParticipant[];
132
+ }
133
+
134
+ export interface RemoveParticipantPayload {
135
+ conferenceSid: string;
136
+ callSid: string;
137
+ }
138
+
139
+ export interface RemoveParticipantResult extends CallResult {
140
+ // Optionally, you can include server response data
141
+ data?: any;
142
+ }
143
+
144
+ export interface RegisterWebhookPayload {
145
+ webhookUrl: string;
146
+ }
147
+
148
+ export interface DeregisterWebhookPayload {
149
+ // No body required; API deregisters based on sdkToken/tenant context.
150
+ }
151
+
152
+ export interface WebhookConfigResult {
153
+ configured: boolean;
154
+ webhookUrl: string | null;
155
+ }
156
+
157
+ export type CallResponse<T = void> =
158
+ | { success: true; message: string; data: T }
159
+ | { success: false; error: Error };
160
+
161
+
162
+
163
+ /**
164
+ * Dependencies (same pattern as webhook)
165
+ */
166
+ export interface WelcomeConfigDeps {
167
+ axiosInstance: AxiosInstance;
168
+ serverUrl: string;
169
+ sdkToken: string;
170
+ ensureAuthenticated: () => void;
171
+ }
172
+
173
+ /**
174
+ * Input types
175
+ */
176
+ export type WelcomeType = "audio" | "tts";
177
+
178
+ export interface UpsertWelcomeConfigInput {
179
+ welcomeType: WelcomeType;
180
+ welcomeMessage: string;
181
+ }
182
+
183
+ /**
184
+ * Output type (matches backend output)
185
+ */
186
+ export interface WelcomeConfigResult {
187
+ success: boolean;
188
+ message: string;
189
+ welcomeType: WelcomeType;
190
+ routingConfigId: number;
191
+ }
192
+
193
+ /**
194
+ * Routing Config
195
+ */
196
+
197
+ export type RoutingStrategy = "RING_ALL" | "RING_SEQUENCE";
198
+
199
+ export type FallbackActionType = "TRANSFER" | "AI" | "VOICEMAIL";
200
+
201
+ export interface FallbackAction {
202
+ type: FallbackActionType;
203
+ target: {
204
+ phoneNumber: string;
205
+ } | null;
206
+ }
207
+ export interface RoutingMember {
208
+ id: number;
209
+ priorityOrder: number;
210
+ isEnabled: boolean;
211
+ userType: "PHONE" | "DESKTOP";
212
+ identifier: string | null;
213
+ email: string | null;
214
+ phoneNumber: string | null;
215
+ ringDuration: number | null;
216
+ }
217
+
218
+
219
+ export interface GetRoutingConfigResult {
220
+ strategy: RoutingStrategy;
221
+ defaultRingTimeout: number | null;
222
+ maxAttempts: number | null;
223
+ fallbackAction: FallbackAction;
224
+ members: RoutingMember[];
225
+ }
226
+
227
+ export interface SetRoutingMember {
228
+ priorityOrder: number;
229
+ enabled: boolean;
230
+ userType: "PHONE" | "DESKTOP";
231
+ identifier: string;
232
+ ringDuration: number;
233
+ contact: {
234
+ email: string | null;
235
+ phoneNumber: string | null;
236
+ };
237
+ }
238
+
239
+ export interface SetRoutingConfigInput {
240
+ strategy: {
241
+ type: RoutingStrategy;
242
+ defaultRingTimeout?: number;
243
+ maxAttempts?: number;
244
+ };
245
+ routingMembers: SetRoutingMember[];
246
+ fallbackAction: FallbackAction;
247
+ }
248
+
249
+
250
+ export interface SetRoutingConfigResult {
251
+ success: boolean;
252
+ message: string;
253
+ strategy: RoutingStrategy;
254
+ memberCount: number;
255
+ }
256
+
257
+ export interface RoutingConfigDeps {
258
+ axiosInstance: any;
259
+ serverUrl: string;
260
+ sdkToken: string;
261
+ ensureAuthenticated: () => void;
262
+ }
263
+
264
+
265
+ // Parse Rules
266
+ export interface ParseRuleDeps {
267
+ axiosInstance: any;
268
+ serverUrl: string;
269
+ sdkToken: string;
270
+ ensureAuthenticated: () => void;
271
+ }
272
+
273
+ export interface CreateParseRuleEntry {
274
+ dataType: "rule" | "parseData";
275
+ fieldName: string;
276
+ parseOptions?: unknown;
277
+ parseOptionCode?: unknown;
278
+ fieldRules?: unknown;
279
+ }
280
+
281
+ export interface ParseRuleRow {
282
+ id: number;
283
+ userId: number;
284
+ dataType: string | string[];
285
+ fieldName: string[];
286
+ fieldRules: string | null;
287
+ fieldRulesParsed: Record<string, any> | null;
288
+ parseOptions?: string | null;
289
+ parseOptionCode?: string | null;
290
+ createdAt: string;
291
+ updatedAt: string;
292
+ }
293
+ export interface ListParseRulesInput {
294
+ page?: number;
295
+ limit?: number;
296
+ dataType?: "rule" | "parseData";
297
+ activeOnly?: boolean;
298
+ }
299
+
300
+ export interface ListParseRulesResult {
301
+ data: ParseRuleRow[];
302
+ total: number;
303
+ page: number;
304
+ limit: number;
305
+ }
306
+
307
+ export interface CreateParseRuleInput {
308
+ parseDataEntries: CreateParseRuleEntry[];
309
+ }
310
+
311
+ export interface CreateParseRuleResult {
312
+ id: number;
313
+ userId: number;
314
+ dataType: string;
315
+ fieldName: string[];
316
+ fieldRules: string | null;
317
+ fieldRulesParsed?: Record<string, unknown> | null;
318
+ parseOptions?: string | null;
319
+ parseOptionCode?: string | null;
320
+ createdAt: string;
321
+ updatedAt: string;
322
+ }
@@ -0,0 +1,37 @@
1
+ import axios, { AxiosError } from "axios";
2
+
3
+ /**
4
+ * Handles errors from Axios requests in a consistent way.
5
+ *
6
+ * @param error - The error thrown by Axios or any other source.
7
+ * @param onErrorCallback - Optional callback to handle the error externally.
8
+ * @param contextMessage - Optional custom message to provide context for the error.
9
+ */
10
+ export function handleAxiosError(
11
+ error: unknown,
12
+ onErrorCallback?: (error: Error) => void,
13
+ contextMessage = "Request failed"
14
+ ): void {
15
+ let finalError: Error;
16
+
17
+ if (axios.isAxiosError(error)) {
18
+ const axiosError = error as AxiosError;
19
+ const status = axiosError.response?.status ?? "Unknown";
20
+ const statusText = axiosError.response?.statusText ?? "No status text";
21
+ const responseData = axiosError.response?.data
22
+ ? JSON.stringify(axiosError.response.data)
23
+ : "No response data";
24
+
25
+ finalError = new Error(
26
+ `[AxiosError] ${contextMessage}: ${status} ${statusText} | Response: ${responseData}`
27
+ );
28
+ } else if (error instanceof Error) {
29
+ finalError = new Error(`[Error] ${contextMessage}: ${error.message}`);
30
+ } else {
31
+ finalError = new Error(`[Unknown Error] ${contextMessage}: ${String(error)}`);
32
+ }
33
+
34
+ console.error(finalError); // Optional: log for debugging
35
+ onErrorCallback?.(finalError);
36
+ }
37
+
@@ -0,0 +1,20 @@
1
+ // src/utils/helper.ts
2
+
3
+ import type { CallResponse } from "../types/interface"; // adjust path if needed
4
+
5
+ /**
6
+ * Helper to create success result
7
+ */
8
+ export function createSuccessResult<T>(message: string, data: T): CallResponse<T> {
9
+ return { success : true, message : message, data };
10
+ }
11
+
12
+ /**
13
+ * Helper to create error result
14
+ */
15
+ export function createErrorResult<T = void>(error: Error | string): CallResponse<T> {
16
+ return {
17
+ success: false,
18
+ error: error instanceof Error ? error : new Error(error),
19
+ };
20
+ }