Skip to content

M3 #31: TypeScript SDK: HTTP and WebSocket client implementation#71

Merged
joaquinbejar merged 1 commit into
mainfrom
M3/issue-31-typescript-client
Feb 2, 2026
Merged

M3 #31: TypeScript SDK: HTTP and WebSocket client implementation#71
joaquinbejar merged 1 commit into
mainfrom
M3/issue-31-typescript-client

Conversation

@joaquinbejar

Copy link
Copy Markdown
Owner

Summary

Implements the HTTP and WebSocket client for the TypeScript SDK, providing a complete client library for JavaScript/TypeScript applications to interact with Matchbook.

Changes

New Module: ts-sdk/src/client/

File Description
config.ts Client configuration with defaults
errors.ts Client-specific error classes
http.ts HTTP client implementation
websocket.ts WebSocket client implementation
index.ts Module exports

HTTP Client (MatchbookClient)

Market Data Methods:

  • getMarkets() — Get all markets
  • getMarket(address) — Get market by address
  • getOrderbook(market, depth?) — Get order book
  • getTrades(market, options?) — Get recent trades

User Data Methods:

  • getOrders(owner, market?) — Get user orders
  • getUserTrades(owner, market?) — Get user trades
  • getBalances(owner) — Get user balances

Transaction Building Methods:

  • buildPlaceOrderTx(params) — Build place order transaction
  • buildCancelOrderTx(params) — Build cancel order transaction
  • buildDepositTx(params) — Build deposit transaction
  • buildWithdrawTx(params) — Build withdraw transaction

WebSocket Client (MatchbookWsClient)

Connection Methods:

  • connect() — Connect to WebSocket server
  • disconnect() — Disconnect from server

Subscription Methods:

  • subscribeBook(market, callback, depth?) — Subscribe to order book
  • subscribeTrades(market, callback) — Subscribe to trades
  • subscribeOrders(callback) — Subscribe to order updates
  • unsubscribe(subscriptionId) — Unsubscribe from channel

Features:

  • Automatic reconnection with exponential backoff
  • Heartbeat ping/pong handling
  • Subscription management and resubscription on reconnect

Error Classes

  • ClientError — Base error class
  • HttpError — HTTP request errors
  • ApiError — API error responses
  • TimeoutError — Request timeouts
  • RateLimitError — Rate limiting (429)
  • NotFoundError — Resource not found (404)
  • UnauthorizedError — Authentication errors (401)
  • WebSocketError — WebSocket errors

Documentation

  • Added comprehensive README.md with usage examples

Technical Decisions

  • Native fetch: Uses native fetch API (Node.js 18+ and browsers)
  • WebSocket: Uses native WebSocket API with reconnection logic
  • Type-safe callbacks: Strongly typed callback functions for subscriptions
  • Error hierarchy: Structured error classes for precise error handling

Testing

  • 26 unit tests for client configuration and errors
  • 60 total tests passing
  • TypeScript type checking passes
  • Build succeeds (ESM, CJS, types)

Checklist

  • TypeScript strict mode enabled
  • JSDoc documentation on all exports
  • Error handling with typed errors
  • All tests pass

Closes #31

- Add client module to TypeScript SDK:
  - config.ts: ClientConfig with base URL, WS URL, timeout, API key
  - errors.ts: Client-specific error classes
  - http.ts: MatchbookClient HTTP client
  - websocket.ts: MatchbookWsClient WebSocket client

- HTTP client methods:
  - getMarkets(), getMarket(address)
  - getOrderbook(market, depth)
  - getTrades(market, options)
  - getOrders(owner, market), getUserTrades(owner, market)
  - getBalances(owner)
  - buildPlaceOrderTx(), buildCancelOrderTx()
  - buildDepositTx(), buildWithdrawTx()

- WebSocket client methods:
  - connect(), disconnect()
  - subscribeBook(market, callback, depth)
  - subscribeTrades(market, callback)
  - subscribeOrders(callback)
  - unsubscribe(subscriptionId)

- Error classes:
  - ClientError, HttpError, ApiError
  - TimeoutError, RateLimitError
  - NotFoundError, UnauthorizedError
  - WebSocketError

- Features:
  - Automatic reconnection with exponential backoff
  - Heartbeat ping/pong handling
  - Subscription management
  - Type-safe callbacks

- Add README.md with usage examples
- 26 unit tests for client configuration and errors
- 60 total tests passing
@joaquinbejar joaquinbejar added this to the M3: Client SDKs milestone Feb 1, 2026
@joaquinbejar joaquinbejar merged commit 7986cf8 into main Feb 2, 2026
5 checks passed
@joaquinbejar joaquinbejar deleted the M3/issue-31-typescript-client branch February 2, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeScript SDK: Full client implementation

1 participant