ajaxter-chat 1.0.1 → 1.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 +15 -2
- package/dist/config/index.d.ts +13 -0
- package/dist/config/index.js +21 -1
- package/dist/services/userService.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -98,7 +98,7 @@ NEXT_PUBLIC_CHAT_TYPE=BOTH
|
|
|
98
98
|
|--------------------|---------------------------------------|------------------------------------------|
|
|
99
99
|
| `CHAT_HOST_URL` | string | Base URL of your chat/user API |
|
|
100
100
|
| `CHAT_HOST_PORT` | number | Port for your API server |
|
|
101
|
-
| `CHAT_USER_LIST` | string |
|
|
101
|
+
| `CHAT_USER_LIST` | string | User list URL — see **User List API** below |
|
|
102
102
|
| `CHAT_STATUS` | `ACTIVE` \| `DISABLE` \| `MAINTENANCE` | Controls widget visibility & state |
|
|
103
103
|
| `CHAT_TYPE` | `SUPPORT` \| `CHAT` \| `BOTH` | Controls which users are shown |
|
|
104
104
|
|
|
@@ -126,7 +126,20 @@ NEXT_PUBLIC_CHAT_TYPE=BOTH
|
|
|
126
126
|
|
|
127
127
|
## User List API
|
|
128
128
|
|
|
129
|
-
The widget
|
|
129
|
+
The widget calls `CHAT_USER_LIST` in three ways:
|
|
130
|
+
|
|
131
|
+
1. **Same-origin / BFF (recommended)** — value starts with `/`, e.g. `/api/v1/chat/users`. The browser only shows a request to **your** app; your route handler proxies to the real API server-side, so the upstream URL (e.g. `http://your-api.com:4000/...`) does not appear as the client request URL in DevTools.
|
|
132
|
+
2. **Full URL** — value starts with `http://` or `https://`; that exact URL is fetched (visible in Network).
|
|
133
|
+
3. **Legacy** — otherwise it is built as
|
|
134
|
+
`CHAT_HOST_URL` + optional `:CHAT_HOST_PORT` + path.
|
|
135
|
+
|
|
136
|
+
Example (Next.js route at `app/api/v1/chat/users/route.ts` that forwards to your backend):
|
|
137
|
+
|
|
138
|
+
```env
|
|
139
|
+
NEXT_PUBLIC_CHAT_USER_LIST=/api/v1/chat/users
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Legacy example:
|
|
130
143
|
|
|
131
144
|
```
|
|
132
145
|
GET ${CHAT_HOST_URL}:${CHAT_HOST_PORT}/${CHAT_USER_LIST}
|
package/dist/config/index.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
1
|
import { ChatConfig } from '../types';
|
|
2
2
|
export declare function loadChatConfig(): ChatConfig;
|
|
3
|
+
/**
|
|
4
|
+
* Resolves the URL used by `fetchUsers`.
|
|
5
|
+
*
|
|
6
|
+
* **Same-origin / internal proxy (recommended):** If `CHAT_USER_LIST` starts with `/`
|
|
7
|
+
* (e.g. `/api/v1/chat/users`), the browser only requests your app origin. The real
|
|
8
|
+
* backend URL stays on the server (Next.js route, BFF, etc.) and does not appear in
|
|
9
|
+
* the client Network tab as a cross-origin call.
|
|
10
|
+
*
|
|
11
|
+
* **Absolute URL:** If `CHAT_USER_LIST` is a full `http(s)://...` string, it is used as-is
|
|
12
|
+
* (visible in Network as that host).
|
|
13
|
+
*
|
|
14
|
+
* **Legacy:** Otherwise the path is joined with `CHAT_HOST_URL` and optional `CHAT_HOST_PORT`.
|
|
15
|
+
*/
|
|
3
16
|
export declare function buildUserListUrl(config: ChatConfig): string;
|
package/dist/config/index.js
CHANGED
|
@@ -83,9 +83,29 @@ export function loadChatConfig() {
|
|
|
83
83
|
widgetDefaultSize: parseSizeRatio(getEnvVar('CHAT_WIDGET_DEFAULT_SIZE'), 0.45),
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Resolves the URL used by `fetchUsers`.
|
|
88
|
+
*
|
|
89
|
+
* **Same-origin / internal proxy (recommended):** If `CHAT_USER_LIST` starts with `/`
|
|
90
|
+
* (e.g. `/api/v1/chat/users`), the browser only requests your app origin. The real
|
|
91
|
+
* backend URL stays on the server (Next.js route, BFF, etc.) and does not appear in
|
|
92
|
+
* the client Network tab as a cross-origin call.
|
|
93
|
+
*
|
|
94
|
+
* **Absolute URL:** If `CHAT_USER_LIST` is a full `http(s)://...` string, it is used as-is
|
|
95
|
+
* (visible in Network as that host).
|
|
96
|
+
*
|
|
97
|
+
* **Legacy:** Otherwise the path is joined with `CHAT_HOST_URL` and optional `CHAT_HOST_PORT`.
|
|
98
|
+
*/
|
|
86
99
|
export function buildUserListUrl(config) {
|
|
100
|
+
const raw = config.userListEndpoint.trim();
|
|
101
|
+
if (raw.startsWith('/')) {
|
|
102
|
+
return raw;
|
|
103
|
+
}
|
|
104
|
+
if (/^https?:\/\//i.test(raw)) {
|
|
105
|
+
return raw;
|
|
106
|
+
}
|
|
87
107
|
const base = config.hostUrl.replace(/\/$/, '');
|
|
88
|
-
const endpoint =
|
|
108
|
+
const endpoint = raw.replace(/^\//, '');
|
|
89
109
|
if (config.hostPort !== undefined) {
|
|
90
110
|
return `${base}:${config.hostPort}/${endpoint}`;
|
|
91
111
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export async function fetchUsers(url) {
|
|
2
|
+
const sameOriginPath = url.startsWith('/');
|
|
2
3
|
const response = await fetch(url, {
|
|
3
4
|
method: 'GET',
|
|
4
5
|
headers: {
|
|
5
6
|
'Content-Type': 'application/json',
|
|
6
7
|
},
|
|
8
|
+
credentials: sameOriginPath ? 'same-origin' : 'omit',
|
|
7
9
|
});
|
|
8
10
|
if (!response.ok) {
|
|
9
11
|
throw new Error(`[ChatWidget] Failed to fetch users: ${response.status} ${response.statusText}`);
|