@websimai/socket-types 0.0.1 → 0.0.3

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
@@ -1,271 +1,215 @@
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
- }
1
+ //#region src/types/utils/index.d.ts
2
+ type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
3
+ type KeyValue = Record<string, any>;
4
+ //#endregion
5
+ //#region src/types/collection-api.d.ts
6
+ interface CollectionAPI<T extends string> {
7
+ getList: <TData extends KeyValue>() => Expand<TData & {
8
+ readonly id: string;
9
+ readonly $type: T;
10
+ readonly created_at: string;
11
+ readonly updated_at: string;
12
+ readonly user_id: string;
13
+ readonly username: string;
14
+ }>[];
15
+ create: <TData extends KeyValue>(data: TData) => Promise<Expand<TData & {
16
+ readonly id: string;
17
+ readonly $type: T;
18
+ readonly created_at: string;
19
+ readonly username: string;
20
+ }>>;
21
+ update: <T_Id extends string, TData extends KeyValue>(id: T_Id, data: TData) => Promise<Expand<TData & {
22
+ readonly id: T_Id;
23
+ readonly $type: T;
24
+ readonly created_at: string;
25
+ readonly username: string;
26
+ }>>;
27
+ upsert: <TData extends KeyValue & {
28
+ id?: T_Id;
29
+ }, T_Id extends string = string>(data: TData) => Promise<Expand<(TData extends {
30
+ id: T_Id;
31
+ } ? TData & KeyValue : TData) & {
32
+ readonly id: T_Id;
33
+ readonly $type: T;
34
+ readonly created_at: string;
35
+ readonly username: string;
36
+ }>>;
37
+ delete: (id: string) => Promise<void>;
38
+ subscribe: (callback: (records: Expand<KeyValue & {
39
+ id: string;
40
+ $type: T;
41
+ created_at: string;
42
+ updated_at: string;
43
+ user_id: string;
44
+ username: string;
45
+ }>[]) => void) => () => void;
46
+ filter: (filters: KeyValue) => CollectionAPI<T>;
47
+ }
48
+ //#endregion
49
+ //#region src/types/peers.d.ts
50
+ type Peers = {
51
+ readonly [clientId: string]: {
52
+ readonly avatarUrl: string;
53
+ readonly username: string;
54
+ };
55
+ };
56
+ //#endregion
57
+ //#region src/types/query-api.d.ts
58
+ interface QueryAPI<T extends readonly KeyValue[] = readonly KeyValue[]> extends PromiseLike<T> {
59
+ getList(): Promise<T>;
60
+ subscribe(callback: (records: T) => void): () => void;
61
+ }
62
+ //#endregion
63
+ //#region src/websim-socket-party.d.ts
64
+ interface WebsimSocketParty {
65
+ /**
66
+ * Object containing the client ID, avatar URL, and username of this connected client.
67
+ */
68
+ readonly client: {
69
+ readonly id: string;
70
+ readonly avatarUrl: `https://${string}/${string}`;
71
+ readonly username: string;
72
+ };
73
+ /**
74
+ * Object containing all connected peers, including this client.
75
+ * This is always up-to-date.
76
+ */
77
+ readonly peers: {
78
+ [id: string]: {
79
+ readonly avatarUrl: `https://${string}/${string}`;
80
+ readonly username: string;
81
+ readonly id: string;
82
+ readonly is_anonymous: boolean;
83
+ };
84
+ };
85
+ subscribe: (callback: (peers: Peers) => void) => () => void;
86
+ /**
87
+ * Object containing the current presence state of all connected peers, including this client.
88
+ * This is always up-to-date after initialization.
89
+ */
90
+ readonly presence: KeyValue;
91
+ /**
92
+ * Updates the current client's presence state.
93
+ * @param presence The new presence state to set.
94
+ */
95
+ updatePresence<TPresence extends KeyValue>(presence: TPresence): void;
96
+ /**
97
+ * Subscribe to presence updates from all peers.
98
+ * @param callback Function to call when presence changes.
99
+ * @returns Function to unsubscribe.
100
+ */
101
+ subscribePresence<TPresence extends KeyValue>(callback: (presence: {
102
+ [clientId: string]: TPresence;
103
+ }) => void): () => void;
104
+ /**
105
+ * Object containing the current room-wide state.
106
+ * This is always up-to-date.
107
+ */
108
+ readonly roomState: KeyValue;
109
+ }
110
+ //#endregion
111
+ //#region src/websim-socket.d.ts
112
+ declare class WebsimSocketClass {
113
+ constructor();
114
+ readonly CONNECTING: 0;
115
+ readonly OPEN: 1;
116
+ readonly CLOSING: 2;
117
+ readonly CLOSED: 3;
118
+ readonly binaryType: "arraybuffer" | (string & {});
119
+ readonly bufferedAmount: number;
120
+ readonly extensions: string;
121
+ readonly protocol: "ws" | "wss";
122
+ readonly readyState: 0 | 1 | 2 | 3;
123
+ readonly url: string;
124
+ accept(): void;
125
+ serializeAttachment(): void;
126
+ deserializeAttachment(): void;
127
+ onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
128
+ onerror: ((this: WebSocket, ev: Event) => any) | null;
129
+ onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
130
+ onopen: ((this: WebSocket, ev: Event) => any) | null;
131
+ dispatchEvent(event: Event | MessageEvent | CloseEvent | ErrorEvent): boolean;
132
+ close(_code?: number, _reason?: string): void;
133
+ send<TData extends string | object>(data: TData): void;
134
+ query<TParams extends unknown = unknown>(queryString: string, params?: TParams[]): QueryAPI;
135
+ collection<T extends string>($type: T): CollectionAPI<T>;
136
+ readonly clientId: string;
137
+ /**
138
+ * Object containing information about the connected client and their peers.
139
+ */
140
+ readonly party: WebsimSocketParty;
141
+ /**
142
+ * Legacy event handler for changes in connected peers.
143
+ * @param peers An object with client IDs as keys, each containing the client's avatar URL and username.
144
+ */
145
+ onPeersChanged: ((peers: Peers) => any) | null;
146
+ /**
147
+ * Initialize the WebSocket connection.
148
+ * @returns A promise that resolves when initialization is complete.
149
+ */
150
+ initialize(): Promise<void>;
151
+ /**
152
+ * Request a presence update from a specific client.
153
+ * @param clientId The ID of the client to request an update from.
154
+ * @param update The update to request.
155
+ */
156
+ requestPresenceUpdate(clientId: string, update: KeyValue): void;
157
+ /**
158
+ * Subscribe to presence update requests from other clients.
159
+ * @param callback Function to call when a presence update is requested.
160
+ * @returns Function to unsubscribe.
161
+ */
162
+ subscribePresenceUpdateRequests<TUpdateRequest extends KeyValue>(callback: (updateRequest: TUpdateRequest, fromClientId: string) => void): () => void;
163
+ /**
164
+ * Updates the room-wide state. This merges with existing state.
165
+ * @param delta The new state to merge with current room state.
166
+ */
167
+ updateRoomState<TDelta extends KeyValue>(delta: TDelta): void;
168
+ /**
169
+ * Subscribe to room state updates.
170
+ * @param callback Function to call when room state changes.
171
+ * @returns Function to unsubscribe.
172
+ */
173
+ subscribeRoomState<TRoomState extends KeyValue>(callback: (state: TRoomState) => void): () => void;
174
+ /**
175
+ * Object containing the current presence state of all connected peers, including this client.
176
+ * This is always up-to-date after initialization.
177
+ */
178
+ readonly presence: {
179
+ [clientId: string]: KeyValue;
180
+ };
181
+ /**
182
+ * Object containing the current room-wide state.
183
+ * This is always up-to-date.
184
+ */
185
+ readonly roomState: KeyValue;
186
+ /**
187
+ * Object containing all connected peers, including this client.
188
+ * This is always up-to-date.
189
+ */
190
+ readonly peers: Peers;
191
+ /**
192
+ * Updates the current client's presence state.
193
+ * @param presence The new presence state to set.
194
+ */
195
+ updatePresence<TPresence extends KeyValue>(presence: TPresence): void;
196
+ /**
197
+ * Subscribe to presence updates from all peers.
198
+ * @param callback Function to call when presence changes.
199
+ * @returns Function to unsubscribe.
200
+ */
201
+ subscribePresence<TPresence extends KeyValue>(callback: (presence: {
202
+ [clientId: string]: TPresence;
203
+ }) => void): () => void;
204
+ }
205
+ //#endregion
206
+ //#region src/globals.d.ts
207
+ type WebsimSocketConstructor = typeof WebsimSocketClass;
208
+ declare global {
209
+ interface Window {
210
+ readonly WebsimSocket: WebsimSocketConstructor;
211
+ }
212
+ const WebsimSocket: WebsimSocketConstructor;
213
+ }
214
+ //#endregion
215
+ export { type CollectionAPI, type QueryAPI, type WebsimSocketClass };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ export { };
package/package.json CHANGED
@@ -1,27 +1,40 @@
1
1
  {
2
2
  "name": "@websimai/socket-types",
3
- "version": "0.0.1",
4
- "description": "WebsimSocket type declarations",
3
+ "version": "0.0.3",
4
+ "description": "Type declarations for the `WebsimSocket` class",
5
+ "type": "module",
5
6
  "author": "GameRoMan",
6
7
  "license": "MIT",
7
- "types": "./src/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./src/index.d.ts"
11
- },
12
- "./socket": {
13
- "types": "./src/socket.d.ts"
14
- }
8
+ "publishConfig": {
9
+ "access": "public"
15
10
  },
16
- "scripts": {
17
- "publish": "bun publish --access public"
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/websimnpm/websim.git",
14
+ "directory": "packages/socket-api-types"
18
15
  },
19
16
  "keywords": [
20
17
  "websim",
21
18
  "types",
22
19
  "typescript"
23
20
  ],
24
- "peerDependencies": {
25
- "typescript": "^5.9.2"
21
+ "scripts": {
22
+ "build": "bunx --bun tsdown",
23
+ "npm:publish": "bun run build && bun publish"
24
+ },
25
+ "files": [
26
+ "dist/**/*",
27
+ "README.md",
28
+ "LICENSE"
29
+ ],
30
+ "main": "./dist/index.mjs",
31
+ "module": "./dist/index.mjs",
32
+ "types": "./dist/index.d.mts",
33
+ "exports": {
34
+ ".": "./dist/index.mjs",
35
+ "./package.json": "./package.json"
36
+ },
37
+ "devDependencies": {
38
+ "tsdown": "^0.17.3"
26
39
  }
27
40
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Roman A
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/src/index.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import type { WebsimSocket as WebsimSocketClass } from "./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
-
13
- type WebsimSocket = InstanceType<WebsimSocketConstructor>;
14
-
15
- export { WebsimSocket };
package/tsconfig.json DELETED
@@ -1,28 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- // Environment setup & latest features
4
- "lib": ["ESNext", "DOM"],
5
- "target": "esnext",
6
- "module": "preserve",
7
- "moduleDetection": "force",
8
-
9
- // Bundler mode
10
- "moduleResolution": "bundler",
11
- "allowImportingTsExtensions": true,
12
- "noEmit": true,
13
-
14
- // Max strictness
15
- "strict": true,
16
- "skipLibCheck": true,
17
- "noFallthroughCasesInSwitch": true,
18
- "noUncheckedIndexedAccess": true,
19
- "noImplicitOverride": true,
20
- "noUnusedLocals": true,
21
- "noUnusedParameters": true,
22
- "noPropertyAccessFromIndexSignature": true,
23
- "noImplicitAny": true,
24
- "strictNullChecks": true,
25
- "forceConsistentCasingInFileNames": true,
26
- "verbatimModuleSyntax": true
27
- }
28
- }