itty-sockets 0.7.1 → 0.8.1

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.
Files changed (2) hide show
  1. package/connect.d.ts +45 -32
  2. package/package.json +2 -2
package/connect.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- type IttySocketEvent<BaseFormat> = BaseFormat extends UseItty ? 'open' | 'close' | 'message' | 'join' | 'leave' : 'open' | 'close' | 'message';
2
1
  type Timestamp = {
3
2
  date: number;
4
3
  };
@@ -6,24 +5,22 @@ type UserDetails = {
6
5
  uid: string;
7
6
  alias?: string;
8
7
  };
9
- type OptionalUserDetails = {
8
+ type EventBase = Timestamp & {
10
9
  uid?: string;
11
10
  alias?: string;
12
11
  };
13
- export type UseItty<MessageType = any> = {
14
- message: MessageType;
15
- } & UserDetails & Timestamp;
16
- export type MessageEvent<MessageType = any> = {
17
- message: MessageType;
18
- } & Timestamp & OptionalUserDetails;
12
+ export type IttyProtocol = UserDetails & Timestamp;
13
+ export type MessageEvent<T = any> = {
14
+ message: T;
15
+ } & EventBase;
19
16
  export type JoinEvent = {
20
17
  type: 'join';
21
18
  users: number;
22
- } & Timestamp & OptionalUserDetails;
19
+ } & EventBase;
23
20
  export type LeaveEvent = {
24
21
  type: 'leave';
25
22
  users: number;
26
- } & Timestamp & OptionalUserDetails;
23
+ } & EventBase;
27
24
  export type ErrorEvent = {
28
25
  type: 'error';
29
26
  message: string;
@@ -34,31 +31,47 @@ export type IttySocketOptions = {
34
31
  echo?: true;
35
32
  announce?: true;
36
33
  };
34
+ type EventUnion<Events> = {
35
+ [K in Exclude<keyof Events & string, 'message'>]: {
36
+ type: K;
37
+ } & Events[K];
38
+ }[Exclude<keyof Events & string, 'message'>] | (Events extends {
39
+ message: infer M;
40
+ } ? M : never);
41
+ type SendFn<Base, Events extends Record<string, any>> = keyof Events extends never ? <T = any>(message: T, ...args: Base extends IttyProtocol ? [uid?: string] : []) => IttySocket<Base, Events> : (message: EventUnion<Events>, ...args: Base extends IttyProtocol ? [uid?: string] : []) => IttySocket<Base, Events>;
42
+ type EmptyEvents = {};
37
43
  export interface IttySocketConnect {
38
- <BaseFormat = object>(...args: BaseFormat extends UseItty ? [channelID: string, options?: IttySocketOptions] : [url: string, queryParams?: any]): IttySocket<BaseFormat>;
44
+ <Events extends Record<string, any> = EmptyEvents>(url: `ws://${string}` | `wss://${string}`, queryParams?: any): IttySocket<object, Events>;
45
+ <Events extends Record<string, any> = EmptyEvents>(channelID: string, options?: IttySocketOptions): IttySocket<IttyProtocol, Events>;
39
46
  }
40
- type UseIttyEvents<BaseFormat> = {
41
- on(type: 'join', listener: (event: JoinEvent) => any): IttySocket<BaseFormat>;
42
- on(type: 'leave', listener: (event: LeaveEvent) => any): IttySocket<BaseFormat>;
43
- on(type: 'error', listener: (event: ErrorEvent) => any): IttySocket<BaseFormat>;
44
- };
45
- type SendMessage<BaseFormat> = BaseFormat extends UseItty ? <MessageFormat = any>(message: MessageFormat, uid?: string) => IttySocket<BaseFormat> : <MessageFormat = any>(message: MessageFormat) => IttySocket<BaseFormat>;
46
- export type IttySocket<BaseFormat = object> = {
47
- open: () => IttySocket<BaseFormat>;
48
- close: () => IttySocket<BaseFormat>;
49
- send: SendMessage<BaseFormat>;
50
- push: SendMessage<BaseFormat>;
51
- remove(type: IttySocketEvent<BaseFormat>, listener: () => any): IttySocket<BaseFormat>;
52
- remove(type: string, listener: () => any): IttySocket<BaseFormat>;
53
- on(type: 'open', listener: () => any): IttySocket<BaseFormat>;
54
- on(type: 'close', listener: () => any): IttySocket<BaseFormat>;
55
- on<MessageFormat = BaseFormat>(type: 'message', listener: (event: BaseFormat & MessageFormat) => any): IttySocket<BaseFormat>;
56
- on<MessageFormat = BaseFormat>(type: string, listener: (event: BaseFormat & MessageFormat & {
47
+ export type IttySocket<Base = object, Events extends Record<string, any> = EmptyEvents> = {
48
+ open: () => IttySocket<Base, Events>;
49
+ close: () => IttySocket<Base, Events>;
50
+ send: SendFn<Base, Events>;
51
+ push: SendFn<Base, Events>;
52
+ remove(type: string, listener: () => any): IttySocket<Base, Events>;
53
+ on(type: 'open', listener: () => any): IttySocket<Base, Events>;
54
+ on(type: 'close', listener: () => any): IttySocket<Base, Events>;
55
+ on<K extends keyof Events & string>(type: K, listener: (event: Base & Events[K] & {
56
+ type: K;
57
+ message: Events[K];
58
+ }) => any): IttySocket<Base, Events>;
59
+ on<T = any>(type: 'message', listener: (event: Base & T & {
60
+ message: T;
61
+ }) => any): IttySocket<Base, Events>;
62
+ } & (Base extends IttyProtocol ? {
63
+ on(type: 'join', listener: (event: JoinEvent) => any): IttySocket<Base, Events>;
64
+ on(type: 'leave', listener: (event: LeaveEvent) => any): IttySocket<Base, Events>;
65
+ on(type: 'error', listener: (event: ErrorEvent) => any): IttySocket<Base, Events>;
66
+ } : object) & {
67
+ on<T = Record<string, any>>(type: string, listener: (event: Base & T & {
57
68
  type: string;
58
- }) => any): IttySocket<BaseFormat>;
59
- on<MessageFormat = BaseFormat>(type: (event?: any) => any, listener: (event: BaseFormat & MessageFormat & {
69
+ message: T;
70
+ }) => any): IttySocket<Base, Events>;
71
+ on<T = Record<string, any>>(type: (event?: any) => any, listener: (event: Base & T & {
60
72
  type: string;
61
- }) => any): IttySocket<BaseFormat>;
62
- } & (BaseFormat extends UseItty ? UseIttyEvents<BaseFormat> : object);
73
+ message: T;
74
+ }) => any): IttySocket<Base, Events>;
75
+ };
63
76
  export declare let connect: IttySocketConnect;
64
77
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "itty-sockets",
3
- "version": "0.7.1",
3
+ "version": "0.8.1",
4
4
  "description": "WebSockets : simplified and minified.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -34,6 +34,6 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/bun": "^1.2.3",
37
- "itty-packager": "^1.7.0"
37
+ "itty-packager": "^1.8.1"
38
38
  }
39
39
  }