cardoctor-call-chat 1.0.0 → 1.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 ADDED
@@ -0,0 +1,14 @@
1
+ # CarDoctor Call Chat
2
+
3
+ 🚀 A React SDK with hooks and utilities for Cardoctor Call Chat.
4
+
5
+ ## 📦 Installation
6
+
7
+ ```bash
8
+ npm install cardoctor-call-chat
9
+ # or
10
+ yarn add cardoctor-call-chat
11
+
12
+ ```
13
+
14
+ # Developed by Tuan Nguyen
@@ -0,0 +1,12 @@
1
+ interface ChatInitState {
2
+ user: any | null;
3
+ loading: boolean;
4
+ }
5
+ interface IPros {
6
+ appId: string;
7
+ region: string;
8
+ userId: string;
9
+ }
10
+ declare function useCallChatInit(props: IPros): ChatInitState;
11
+
12
+ export { useCallChatInit };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,12 @@
1
- export declare function sayHello(name: string): string;
2
- export declare class MySDK {
3
- private token;
4
- constructor(token: string);
5
- getAuthHeader(): {
6
- Authorization: string;
7
- };
1
+ interface ChatInitState {
2
+ user: any | null;
3
+ loading: boolean;
8
4
  }
5
+ interface IPros {
6
+ appId: string;
7
+ region: string;
8
+ userId: string;
9
+ }
10
+ declare function useCallChatInit(props: IPros): ChatInitState;
11
+
12
+ export { useCallChatInit };
package/dist/index.js CHANGED
@@ -1,16 +1,127 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MySDK = void 0;
4
- exports.sayHello = sayHello;
5
- function sayHello(name) {
6
- return `Hello, ${name}! 👋`;
7
- }
8
- class MySDK {
9
- constructor(token) {
10
- this.token = token;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __async = (__this, __arguments, generator) => {
20
+ return new Promise((resolve, reject) => {
21
+ var fulfilled = (value) => {
22
+ try {
23
+ step(generator.next(value));
24
+ } catch (e) {
25
+ reject(e);
26
+ }
27
+ };
28
+ var rejected = (value) => {
29
+ try {
30
+ step(generator.throw(value));
31
+ } catch (e) {
32
+ reject(e);
33
+ }
34
+ };
35
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
+ step((generator = generator.apply(__this, __arguments)).next());
37
+ });
38
+ };
39
+
40
+ // src/index.ts
41
+ var index_exports = {};
42
+ __export(index_exports, {
43
+ useCallChatInit: () => useCallChatInit
44
+ });
45
+ module.exports = __toCommonJS(index_exports);
46
+
47
+ // src/cometchat/use-call-chat-init.tsx
48
+ var import_react = require("react");
49
+ var import_chat_uikit_react2 = require("@cometchat/chat-uikit-react");
50
+
51
+ // src/cometchat/api/get-auth-token.ts
52
+ var myHeaders = new Headers();
53
+ myHeaders.append("accept", "application/json");
54
+ myHeaders.append("apikey", "07ffe79d29cf9a3596e5fe369125a48b829ea400");
55
+ myHeaders.append("content-type", "application/json");
56
+ var requestOptions = {
57
+ method: "POST",
58
+ headers: myHeaders
59
+ };
60
+ var getAuthToken = (userId) => __async(null, null, function* () {
61
+ const getToken = yield fetch(
62
+ `https://2772864c1a441d73.api-in.cometchat.io/v3/users/${userId}/auth_tokens`,
63
+ requestOptions
64
+ );
65
+ const response = yield getToken.json();
66
+ return response.data.authToken;
67
+ });
68
+
69
+ // src/cometchat/func/login-user.ts
70
+ var import_chat_uikit_react = require("@cometchat/chat-uikit-react");
71
+ var isLoggingIn = false;
72
+ var loginWithAuthToken = (authToken) => __async(null, null, function* () {
73
+ try {
74
+ if (isLoggingIn) {
75
+ console.log("\u26A0\uFE0F Login request already in progress...");
76
+ return;
11
77
  }
12
- getAuthHeader() {
13
- return { Authorization: `Bearer ${this.token}` };
78
+ const existingUser = yield import_chat_uikit_react.CometChatUIKit.getLoggedinUser();
79
+ if (existingUser) {
80
+ console.log("\u{1F539} Already logged in:", existingUser);
81
+ return existingUser;
14
82
  }
83
+ isLoggingIn = true;
84
+ const user = yield import_chat_uikit_react.CometChatUIKit.loginWithAuthToken(authToken);
85
+ console.log("\u2705 Login successful:", user);
86
+ return user;
87
+ } catch (error) {
88
+ console.error("\u274C Login failed:", error);
89
+ } finally {
90
+ isLoggingIn = false;
91
+ }
92
+ });
93
+
94
+ // src/cometchat/use-call-chat-init.tsx
95
+ function useCallChatInit(props) {
96
+ const [user, setUser] = (0, import_react.useState)(null);
97
+ const [loading, setLoading] = (0, import_react.useState)(true);
98
+ (0, import_react.useEffect)(() => {
99
+ let mounted = true;
100
+ const init = () => __async(null, null, function* () {
101
+ try {
102
+ const UIKitSettings = new import_chat_uikit_react2.UIKitSettingsBuilder().setAppId(props.appId).setRegion(props.region).subscribePresenceForAllUsers().build();
103
+ yield import_chat_uikit_react2.CometChatUIKit.init(UIKitSettings);
104
+ console.log("\u2705 CometChat UI Kit initialized successfully.");
105
+ const token = yield getAuthToken(props.userId);
106
+ console.log("\u{1F680} ~ init ~ token:", token);
107
+ const loggedInUser = yield loginWithAuthToken(token);
108
+ if (mounted) {
109
+ setUser(loggedInUser);
110
+ setLoading(false);
111
+ }
112
+ } catch (error) {
113
+ console.error("\u274C CometChat init failed:", error);
114
+ if (mounted) setLoading(false);
115
+ }
116
+ });
117
+ init();
118
+ return () => {
119
+ mounted = false;
120
+ };
121
+ }, []);
122
+ return { user, loading };
15
123
  }
16
- exports.MySDK = MySDK;
124
+ // Annotate the CommonJS export names for ESM import in node:
125
+ 0 && (module.exports = {
126
+ useCallChatInit
127
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,101 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // src/cometchat/use-call-chat-init.tsx
23
+ import { useState, useEffect } from "react";
24
+ import { UIKitSettingsBuilder, CometChatUIKit as CometChatUIKit2 } from "@cometchat/chat-uikit-react";
25
+
26
+ // src/cometchat/api/get-auth-token.ts
27
+ var myHeaders = new Headers();
28
+ myHeaders.append("accept", "application/json");
29
+ myHeaders.append("apikey", "07ffe79d29cf9a3596e5fe369125a48b829ea400");
30
+ myHeaders.append("content-type", "application/json");
31
+ var requestOptions = {
32
+ method: "POST",
33
+ headers: myHeaders
34
+ };
35
+ var getAuthToken = (userId) => __async(null, null, function* () {
36
+ const getToken = yield fetch(
37
+ `https://2772864c1a441d73.api-in.cometchat.io/v3/users/${userId}/auth_tokens`,
38
+ requestOptions
39
+ );
40
+ const response = yield getToken.json();
41
+ return response.data.authToken;
42
+ });
43
+
44
+ // src/cometchat/func/login-user.ts
45
+ import { CometChatUIKit } from "@cometchat/chat-uikit-react";
46
+ var isLoggingIn = false;
47
+ var loginWithAuthToken = (authToken) => __async(null, null, function* () {
48
+ try {
49
+ if (isLoggingIn) {
50
+ console.log("\u26A0\uFE0F Login request already in progress...");
51
+ return;
52
+ }
53
+ const existingUser = yield CometChatUIKit.getLoggedinUser();
54
+ if (existingUser) {
55
+ console.log("\u{1F539} Already logged in:", existingUser);
56
+ return existingUser;
57
+ }
58
+ isLoggingIn = true;
59
+ const user = yield CometChatUIKit.loginWithAuthToken(authToken);
60
+ console.log("\u2705 Login successful:", user);
61
+ return user;
62
+ } catch (error) {
63
+ console.error("\u274C Login failed:", error);
64
+ } finally {
65
+ isLoggingIn = false;
66
+ }
67
+ });
68
+
69
+ // src/cometchat/use-call-chat-init.tsx
70
+ function useCallChatInit(props) {
71
+ const [user, setUser] = useState(null);
72
+ const [loading, setLoading] = useState(true);
73
+ useEffect(() => {
74
+ let mounted = true;
75
+ const init = () => __async(null, null, function* () {
76
+ try {
77
+ const UIKitSettings = new UIKitSettingsBuilder().setAppId(props.appId).setRegion(props.region).subscribePresenceForAllUsers().build();
78
+ yield CometChatUIKit2.init(UIKitSettings);
79
+ console.log("\u2705 CometChat UI Kit initialized successfully.");
80
+ const token = yield getAuthToken(props.userId);
81
+ console.log("\u{1F680} ~ init ~ token:", token);
82
+ const loggedInUser = yield loginWithAuthToken(token);
83
+ if (mounted) {
84
+ setUser(loggedInUser);
85
+ setLoading(false);
86
+ }
87
+ } catch (error) {
88
+ console.error("\u274C CometChat init failed:", error);
89
+ if (mounted) setLoading(false);
90
+ }
91
+ });
92
+ init();
93
+ return () => {
94
+ mounted = false;
95
+ };
96
+ }, []);
97
+ return { user, loading };
98
+ }
99
+ export {
100
+ useCallChatInit
101
+ };
package/package.json CHANGED
@@ -1,14 +1,24 @@
1
1
  {
2
2
  "name": "cardoctor-call-chat",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
7
- "build": "tsc",
7
+ "build": "tsup src/index.ts --format esm,cjs --dts --external react",
8
8
  "prepublishOnly": "npm run build"
9
9
  },
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
+ "@types/react": "^19.1.9",
13
+ "react": "^19.1.1",
14
+ "tsup": "^8.5.0",
12
15
  "typescript": "^5.9.2"
16
+ },
17
+ "dependencies": {
18
+ "@cometchat/calls-sdk-javascript": "^4.1.0",
19
+ "@cometchat/chat-uikit-react": "^6.1.2"
20
+ },
21
+ "peerDependencies": {
22
+ "react": ">=18.0.0"
13
23
  }
14
24
  }
@@ -0,0 +1,18 @@
1
+ const myHeaders = new Headers();
2
+ myHeaders.append("accept", "application/json");
3
+ myHeaders.append("apikey", "07ffe79d29cf9a3596e5fe369125a48b829ea400");
4
+ myHeaders.append("content-type", "application/json");
5
+
6
+ const requestOptions = {
7
+ method: "POST",
8
+ headers: myHeaders
9
+ };
10
+
11
+ export const getAuthToken = async (userId: string) => {
12
+ const getToken = await fetch(
13
+ `https://2772864c1a441d73.api-in.cometchat.io/v3/users/${userId}/auth_tokens`,
14
+ requestOptions
15
+ )
16
+ const response = await getToken.json()
17
+ return response.data.authToken
18
+ }
@@ -0,0 +1,25 @@
1
+ import { CometChatUIKit } from "@cometchat/chat-uikit-react";
2
+ let isLoggingIn = false;
3
+ export const loginWithAuthToken = async (authToken: string) => {
4
+ try {
5
+ if (isLoggingIn) {
6
+ console.log("⚠️ Login request already in progress...");
7
+ return;
8
+ }
9
+
10
+ const existingUser = await CometChatUIKit.getLoggedinUser();
11
+ if (existingUser) {
12
+ console.log("🔹 Already logged in:", existingUser);
13
+ return existingUser;
14
+ }
15
+
16
+ isLoggingIn = true;
17
+ const user = await CometChatUIKit.loginWithAuthToken(authToken);
18
+ console.log("✅ Login successful:", user);
19
+ return user;
20
+ } catch (error) {
21
+ console.error("❌ Login failed:", error);
22
+ } finally {
23
+ isLoggingIn = false;
24
+ }
25
+ };
@@ -0,0 +1,57 @@
1
+ import { useState, useEffect } from "react";
2
+ import { UIKitSettingsBuilder, CometChatUIKit } from "@cometchat/chat-uikit-react";
3
+ import { getAuthToken } from "./api/get-auth-token";
4
+ import { loginWithAuthToken } from "./func/login-user";
5
+
6
+ interface ChatInitState {
7
+ user: any | null;
8
+ loading: boolean;
9
+ }
10
+
11
+ interface IPros {
12
+ appId: string;
13
+ region: string;
14
+ userId: string
15
+ }
16
+
17
+ export function useCallChatInit(props: IPros): ChatInitState {
18
+ const [user, setUser] = useState<any | null>(null);
19
+ const [loading, setLoading] = useState(true);
20
+
21
+ useEffect(() => {
22
+ let mounted = true;
23
+
24
+ const init = async () => {
25
+ try {
26
+ const UIKitSettings = new UIKitSettingsBuilder()
27
+ .setAppId(props.appId)
28
+ .setRegion(props.region)
29
+ .subscribePresenceForAllUsers()
30
+ .build();
31
+
32
+ await CometChatUIKit.init(UIKitSettings);
33
+ console.log("✅ CometChat UI Kit initialized successfully.");
34
+
35
+ const token = await getAuthToken(props.userId);
36
+ console.log("🚀 ~ init ~ token:", token)
37
+ const loggedInUser = await loginWithAuthToken(token);
38
+
39
+ if (mounted) {
40
+ setUser(loggedInUser);
41
+ setLoading(false);
42
+ }
43
+ } catch (error) {
44
+ console.error("❌ CometChat init failed:", error);
45
+ if (mounted) setLoading(false);
46
+ }
47
+ };
48
+
49
+ init();
50
+
51
+ return () => {
52
+ mounted = false;
53
+ };
54
+ }, []);
55
+
56
+ return { user, loading };
57
+ }
package/src/index.ts CHANGED
@@ -1,15 +1 @@
1
- export function sayHello(name: string): string {
2
- return `Hello, ${name}! 👋`;
3
- }
4
-
5
- export class MySDK {
6
- private token: string;
7
-
8
- constructor(token: string) {
9
- this.token = token;
10
- }
11
-
12
- getAuthHeader() {
13
- return { Authorization: `Bearer ${this.token}` };
14
- }
15
- }
1
+ export * from "./cometchat/use-call-chat-init";
package/tsconfig.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "outDir": "./dist",
4
- "declaration": true,
3
+ "jsx": "react-jsx",
5
4
  "esModuleInterop": true,
6
5
  "moduleResolution": "node",
7
- "target": "ES2019",
8
- "module": "commonjs",
9
- "strict": true
6
+ "module": "esnext",
7
+ "target": "es6",
8
+ "skipLibCheck": true,
9
+ "strict": true,
10
+ "declaration": true,
11
+ "outDir": "dist"
10
12
  },
11
13
  "include": ["src"]
12
14
  }