aiforwork-sdk-core
v0.0.1
Published
A comprehensive React Native SDK for integrating with AIforWork platform, providing socket connectivity, API management, and state management for threads/boards.
Downloads
20
Readme
AIforWork React Native Core SDK
A comprehensive React Native SDK for integrating with AIforWork platform, providing socket connectivity, API management, and state management for threads/boards.
🚀 Features
- WebSocket Integration: Real-time communication with socket.io
- API Client: Axios-based HTTP client with automatic device headers
- State Management: Zustand-powered stores for threads/boards
- Device Integration: Automatic device information headers
- Thread Management: Fetch and rename threads/boards
- Error Handling: Comprehensive error handling and logging
- TypeScript Support: Full TypeScript implementation
📋 Prerequisites
- React Native 0.77.0+
- Node.js 18+
- TypeScript 5.0+
📦 Installation
npm install
# or
yarn installDependencies
The SDK includes these key dependencies:
axios- HTTP clientzustand- State managementsocket.io-client- WebSocket communicationreact-native-device-info- Device informationimmer- Immutable state updatesuuid- Unique identifier generation
🏗️ Architecture
Core Components
src/
├── sdk/
│ └── sdk.config.ts # SDK configuration
├── restapi/
│ └── axios/
│ ├── axios_base.ts # Base Axios client
│ └── api_client.ts # API client implementation
├── socket/
│ └── socket.service.ts # WebSocket service
├── store/
│ └── threadsStore.ts # Threads state management
└── utils/
└── utils.ts # Utility functions⚙️ Configuration
Initialize the SDK
import { initializeSDK } from './src/sdk/sdk.config';
initializeSDK({
accessToken: 'your-access-token',
apiUrl: 'https://work-qa.kore.ai/',
presenceUrl: 'https://work-qa.kore.ai/',
userId: 'your-user-id'
});🔌 API Client
Features
- Automatic authentication headers
- Device information headers
- Error handling and retry logic
- URL parameter resolution
Device Headers
Automatically includes device information:
{
channel: "iOS" | "Android",
version: "1.0.0",
deviceId: "unique-device-id",
name: "Apple, iPhone 14",
tz: "America/New_York",
manufacturer: "Apple"
}Usage
import ApiClient from './src/restapi/axios/api_client';
const apiClient = ApiClient.getInstance();
// GET request
const data = await apiClient.getCall('/api/endpoint');
// POST request
const result = await apiClient.postCall('/api/endpoint', payload);
// PUT request
const updated = await apiClient.putCall('/api/endpoint', data);🧵 Threads Store
Zustand-based state management for threads/boards.
Store Structure
interface ThreadsState {
boards: any[];
loading: boolean;
error: string | null;
moreAvailable: boolean;
currentBoard: any;
}Available Actions
fetchThreads()- Fetch threads/boards listrenameThread(boardId, data)- Rename a specific thread/boardclearError()- Clear error stateresetStore()- Reset to initial state
Usage Example
See App.tsx file
🔌 WebSocket Service
Real-time communication service with connection monitoring.
Features
- Auto-reconnection
- Connection state management
- Message queuing
- Latency monitoring
- Custom event handling
Usage
import socketService from './src/socket/socket.service';
// Connect
socketService.connectToWebSocket();
// Listen for events
socketService.on('connectionStateChange', (data) => {
console.log('Connection state:', data.newState);
});
// Disconnect
socketService.disconnect();🚀 Getting Started
- Initialize the SDK in your App component:
componentDidMount() {
initializeSDK({
accessToken: 'your-token',
apiUrl: 'https://work-qa.kore.ai/',
presenceUrl: 'https://work-qa.kore.ai/',
userId: 'your-user-id'
});
}- Use the Threads Store:
const { fetchThreads, boards } = useThreadsStore();
useEffect(() => {
fetchThreads();
}, []);- Connect to WebSocket (optional):
socketService.connectToWebSocket();🛠️ Development
Scripts
# Start Metro bundler
npm start
# Run on iOS
npm run ios
# Run on Android
npm run android
# Lint code
npm run lintProject Structure
src/sdk/- SDK configuration and initializationsrc/restapi/- HTTP client and API managementsrc/socket/- WebSocket servicesrc/store/- State management (Zustand stores)src/utils/- Utility functions
🔧 Error Handling
The SDK includes comprehensive error handling:
- API errors are caught and stored in state
- WebSocket connection errors are handled gracefully
- Device information errors fallback to defaults
- All errors are logged to console for debugging
📱 Platform Support
- ✅ iOS
- ✅ Android
- ⚡ React Native 0.77.0+
🔗 Related
Note: This SDK is designed for internal AIforWork platform integration. Ensure proper authentication tokens and API endpoints are configured before use.
