@mademi_dev/chatemi 1.0.0 → 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/README.md CHANGED
@@ -13,12 +13,34 @@ ChatEmi is a publish-ready React messaging package for building in-app messenger
13
13
  - `useChatEmi` for product code that needs chat actions and state.
14
14
  - `ChatEmiMessenger`, a default responsive light/dark Telegram-style UI that can be used immediately or customized.
15
15
 
16
+ Repository: [github.com/mademi-dev/ChatEmi](https://github.com/mademi-dev/ChatEmi)
17
+
18
+ ## Requirements
19
+
20
+ - React `>=18`
21
+ - React DOM `>=18`
22
+ - MongoDB driver `>=7` (optional, server-side only)
23
+
16
24
  ## Install
17
25
 
18
26
  ```bash
19
- npm install chatemi
27
+ npm install @mademi_dev/chatemi react react-dom
20
28
  ```
21
29
 
30
+ For MongoDB server helpers in your API backend:
31
+
32
+ ```bash
33
+ npm install @mademi_dev/chatemi mongodb
34
+ ```
35
+
36
+ ## Package exports
37
+
38
+ | Import | Purpose |
39
+ | --- | --- |
40
+ | `@mademi_dev/chatemi` | Provider, hook, API client, socket client, launcher, messenger UI, and types |
41
+ | `@mademi_dev/chatemi/styles.css` | Default UI, themes, launcher modal, and notification badge styles |
42
+ | `@mademi_dev/chatemi/server` | Node.js MongoDB connection helper and index setup |
43
+
22
44
  ## Documentation and examples
23
45
 
24
46
  - Full implementation guide: [`docs/IMPLEMENTATION_GUIDE.md`](docs/IMPLEMENTATION_GUIDE.md)
@@ -26,8 +48,8 @@ npm install chatemi
26
48
  - Next.js launcher example: [`examples/nextjs-chat-widget`](examples/nextjs-chat-widget)
27
49
 
28
50
  ```tsx
29
- import { ChatEmiLauncher, ChatEmiMessenger, ChatEmiProvider, useChatEmi } from "chatemi";
30
- import "chatemi/styles.css";
51
+ import { ChatEmiLauncher, ChatEmiMessenger, ChatEmiProvider, useChatEmi } from "@mademi_dev/chatemi";
52
+ import "@mademi_dev/chatemi/styles.css";
31
53
 
32
54
  export function App() {
33
55
  return (
@@ -62,7 +84,7 @@ export function App() {
62
84
  Import package CSS once from `app/layout.tsx`:
63
85
 
64
86
  ```tsx
65
- import "chatemi/styles.css";
87
+ import "@mademi_dev/chatemi/styles.css";
66
88
  import type { Metadata } from "next";
67
89
 
68
90
  export const metadata: Metadata = {
@@ -83,7 +105,7 @@ Create a client component for the widget:
83
105
  ```tsx
84
106
  "use client";
85
107
 
86
- import { ChatEmiLauncher, ChatEmiProvider } from "chatemi";
108
+ import { ChatEmiLauncher, ChatEmiProvider } from "@mademi_dev/chatemi";
87
109
 
88
110
  export function ChatWidget() {
89
111
  return (
@@ -114,10 +136,50 @@ export function ChatWidget() {
114
136
 
115
137
  Then render `<ChatWidget />` from any client component or include it in a page layout. The provider keeps the socket connected while the launcher modal is closed, so incoming `notification` and `message.created` events continue updating the badge in the background.
116
138
 
139
+ ## Provider configuration
140
+
141
+ `ChatEmiProvider` accepts:
142
+
143
+ | Prop | Default | Purpose |
144
+ | --- | --- | --- |
145
+ | `config` | required | API, socket, auth, theme, and notification settings |
146
+ | `autoConnect` | `true` | Connect the socket on mount when `socketUrl` is set |
147
+ | `initialConversations` | `[]` | Seed conversation list before the first REST fetch |
148
+ | `initialActiveConversationId` | first conversation | Pre-select a conversation |
149
+ | `initialNotifications` | `[]` | Seed notification tray state |
150
+
151
+ `config` fields:
152
+
153
+ ```ts
154
+ type ChatEmiConfig = {
155
+ apiBaseUrl: string;
156
+ socketUrl?: string;
157
+ token?: string | (() => string | Promise<string | undefined> | undefined);
158
+ headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
159
+ currentUser?: ChatEmiUser;
160
+ fetchImpl?: typeof fetch;
161
+ websocketFactory?: (url: string) => WebSocket;
162
+ endpoints?: ChatEmiEndpointOverrides;
163
+ userDirectory?: ChatEmiUserDirectoryConfig;
164
+ theme?: ChatEmiTheme;
165
+ notifications?: ChatEmiNotificationConfig;
166
+ reconnect?: {
167
+ enabled?: boolean;
168
+ maxAttempts?: number;
169
+ initialDelayMs?: number;
170
+ maxDelayMs?: number;
171
+ };
172
+ };
173
+ ```
174
+
175
+ The socket appends `?token=<token>` to `socketUrl` when a token is configured. Override any REST path with `config.endpoints`.
176
+
117
177
  ## Hook usage
118
178
 
179
+ `useChatEmi()` returns provider state, low-level clients, and actions:
180
+
119
181
  ```tsx
120
- import { useChatEmi } from "chatemi";
182
+ import { useChatEmi } from "@mademi_dev/chatemi";
121
183
 
122
184
  export function SendWelcomeButton({ conversationId }: { conversationId: string }) {
123
185
  const { actions, activeMessages, connectionStatus } = useChatEmi();
@@ -138,6 +200,51 @@ export function SendWelcomeButton({ conversationId }: { conversationId: string }
138
200
  }
139
201
  ```
140
202
 
203
+ State highlights:
204
+
205
+ - `currentUser`, `conversations`, `activeConversation`, `activeMessages`
206
+ - `notifications`, `unreadNotificationCount`
207
+ - `typingByConversation`, `presenceByUser`
208
+ - `connectionStatus`, `loading`, `error`, `theme`
209
+ - `api`, `socket`
210
+
211
+ Action highlights:
212
+
213
+ - Conversations: `refreshConversations`, `openConversation`, `createConversation`
214
+ - Messages: `sendMessage`, `editMessage`, `deleteMessage`, `forwardMessage`, `searchMessages`
215
+ - Receipts: `markRead`, `markDelivered`
216
+ - Reactions and media: `addReaction`, `removeReaction`, `uploadAttachment`, `updateAvatar`
217
+ - Members: `addMembers`, `updateMember`, `removeMember`
218
+ - Presence: `startTyping`, `stopTyping`, `setPresence`
219
+ - Notifications: `dismissNotification`, `markNotificationsRead`, `clearNotifications`, `requestNotificationPermission`
220
+ - Connection: `connect`, `disconnect`
221
+
222
+ ## Low-level clients
223
+
224
+ You can use the typed clients outside React when needed:
225
+
226
+ ```ts
227
+ import { ChatEmiApi, ChatEmiSocket } from "@mademi_dev/chatemi";
228
+
229
+ const api = new ChatEmiApi({
230
+ apiBaseUrl: "https://api.example.com/chat",
231
+ token: () => getAccessToken()
232
+ });
233
+
234
+ const socket = new ChatEmiSocket({
235
+ apiBaseUrl: "https://api.example.com/chat",
236
+ socketUrl: "wss://api.example.com/chat/socket",
237
+ token: () => getAccessToken()
238
+ });
239
+
240
+ await socket.connect();
241
+ socket.on("message.created", (message) => {
242
+ console.log(message);
243
+ });
244
+ ```
245
+
246
+ `ChatEmiApiError` is exported for typed REST error handling.
247
+
141
248
  ## Expected REST API contract
142
249
 
143
250
  By default ChatEmi calls these paths under `apiBaseUrl`:
@@ -159,7 +266,7 @@ By default ChatEmi calls these paths under `apiBaseUrl`:
159
266
  | Attachment upload | `POST /attachments` multipart form data |
160
267
  | Search | `GET /search/messages?q=...` |
161
268
 
162
- If your backend uses different paths, pass `config.endpoints` to override any route.
269
+ If your backend uses different paths, pass `config.endpoints` to override any route. See [`docs/BACKEND_CONTRACT.md`](docs/BACKEND_CONTRACT.md) for request/response shapes.
163
270
 
164
271
  ## Socket event contract
165
272
 
@@ -211,12 +318,27 @@ Use `ChatEmiLauncher` when you want a floating in-app messenger:
211
318
  theme="midnight"
212
319
  title="Messages"
213
320
  subtitle="Team chat"
321
+ defaultOpen={false}
322
+ showNotificationList
323
+ markNotificationsReadOnOpen
214
324
  initialSize={{ width: 460, height: 720 }}
215
325
  minSize={{ width: 360, height: 520 }}
216
326
  maxSize={{ width: 960, height: 860 }}
217
327
  />
218
328
  ```
219
329
 
330
+ Launcher props:
331
+
332
+ | Prop | Default | Purpose |
333
+ | --- | --- | --- |
334
+ | `placement` | `bottom-right` | `bottom-right`, `bottom-left`, `top-right`, or `top-left` |
335
+ | `defaultOpen` | `false` | Whether the modal starts open |
336
+ | `showNotificationList` | `true` | Show the compact tray above the chat |
337
+ | `markNotificationsReadOnOpen` | `true` | Mark notifications read when opening |
338
+ | `badgeCount` | unread notifications or conversation unread total | Override launcher badge count |
339
+ | `launcherIcon` | built-in icon | Custom toggle button content |
340
+ | `initialSize` / `minSize` / `maxSize` | `420x680` / `340x480` / `920x860` | Desktop modal sizing |
341
+
220
342
  The launcher includes:
221
343
 
222
344
  - toggle button with unread notification badge
@@ -379,11 +501,11 @@ Use `config.userDirectory` when users live outside your chat backend. ChatEmi wi
379
501
  MongoDB must be connected from your API server, not from browser React code. Install the optional peer dependency in your backend:
380
502
 
381
503
  ```bash
382
- npm install chatemi mongodb
504
+ npm install @mademi_dev/chatemi mongodb
383
505
  ```
384
506
 
385
507
  ```ts
386
- import { createChatEmiMongoConnection } from "chatemi/server";
508
+ import { createChatEmiMongoConnection, ensureChatEmiIndexes } from "@mademi_dev/chatemi/server";
387
509
 
388
510
  const chatDb = await createChatEmiMongoConnection({
389
511
  uri: process.env.MONGODB_URI!,
@@ -405,6 +527,16 @@ export const receipts = chatDb.collections.receipts;
405
527
  export const attachments = chatDb.collections.attachments;
406
528
  ```
407
529
 
530
+ Default collection names:
531
+
532
+ - `chatemi_conversations`
533
+ - `chatemi_messages`
534
+ - `chatemi_members`
535
+ - `chatemi_receipts`
536
+ - `chatemi_attachments`
537
+
538
+ Override them with `collectionNames` when creating the connection. The helper reuses one MongoDB client per process and URI/options pair.
539
+
408
540
  Connection guidance:
409
541
 
410
542
  - Create one MongoDB client per server process and reuse it.
@@ -422,6 +554,18 @@ Use `theme="light"`, `theme="dark"`, `theme="system"`, `theme="midnight"`, `them
422
554
 
423
555
  ## Customizing the UI
424
556
 
557
+ `ChatEmiMessenger` props:
558
+
559
+ | Prop | Default | Purpose |
560
+ | --- | --- | --- |
561
+ | `showSidebar` | `true` | Conversation list sidebar |
562
+ | `enableAdminControls` | `true` | Member management panel for admins |
563
+ | `enableMessageActions` | `true` | Reply, react, and forward actions |
564
+ | `enableMediaPreview` | `true` | Inline image/video/audio previews |
565
+ | `composerPlaceholder` | `Write a message...` | Composer input placeholder |
566
+ | `renderConversation` | built-in row | Custom conversation list item |
567
+ | `renderMessage` | built-in bubble | Custom message renderer |
568
+
425
569
  ```tsx
426
570
  <ChatEmiMessenger
427
571
  composerPlaceholder="Message the team"
@@ -442,10 +586,12 @@ npm run typecheck
442
586
  npm run build
443
587
  ```
444
588
 
589
+ `prepublishOnly` runs `npm run build` automatically before publish.
590
+
445
591
  ## Publishing
446
592
 
447
- Update the version in `package.json`, then run:
593
+ This package is published under the `@mademi_dev` scope. Update the version in `package.json`, then run:
448
594
 
449
595
  ```bash
450
- npm publish
596
+ npm publish --access public
451
597
  ```
@@ -8,9 +8,9 @@ ChatEmi is split into browser-safe and server-side pieces:
8
8
 
9
9
  | Area | Import | Runs in | Purpose |
10
10
  | --- | --- | --- | --- |
11
- | React package | `chatemi` | Browser / React client | Provider, hook, API client, socket client, launcher, messenger UI |
12
- | Styles | `chatemi/styles.css` | Browser | All default UI, themes, launcher modal, notification badge |
13
- | Server helpers | `chatemi/server` | Node.js only | MongoDB connection helper and collection/index setup |
11
+ | React package | `@mademi_dev/chatemi` | Browser / React client | Provider, hook, API client, socket client, launcher, messenger UI |
12
+ | Styles | `@mademi_dev/chatemi/styles.css` | Browser | All default UI, themes, launcher modal, notification badge |
13
+ | Server helpers | `@mademi_dev/chatemi/server` | Node.js only | MongoDB connection helper and collection/index setup |
14
14
 
15
15
  Do not expose database credentials to browser code. Browser code should call your authenticated HTTP and WebSocket APIs.
16
16
 
@@ -19,7 +19,7 @@ Do not expose database credentials to browser code. Browser code should call you
19
19
  ### Install
20
20
 
21
21
  ```bash
22
- npm install chatemi
22
+ npm install @mademi_dev/chatemi
23
23
  ```
24
24
 
25
25
  If your API server uses ChatEmi MongoDB helpers:
@@ -33,7 +33,7 @@ npm install mongodb
33
33
  In App Router, import CSS from `app/layout.tsx`:
34
34
 
35
35
  ```tsx
36
- import "chatemi/styles.css";
36
+ import "@mademi_dev/chatemi/styles.css";
37
37
  ```
38
38
 
39
39
  ### Create a client widget
@@ -43,7 +43,7 @@ The widget uses browser APIs (`localStorage`, WebSocket, notifications), so it m
43
43
  ```tsx
44
44
  "use client";
45
45
 
46
- import { ChatEmiLauncher, ChatEmiProvider } from "chatemi";
46
+ import { ChatEmiLauncher, ChatEmiProvider } from "@mademi_dev/chatemi";
47
47
  import { useMemo } from "react";
48
48
 
49
49
  export function ChatWidget() {
@@ -210,10 +210,10 @@ See `docs/BACKEND_CONTRACT.md` for full details.
210
210
 
211
211
  ## 7. MongoDB integration
212
212
 
213
- Use `chatemi/server` only from Node.js:
213
+ Use `@mademi_dev/chatemi/server` only from Node.js:
214
214
 
215
215
  ```ts
216
- import { createChatEmiMongoConnection } from "chatemi/server";
216
+ import { createChatEmiMongoConnection } from "@mademi_dev/chatemi/server";
217
217
 
218
218
  const chatDb = await createChatEmiMongoConnection({
219
219
  uri: process.env.MONGODB_URI!,
@@ -10,12 +10,12 @@ This example shows how to use ChatEmi in a Next.js App Router application with:
10
10
  - external user-directory configuration
11
11
  - server-side MongoDB helper usage
12
12
 
13
- The files are intentionally small and copy-paste friendly. They are not a standalone app inside this repository; copy them into a Next.js project that has `next`, `react`, `react-dom`, `chatemi`, and optionally `mongodb` installed.
13
+ The files are intentionally small and copy-paste friendly. They are not a standalone app inside this repository; copy them into a Next.js project that has `next`, `react`, `react-dom`, `@mademi_dev/chatemi`, and optionally `mongodb` installed.
14
14
 
15
15
  ## Install in your app
16
16
 
17
17
  ```bash
18
- npm install chatemi
18
+ npm install @mademi_dev/chatemi
19
19
  ```
20
20
 
21
21
  If you also use the MongoDB server helpers:
@@ -38,7 +38,7 @@ Only `NEXT_PUBLIC_*` values are sent to the browser. Keep `MONGODB_URI` server-s
38
38
 
39
39
  ## Files
40
40
 
41
- - `app/layout.tsx` imports `chatemi/styles.css` once.
41
+ - `app/layout.tsx` imports `@mademi_dev/chatemi/styles.css` once.
42
42
  - `app/components/ChatWidget.tsx` is the client-side launcher widget.
43
43
  - `app/lib/chatemi-config.ts` centralizes browser-safe config.
44
44
  - `app/lib/chatemi-mongo.ts` shows server-side MongoDB setup.
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
 
3
- import { ChatEmiLauncher, ChatEmiProvider } from "chatemi";
3
+ import { ChatEmiLauncher, ChatEmiProvider } from "@mademi_dev/chatemi";
4
4
  import { useMemo } from "react";
5
5
  import { createChatEmiConfig } from "../lib/chatemi-config";
6
6
 
@@ -1,4 +1,4 @@
1
- import "chatemi/styles.css";
1
+ import "@mademi_dev/chatemi/styles.css";
2
2
  import type { Metadata } from "next";
3
3
  import type { ReactNode } from "react";
4
4
  import { ChatWidget } from "./components/ChatWidget";
@@ -1,4 +1,4 @@
1
- import type { ChatEmiConfig } from "chatemi";
1
+ import type { ChatEmiConfig } from "@mademi_dev/chatemi";
2
2
 
3
3
  export function createChatEmiConfig(): ChatEmiConfig {
4
4
  return {
@@ -1,4 +1,4 @@
1
- import { createChatEmiMongoConnection } from "chatemi/server";
1
+ import { createChatEmiMongoConnection } from "@mademi_dev/chatemi/server";
2
2
 
3
3
  let connectionPromise: ReturnType<typeof createChatEmiMongoConnection> | undefined;
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mademi_dev/chatemi",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A React messaging kit with API clients, realtime socket support, hooks, provider state, and default Telegram-style UI.",
5
5
  "type": "module",
6
6
  "main": "dist/chatemi.umd.cjs",