@pokertools/sdk 1.0.15 โ 1.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -10
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.cjs +12 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +12 -6
- package/dist/index.js.map +1 -1
- package/dist/react/index.cjs +27 -18
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.js +27 -18
- package/dist/react/index.js.map +1 -1
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
|
-
# @pokertools/sdk
|
|
1
|
+
# ๐ @pokertools/sdk
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@pokertools/sdk)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
|
|
6
6
|
The official TypeScript SDK for the **PokerTools** platform. Build real-time Texas Hold'em applications with ease, featuring robust state management, WebSocket integration, and React hooks.
|
|
7
7
|
|
|
8
|
+
## Table of Contents
|
|
9
|
+
|
|
10
|
+
- [โจ Features](#-features)
|
|
11
|
+
- [๐ฆ Installation](#-installation)
|
|
12
|
+
- [๐ Quick Start (React)](#-quick-start-react)
|
|
13
|
+
- [๐๏ธ Architecture](#๏ธ-architecture)
|
|
14
|
+
- [๐ Authentication (SIWE)](#-authentication-siwe)
|
|
15
|
+
- [๐ก Real-time Events](#-real-time-events)
|
|
16
|
+
- [๐ ๏ธ Configuration](#๏ธ-configuration)
|
|
17
|
+
- [๐ด Error Handling](#-error-handling)
|
|
18
|
+
- [๐ ๏ธ API Reference](#๏ธ-api-reference)
|
|
19
|
+
- [React Hooks](#react-hooks)
|
|
20
|
+
- [PokerClient (REST API)](#pokerclient-rest-api)
|
|
21
|
+
- [PokerSocket (WebSocket)](#pokersocket-websocket)
|
|
22
|
+
- [Auth Helpers (SIWE)](#auth-helpers-siwe)
|
|
23
|
+
- [Utilities](#utilities)
|
|
24
|
+
- [Main Exports](#main-exports)
|
|
25
|
+
- [๐งช Testing](#-testing)
|
|
26
|
+
- [๐ฆ Related Packages](#-related-packages)
|
|
27
|
+
- [๐ License](#-license)
|
|
28
|
+
|
|
8
29
|
## โจ Features
|
|
9
30
|
|
|
10
31
|
- ๐ **Real-time WebSocket Client**: Automatic reconnection, heartbeats, and typed events. JWTs are sent as WebSocket subprotocol credentials (not in the URL query string) to avoid leaking tokens in access logs.
|
|
@@ -24,7 +45,7 @@ yarn add @pokertools/sdk @pokertools/types
|
|
|
24
45
|
pnpm add @pokertools/sdk @pokertools/types
|
|
25
46
|
```
|
|
26
47
|
|
|
27
|
-
`@pokertools/sdk` (v1.0.
|
|
48
|
+
`@pokertools/sdk` (v1.0.17) depends on `@pokertools/types` (v1.0.17) for shared TypeScript types.
|
|
28
49
|
The React hooks require `react >= 19.2.3` as an **optional** peer dependency โ install `react`
|
|
29
50
|
and `react-dom` only if you plan to use the React integration.
|
|
30
51
|
|
|
@@ -278,6 +299,89 @@ socket.leave("table-1");
|
|
|
278
299
|
socket.disconnect();
|
|
279
300
|
```
|
|
280
301
|
|
|
302
|
+
## ๐ ๏ธ Configuration
|
|
303
|
+
|
|
304
|
+
All configuration flows through two main interfaces: `PokerSDKConfig` (for the REST client and SDK initialization) and the `PokerSocket` constructor options (for WebSocket-specific settings).
|
|
305
|
+
|
|
306
|
+
### PokerSDKConfig
|
|
307
|
+
|
|
308
|
+
Used by `new PokerClient(config)`, `new PokerSocket(config)`, and `<PokerProvider config={...}>`.
|
|
309
|
+
|
|
310
|
+
| Option | Type | Default | Description |
|
|
311
|
+
| ----------- | ------------------ | --------------------------------------- | ------------------------------------------------------- |
|
|
312
|
+
| `baseUrl` | `string` | โ **(required)** | API base URL (e.g., `"https://api.poker.example.com"`). |
|
|
313
|
+
| `wsUrl` | `string` | baseUrl with `ws://` | WebSocket server URL. |
|
|
314
|
+
| `token` | `string` | `undefined` | JWT token for authentication. |
|
|
315
|
+
| `timeout` | `number` | `30000` | Request timeout in milliseconds. |
|
|
316
|
+
| `retry` | `RetryConfig` | `{ count: 3, delay: 1000, backoff: 2 }` | Retry configuration. |
|
|
317
|
+
| `fetch` | `typeof fetch` | `globalThis.fetch` | Custom fetch implementation (e.g., for React Native). |
|
|
318
|
+
| `WebSocket` | `typeof WebSocket` | `globalThis.WebSocket` | Custom WebSocket implementation. |
|
|
319
|
+
| `debug` | `boolean` | `false` | Enable request/response debug logging. |
|
|
320
|
+
|
|
321
|
+
### PokerSocket Constructor Options
|
|
322
|
+
|
|
323
|
+
When constructing a `PokerSocket` directly (non-React):
|
|
324
|
+
|
|
325
|
+
| Option | Type | Default | Description |
|
|
326
|
+
| ------------------- | ------------------ | ---------------------- | ------------------------------------- |
|
|
327
|
+
| `url` | `string` | โ **(required)** | WebSocket server URL. |
|
|
328
|
+
| `token` | `string` | โ **(required)** | JWT for subprotocol authentication. |
|
|
329
|
+
| `heartbeatInterval` | `number` | `25000` | Application-level ping interval (ms). |
|
|
330
|
+
| `reconnectAttempts` | `number` | `10` | Max reconnection attempts. |
|
|
331
|
+
| `reconnectDelay` | `number` | `1000` | Base reconnection delay (ms). |
|
|
332
|
+
| `maxReconnectDelay` | `number` | `30000` | Max reconnection delay (ms). |
|
|
333
|
+
| `WebSocket` | `typeof WebSocket` | `globalThis.WebSocket` | Custom WebSocket implementation. |
|
|
334
|
+
| `debug` | `boolean` | `false` | Enable debug logging. |
|
|
335
|
+
|
|
336
|
+
Static factory: `PokerSocket.fromConfig(config: PokerSDKConfig)` creates a socket from a full SDK config object.
|
|
337
|
+
|
|
338
|
+
## ๐ด Error Handling
|
|
339
|
+
|
|
340
|
+
### PokerSDKError
|
|
341
|
+
|
|
342
|
+
All SDK errors are thrown as `PokerSDKError` instances, which extend the standard `Error` class with additional metadata:
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
import { PokerSDKError } from "@pokertools/sdk";
|
|
346
|
+
|
|
347
|
+
try {
|
|
348
|
+
await client.buyIn("table-1", { amount: 500, seat: 3 });
|
|
349
|
+
} catch (error) {
|
|
350
|
+
if (error instanceof PokerSDKError) {
|
|
351
|
+
console.log("Code:", error.code); // e.g., "NOT_MODIFIED", "VALIDATION_ERROR"
|
|
352
|
+
console.log("Status:", error.statusCode); // e.g., 304, 400, 429, 500
|
|
353
|
+
console.log("Details:", error.details); // Server-provided error details
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Common error codes:
|
|
359
|
+
|
|
360
|
+
- `NOT_MODIFIED` (304) โ State unchanged; returned by `getTableState()` when the version matches the `since` parameter.
|
|
361
|
+
- `TIMEOUT` โ Request exceeded the configured timeout.
|
|
362
|
+
- `REQUEST_FAILED` โ Network failure or max retries exhausted.
|
|
363
|
+
|
|
364
|
+
### Retry Behavior
|
|
365
|
+
|
|
366
|
+
`PokerClient` automatically retries failed requests using exponential backoff:
|
|
367
|
+
|
|
368
|
+
1. **Retries**: Up to `3` attempts by default (configurable via `retry.count`).
|
|
369
|
+
2. **Backoff**: Each retry waits `delay ร backoff^attempt` ms (default: `1000ms ร 2^n`).
|
|
370
|
+
3. **Retryable errors**: 5xx server errors, network failures, and rate-limited (429) responses.
|
|
371
|
+
4. **Non-retryable errors**: 4xx client errors (except 429) and aborted requests are not retried.
|
|
372
|
+
|
|
373
|
+
### Debug Logging
|
|
374
|
+
|
|
375
|
+
Set `debug: true` on `PokerSDKConfig` to enable verbose console output:
|
|
376
|
+
|
|
377
|
+
```
|
|
378
|
+
[PokerSDK] GET https://api.example.com/tables
|
|
379
|
+
[PokerSDK] Response 200 (45ms)
|
|
380
|
+
[PokerSDK] Retry 2/3 in 2000ms
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
This is useful during development for tracing request lifecycle and retry behavior.
|
|
384
|
+
|
|
281
385
|
## ๐ ๏ธ API Reference
|
|
282
386
|
|
|
283
387
|
### React Hooks
|
|
@@ -315,13 +419,13 @@ Returns the `PokerSocket` instance (or `null` if not connected).
|
|
|
315
419
|
|
|
316
420
|
Returns:
|
|
317
421
|
|
|
318
|
-
| Field | Type | Description
|
|
319
|
-
| ----------- | ---------------------- |
|
|
320
|
-
| `profile` | `UserProfile \| null` | Full profile including `username`, `address`, `role`, `balances`.
|
|
321
|
-
| `balances` | `UserBalances \| null` | `{ main: number
|
|
322
|
-
| `isLoading` | `boolean` | Initial fetch in progress.
|
|
323
|
-
| `error` | `Error \| null` | Fetch error if any.
|
|
324
|
-
| `refresh()` | `() => Promise<void>` | Re-fetch profile from API.
|
|
422
|
+
| Field | Type | Description |
|
|
423
|
+
| ----------- | ---------------------- | ----------------------------------------------------------------------- |
|
|
424
|
+
| `profile` | `UserProfile \| null` | Full profile including `username`, `address`, `role`, `balances`. |
|
|
425
|
+
| `balances` | `UserBalances \| null` | `{ main: number; inPlay: number; pendingWithdrawal: number }` in cents. |
|
|
426
|
+
| `isLoading` | `boolean` | Initial fetch in progress. |
|
|
427
|
+
| `error` | `Error \| null` | Fetch error if any. |
|
|
428
|
+
| `refresh()` | `() => Promise<void>` | Re-fetch profile from API. |
|
|
325
429
|
|
|
326
430
|
#### `useTable(tableId, options?)`
|
|
327
431
|
|
|
@@ -589,4 +693,41 @@ Exported from `@pokertools/sdk` (25+ helpers for formatting, state inspection, a
|
|
|
589
693
|
|
|
590
694
|
---
|
|
591
695
|
|
|
592
|
-
|
|
696
|
+
## ๐งช Testing
|
|
697
|
+
|
|
698
|
+
The SDK ships with a comprehensive test suite using [Vitest](https://vitest.dev/). Run all tests from the repository root:
|
|
699
|
+
|
|
700
|
+
```bash
|
|
701
|
+
npm test -w @pokertools/sdk
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
Or run them directly from the package directory:
|
|
705
|
+
|
|
706
|
+
```bash
|
|
707
|
+
cd packages/sdk && npx vitest run
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
The test suite comprises **7 test suites** covering:
|
|
711
|
+
|
|
712
|
+
| Suite | Description |
|
|
713
|
+
| --------------------------- | ---------------------------------------------------- |
|
|
714
|
+
| `auth.test.ts` | SIWE helpers: message creation, parsing, expiry. |
|
|
715
|
+
| `client.test.ts` | REST client: request lifecycle, retry, error paths. |
|
|
716
|
+
| `socket.test.ts` | WebSocket client: connect, events, reconnect logic. |
|
|
717
|
+
| `react.test.tsx` | React hooks: provider, state management, user data. |
|
|
718
|
+
| `edge-cases.test.ts` | Boundary conditions and error-handling scenarios. |
|
|
719
|
+
| `types-regressions.test.ts` | Type-level regression safety for exported types. |
|
|
720
|
+
| `utils.test.ts` | Formatting helpers, state inspection, display utils. |
|
|
721
|
+
|
|
722
|
+
## ๐ Related Packages
|
|
723
|
+
|
|
724
|
+
| Package | Description |
|
|
725
|
+
| ------------------------------------- | -------------------------------------------- |
|
|
726
|
+
| [@pokertools/types](../types) | Shared TypeScript types and type guards. |
|
|
727
|
+
| [@pokertools/api](../api) | REST/WebSocket API (Fastify) |
|
|
728
|
+
| [@pokertools/engine](../engine) | Core game engine and state machine. |
|
|
729
|
+
| [@pokertools/evaluator](../evaluator) | High-performance lookup-table hand evaluator |
|
|
730
|
+
|
|
731
|
+
## ๐ License
|
|
732
|
+
|
|
733
|
+
MIT ยฉ A.Aurelius
|