@only-chat/types 0.2.0-beta.9 → 1.0.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.
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export type * as Log from './log.d.ts'
2
- export type * as Queue from './queue.js'
3
- export type * as Store from './store.js'
4
- export type * as Transport from './transport.ts'
2
+ export type * as Queue from './queue.d.ts'
3
+ export type * as Store from './store.d.ts'
4
+ export type * as Transport from './transport.d.ts'
5
+ export type * as UserStore from './userStore.d.ts'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@only-chat/types",
3
- "version": "0.2.0-beta.9",
3
+ "version": "1.0.1",
4
4
  "description": "TypeScript definitions for only-chat",
5
5
  "homepage": "https://github.com/only-chat/node-backend/packages/types",
6
6
  "main": "",
@@ -13,11 +13,11 @@
13
13
  "scripts": {},
14
14
  "author": "only-chat",
15
15
  "license": "MIT",
16
- "dependencies": {
17
- "@types/node": "^22.7.4"
18
- },
19
16
  "keywords": [
20
17
  "only-chat",
21
18
  "type"
22
- ]
23
- }
19
+ ],
20
+ "devDependencies": {
21
+ "@types/node": "^25.2.3"
22
+ }
23
+ }
package/queue.d.ts CHANGED
@@ -20,6 +20,7 @@ export interface TextMessage {
20
20
 
21
21
  export interface MessageDelete {
22
22
  messageId: string
23
+ deletedAt: Date
23
24
  }
24
25
 
25
26
  export interface MessageUpdate extends Partial<FileMessage>, Partial<TextMessage> {
@@ -31,6 +32,12 @@ export type MessageType = 'connected' | 'disconnected' | 'joined' | 'left' | 'cl
31
32
 
32
33
  export type MessageData = ConversationUpdate | FileMessage | MessageDelete | MessageUpdate | TextMessage | null
33
34
 
35
+ export type MessageMetadataValue = string | number | boolean | null
36
+
37
+ export type MessageMetadata = {
38
+ [name: string]: MessageMetadataValue | MessageMetadataValue[]
39
+ }
40
+
34
41
  export interface Message {
35
42
  type: MessageType
36
43
  id?: string
@@ -41,14 +48,38 @@ export interface Message {
41
48
  connectionId: string
42
49
  fromId: string
43
50
  data: MessageData
51
+ metadata?: MessageMetadata
44
52
  createdAt: Date
45
53
  updatedAt?: Date
46
54
  deletedAt?: Date
47
55
  }
48
56
 
49
57
  export interface MessageQueue {
50
- acceptTypes?: MessageType[]
58
+ // Provide list of accepted message types
59
+ readonly acceptTypes?: MessageType[]
60
+ /**
61
+ * Publishes a message to the queue if its type is accepted
62
+ *
63
+ * @param msg - The message object to publish
64
+ * @returns Promise<boolean> - true if message was published, false if message type is not accepted
65
+ */
51
66
  publish: (msg: Message) => Promise<boolean>
67
+ /**
68
+ * Subscribes a callback function to receive messages from the queue
69
+ *
70
+ * @param callback - Async function that processes incoming messages
71
+ * @returns Promise<boolean> - Always returns true when subscription is added
72
+ *
73
+ * Note: Multiple subscribers can be registered, and all will be called for each message
74
+ */
52
75
  subscribe: (callback: (msg: Message) => Promise<void>) => Promise<boolean>
76
+ /**
77
+ * Unsubscribes a previously registered callback function
78
+ *
79
+ * @param callback - The callback function to remove
80
+ * @returns Promise<boolean> - true if callback was found and removed, false otherwise
81
+ *
82
+ * Note: Only removes the last occurrence if the same callback was added multiple times
83
+ */
53
84
  unsubscribe?: (callback: (msg: Message) => Promise<void>) => Promise<boolean>
54
85
  }
package/store.d.ts CHANGED
@@ -38,10 +38,12 @@ export interface TextMessage {
38
38
 
39
39
  export interface MessageDelete {
40
40
  messageId: string
41
+ deletedAt: Date
41
42
  }
42
43
 
43
44
  export interface MessageUpdate extends Partial<FileMessage>, Partial<TextMessage> {
44
45
  messageId: string
46
+ updatedAt: Date
45
47
  }
46
48
 
47
49
  export interface FindRequest {
@@ -71,6 +73,12 @@ export type MessageType = 'joined' | 'left' | 'closed' | 'deleted' | 'updated' |
71
73
 
72
74
  export type MessageData = ConversationUpdate | FileMessage | MessageDelete | MessageUpdate | TextMessage | null
73
75
 
76
+ export type MessageMetadataValue = string | number | boolean | null
77
+
78
+ export type MessageMetadata = {
79
+ [name: string]: MessageMetadataValue | MessageMetadataValue[]
80
+ }
81
+
74
82
  export interface Message {
75
83
  type: MessageType
76
84
  id?: string
@@ -80,6 +88,7 @@ export interface Message {
80
88
  connectionId: string
81
89
  fromId: string
82
90
  data: MessageData
91
+ metadata?: MessageMetadata
83
92
  createdAt: Date
84
93
  updatedAt?: Date
85
94
  deletedAt?: Date
@@ -92,6 +101,11 @@ export interface SaveResponse {
92
101
 
93
102
  export type ConversationLastMessages = Record<string, { latest?: Message, left?: Date }>
94
103
 
104
+ export interface ConversationIdResult {
105
+ id?: string
106
+ result?: string
107
+ }
108
+
95
109
  export interface ConversationsResult {
96
110
  conversations: Conversation[]
97
111
  from: number
@@ -100,14 +114,119 @@ export interface ConversationsResult {
100
114
  }
101
115
 
102
116
  export interface MessageStore {
117
+ /**
118
+ * Searches for messages based on filter criteria with pagination and sorting support
119
+ *
120
+ * @param r - FindRequest object containing search parameters including:
121
+ * - ids: Message IDs to retrieve
122
+ * - conversationIds: Messages from specific conversations
123
+ * - text: Text content filter (searches within text messages)
124
+ * - fromIds: Sender IDs
125
+ * - types: Message types (e.g., 'text', 'file')
126
+ * - clientMessageIds: Client-provided message IDs
127
+ * - excludeIds: Message IDs to exclude from results
128
+ * - createdFrom/createdTo: Date range filters
129
+ * - sort: Field to sort by (e.g., 'createdAt')
130
+ * - sortDesc: Whether to sort in descending order
131
+ * - from: Pagination starting index
132
+ * - size: Maximum number of results to return
133
+ *
134
+ * @returns Promise<FindResult> containing filtered, sorted, and paginated messages
135
+ */
103
136
  findMessages: (r: FindRequest) => Promise<FindResult>
104
- getConversationById: (id: string) => Promise<Conversation | undefined>
137
+ /**
138
+ * Retrieves the latest message and last participant message for each specified conversation
139
+ *
140
+ * @param participant - The user ID of the participant requesting the information
141
+ * @param conversationId - Array of conversation IDs to fetch last message info for
142
+ *
143
+ * @returns Promise<ConversationLastMessages> - Object mapping conversation IDs to:
144
+ * - latest: The most recent non-deleted message of type 'text' or 'file'
145
+ * - left: Timestamp of the last message sent by the specified participant
146
+ */
105
147
  getLastMessagesTimestamps: (fromId: string, conversationId: string[]) => Promise<ConversationLastMessages>
148
+ /**
149
+ * Retrieves a single conversation by ID for a specific participant
150
+ *
151
+ * @param participant - Optional participant ID; if provided, verifies the participant is part of the conversation
152
+ * @param id - The unique identifier of the conversation to retrieve
153
+ *
154
+ * @returns Promise<Conversation | undefined> - The conversation object if found and accessible,
155
+ * undefined if conversation doesn't exist, is deleted, or doesn't have specified participant
156
+ */
106
157
  getParticipantConversationById: (participant: string | undefined, id: string) => Promise<Conversation | undefined>
107
- getParticipantConversations: (participant: string, excludeIds: string[], from: number, size: number) => Promise<ConversationsResult>
158
+ /**
159
+ * Retrieves a paginated list of conversations for a specific participant with optional filtering
160
+ *
161
+ * @param participant - The user ID of the participant whose conversations to retrieve
162
+ * @param ids - Optional array of conversation IDs to filter by (if undefined, returns all conversations)
163
+ * @param excludeIds - Optional array of conversation IDs to exclude from results
164
+ * @param from - Pagination starting index (default: 0)
165
+ * @param size - Maximum number of conversations to return (default: 100)
166
+ *
167
+ * @returns Promise<ConversationsResult> - Paginated result containing:
168
+ * - conversations: Array of conversations sorted by creation date (newest first)
169
+ * - from: The starting index used for pagination
170
+ * - size: The number of conversations returned
171
+ * - total: The total number of conversations for the participant
172
+ */
173
+ getParticipantConversations: (participant: string, ids?: string[], excludeIds?: string[], from?: number, size?: number) => Promise<ConversationsResult>
174
+ /**
175
+ * Retrieves the last non-deleted message sent by a specific participant in a conversation
176
+ *
177
+ * @param participant - The user ID of the participant whose last message to find
178
+ * @param conversationId - The unique identifier of the conversation
179
+ *
180
+ * @returns Promise<Message | undefined> - The last message sent by the participant in the conversation,
181
+ * undefined if conversation doesn't exist, is deleted, participant is not in conversation,
182
+ * or participant has no messages
183
+ */
108
184
  getParticipantLastMessage: (participant: string, conversationId: string) => Promise<Message | undefined>
109
- getPeerToPeerConversationId(peer1: string, peer2: string): Promise<string | undefined>
185
+ /**
186
+ * Gets or creates a peer-to-peer conversation ID between two users
187
+ *
188
+ * @param peer1 - First user ID
189
+ * @param peer2 - Second user ID
190
+ *
191
+ * @returns Promise<ConversationIdResult | undefined> - Object containing:
192
+ * - id: The conversation ID (existing or newly created)
193
+ * - result: 'created' if a new conversation was created, undefined if existing conversation was found
194
+ *
195
+ * Note: The conversation ID is deterministic - same two users will always get the same ID
196
+ * regardless of parameter order (peer1 and peer2 are sorted internally)
197
+ */
198
+ getPeerToPeerConversationId(peer1: string, peer2: string): Promise<ConversationIdResult | undefined>
199
+ /**
200
+ * Saves a connection record for a user instance (e.g., WebSocket connection)
201
+ *
202
+ * @param userId - The ID of the user establishing the connection
203
+ * @param instanceId - The instance identifier for this connection
204
+ *
205
+ * @returns Promise<SaveResponse> - Object containing:
206
+ * - _id: The auto-generated unique connection ID
207
+ * - result: Always 'created' since connections are always new entries
208
+ */
110
209
  saveConnection: (userId: string, instanceId: string) => Promise<SaveResponse>
210
+ /**
211
+ * Saves or updates a conversation in the store
212
+ *
213
+ * @param c - Conversation to save. If the conversation has an ID,
214
+ * it updates the existing conversation; otherwise creates a new one
215
+ *
216
+ * @returns Promise<SaveResponse> - Object containing:
217
+ * - _id: The conversation ID (preserved if provided, auto-generated if new)
218
+ * - result: 'created' for new conversations, 'updated' for existing ones
219
+ */
111
220
  saveConversation: (c: Conversation) => Promise<SaveResponse>
221
+ /**
222
+ * Saves or updates a message in the store and associates it with a conversation
223
+ *
224
+ * @param m - Message to save. If the message has an ID,
225
+ * it updates the existing message; otherwise creates a new one
226
+ *
227
+ * @returns Promise<SaveResponse> - Object containing:
228
+ * - _id: The message ID (preserved if provided, auto-generated if new)
229
+ * - result: 'created' for new messages, 'updated' for existing ones
230
+ */
112
231
  saveMessage: (m: Message) => Promise<SaveResponse>
113
232
  }
package/transport.d.ts CHANGED
@@ -16,7 +16,7 @@ export interface Transport extends EventEmitter {
16
16
 
17
17
  close(code?: number, data?: string | Buffer): void
18
18
  send(data: string, options: {
19
- binary?: boolean | undefined
20
- fin?: boolean | undefined
21
- },): void
19
+ binary?: boolean
20
+ fin?: boolean
21
+ }): void
22
22
  }
package/userStore.d.ts CHANGED
@@ -2,10 +2,15 @@ export interface AuthenticationInfo {}
2
2
 
3
3
  export interface UserStore {
4
4
  /**
5
- * Verifies provided user credentials
5
+ * Authenticates a user
6
6
  *
7
- * @param authInfo User credentials
8
- * @returns User identifier
7
+ * @param info - Authentication information containing name and password
8
+ *
9
+ * @returns Promise<string | undefined> - User ID if authentication successful
10
+ * or new user created, undefined if no name provided
11
+ *
12
+ * Security Consideration: In production, passwords should be hashed
13
+ * before storage.
9
14
  */
10
15
  authenticate(authInfo: AuthenticationInfo): Promise<string | undefined>
11
16
  }