@telnyx/react-voice-commons-sdk 0.1.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.
- package/TelnyxVoiceCommons.podspec +32 -0
- package/ios/CallKitBridge.m +44 -0
- package/ios/CallKitBridge.swift +879 -0
- package/ios/README.md +211 -0
- package/ios/VoicePnBridge.m +31 -0
- package/ios/VoicePnBridge.swift +87 -0
- package/lib/callkit/callkit-coordinator.d.ts +126 -0
- package/lib/callkit/callkit-coordinator.js +728 -0
- package/lib/callkit/callkit.d.ts +49 -0
- package/lib/callkit/callkit.js +262 -0
- package/lib/callkit/index.d.ts +4 -0
- package/lib/callkit/index.js +15 -0
- package/lib/callkit/use-callkit-coordinator.d.ts +21 -0
- package/lib/callkit/use-callkit-coordinator.js +53 -0
- package/lib/callkit/use-callkit.d.ts +28 -0
- package/lib/callkit/use-callkit.js +279 -0
- package/lib/context/TelnyxVoiceContext.d.ts +18 -0
- package/lib/context/TelnyxVoiceContext.js +18 -0
- package/lib/hooks/use-callkit-coordinator.d.ts +13 -0
- package/lib/hooks/use-callkit-coordinator.js +48 -0
- package/lib/hooks/useAppReadyNotifier.d.ts +9 -0
- package/lib/hooks/useAppReadyNotifier.js +25 -0
- package/lib/hooks/useAppStateHandler.d.ts +16 -0
- package/lib/hooks/useAppStateHandler.js +105 -0
- package/lib/index.d.ts +24 -0
- package/lib/index.js +66 -0
- package/lib/internal/CallKitHandler.d.ts +17 -0
- package/lib/internal/CallKitHandler.js +110 -0
- package/lib/internal/callkit-manager.d.ts +69 -0
- package/lib/internal/callkit-manager.js +326 -0
- package/lib/internal/calls/call-state-controller.d.ts +92 -0
- package/lib/internal/calls/call-state-controller.js +294 -0
- package/lib/internal/session/session-manager.d.ts +87 -0
- package/lib/internal/session/session-manager.js +385 -0
- package/lib/internal/user-defaults-helpers.d.ts +10 -0
- package/lib/internal/user-defaults-helpers.js +69 -0
- package/lib/internal/voice-pn-bridge.d.ts +14 -0
- package/lib/internal/voice-pn-bridge.js +5 -0
- package/lib/models/call-state.d.ts +61 -0
- package/lib/models/call-state.js +87 -0
- package/lib/models/call.d.ts +145 -0
- package/lib/models/call.js +372 -0
- package/lib/models/config.d.ts +64 -0
- package/lib/models/config.js +92 -0
- package/lib/models/connection-state.d.ts +34 -0
- package/lib/models/connection-state.js +50 -0
- package/lib/telnyx-voice-app.d.ts +48 -0
- package/lib/telnyx-voice-app.js +486 -0
- package/lib/telnyx-voip-client.d.ts +184 -0
- package/lib/telnyx-voip-client.js +386 -0
- package/package.json +104 -0
- package/src/callkit/callkit-coordinator.ts +846 -0
- package/src/callkit/callkit.ts +322 -0
- package/src/callkit/index.ts +4 -0
- package/src/callkit/use-callkit.ts +345 -0
- package/src/context/TelnyxVoiceContext.tsx +33 -0
- package/src/hooks/use-callkit-coordinator.ts +60 -0
- package/src/hooks/useAppReadyNotifier.ts +25 -0
- package/src/hooks/useAppStateHandler.ts +134 -0
- package/src/index.ts +56 -0
- package/src/internal/CallKitHandler.tsx +149 -0
- package/src/internal/callkit-manager.ts +335 -0
- package/src/internal/calls/call-state-controller.ts +384 -0
- package/src/internal/session/session-manager.ts +467 -0
- package/src/internal/user-defaults-helpers.ts +58 -0
- package/src/internal/voice-pn-bridge.ts +18 -0
- package/src/models/call-state.ts +98 -0
- package/src/models/call.ts +388 -0
- package/src/models/config.ts +125 -0
- package/src/models/connection-state.ts +50 -0
- package/src/telnyx-voice-app.tsx +690 -0
- package/src/telnyx-voip-client.ts +475 -0
- package/src/types/telnyx-sdk.d.ts +79 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { TelnyxConnectionState } from './models/connection-state';
|
|
3
|
+
import { Call } from './models/call';
|
|
4
|
+
import { CredentialConfig, TokenConfig } from './models/config';
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for TelnyxVoipClient
|
|
7
|
+
*/
|
|
8
|
+
export interface TelnyxVoipClientOptions {
|
|
9
|
+
/** Enable automatic app state management (background/foreground behavior) - default: true */
|
|
10
|
+
enableAppStateManagement?: boolean;
|
|
11
|
+
/** Enable debug logging */
|
|
12
|
+
debug?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The main public interface for the react-voice-commons module.
|
|
16
|
+
*
|
|
17
|
+
* This class serves as the Façade for the entire module, providing a simplified
|
|
18
|
+
* API that completely hides the underlying complexity. It is the sole entry point
|
|
19
|
+
* for developers using the react-voice-commons package.
|
|
20
|
+
*
|
|
21
|
+
* The TelnyxVoipClient is designed to be state-management agnostic, exposing
|
|
22
|
+
* all observable state via RxJS streams. This allows developers to integrate it
|
|
23
|
+
* into their chosen state management solution naturally.
|
|
24
|
+
*/
|
|
25
|
+
export declare class TelnyxVoipClient {
|
|
26
|
+
private readonly _sessionManager;
|
|
27
|
+
private readonly _callStateController;
|
|
28
|
+
private readonly _options;
|
|
29
|
+
private _disposed;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new TelnyxVoipClient instance.
|
|
32
|
+
*
|
|
33
|
+
* @param options Configuration options for the client
|
|
34
|
+
*/
|
|
35
|
+
constructor(options?: TelnyxVoipClientOptions);
|
|
36
|
+
/**
|
|
37
|
+
* Stream of connection state changes.
|
|
38
|
+
*
|
|
39
|
+
* Emits the current status of the connection to the Telnyx backend.
|
|
40
|
+
* Values include connecting, connected, disconnected, and error states.
|
|
41
|
+
* Listen to this to show connection indicators in your UI.
|
|
42
|
+
*/
|
|
43
|
+
get connectionState$(): Observable<TelnyxConnectionState>;
|
|
44
|
+
/**
|
|
45
|
+
* Stream of all current calls.
|
|
46
|
+
*
|
|
47
|
+
* Emits a list of all current Call objects. Use this for applications
|
|
48
|
+
* that need to support multiple simultaneous calls (e.g., call waiting,
|
|
49
|
+
* conference calls).
|
|
50
|
+
*/
|
|
51
|
+
get calls$(): Observable<Call[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Stream of the currently active call.
|
|
54
|
+
*
|
|
55
|
+
* A convenience stream that emits the currently active Call object.
|
|
56
|
+
* It emits null when no call is in progress. Ideal for applications
|
|
57
|
+
* that only handle a single call at a time.
|
|
58
|
+
*/
|
|
59
|
+
get activeCall$(): Observable<Call | null>;
|
|
60
|
+
/**
|
|
61
|
+
* Current connection state (synchronous access).
|
|
62
|
+
*/
|
|
63
|
+
get currentConnectionState(): TelnyxConnectionState;
|
|
64
|
+
/**
|
|
65
|
+
* Current list of calls (synchronous access).
|
|
66
|
+
*/
|
|
67
|
+
get currentCalls(): Call[];
|
|
68
|
+
/**
|
|
69
|
+
* Current active call (synchronous access).
|
|
70
|
+
*/
|
|
71
|
+
get currentActiveCall(): Call | null;
|
|
72
|
+
/**
|
|
73
|
+
* Current session ID (UUID) for this connection.
|
|
74
|
+
*/
|
|
75
|
+
get sessionId(): string;
|
|
76
|
+
/**
|
|
77
|
+
* Configuration options for this client instance.
|
|
78
|
+
*/
|
|
79
|
+
get options(): Required<TelnyxVoipClientOptions>;
|
|
80
|
+
/**
|
|
81
|
+
* Connects to the Telnyx platform using credential authentication.
|
|
82
|
+
*
|
|
83
|
+
* @param config The credential configuration containing SIP username and password
|
|
84
|
+
* @returns A Promise that completes when the connection attempt is initiated
|
|
85
|
+
*
|
|
86
|
+
* Listen to connectionState$ to monitor the actual connection status.
|
|
87
|
+
*/
|
|
88
|
+
login(config: CredentialConfig): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Connects to the Telnyx platform using token authentication.
|
|
91
|
+
*
|
|
92
|
+
* @param config The token configuration containing the authentication token
|
|
93
|
+
* @returns A Promise that completes when the connection attempt is initiated
|
|
94
|
+
*
|
|
95
|
+
* Listen to connectionState$ to monitor the actual connection status.
|
|
96
|
+
*/
|
|
97
|
+
loginWithToken(config: TokenConfig): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Disconnects from the Telnyx platform.
|
|
100
|
+
*
|
|
101
|
+
* This method terminates the connection, ends any active calls, and
|
|
102
|
+
* cleans up all related resources.
|
|
103
|
+
*/
|
|
104
|
+
logout(): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Attempts to reconnect using previously stored configuration.
|
|
107
|
+
*
|
|
108
|
+
* This method is used for auto-reconnection scenarios where the app
|
|
109
|
+
* comes back to the foreground and needs to restore the connection.
|
|
110
|
+
*
|
|
111
|
+
* @returns Promise<boolean> - true if reconnection was successful, false otherwise
|
|
112
|
+
*/
|
|
113
|
+
loginFromStoredConfig(): Promise<boolean>;
|
|
114
|
+
/**
|
|
115
|
+
* Initiates a new outgoing call.
|
|
116
|
+
*
|
|
117
|
+
* @param destination The destination number or SIP URI to call
|
|
118
|
+
* @param debug Optional flag to enable call quality metrics for this call
|
|
119
|
+
* @returns A Promise that completes with the Call object once the invitation has been sent
|
|
120
|
+
*
|
|
121
|
+
* The call's state can be monitored through the returned Call object's streams.
|
|
122
|
+
*/
|
|
123
|
+
newCall(destination: string, debug?: boolean): Promise<Call>;
|
|
124
|
+
/**
|
|
125
|
+
* Handle push notification payload.
|
|
126
|
+
*
|
|
127
|
+
* This is the unified entry point for all push notifications. It intelligently
|
|
128
|
+
* determines whether to show a new incoming call UI or to process an already
|
|
129
|
+
* actioned (accepted/declined) call upon app launch.
|
|
130
|
+
*
|
|
131
|
+
* @param payload The push notification payload
|
|
132
|
+
*/
|
|
133
|
+
handlePushNotification(payload: Record<string, any>): Promise<void>;
|
|
134
|
+
/**
|
|
135
|
+
* Disables push notifications for the current session.
|
|
136
|
+
*
|
|
137
|
+
* This method sends a request to the Telnyx backend to disable push
|
|
138
|
+
* notifications for the current registered device/session.
|
|
139
|
+
*/
|
|
140
|
+
disablePushNotifications(): void;
|
|
141
|
+
/**
|
|
142
|
+
* Set a call to connecting state (used for push notification calls when answered via CallKit)
|
|
143
|
+
* @param callId The ID of the call to set to connecting state
|
|
144
|
+
* @internal
|
|
145
|
+
*/
|
|
146
|
+
setCallConnecting(callId: string): void;
|
|
147
|
+
/**
|
|
148
|
+
* Find a call by its underlying Telnyx call object
|
|
149
|
+
* @param telnyxCall The Telnyx call object to find
|
|
150
|
+
* @internal
|
|
151
|
+
*/
|
|
152
|
+
findCallByTelnyxCall(telnyxCall: any): Call | null;
|
|
153
|
+
/**
|
|
154
|
+
* Queue an answer action for when the call invite arrives (for CallKit integration)
|
|
155
|
+
* This should be called when the user answers from CallKit before the socket connection is established
|
|
156
|
+
* @param customHeaders Optional custom headers to include with the answer
|
|
157
|
+
*/
|
|
158
|
+
queueAnswerFromCallKit(customHeaders?: Record<string, string>): void;
|
|
159
|
+
/**
|
|
160
|
+
* Queue an end action for when the call invite arrives (for CallKit integration)
|
|
161
|
+
* This should be called when the user ends from CallKit before the socket connection is established
|
|
162
|
+
*/
|
|
163
|
+
queueEndFromCallKit(): void;
|
|
164
|
+
/**
|
|
165
|
+
* Dispose of the client and clean up all resources.
|
|
166
|
+
*
|
|
167
|
+
* After calling this method, the client instance should not be used anymore.
|
|
168
|
+
* This is particularly important for background clients that should be
|
|
169
|
+
* disposed after handling push notifications.
|
|
170
|
+
*/
|
|
171
|
+
dispose(): void;
|
|
172
|
+
/**
|
|
173
|
+
* Throw an error if the client has been disposed
|
|
174
|
+
*/
|
|
175
|
+
private _throwIfDisposed;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Create a new TelnyxVoipClient instance for normal app usage
|
|
179
|
+
*/
|
|
180
|
+
export declare function createTelnyxVoipClient(options?: TelnyxVoipClientOptions): TelnyxVoipClient;
|
|
181
|
+
/**
|
|
182
|
+
* Create a new TelnyxVoipClient instance for background push notification handling
|
|
183
|
+
*/
|
|
184
|
+
export declare function createBackgroundTelnyxVoipClient(options?: TelnyxVoipClientOptions): TelnyxVoipClient;
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TelnyxVoipClient = void 0;
|
|
4
|
+
exports.createTelnyxVoipClient = createTelnyxVoipClient;
|
|
5
|
+
exports.createBackgroundTelnyxVoipClient = createBackgroundTelnyxVoipClient;
|
|
6
|
+
const connection_state_1 = require("./models/connection-state");
|
|
7
|
+
const config_1 = require("./models/config");
|
|
8
|
+
const session_manager_1 = require("./internal/session/session-manager");
|
|
9
|
+
const call_state_controller_1 = require("./internal/calls/call-state-controller");
|
|
10
|
+
/**
|
|
11
|
+
* The main public interface for the react-voice-commons module.
|
|
12
|
+
*
|
|
13
|
+
* This class serves as the Façade for the entire module, providing a simplified
|
|
14
|
+
* API that completely hides the underlying complexity. It is the sole entry point
|
|
15
|
+
* for developers using the react-voice-commons package.
|
|
16
|
+
*
|
|
17
|
+
* The TelnyxVoipClient is designed to be state-management agnostic, exposing
|
|
18
|
+
* all observable state via RxJS streams. This allows developers to integrate it
|
|
19
|
+
* into their chosen state management solution naturally.
|
|
20
|
+
*/
|
|
21
|
+
class TelnyxVoipClient {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new TelnyxVoipClient instance.
|
|
24
|
+
*
|
|
25
|
+
* @param options Configuration options for the client
|
|
26
|
+
*/
|
|
27
|
+
constructor(options = {}) {
|
|
28
|
+
this._disposed = false;
|
|
29
|
+
this._options = {
|
|
30
|
+
enableAppStateManagement: true,
|
|
31
|
+
debug: false,
|
|
32
|
+
...options,
|
|
33
|
+
};
|
|
34
|
+
// Initialize core components
|
|
35
|
+
this._sessionManager = new session_manager_1.SessionManager();
|
|
36
|
+
this._callStateController = new call_state_controller_1.CallStateController(this._sessionManager);
|
|
37
|
+
// Set up callback to initialize call state controller listeners when client is ready
|
|
38
|
+
this._sessionManager.setOnClientReady(() => {
|
|
39
|
+
console.log('🔧 TelnyxVoipClient: Client ready, initializing call state controller listeners');
|
|
40
|
+
this._callStateController.initializeClientListeners();
|
|
41
|
+
});
|
|
42
|
+
if (this._options.debug) {
|
|
43
|
+
console.log('TelnyxVoipClient initialized with options:', this._options);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// ========== Observable Streams ==========
|
|
47
|
+
/**
|
|
48
|
+
* Stream of connection state changes.
|
|
49
|
+
*
|
|
50
|
+
* Emits the current status of the connection to the Telnyx backend.
|
|
51
|
+
* Values include connecting, connected, disconnected, and error states.
|
|
52
|
+
* Listen to this to show connection indicators in your UI.
|
|
53
|
+
*/
|
|
54
|
+
get connectionState$() {
|
|
55
|
+
return this._sessionManager.connectionState$;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Stream of all current calls.
|
|
59
|
+
*
|
|
60
|
+
* Emits a list of all current Call objects. Use this for applications
|
|
61
|
+
* that need to support multiple simultaneous calls (e.g., call waiting,
|
|
62
|
+
* conference calls).
|
|
63
|
+
*/
|
|
64
|
+
get calls$() {
|
|
65
|
+
return this._callStateController.calls$;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Stream of the currently active call.
|
|
69
|
+
*
|
|
70
|
+
* A convenience stream that emits the currently active Call object.
|
|
71
|
+
* It emits null when no call is in progress. Ideal for applications
|
|
72
|
+
* that only handle a single call at a time.
|
|
73
|
+
*/
|
|
74
|
+
get activeCall$() {
|
|
75
|
+
return this._callStateController.activeCall$;
|
|
76
|
+
}
|
|
77
|
+
// ========== Synchronous State Access ==========
|
|
78
|
+
/**
|
|
79
|
+
* Current connection state (synchronous access).
|
|
80
|
+
*/
|
|
81
|
+
get currentConnectionState() {
|
|
82
|
+
return this._sessionManager.currentState;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Current list of calls (synchronous access).
|
|
86
|
+
*/
|
|
87
|
+
get currentCalls() {
|
|
88
|
+
return this._callStateController.currentCalls;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Current active call (synchronous access).
|
|
92
|
+
*/
|
|
93
|
+
get currentActiveCall() {
|
|
94
|
+
return this._callStateController.currentActiveCall;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Current session ID (UUID) for this connection.
|
|
98
|
+
*/
|
|
99
|
+
get sessionId() {
|
|
100
|
+
return this._sessionManager.sessionId;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Configuration options for this client instance.
|
|
104
|
+
*/
|
|
105
|
+
get options() {
|
|
106
|
+
return this._options;
|
|
107
|
+
}
|
|
108
|
+
// ========== Authentication Methods ==========
|
|
109
|
+
/**
|
|
110
|
+
* Connects to the Telnyx platform using credential authentication.
|
|
111
|
+
*
|
|
112
|
+
* @param config The credential configuration containing SIP username and password
|
|
113
|
+
* @returns A Promise that completes when the connection attempt is initiated
|
|
114
|
+
*
|
|
115
|
+
* Listen to connectionState$ to monitor the actual connection status.
|
|
116
|
+
*/
|
|
117
|
+
async login(config) {
|
|
118
|
+
this._throwIfDisposed();
|
|
119
|
+
const errors = (0, config_1.validateConfig)(config);
|
|
120
|
+
if (errors.length > 0) {
|
|
121
|
+
throw new Error(`Invalid configuration: ${errors.join(', ')}`);
|
|
122
|
+
}
|
|
123
|
+
if (this._options.debug) {
|
|
124
|
+
console.log('TelnyxVoipClient: Logging in with credentials for user:', config.sipUser);
|
|
125
|
+
}
|
|
126
|
+
await this._sessionManager.connectWithCredential(config);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Connects to the Telnyx platform using token authentication.
|
|
130
|
+
*
|
|
131
|
+
* @param config The token configuration containing the authentication token
|
|
132
|
+
* @returns A Promise that completes when the connection attempt is initiated
|
|
133
|
+
*
|
|
134
|
+
* Listen to connectionState$ to monitor the actual connection status.
|
|
135
|
+
*/
|
|
136
|
+
async loginWithToken(config) {
|
|
137
|
+
this._throwIfDisposed();
|
|
138
|
+
const errors = (0, config_1.validateConfig)(config);
|
|
139
|
+
if (errors.length > 0) {
|
|
140
|
+
throw new Error(`Invalid configuration: ${errors.join(', ')}`);
|
|
141
|
+
}
|
|
142
|
+
if (this._options.debug) {
|
|
143
|
+
console.log('TelnyxVoipClient: Logging in with token');
|
|
144
|
+
}
|
|
145
|
+
await this._sessionManager.connectWithToken(config);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Disconnects from the Telnyx platform.
|
|
149
|
+
*
|
|
150
|
+
* This method terminates the connection, ends any active calls, and
|
|
151
|
+
* cleans up all related resources.
|
|
152
|
+
*/
|
|
153
|
+
async logout() {
|
|
154
|
+
if (this._disposed) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (this._options.debug) {
|
|
158
|
+
console.log('TelnyxVoipClient: Logging out');
|
|
159
|
+
}
|
|
160
|
+
await this._sessionManager.disconnect();
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Attempts to reconnect using previously stored configuration.
|
|
164
|
+
*
|
|
165
|
+
* This method is used for auto-reconnection scenarios where the app
|
|
166
|
+
* comes back to the foreground and needs to restore the connection.
|
|
167
|
+
*
|
|
168
|
+
* @returns Promise<boolean> - true if reconnection was successful, false otherwise
|
|
169
|
+
*/
|
|
170
|
+
async loginFromStoredConfig() {
|
|
171
|
+
this._throwIfDisposed();
|
|
172
|
+
if (this._options.debug) {
|
|
173
|
+
console.log('TelnyxVoipClient: Attempting to login from stored config');
|
|
174
|
+
}
|
|
175
|
+
try {
|
|
176
|
+
// Try to retrieve stored credentials and token from AsyncStorage
|
|
177
|
+
const AsyncStorage = require('@react-native-async-storage/async-storage').default;
|
|
178
|
+
const storedUsername = await AsyncStorage.getItem('@telnyx_username');
|
|
179
|
+
const storedPassword = await AsyncStorage.getItem('@telnyx_password');
|
|
180
|
+
const storedCredentialToken = await AsyncStorage.getItem('@credential_token');
|
|
181
|
+
const storedPushToken = await AsyncStorage.getItem('@push_token');
|
|
182
|
+
// Check if we have credential-based authentication data
|
|
183
|
+
if (storedUsername && storedPassword) {
|
|
184
|
+
// Create credential config from stored data
|
|
185
|
+
const { createCredentialConfig } = require('./models/config');
|
|
186
|
+
const config = createCredentialConfig(storedUsername, storedPassword, {
|
|
187
|
+
pushNotificationDeviceToken: storedPushToken,
|
|
188
|
+
});
|
|
189
|
+
if (this._options.debug) {
|
|
190
|
+
console.log('TelnyxVoipClient: Reconnecting with stored credentials for user:', storedUsername);
|
|
191
|
+
}
|
|
192
|
+
await this._sessionManager.connectWithCredential(config);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
// Check if we have token-based authentication data
|
|
196
|
+
if (storedCredentialToken) {
|
|
197
|
+
// Create token config from stored data
|
|
198
|
+
const { createTokenConfig } = require('./models/config');
|
|
199
|
+
const config = createTokenConfig(storedCredentialToken, {
|
|
200
|
+
pushNotificationDeviceToken: storedPushToken,
|
|
201
|
+
});
|
|
202
|
+
if (this._options.debug) {
|
|
203
|
+
console.log('TelnyxVoipClient: Reconnecting with stored token');
|
|
204
|
+
}
|
|
205
|
+
await this._sessionManager.connectWithToken(config);
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
// No stored authentication data found
|
|
209
|
+
if (this._options.debug) {
|
|
210
|
+
console.log('TelnyxVoipClient: No stored credentials or token found');
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
if (this._options.debug) {
|
|
216
|
+
console.log('TelnyxVoipClient: Failed to login from stored config:', error);
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// ========== Call Management Methods ==========
|
|
222
|
+
/**
|
|
223
|
+
* Initiates a new outgoing call.
|
|
224
|
+
*
|
|
225
|
+
* @param destination The destination number or SIP URI to call
|
|
226
|
+
* @param debug Optional flag to enable call quality metrics for this call
|
|
227
|
+
* @returns A Promise that completes with the Call object once the invitation has been sent
|
|
228
|
+
*
|
|
229
|
+
* The call's state can be monitored through the returned Call object's streams.
|
|
230
|
+
*/
|
|
231
|
+
async newCall(destination, debug = false) {
|
|
232
|
+
this._throwIfDisposed();
|
|
233
|
+
if (!destination || destination.trim() === '') {
|
|
234
|
+
throw new Error('Destination is required');
|
|
235
|
+
}
|
|
236
|
+
if (this.currentConnectionState !== connection_state_1.TelnyxConnectionState.CONNECTED) {
|
|
237
|
+
throw new Error(`Cannot make call when connection state is: ${this.currentConnectionState}`);
|
|
238
|
+
}
|
|
239
|
+
if (this._options.debug) {
|
|
240
|
+
console.log('TelnyxVoipClient: Creating new call to:', destination);
|
|
241
|
+
}
|
|
242
|
+
return await this._callStateController.newCall(destination, undefined, undefined, debug);
|
|
243
|
+
}
|
|
244
|
+
// ========== Push Notification Methods ==========
|
|
245
|
+
/**
|
|
246
|
+
* Handle push notification payload.
|
|
247
|
+
*
|
|
248
|
+
* This is the unified entry point for all push notifications. It intelligently
|
|
249
|
+
* determines whether to show a new incoming call UI or to process an already
|
|
250
|
+
* actioned (accepted/declined) call upon app launch.
|
|
251
|
+
*
|
|
252
|
+
* @param payload The push notification payload
|
|
253
|
+
*/
|
|
254
|
+
async handlePushNotification(payload) {
|
|
255
|
+
this._throwIfDisposed();
|
|
256
|
+
if (this._options.debug) {
|
|
257
|
+
console.log('TelnyxVoipClient: Handling push notification:', payload);
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
// First, pass the push notification to the session manager for processing
|
|
261
|
+
// This will set the isCallFromPush flag on the TelnyxRTC client
|
|
262
|
+
await this._sessionManager.handlePushNotification(payload);
|
|
263
|
+
// Connect if not already connected
|
|
264
|
+
const currentState = this.currentConnectionState;
|
|
265
|
+
if (currentState !== connection_state_1.TelnyxConnectionState.CONNECTED) {
|
|
266
|
+
// Try to login from stored config - now the push flags should be set
|
|
267
|
+
const loginSuccess = await this.loginFromStoredConfig();
|
|
268
|
+
if (!loginSuccess) {
|
|
269
|
+
console.warn('TelnyxVoipClient: Could not login from stored config for push notification');
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
console.error('TelnyxVoipClient: Error handling push notification:', error);
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Disables push notifications for the current session.
|
|
281
|
+
*
|
|
282
|
+
* This method sends a request to the Telnyx backend to disable push
|
|
283
|
+
* notifications for the current registered device/session.
|
|
284
|
+
*/
|
|
285
|
+
disablePushNotifications() {
|
|
286
|
+
this._throwIfDisposed();
|
|
287
|
+
if (this._options.debug) {
|
|
288
|
+
console.log('TelnyxVoipClient: Disabling push notifications');
|
|
289
|
+
}
|
|
290
|
+
this._sessionManager.disablePushNotifications();
|
|
291
|
+
}
|
|
292
|
+
// ========== CallKit Integration Methods ==========
|
|
293
|
+
/**
|
|
294
|
+
* Set a call to connecting state (used for push notification calls when answered via CallKit)
|
|
295
|
+
* @param callId The ID of the call to set to connecting state
|
|
296
|
+
* @internal
|
|
297
|
+
*/
|
|
298
|
+
setCallConnecting(callId) {
|
|
299
|
+
this._callStateController.setCallConnecting(callId);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Find a call by its underlying Telnyx call object
|
|
303
|
+
* @param telnyxCall The Telnyx call object to find
|
|
304
|
+
* @internal
|
|
305
|
+
*/
|
|
306
|
+
findCallByTelnyxCall(telnyxCall) {
|
|
307
|
+
return this._callStateController.findCallByTelnyxCall(telnyxCall);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Queue an answer action for when the call invite arrives (for CallKit integration)
|
|
311
|
+
* This should be called when the user answers from CallKit before the socket connection is established
|
|
312
|
+
* @param customHeaders Optional custom headers to include with the answer
|
|
313
|
+
*/
|
|
314
|
+
queueAnswerFromCallKit(customHeaders = {}) {
|
|
315
|
+
this._throwIfDisposed();
|
|
316
|
+
if (this._options.debug) {
|
|
317
|
+
console.log('TelnyxVoipClient: Queuing answer action from CallKit', customHeaders);
|
|
318
|
+
}
|
|
319
|
+
const telnyxClient = this._sessionManager.telnyxClient;
|
|
320
|
+
if (telnyxClient && typeof telnyxClient.queueAnswerFromCallKit === 'function') {
|
|
321
|
+
telnyxClient.queueAnswerFromCallKit(customHeaders);
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
console.warn('TelnyxVoipClient: TelnyxRTC client not available or method not found for queueAnswerFromCallKit');
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Queue an end action for when the call invite arrives (for CallKit integration)
|
|
329
|
+
* This should be called when the user ends from CallKit before the socket connection is established
|
|
330
|
+
*/
|
|
331
|
+
queueEndFromCallKit() {
|
|
332
|
+
this._throwIfDisposed();
|
|
333
|
+
if (this._options.debug) {
|
|
334
|
+
console.log('TelnyxVoipClient: Queuing end action from CallKit');
|
|
335
|
+
}
|
|
336
|
+
const telnyxClient = this._sessionManager.telnyxClient;
|
|
337
|
+
if (telnyxClient && typeof telnyxClient.queueEndFromCallKit === 'function') {
|
|
338
|
+
telnyxClient.queueEndFromCallKit();
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
console.warn('TelnyxVoipClient: TelnyxRTC client not available or method not found for queueEndFromCallKit');
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
// ========== Lifecycle Methods ==========
|
|
345
|
+
/**
|
|
346
|
+
* Dispose of the client and clean up all resources.
|
|
347
|
+
*
|
|
348
|
+
* After calling this method, the client instance should not be used anymore.
|
|
349
|
+
* This is particularly important for background clients that should be
|
|
350
|
+
* disposed after handling push notifications.
|
|
351
|
+
*/
|
|
352
|
+
dispose() {
|
|
353
|
+
if (this._disposed) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
if (this._options.debug) {
|
|
357
|
+
console.log('TelnyxVoipClient: Disposing client');
|
|
358
|
+
}
|
|
359
|
+
this._disposed = true;
|
|
360
|
+
this._callStateController.dispose();
|
|
361
|
+
this._sessionManager.dispose();
|
|
362
|
+
}
|
|
363
|
+
// ========== Private Methods ==========
|
|
364
|
+
/**
|
|
365
|
+
* Throw an error if the client has been disposed
|
|
366
|
+
*/
|
|
367
|
+
_throwIfDisposed() {
|
|
368
|
+
if (this._disposed) {
|
|
369
|
+
throw new Error('TelnyxVoipClient has been disposed');
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
exports.TelnyxVoipClient = TelnyxVoipClient;
|
|
374
|
+
// ========== Factory Functions ==========
|
|
375
|
+
/**
|
|
376
|
+
* Create a new TelnyxVoipClient instance for normal app usage
|
|
377
|
+
*/
|
|
378
|
+
function createTelnyxVoipClient(options) {
|
|
379
|
+
return new TelnyxVoipClient(options);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Create a new TelnyxVoipClient instance for background push notification handling
|
|
383
|
+
*/
|
|
384
|
+
function createBackgroundTelnyxVoipClient(options) {
|
|
385
|
+
return new TelnyxVoipClient(options);
|
|
386
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@telnyx/react-voice-commons-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A high-level, state-agnostic, drop-in module for the Telnyx React Native SDK that simplifies WebRTC voice calling integration",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"module": "lib/index.js",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"react-native": "lib/index.js",
|
|
9
|
+
"source": "src/index.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./lib/index.js",
|
|
13
|
+
"require": "./lib/index.js",
|
|
14
|
+
"types": "./lib/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"lib",
|
|
19
|
+
"src",
|
|
20
|
+
"ios",
|
|
21
|
+
"TelnyxVoiceCommons.podspec",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"dev": "tsc --watch",
|
|
27
|
+
"test": "jest",
|
|
28
|
+
"test:watch": "jest --watch",
|
|
29
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
30
|
+
"clean": "rm -rf lib",
|
|
31
|
+
"android": "expo run:android",
|
|
32
|
+
"ios": "expo run:ios"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"react-native",
|
|
36
|
+
"telnyx",
|
|
37
|
+
"webrtc",
|
|
38
|
+
"voice",
|
|
39
|
+
"calling",
|
|
40
|
+
"sip",
|
|
41
|
+
"voip",
|
|
42
|
+
"commons",
|
|
43
|
+
"headless"
|
|
44
|
+
],
|
|
45
|
+
"author": "Telnyx",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"homepage": "https://github.com/team-telnyx/react-native-voice-commons",
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/team-telnyx/react-native-voice-commons.git"
|
|
51
|
+
},
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/team-telnyx/react-native-voice-commons/issues"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@react-native-async-storage/async-storage": "^2.1.0",
|
|
57
|
+
"expo-router": "^5.1.0",
|
|
58
|
+
"react": "^19.0.0",
|
|
59
|
+
"react-native": "^0.79.0",
|
|
60
|
+
"react-native-webrtc": "^124.0.5"
|
|
61
|
+
},
|
|
62
|
+
"peerDependenciesMeta": {
|
|
63
|
+
"@react-native-async-storage/async-storage": {
|
|
64
|
+
"optional": false
|
|
65
|
+
},
|
|
66
|
+
"expo-router": {
|
|
67
|
+
"optional": false
|
|
68
|
+
},
|
|
69
|
+
"react": {
|
|
70
|
+
"optional": false
|
|
71
|
+
},
|
|
72
|
+
"react-native": {
|
|
73
|
+
"optional": false
|
|
74
|
+
},
|
|
75
|
+
"react-native-webrtc": {
|
|
76
|
+
"optional": false
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"dependencies": {
|
|
80
|
+
"@react-native-community/eslint-config": "^3.2.0",
|
|
81
|
+
"@telnyx/react-native-voice-sdk": "^0.1.0",
|
|
82
|
+
"eventemitter3": "^5.0.1",
|
|
83
|
+
"expo": "~53.0.22",
|
|
84
|
+
"react-native-voip-push-notification": "^3.3.3",
|
|
85
|
+
"rxjs": "^7.8.2"
|
|
86
|
+
},
|
|
87
|
+
"devDependencies": {
|
|
88
|
+
"@types/jest": "^29.5.0",
|
|
89
|
+
"@types/react": "~19.0.14",
|
|
90
|
+
"@types/react-native": "^0.72.8",
|
|
91
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
92
|
+
"@typescript-eslint/parser": "^6.0.0",
|
|
93
|
+
"eslint": "^8.0.0",
|
|
94
|
+
"jest": "^29.5.0",
|
|
95
|
+
"ts-jest": "^29.1.0",
|
|
96
|
+
"typescript": "^5.0.0"
|
|
97
|
+
},
|
|
98
|
+
"engines": {
|
|
99
|
+
"node": ">=16"
|
|
100
|
+
},
|
|
101
|
+
"publishConfig": {
|
|
102
|
+
"access": "public"
|
|
103
|
+
}
|
|
104
|
+
}
|