@pokertools/types 1.0.1 → 1.0.6

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.
@@ -0,0 +1,232 @@
1
+ import { PublicState } from "./PublicState";
2
+ /**
3
+ * WebSocket Protocol Definitions
4
+ *
5
+ * This file defines the complete WebSocket message protocol for real-time
6
+ * poker game communication. All messages are strongly typed discriminated unions.
7
+ */
8
+ /**
9
+ * Join a table's real-time updates
10
+ */
11
+ export interface JoinTableMessage {
12
+ readonly type: "JOIN";
13
+ readonly tableId: string;
14
+ readonly requestId?: string;
15
+ }
16
+ /**
17
+ * Leave a table's real-time updates
18
+ */
19
+ export interface LeaveTableMessage {
20
+ readonly type: "LEAVE";
21
+ readonly tableId: string;
22
+ readonly requestId?: string;
23
+ }
24
+ /**
25
+ * Application-level heartbeat (in addition to WebSocket ping/pong)
26
+ * Useful for detecting issues at application layer
27
+ */
28
+ export interface PingMessage {
29
+ readonly type: "PING";
30
+ readonly requestId: string;
31
+ readonly timestamp?: number;
32
+ }
33
+ /**
34
+ * Union of all client-to-server messages
35
+ */
36
+ export type ClientMessage = JoinTableMessage | LeaveTableMessage | PingMessage;
37
+ /**
38
+ * State snapshot sent when client joins a table
39
+ * or when requested via REST API
40
+ */
41
+ export interface SnapshotMessage {
42
+ readonly type: "SNAPSHOT";
43
+ readonly tableId: string;
44
+ readonly state: PublicState;
45
+ readonly version: number;
46
+ readonly timestamp: number;
47
+ }
48
+ /**
49
+ * Lightweight notification that state has changed
50
+ * Client should fetch full state via REST if needed
51
+ */
52
+ export interface StateUpdateMessage {
53
+ readonly type: "STATE_UPDATE";
54
+ readonly tableId: string;
55
+ readonly version: number;
56
+ readonly timestamp: number;
57
+ }
58
+ /**
59
+ * Error message
60
+ */
61
+ export interface ErrorMessage {
62
+ readonly type: "ERROR";
63
+ readonly code: string;
64
+ readonly message: string;
65
+ readonly requestId?: string;
66
+ readonly context?: Record<string, unknown>;
67
+ }
68
+ /**
69
+ * Acknowledgment of successful operation
70
+ */
71
+ export interface AckMessage {
72
+ readonly type: "ACK";
73
+ readonly requestId: string;
74
+ readonly message?: string;
75
+ }
76
+ /**
77
+ * Pong response to client PING
78
+ */
79
+ export interface PongMessage {
80
+ readonly type: "PONG";
81
+ readonly requestId: string;
82
+ readonly timestamp: number;
83
+ }
84
+ /**
85
+ * Player action notification (informational only)
86
+ * Not required for state sync, but useful for animations/UX
87
+ */
88
+ export interface ActionNotificationMessage {
89
+ readonly type: "ACTION";
90
+ readonly tableId: string;
91
+ readonly playerId: string;
92
+ readonly actionType: string;
93
+ readonly amount?: number;
94
+ readonly timestamp: number;
95
+ }
96
+ /**
97
+ * Union of all server-to-client messages
98
+ */
99
+ export type ServerMessage = SnapshotMessage | StateUpdateMessage | ErrorMessage | AckMessage | PongMessage | ActionNotificationMessage;
100
+ export declare function isClientMessage(msg: unknown): msg is ClientMessage;
101
+ export declare function isJoinMessage(msg: ClientMessage): msg is JoinTableMessage;
102
+ export declare function isLeaveMessage(msg: ClientMessage): msg is LeaveTableMessage;
103
+ export declare function isPingMessage(msg: ClientMessage): msg is PingMessage;
104
+ export declare function isServerMessage(msg: unknown): msg is ServerMessage;
105
+ import { z } from "zod";
106
+ export declare const JoinTableMessageSchema: z.ZodObject<{
107
+ type: z.ZodLiteral<"JOIN">;
108
+ tableId: z.ZodString;
109
+ requestId: z.ZodOptional<z.ZodString>;
110
+ }, z.core.$strip>;
111
+ export declare const LeaveTableMessageSchema: z.ZodObject<{
112
+ type: z.ZodLiteral<"LEAVE">;
113
+ tableId: z.ZodString;
114
+ requestId: z.ZodOptional<z.ZodString>;
115
+ }, z.core.$strip>;
116
+ export declare const PingMessageSchema: z.ZodObject<{
117
+ type: z.ZodLiteral<"PING">;
118
+ requestId: z.ZodString;
119
+ timestamp: z.ZodOptional<z.ZodNumber>;
120
+ }, z.core.$strip>;
121
+ export declare const ClientMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
122
+ type: z.ZodLiteral<"JOIN">;
123
+ tableId: z.ZodString;
124
+ requestId: z.ZodOptional<z.ZodString>;
125
+ }, z.core.$strip>, z.ZodObject<{
126
+ type: z.ZodLiteral<"LEAVE">;
127
+ tableId: z.ZodString;
128
+ requestId: z.ZodOptional<z.ZodString>;
129
+ }, z.core.$strip>, z.ZodObject<{
130
+ type: z.ZodLiteral<"PING">;
131
+ requestId: z.ZodString;
132
+ timestamp: z.ZodOptional<z.ZodNumber>;
133
+ }, z.core.$strip>], "type">;
134
+ /**
135
+ * Utility to parse and validate a client message
136
+ */
137
+ export declare function parseClientMessage(data: unknown): ClientMessage;
138
+ /**
139
+ * Safe parser that returns result object instead of throwing
140
+ */
141
+ export declare function safeParseClientMessage(data: unknown): {
142
+ success: true;
143
+ data: ClientMessage;
144
+ } | {
145
+ success: false;
146
+ error: z.ZodError;
147
+ };
148
+ export declare const SnapshotMessageSchema: z.ZodObject<{
149
+ type: z.ZodLiteral<"SNAPSHOT">;
150
+ tableId: z.ZodString;
151
+ state: z.ZodRecord<z.ZodString, z.ZodUnknown>;
152
+ version: z.ZodNumber;
153
+ timestamp: z.ZodNumber;
154
+ }, z.core.$strip>;
155
+ export declare const StateUpdateMessageSchema: z.ZodObject<{
156
+ type: z.ZodLiteral<"STATE_UPDATE">;
157
+ tableId: z.ZodString;
158
+ version: z.ZodNumber;
159
+ timestamp: z.ZodNumber;
160
+ }, z.core.$strip>;
161
+ export declare const ErrorMessageSchema: z.ZodObject<{
162
+ type: z.ZodLiteral<"ERROR">;
163
+ code: z.ZodString;
164
+ message: z.ZodString;
165
+ requestId: z.ZodOptional<z.ZodString>;
166
+ context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
167
+ }, z.core.$strip>;
168
+ export declare const AckMessageSchema: z.ZodObject<{
169
+ type: z.ZodLiteral<"ACK">;
170
+ requestId: z.ZodString;
171
+ message: z.ZodOptional<z.ZodString>;
172
+ }, z.core.$strip>;
173
+ export declare const PongMessageSchema: z.ZodObject<{
174
+ type: z.ZodLiteral<"PONG">;
175
+ requestId: z.ZodString;
176
+ timestamp: z.ZodNumber;
177
+ }, z.core.$strip>;
178
+ export declare const ActionNotificationMessageSchema: z.ZodObject<{
179
+ type: z.ZodLiteral<"ACTION">;
180
+ tableId: z.ZodString;
181
+ playerId: z.ZodString;
182
+ actionType: z.ZodString;
183
+ amount: z.ZodOptional<z.ZodNumber>;
184
+ timestamp: z.ZodNumber;
185
+ }, z.core.$strip>;
186
+ export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
187
+ type: z.ZodLiteral<"SNAPSHOT">;
188
+ tableId: z.ZodString;
189
+ state: z.ZodRecord<z.ZodString, z.ZodUnknown>;
190
+ version: z.ZodNumber;
191
+ timestamp: z.ZodNumber;
192
+ }, z.core.$strip>, z.ZodObject<{
193
+ type: z.ZodLiteral<"STATE_UPDATE">;
194
+ tableId: z.ZodString;
195
+ version: z.ZodNumber;
196
+ timestamp: z.ZodNumber;
197
+ }, z.core.$strip>, z.ZodObject<{
198
+ type: z.ZodLiteral<"ERROR">;
199
+ code: z.ZodString;
200
+ message: z.ZodString;
201
+ requestId: z.ZodOptional<z.ZodString>;
202
+ context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
203
+ }, z.core.$strip>, z.ZodObject<{
204
+ type: z.ZodLiteral<"ACK">;
205
+ requestId: z.ZodString;
206
+ message: z.ZodOptional<z.ZodString>;
207
+ }, z.core.$strip>, z.ZodObject<{
208
+ type: z.ZodLiteral<"PONG">;
209
+ requestId: z.ZodString;
210
+ timestamp: z.ZodNumber;
211
+ }, z.core.$strip>, z.ZodObject<{
212
+ type: z.ZodLiteral<"ACTION">;
213
+ tableId: z.ZodString;
214
+ playerId: z.ZodString;
215
+ actionType: z.ZodString;
216
+ amount: z.ZodOptional<z.ZodNumber>;
217
+ timestamp: z.ZodNumber;
218
+ }, z.core.$strip>], "type">;
219
+ /**
220
+ * Parse and validate a server message (throws on invalid)
221
+ */
222
+ export declare function parseServerMessage(data: unknown): ServerMessage;
223
+ /**
224
+ * Safe parser for server messages
225
+ */
226
+ export declare function safeParseServerMessage(data: unknown): {
227
+ success: true;
228
+ data: ServerMessage;
229
+ } | {
230
+ success: false;
231
+ error: z.ZodError;
232
+ };
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServerMessageSchema = exports.ActionNotificationMessageSchema = exports.PongMessageSchema = exports.AckMessageSchema = exports.ErrorMessageSchema = exports.StateUpdateMessageSchema = exports.SnapshotMessageSchema = exports.ClientMessageSchema = exports.PingMessageSchema = exports.LeaveTableMessageSchema = exports.JoinTableMessageSchema = void 0;
4
+ exports.isClientMessage = isClientMessage;
5
+ exports.isJoinMessage = isJoinMessage;
6
+ exports.isLeaveMessage = isLeaveMessage;
7
+ exports.isPingMessage = isPingMessage;
8
+ exports.isServerMessage = isServerMessage;
9
+ exports.parseClientMessage = parseClientMessage;
10
+ exports.safeParseClientMessage = safeParseClientMessage;
11
+ exports.parseServerMessage = parseServerMessage;
12
+ exports.safeParseServerMessage = safeParseServerMessage;
13
+ // ============================================================================
14
+ // Type Guards
15
+ // ============================================================================
16
+ function isClientMessage(msg) {
17
+ if (typeof msg !== "object" || msg === null)
18
+ return false;
19
+ const { type } = msg;
20
+ return type === "JOIN" || type === "LEAVE" || type === "PING";
21
+ }
22
+ function isJoinMessage(msg) {
23
+ return msg.type === "JOIN";
24
+ }
25
+ function isLeaveMessage(msg) {
26
+ return msg.type === "LEAVE";
27
+ }
28
+ function isPingMessage(msg) {
29
+ return msg.type === "PING";
30
+ }
31
+ function isServerMessage(msg) {
32
+ if (typeof msg !== "object" || msg === null)
33
+ return false;
34
+ const { type } = msg;
35
+ return (type === "SNAPSHOT" ||
36
+ type === "STATE_UPDATE" ||
37
+ type === "ERROR" ||
38
+ type === "ACK" ||
39
+ type === "PONG" ||
40
+ type === "ACTION");
41
+ }
42
+ // ============================================================================
43
+ // Zod Schemas for Runtime Validation
44
+ // ============================================================================
45
+ const zod_1 = require("zod");
46
+ exports.JoinTableMessageSchema = zod_1.z.object({
47
+ type: zod_1.z.literal("JOIN"),
48
+ tableId: zod_1.z.string().min(1),
49
+ requestId: zod_1.z.string().optional(),
50
+ });
51
+ exports.LeaveTableMessageSchema = zod_1.z.object({
52
+ type: zod_1.z.literal("LEAVE"),
53
+ tableId: zod_1.z.string().min(1),
54
+ requestId: zod_1.z.string().optional(),
55
+ });
56
+ exports.PingMessageSchema = zod_1.z.object({
57
+ type: zod_1.z.literal("PING"),
58
+ requestId: zod_1.z.string().min(1),
59
+ timestamp: zod_1.z.number().optional(),
60
+ });
61
+ exports.ClientMessageSchema = zod_1.z.discriminatedUnion("type", [
62
+ exports.JoinTableMessageSchema,
63
+ exports.LeaveTableMessageSchema,
64
+ exports.PingMessageSchema,
65
+ ]);
66
+ /**
67
+ * Utility to parse and validate a client message
68
+ */
69
+ function parseClientMessage(data) {
70
+ return exports.ClientMessageSchema.parse(data);
71
+ }
72
+ /**
73
+ * Safe parser that returns result object instead of throwing
74
+ */
75
+ function safeParseClientMessage(data) {
76
+ const result = exports.ClientMessageSchema.safeParse(data);
77
+ if (result.success) {
78
+ return { success: true, data: result.data };
79
+ }
80
+ return { success: false, error: result.error };
81
+ }
82
+ // ============================================================================
83
+ // Server Message Zod Schemas
84
+ // ============================================================================
85
+ exports.SnapshotMessageSchema = zod_1.z.object({
86
+ type: zod_1.z.literal("SNAPSHOT"),
87
+ tableId: zod_1.z.string().min(1),
88
+ state: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
89
+ version: zod_1.z.number(),
90
+ timestamp: zod_1.z.number(),
91
+ });
92
+ exports.StateUpdateMessageSchema = zod_1.z.object({
93
+ type: zod_1.z.literal("STATE_UPDATE"),
94
+ tableId: zod_1.z.string().min(1),
95
+ version: zod_1.z.number(),
96
+ timestamp: zod_1.z.number(),
97
+ });
98
+ exports.ErrorMessageSchema = zod_1.z.object({
99
+ type: zod_1.z.literal("ERROR"),
100
+ code: zod_1.z.string(),
101
+ message: zod_1.z.string(),
102
+ requestId: zod_1.z.string().optional(),
103
+ context: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional(),
104
+ });
105
+ exports.AckMessageSchema = zod_1.z.object({
106
+ type: zod_1.z.literal("ACK"),
107
+ requestId: zod_1.z.string(),
108
+ message: zod_1.z.string().optional(),
109
+ });
110
+ exports.PongMessageSchema = zod_1.z.object({
111
+ type: zod_1.z.literal("PONG"),
112
+ requestId: zod_1.z.string(),
113
+ timestamp: zod_1.z.number(),
114
+ });
115
+ exports.ActionNotificationMessageSchema = zod_1.z.object({
116
+ type: zod_1.z.literal("ACTION"),
117
+ tableId: zod_1.z.string().min(1),
118
+ playerId: zod_1.z.string(),
119
+ actionType: zod_1.z.string(),
120
+ amount: zod_1.z.number().optional(),
121
+ timestamp: zod_1.z.number(),
122
+ });
123
+ exports.ServerMessageSchema = zod_1.z.discriminatedUnion("type", [
124
+ exports.SnapshotMessageSchema,
125
+ exports.StateUpdateMessageSchema,
126
+ exports.ErrorMessageSchema,
127
+ exports.AckMessageSchema,
128
+ exports.PongMessageSchema,
129
+ exports.ActionNotificationMessageSchema,
130
+ ]);
131
+ /**
132
+ * Parse and validate a server message (throws on invalid)
133
+ */
134
+ function parseServerMessage(data) {
135
+ return exports.ServerMessageSchema.parse(data);
136
+ }
137
+ /**
138
+ * Safe parser for server messages
139
+ */
140
+ function safeParseServerMessage(data) {
141
+ const result = exports.ServerMessageSchema.safeParse(data);
142
+ if (result.success) {
143
+ return { success: true, data: result.data };
144
+ }
145
+ return { success: false, error: result.error };
146
+ }
@@ -0,0 +1,14 @@
1
+ export interface LoginRequest {
2
+ message: string;
3
+ signature: `0x${string}`;
4
+ }
5
+ export interface LoginResponse {
6
+ token: string;
7
+ user: {
8
+ id: string;
9
+ username: string;
10
+ };
11
+ }
12
+ export interface NonceResponse {
13
+ nonce: string;
14
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ export interface ApiErrorResponse {
2
+ error: string;
3
+ message?: string;
4
+ code?: string;
5
+ }
6
+ export interface SuccessResponse {
7
+ success: true;
8
+ }
9
+ export type GameMode = "CASH" | "TOURNAMENT";
10
+ export type TableStatus = "WAITING" | "ACTIVE" | "FINISHED";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ import { TableConfig } from "../Config";
2
+ import { PublicState } from "../PublicState";
3
+ import { TableStatus } from "./common";
4
+ export interface TableListItem {
5
+ id: string;
6
+ name: string;
7
+ config: TableConfig;
8
+ status: TableStatus;
9
+ }
10
+ export interface GetTablesResponse {
11
+ tables: TableListItem[];
12
+ }
13
+ export interface StandRequest {
14
+ }
15
+ export interface TableStateResponse {
16
+ state: PublicState;
17
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/index.d.ts CHANGED
@@ -2,6 +2,13 @@ export * from "./Player";
2
2
  export * from "./Pot";
3
3
  export * from "./Config";
4
4
  export * from "./Action";
5
+ export * from "./ActionWhitelist";
5
6
  export * from "./GameState";
6
7
  export * from "./PublicState";
7
8
  export * from "./HandHistory";
9
+ export * from "./schemas";
10
+ export * from "./WebSocketMessages";
11
+ export * from "./ErrorCodes";
12
+ export * from "./api/common";
13
+ export * from "./api/auth";
14
+ export * from "./api/tables";
package/dist/index.js CHANGED
@@ -19,6 +19,14 @@ __exportStar(require("./Player"), exports);
19
19
  __exportStar(require("./Pot"), exports);
20
20
  __exportStar(require("./Config"), exports);
21
21
  __exportStar(require("./Action"), exports);
22
+ __exportStar(require("./ActionWhitelist"), exports);
22
23
  __exportStar(require("./GameState"), exports);
23
24
  __exportStar(require("./PublicState"), exports);
24
25
  __exportStar(require("./HandHistory"), exports);
26
+ __exportStar(require("./schemas"), exports);
27
+ __exportStar(require("./WebSocketMessages"), exports);
28
+ __exportStar(require("./ErrorCodes"), exports);
29
+ // API DTOs
30
+ __exportStar(require("./api/common"), exports);
31
+ __exportStar(require("./api/auth"), exports);
32
+ __exportStar(require("./api/tables"), exports);