Universal client for imean-service-engine
npm install imean-service-clientbash
npm install imean-service-client
`
Usage
$3
`typescript
import { MicroserviceClient } from "imean-service-client";
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
});
// Normal request
const result = await client.someModule.someMethod("arg1", "arg2");
// Stream request
for await (const item of await client.someModule.streamMethod(10)) {
console.log(item);
}
`
$3
`typescript
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
websocket: true, // or { timeout: 5000, retryInterval: 1000 }
});
`
$3
`typescript
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
interceptors: [
{
onRequest: async (config) => {
config.headers["Authorization"] = "Bearer token";
},
onResponse: async (response) => {
// Transform response
return response;
},
onError: async (error) => {
// Handle error
return error;
},
},
],
});
`
$3
`typescript
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
retry: {
maxAttempts: 3,
delays: [1000, 2000, 5000],
shouldRetry: (error) => error instanceof ConnectionError,
},
});
`
$3
`typescript
const client = new MicroserviceClient({
baseUrl: "http://localhost:3000",
prefix: "/api",
request: {
concurrency: 5, // Maximum concurrent requests
timeout: 5000,
},
});
`
API Reference
$3
#### Constructor Options
`typescript
interface ClientConfig {
baseUrl: string;
prefix?: string;
headers?: Record;
fetch?: typeof fetch;
websocket?: boolean | Partial;
retry?: RetryOptions;
request?: RequestOptions;
stream?: StreamOptions;
interceptors?: RequestInterceptor[];
}
`
#### WebSocket Options
`typescript
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;
}
``