@imean/service-client
Universal TypeScript client for @imean/service-engine, supporting both HTTP and WebSocket protocols.
Features
- 🔄 Automatic WebSocket reconnection
- 📡 Streaming support
- 🔁 Retry mechanism
- ⚡ Request interceptors
- 🚦 Request queue management
- 💪 Type-safe API
- 🌐 Cross-platform (Node.js, Deno, Browser)
Installation
npm install imean-service-client
Usage
Basic Usage
import { MicroserviceClient } from "imean-service-client";
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
});
const result = await client.someModule.someMethod("arg1", "arg2");
for await (const item of await client.someModule.streamMethod(10)) {
console.log(item);
}
WebSocket Support
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
websocket: true,
});
Interceptors
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
interceptors: [
{
onRequest: async (config) => {
config.headers["Authorization"] = "Bearer token";
},
onResponse: async (response) => {
return response;
},
onError: async (error) => {
return error;
},
},
],
});
Retry Configuration
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
retry: {
maxAttempts: 3,
delays: [1000, 2000, 5000],
shouldRetry: (error) => error instanceof ConnectionError,
},
});
Request Queue
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
request: {
concurrency: 5,
timeout: 5000,
},
});
API Reference
MicroserviceClient
Constructor Options
interface ClientConfig {
baseUrl: string;
prefix?: string;
headers?: Record<string, string>;
fetch?: typeof fetch;
websocket?: boolean | Partial<WebSocketOptions>;
retry?: RetryOptions;
request?: RequestOptions;
stream?: StreamOptions;
interceptors?: RequestInterceptor[];
}
WebSocket Options
interface WebSocketOptions {
url: string;
timeout?: number;
retryInterval?: number;
maxRetries?: number;
pingInterval?: number;
WebSocket?: typeof WebSocket;
onClose?: () => void;
onError?: (error: Error) => void;
onReconnect?: () => void;
onReconnected?: () => void;
}
For more details, please check the source code and tests.
License
MIT