1.5.0 • Published 5 months ago

imean-service-client v1.5.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

@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",
});

// 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);
}

WebSocket Support

const client = new MicroserviceClient({
  baseUrl: "http://localhost:3000",
  prefix: "/api",
  websocket: true, // or { timeout: 5000, retryInterval: 1000 }
});

Interceptors

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;
      },
    },
  ],
});

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, // Maximum concurrent requests
    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

1.5.0

5 months ago

1.4.4

5 months ago

1.4.3

5 months ago

1.4.2

5 months ago

1.4.1

5 months ago

1.4.0

5 months ago

1.3.0

6 months ago

1.2.0

6 months ago

1.1.0

6 months ago

1.0.0

6 months ago