@websimai/socket-types 0.0.1 → 0.0.2

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 CHANGED
@@ -1,3 +1,3 @@
1
1
  # @websimai/socket-types
2
2
 
3
- WebsimSocket type declarations
3
+ Type declarations for the `WebsimSocket` class
package/package.json CHANGED
@@ -1,20 +1,17 @@
1
1
  {
2
2
  "name": "@websimai/socket-types",
3
- "version": "0.0.1",
4
- "description": "WebsimSocket type declarations",
3
+ "version": "0.0.2",
4
+ "description": "Type declarations for the `WebsimSocket` class",
5
5
  "author": "GameRoMan",
6
6
  "license": "MIT",
7
7
  "types": "./src/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
10
  "types": "./src/index.d.ts"
11
- },
12
- "./socket": {
13
- "types": "./src/socket.d.ts"
14
11
  }
15
12
  },
16
13
  "scripts": {
17
- "publish": "bun publish --access public"
14
+ "npm:publish": "bun publish --access public"
18
15
  },
19
16
  "keywords": [
20
17
  "websim",
@@ -0,0 +1,13 @@
1
+ import { WebsimSocket as WebsimSocketClass } from "./websim-socket";
2
+
3
+ type WebsimSocketConstructor = typeof WebsimSocketClass;
4
+
5
+ declare global {
6
+ interface Window {
7
+ readonly WebsimSocket: WebsimSocketConstructor;
8
+ }
9
+
10
+ const WebsimSocket: WebsimSocketConstructor;
11
+
12
+ type WebsimSocket = WebsimSocketClass;
13
+ }
package/src/index.d.ts CHANGED
@@ -1,15 +1,4 @@
1
- import type { WebsimSocket as WebsimSocketClass } from "./socket";
1
+ export type { WebsimSocket } from "./websim-socket";
2
+ export type * from "./types";
2
3
 
3
- type WebsimSocketConstructor = typeof WebsimSocketClass;
4
-
5
- declare global {
6
- interface Window {
7
- readonly WebsimSocket: WebsimSocketConstructor;
8
- }
9
-
10
- const WebsimSocket: WebsimSocketConstructor;
11
- }
12
-
13
- type WebsimSocket = InstanceType<WebsimSocketConstructor>;
14
-
15
- export { WebsimSocket };
4
+ export type * from "./globals";
@@ -0,0 +1,70 @@
1
+ import type { KeyValue, Expand } from "./utils";
2
+
3
+ export interface CollectionAPI<T extends string> {
4
+ getList: <TData extends KeyValue>() => Expand<
5
+ TData & {
6
+ readonly id: string;
7
+ readonly $type: T;
8
+ readonly created_at: string;
9
+ readonly updated_at: string;
10
+ readonly user_id: string;
11
+ readonly username: string;
12
+ }
13
+ >[];
14
+ create: <TData extends KeyValue>(
15
+ data: TData
16
+ ) => Promise<
17
+ Expand<
18
+ TData & {
19
+ readonly id: string;
20
+ readonly $type: T;
21
+ readonly created_at: string;
22
+ readonly username: string;
23
+ }
24
+ >
25
+ >;
26
+ update: <T_Id extends string, TData extends KeyValue>(
27
+ id: T_Id,
28
+ data: TData
29
+ ) => Promise<
30
+ Expand<
31
+ TData & {
32
+ readonly id: T_Id;
33
+ readonly $type: T;
34
+ readonly created_at: string;
35
+ readonly username: string;
36
+ }
37
+ >
38
+ >;
39
+ upsert: <
40
+ TData extends KeyValue & { id?: T_Id },
41
+ T_Id extends string = string
42
+ >(
43
+ data: TData
44
+ ) => Promise<
45
+ Expand<
46
+ (TData extends { id: T_Id } ? TData & KeyValue : TData) & {
47
+ readonly id: T_Id;
48
+ readonly $type: T;
49
+ readonly created_at: string;
50
+ readonly username: string;
51
+ }
52
+ >
53
+ >;
54
+ delete: (id: string) => Promise<void>;
55
+ subscribe: (
56
+ callback: (
57
+ records: Expand<
58
+ KeyValue & {
59
+ id: string;
60
+ $type: T;
61
+ created_at: string;
62
+ updated_at: string;
63
+ user_id: string;
64
+ username: string;
65
+ }
66
+ >[]
67
+ ) => void
68
+ ) => () => void;
69
+ filter: (filters: KeyValue) => CollectionAPI<T>;
70
+ }
@@ -0,0 +1,2 @@
1
+ export type * from "./collection-api";
2
+ export type * from "./query-api";
@@ -0,0 +1,7 @@
1
+ import type { KeyValue } from "./utils";
2
+
3
+ export interface QueryAPI<T extends readonly KeyValue[] = readonly KeyValue[]>
4
+ extends PromiseLike<T> {
5
+ getList(): Promise<T>;
6
+ subscribe(callback: (records: T) => void): () => void;
7
+ }
@@ -0,0 +1,3 @@
1
+ export type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
2
+
3
+ export type KeyValue = Record<string, any>;
@@ -0,0 +1,61 @@
1
+ import type { KeyValue } from "./types/utils";
2
+
3
+ export interface WebsimSocketParty {
4
+ /**
5
+ * Object containing the client ID, avatar URL, and username of this connected client.
6
+ */
7
+ readonly client: {
8
+ readonly id: string;
9
+ readonly avatarUrl: `https://${string}/${string}`;
10
+ readonly username: string;
11
+ };
12
+
13
+ /**
14
+ * Object containing all connected peers, including this client.
15
+ * This is always up-to-date.
16
+ */
17
+ readonly peers: {
18
+ [id: string]: {
19
+ readonly avatarUrl: `https://${string}/${string}`;
20
+ readonly username: string;
21
+ readonly id: string;
22
+ readonly is_anonymous: boolean;
23
+ };
24
+ };
25
+
26
+ subscribe: (
27
+ callback: (peers: {
28
+ [clientId: string]: {
29
+ avatarUrl: string;
30
+ username: string;
31
+ };
32
+ }) => void
33
+ ) => () => void;
34
+
35
+ /**
36
+ * Object containing the current presence state of all connected peers, including this client.
37
+ * This is always up-to-date after initialization.
38
+ */
39
+ readonly presence: KeyValue;
40
+
41
+ /**
42
+ * Updates the current client's presence state.
43
+ * @param presence The new presence state to set.
44
+ */
45
+ updatePresence<TPresence extends KeyValue>(presence: TPresence): void;
46
+
47
+ /**
48
+ * Subscribe to presence updates from all peers.
49
+ * @param callback Function to call when presence changes.
50
+ * @returns Function to unsubscribe.
51
+ */
52
+ subscribePresence<TPresence extends KeyValue>(
53
+ callback: (presence: { [clientId: string]: TPresence }) => void
54
+ ): () => void;
55
+
56
+ /**
57
+ * Object containing the current room-wide state.
58
+ * This is always up-to-date.
59
+ */
60
+ readonly roomState: KeyValue;
61
+ }
@@ -0,0 +1,134 @@
1
+ import type { KeyValue } from "./types/utils";
2
+
3
+ import type { WebsimSocketParty } from "./websim-socket-party";
4
+
5
+ import type { CollectionAPI, QueryAPI } from "./types";
6
+
7
+ export class WebsimSocket {
8
+ constructor();
9
+
10
+ readonly CONNECTING: 0;
11
+ readonly OPEN: 1;
12
+ readonly CLOSING: 2;
13
+ readonly CLOSED: 3;
14
+
15
+ readonly binaryType: "arraybuffer" | (string & {});
16
+ readonly bufferedAmount: number;
17
+ readonly extensions: string;
18
+ readonly protocol: "ws" | "wss";
19
+ readonly readyState: 0 | 1 | 2 | 3;
20
+ readonly url: string;
21
+
22
+ accept(): void;
23
+ serializeAttachment(): void;
24
+ deserializeAttachment(): void;
25
+
26
+ onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
27
+ onerror: ((this: WebSocket, ev: Event) => any) | null;
28
+ onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
29
+ onopen: ((this: WebSocket, ev: Event) => any) | null;
30
+
31
+ dispatchEvent(event: Event | MessageEvent | CloseEvent | ErrorEvent): boolean;
32
+
33
+ close(_code?: number, _reason?: string): void;
34
+ send<TData extends string | object>(data: TData): void;
35
+
36
+ query(queryString: string, params?: any[]): QueryAPI;
37
+
38
+ collection<T extends string>($type: T): CollectionAPI<T>;
39
+
40
+ readonly clientId: string;
41
+
42
+ /**
43
+ * Object containing information about the connected client and their peers.
44
+ */
45
+ readonly party: WebsimSocketParty;
46
+
47
+ /**
48
+ * Legacy event handler for changes in connected peers.
49
+ * @param peers An object with client IDs as keys, each containing the client's avatar URL and username.
50
+ */
51
+ onPeersChanged:
52
+ | ((peers: {
53
+ [clientId: string]: {
54
+ avatarUrl: string;
55
+ username: string;
56
+ };
57
+ }) => any)
58
+ | null;
59
+
60
+ /**
61
+ * Initialize the WebSocket connection.
62
+ * @returns A promise that resolves when initialization is complete.
63
+ */
64
+ initialize(): Promise<void>;
65
+
66
+ /**
67
+ * Request a presence update from a specific client.
68
+ * @param clientId The ID of the client to request an update from.
69
+ * @param update The update to request.
70
+ */
71
+ requestPresenceUpdate(clientId: string, update: KeyValue): void;
72
+
73
+ /**
74
+ * Subscribe to presence update requests from other clients.
75
+ * @param callback Function to call when a presence update is requested.
76
+ * @returns Function to unsubscribe.
77
+ */
78
+ subscribePresenceUpdateRequests<TUpdateRequest extends KeyValue>(
79
+ callback: (updateRequest: TUpdateRequest, fromClientId: string) => void
80
+ ): () => void;
81
+
82
+ /**
83
+ * Updates the room-wide state. This merges with existing state.
84
+ * @param delta The new state to merge with current room state.
85
+ */
86
+ updateRoomState<TDelta extends KeyValue>(delta: TDelta): void;
87
+
88
+ /**
89
+ * Subscribe to room state updates.
90
+ * @param callback Function to call when room state changes.
91
+ * @returns Function to unsubscribe.
92
+ */
93
+ subscribeRoomState<TRoomState extends KeyValue>(
94
+ callback: (state: TRoomState) => void
95
+ ): () => void;
96
+
97
+ /**
98
+ * Object containing the current presence state of all connected peers, including this client.
99
+ * This is always up-to-date after initialization.
100
+ */
101
+ readonly presence: { [clientId: string]: KeyValue };
102
+
103
+ /**
104
+ * Object containing the current room-wide state.
105
+ * This is always up-to-date.
106
+ */
107
+ readonly roomState: KeyValue;
108
+
109
+ /**
110
+ * Object containing all connected peers, including this client.
111
+ * This is always up-to-date.
112
+ */
113
+ readonly peers: {
114
+ [clientId: string]: {
115
+ avatarUrl: string;
116
+ username: string;
117
+ };
118
+ };
119
+
120
+ /**
121
+ * Updates the current client's presence state.
122
+ * @param presence The new presence state to set.
123
+ */
124
+ updatePresence<TPresence extends KeyValue>(presence: TPresence): void;
125
+
126
+ /**
127
+ * Subscribe to presence updates from all peers.
128
+ * @param callback Function to call when presence changes.
129
+ * @returns Function to unsubscribe.
130
+ */
131
+ subscribePresence<TPresence extends KeyValue>(
132
+ callback: (presence: { [clientId: string]: TPresence }) => void
133
+ ): () => void;
134
+ }
package/tsconfig.json CHANGED
@@ -11,9 +11,12 @@
11
11
  "allowImportingTsExtensions": true,
12
12
  "noEmit": true,
13
13
 
14
+ // This is a type declarations package
15
+ "skipLibCheck": false,
16
+ "skipDefaultLibCheck": true,
17
+
14
18
  // Max strictness
15
19
  "strict": true,
16
- "skipLibCheck": true,
17
20
  "noFallthroughCasesInSwitch": true,
18
21
  "noUncheckedIndexedAccess": true,
19
22
  "noImplicitOverride": true,
package/src/socket.d.ts DELETED
@@ -1,271 +0,0 @@
1
- type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
2
-
3
- type KeyValue = Record<string, any>;
4
-
5
- interface WebsimSocketParty {
6
- /**
7
- * Object containing the client ID, avatar URL, and username of this connected client.
8
- */
9
- client: {
10
- id: string;
11
- avatarUrl: `https://${string}`;
12
- username: string;
13
- };
14
-
15
- /**
16
- * Object containing all connected peers, including this client.
17
- * This is always up-to-date.
18
- */
19
- peers: {
20
- [id: string]: {
21
- avatarUrl: `https://${string}`;
22
- username: string;
23
- id: string;
24
- is_anonymous: boolean;
25
- };
26
- };
27
-
28
- subscribe: (
29
- callback: (peers: {
30
- [clientId: string]: { avatarUrl: string; username: string };
31
- }) => void
32
- ) => () => void;
33
-
34
- /**
35
- * Object containing the current presence state of all connected peers, including this client.
36
- * This is always up-to-date after initialization.
37
- */
38
- presence: KeyValue;
39
-
40
- /**
41
- * Updates the current client's presence state.
42
- * @param presence The new presence state to set.
43
- */
44
- updatePresence<TPresence extends KeyValue>(presence: TPresence): void;
45
-
46
- /**
47
- * Subscribe to presence updates from all peers.
48
- * @param callback Function to call when presence changes.
49
- * @returns Function to unsubscribe.
50
- */
51
- subscribePresence<TPresence extends KeyValue>(
52
- callback: (presence: { [clientId: string]: TPresence }) => void
53
- ): () => void;
54
-
55
- /**
56
- * Object containing the current room-wide state.
57
- * This is always up-to-date.
58
- */
59
- roomState: KeyValue;
60
- }
61
-
62
- interface CollectionAPI<T extends string> {
63
- getList: <TData extends KeyValue>() => Promise<
64
- Expand<
65
- TData & {
66
- id: string;
67
- $type: T;
68
- created_at: string;
69
- updated_at: string;
70
- user_id: string;
71
- username: string;
72
- }
73
- >[]
74
- >;
75
- create: <TData extends KeyValue>(
76
- data: TData
77
- ) => Promise<
78
- Expand<
79
- TData & {
80
- id: string;
81
- $type: T;
82
- created_at: string;
83
- username: string;
84
- }
85
- >
86
- >;
87
- update: <T_Id extends string, TData extends KeyValue>(
88
- id: T_Id,
89
- data: TData
90
- ) => Promise<
91
- Expand<
92
- TData & {
93
- id: T_Id;
94
- $type: T;
95
- created_at: string;
96
- username: string;
97
- }
98
- >
99
- >;
100
- upsert: <
101
- TData extends KeyValue & { id?: T_Id },
102
- T_Id extends string = string
103
- >(
104
- data: TData
105
- ) => Promise<
106
- Expand<
107
- TData extends { id: T_Id }
108
- ? TData &
109
- KeyValue & {
110
- id: T_Id;
111
- $type: T;
112
- created_at: string;
113
- username: string;
114
- }
115
- : TData & {
116
- id: T_Id;
117
- $type: T;
118
- created_at: string;
119
- username: string;
120
- }
121
- >
122
- >;
123
- delete: (id: string) => Promise<void>;
124
- subscribe: (
125
- callback: (
126
- records: Expand<
127
- KeyValue & {
128
- id: string;
129
- $type: T;
130
- created_at: string;
131
- updated_at: string;
132
- user_id: string;
133
- username: string;
134
- }
135
- >[]
136
- ) => void
137
- ) => () => void;
138
- filter: (filters: KeyValue) => CollectionAPI<T>;
139
- }
140
-
141
- interface QueryAPI<T extends readonly KeyValue[] = KeyValue[]>
142
- extends PromiseLike<T> {
143
- getList: () => Promise<T>;
144
- subscribe: (callback: (records: T) => void) => () => void;
145
- }
146
-
147
- export class WebsimSocket {
148
- constructor();
149
-
150
- readonly CONNECTING: 0;
151
- readonly OPEN: 1;
152
- readonly CLOSING: 2;
153
- readonly CLOSED: 3;
154
-
155
- readonly binaryType: "arraybuffer" | (string & {});
156
- readonly bufferedAmount: number;
157
- readonly extensions: string;
158
- readonly protocol: "ws" | "wss";
159
- readonly readyState: 0 | 1 | 2 | 3;
160
- readonly url: string;
161
-
162
- accept(): void;
163
- serializeAttachment(): void;
164
- deserializeAttachment(): void;
165
-
166
- onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
167
- onerror: ((this: WebSocket, ev: Event) => any) | null;
168
- onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
169
- onopen: ((this: WebSocket, ev: Event) => any) | null;
170
-
171
- dispatchEvent(event: Event | MessageEvent | CloseEvent | ErrorEvent): boolean;
172
-
173
- close(_code?: number, _reason?: string): void;
174
- send<TData extends string | object>(data: TData): void;
175
-
176
- query(queryString: string, params?: any[]): QueryAPI;
177
-
178
- collection<T extends string>($type: T): CollectionAPI<T>;
179
-
180
- clientId: string;
181
-
182
- /**
183
- * Object containing information about the connected client and their peers.
184
- */
185
- party: WebsimSocketParty;
186
-
187
- /**
188
- * Legacy event handler for changes in connected peers.
189
- * @param peers An object with client IDs as keys, each containing the client's avatar URL and username.
190
- */
191
- onPeersChanged:
192
- | ((peers: {
193
- [clientId: string]: { avatarUrl: string; username: string };
194
- }) => any)
195
- | null;
196
-
197
- /**
198
- * Initialize the WebSocket connection.
199
- * @returns A promise that resolves when initialization is complete.
200
- */
201
- initialize(): Promise<void>;
202
-
203
- /**
204
- * Request a presence update from a specific client.
205
- * @param clientId The ID of the client to request an update from.
206
- * @param update The update to request.
207
- */
208
- requestPresenceUpdate(clientId: string, update: KeyValue): void;
209
-
210
- /**
211
- * Subscribe to presence update requests from other clients.
212
- * @param callback Function to call when a presence update is requested.
213
- * @returns Function to unsubscribe.
214
- */
215
- subscribePresenceUpdateRequests<TUpdateRequest extends KeyValue>(
216
- callback: (updateRequest: TUpdateRequest, fromClientId: string) => void
217
- ): () => void;
218
-
219
- /**
220
- * Updates the room-wide state. This merges with existing state.
221
- * @param delta The new state to merge with current room state.
222
- */
223
- updateRoomState<TDelta extends KeyValue>(delta: TDelta): void;
224
-
225
- /**
226
- * Subscribe to room state updates.
227
- * @param callback Function to call when room state changes.
228
- * @returns Function to unsubscribe.
229
- */
230
- subscribeRoomState<TRoomState extends KeyValue>(
231
- callback: (state: TRoomState) => void
232
- ): () => void;
233
-
234
- /**
235
- * Object containing the current presence state of all connected peers, including this client.
236
- * This is always up-to-date after initialization.
237
- */
238
- presence: { [clientId: string]: KeyValue };
239
-
240
- /**
241
- * Object containing the current room-wide state.
242
- * This is always up-to-date.
243
- */
244
- roomState: KeyValue;
245
-
246
- /**
247
- * Object containing all connected peers, including this client.
248
- * This is always up-to-date.
249
- */
250
- peers: {
251
- [clientId: string]: {
252
- avatarUrl: string;
253
- username: string;
254
- };
255
- };
256
-
257
- /**
258
- * Updates the current client's presence state.
259
- * @param presence The new presence state to set.
260
- */
261
- updatePresence<TPresence extends KeyValue>(presence: TPresence): void;
262
-
263
- /**
264
- * Subscribe to presence updates from all peers.
265
- * @param callback Function to call when presence changes.
266
- * @returns Function to unsubscribe.
267
- */
268
- subscribePresence<TPresence extends KeyValue>(
269
- callback: (presence: { [clientId: string]: TPresence }) => void
270
- ): () => void;
271
- }