Skip to content

Repository files navigation

eh-api-client

Logo eh-api-client

Battle-proven HTTP client for communication between microservices.

Built for production service-to-service traffic: contextual authentication, correlation ID propagation, connection pooling, and automatic recovery from transient network failures.

Install

npm install eh-api-client

Quick start

constClientFactory=require("eh-api-client");constusers=newClientFactory("http://users-service/v1");constclient=users.getClient(42,"orders-service");constuser=awaitclient.get(["/users/??",42]);awaitclient.patch(["/users/??",42],{active: true});

Every method supports both Promises and Node-style callbacks. Available methods are get, post, put, patch, delete, head, and exists.

Correlation IDs

Propagate x-request-id automatically across asynchronous call chains with AsyncLocalStorage:

const{ AsyncLocalStorage }=require("async_hooks");constClientFactory=require("eh-api-client");constrequestContext=newAsyncLocalStorage();ClientFactory.setAsyncLocalStorage(requestContext);requestContext.run(newMap([["requestId","req-123"]]),async()=>{awaitclient.get("/profile");// sends x-request-id: req-123});

Request, session, and device IDs can also be assigned directly with setRequestId, setSessionId, and setDeviceId.

Resilient by default

Transient network failures are retried automatically for GET requests. Retries are bounded and configurable:

users.setRetryOptions({maxAttempts: 5,retryDelay: 100});

For safe-to-retry writes, set retryOnTransientError: true. Stream bodies are never retried.

Production features

  • Shared keep-alive connection pool
  • Internal, bearer-token, and secret-based authentication
  • URL placeholders with safe value encoding
  • Forked clients for nested API paths
  • Request modifiers and configurable defaults
  • Structured HTTP and network errors
  • request-done and network-error lifecycle events
  • TypeScript declarations included

See the test suite for focused examples of each feature.

License

ISC

About

REST Client

Resources

Stars

2 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages