Pluskode Universal Client SDK for Web - TypeScript/JavaScript client with HTTP, WebSocket, SSE, gRPC, MQTT, and Binary Stream support
npm install @pluskode/clientbash
npm install @pluskode/client
`
Or with yarn:
`bash
yarn add @pluskode/client
`
---
Quick Start
`typescript
import { PluskodeClient } from '@pluskode/client';
const client = new PluskodeClient({
baseURL: 'http://localhost:3000'
});
// GET request
const users = await client.get('/api/users');
// POST request
const newUser = await client.post('/api/users', {
name: 'John',
email: 'john@example.com'
});
// WebSocket subscription
client.subscribe('chat/room1', (data) => {
console.log('New message:', data);
});
`
---
Features
- ✅ HTTP Methods - GET, POST, PUT, DELETE, PATCH
- ✅ WebSocket - Auto-reconnect, channel subscriptions
- ✅ Server-Sent Events (SSE) - EventSource with auto-reconnect
- ✅ gRPC - Unary RPC calls
- ✅ MQTT - Topic subscriptions and publishing
- ✅ Binary Streams - WebSocket binary data handling
- ✅ Authentication - Bearer and Basic auth
- ✅ Retry Logic - Exponential backoff
- ✅ Offline Queue - Queue requests when offline
- ✅ TypeScript - Full type definitions
- ✅ Error Handling - Comprehensive error handling
---
API Reference
$3
`typescript
const client = new PluskodeClient({
baseURL: string, // Required: Base URL
timeout?: number, // Default: 30000ms
headers?: Record, // Default headers
retries?: number, // Default: 3
retryDelay?: number, // Default: 1000ms
wsReconnectDelay?: number, // Default: 3000ms
wsMaxReconnectAttempts?: number, // Default: Infinity
});
`
$3
#### GET
`typescript
const response = await client.get('/api/users', {
params?: { key: 'value' },
headers?: { 'X-Custom': 'value' },
timeout?: 5000,
signal?: AbortSignal,
});
`
#### POST
`typescript
const response = await client.post('/api/users', {
name: 'John',
email: 'john@example.com'
}, {
headers?: { 'Content-Type': 'application/json' }
});
`
#### PUT, PATCH, DELETE
`typescript
await client.put('/api/users/123', { name: 'Jane' });
await client.patch('/api/users/123', { email: 'jane@example.com' });
await client.delete('/api/users/123');
`
$3
`typescript
// Bearer token
client.setAuth('Bearer', 'your-jwt-token');
// Basic auth
client.setAuth('Basic', btoa('username:password'));
// Clear auth
client.clearAuth();
// Set custom header
client.setHeader('X-API-Key', 'key-123');
`
$3
`typescript
// Subscribe to channel
const unsubscribe = client.subscribe('chat/room1', (data) => {
console.log('Message:', data);
});
// Send message
client.send('chat/room1', { text: 'Hello!' });
// Unsubscribe
unsubscribe();
// Disconnect
client.disconnectWebSocket();
`
$3
`typescript
// Subscribe to SSE stream
const unsubscribe = client.subscribeSSE('/events/stream', (event) => {
console.log('Event:', event.event, event.data);
});
// Close SSE connection
unsubscribe();
// or
client.closeSSE('/events/stream');
`
$3
`typescript
// Call gRPC unary RPC
const user = await client.rpc('UserService', 'GetUser', {
id: '123'
});
const data = await client.rpc('DataService', 'GetData', {
query: 'test'
});
`
$3
`typescript
// Subscribe to MQTT topic
const unsubscribe = client.subscribeMQTT('sensors/temperature',
(topic, message, qos) => {
console.log(${topic}: ${message} (QoS: ${qos}));
},
{ qos: 1 }
);
// Publish MQTT message
await client.publishMQTT('sensors/temperature', '25.5', {
qos: 1,
retain: false
});
// Unsubscribe
unsubscribe();
`
$3
`typescript
// Connect to binary stream
const close = await client.connectBinary('/custom-protocol', (data) => {
console.log('Received:', data.byteLength, 'bytes');
// Process ArrayBuffer
});
// Close connection
close();
`
$3
`typescript
try {
const data = await client.get('/api/data');
console.log('Success:', data.data);
} catch (error: any) {
console.error('Request failed:', error.message);
}
// With AbortController
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);
try {
const data = await client.get('/api/slow', {
signal: controller.signal
});
} catch (error: any) {
if (error.name === 'AbortError') {
console.log('Request aborted');
}
}
`
$3
`typescript
// Cleanup all connections
client.destroy();
`
---
Examples
$3
`tsx
import { useEffect, useState } from 'react';
import { PluskodeClient } from '@pluskode/client';
const client = new PluskodeClient({
baseURL: 'http://localhost:3000'
});
function UsersList() {
const [users, setUsers] = useState([]);
useEffect(() => {
client.get('/api/users')
.then(res => setUsers(res.data))
.catch(err => console.error(err));
}, []);
return (
{users.map(user => (
- {user.name}
))}
);
}
`
$3
`vue
-
{{ user.name }}
`
$3
`typescript
import { PluskodeClient } from '@pluskode/client';
const client = new PluskodeClient({
baseURL: 'http://localhost:3000'
});
async function main() {
// HTTP
const users = await client.get('/api/users');
console.log('Users:', users.data);
// WebSocket
client.subscribe('notifications', (data) => {
console.log('Notification:', data);
});
// gRPC
const result = await client.rpc('Service', 'Method', { data: 'test' });
console.log('Result:', result);
}
main();
`
---
TypeScript Support
Full TypeScript definitions are included. Import types as needed:
`typescript
import {
PluskodeClient,
PluskodeClientOptions,
RequestOptions,
Response,
SSEEvent,
WebSocketMessage,
GRPCOptions,
MQTTOptions,
} from '@pluskode/client';
``