@thrillee/aegischat 0.1.9 → 0.1.11
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/dist/index.js +32 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useChat.ts +2 -2
- package/src/services/api.ts +105 -77
package/src/services/api.ts
CHANGED
|
@@ -14,10 +14,10 @@ import type {
|
|
|
14
14
|
ReactionSummary,
|
|
15
15
|
FileAttachment,
|
|
16
16
|
UploadUrlResponse,
|
|
17
|
-
} from
|
|
17
|
+
} from "../types";
|
|
18
18
|
|
|
19
|
-
let baseUrl =
|
|
20
|
-
let getAccessToken: () => Promise<string> | string = () =>
|
|
19
|
+
let baseUrl = "";
|
|
20
|
+
let getAccessToken: () => Promise<string> | string = () => "";
|
|
21
21
|
let onUnauthorized: (() => void) | undefined;
|
|
22
22
|
|
|
23
23
|
export function configureApiClient(config: {
|
|
@@ -32,16 +32,16 @@ export function configureApiClient(config: {
|
|
|
32
32
|
|
|
33
33
|
async function fetchWithAuth<T>(
|
|
34
34
|
path: string,
|
|
35
|
-
options: RequestInit = {}
|
|
35
|
+
options: RequestInit = {},
|
|
36
36
|
): Promise<T> {
|
|
37
|
-
const token = await (typeof getAccessToken ===
|
|
38
|
-
? getAccessToken()
|
|
37
|
+
const token = await (typeof getAccessToken === "function"
|
|
38
|
+
? getAccessToken()
|
|
39
39
|
: getAccessToken);
|
|
40
40
|
|
|
41
41
|
const response = await fetch(`${baseUrl}${path}`, {
|
|
42
42
|
...options,
|
|
43
43
|
headers: {
|
|
44
|
-
|
|
44
|
+
"Content-Type": "application/json",
|
|
45
45
|
Authorization: `Bearer ${token}`,
|
|
46
46
|
...options.headers,
|
|
47
47
|
},
|
|
@@ -49,7 +49,7 @@ async function fetchWithAuth<T>(
|
|
|
49
49
|
|
|
50
50
|
if (response.status === 401) {
|
|
51
51
|
onUnauthorized?.();
|
|
52
|
-
throw new Error(
|
|
52
|
+
throw new Error("Unauthorized");
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
if (!response.ok) {
|
|
@@ -66,10 +66,10 @@ export const chatApi = {
|
|
|
66
66
|
*/
|
|
67
67
|
async connect(
|
|
68
68
|
params: ChatConnectParams,
|
|
69
|
-
signal?: AbortSignal
|
|
69
|
+
signal?: AbortSignal,
|
|
70
70
|
): Promise<ApiResponse<ChatSession>> {
|
|
71
|
-
return fetchWithAuth(
|
|
72
|
-
method:
|
|
71
|
+
return fetchWithAuth("/chat/connect", {
|
|
72
|
+
method: "POST",
|
|
73
73
|
body: JSON.stringify(params),
|
|
74
74
|
signal,
|
|
75
75
|
});
|
|
@@ -80,10 +80,10 @@ export const chatApi = {
|
|
|
80
80
|
*/
|
|
81
81
|
async refreshToken(
|
|
82
82
|
refreshToken: string,
|
|
83
|
-
signal?: AbortSignal
|
|
83
|
+
signal?: AbortSignal,
|
|
84
84
|
): Promise<ApiResponse<{ access_token: string; expires_in: number }>> {
|
|
85
|
-
return fetchWithAuth(
|
|
86
|
-
method:
|
|
85
|
+
return fetchWithAuth("/chat/refresh", {
|
|
86
|
+
method: "POST",
|
|
87
87
|
body: JSON.stringify({ refresh_token: refreshToken }),
|
|
88
88
|
signal,
|
|
89
89
|
});
|
|
@@ -96,20 +96,23 @@ export const channelsApi = {
|
|
|
96
96
|
*/
|
|
97
97
|
async list(
|
|
98
98
|
options: { type?: string; limit?: number } = {},
|
|
99
|
-
signal?: AbortSignal
|
|
99
|
+
signal?: AbortSignal,
|
|
100
100
|
): Promise<ApiResponse<{ channels: ChannelListItem[] }>> {
|
|
101
101
|
const params = new URLSearchParams();
|
|
102
|
-
if (options.type) params.append(
|
|
103
|
-
if (options.limit) params.append(
|
|
104
|
-
const query = params.toString() ? `?${params.toString()}` :
|
|
105
|
-
return fetchWithAuth(`/
|
|
102
|
+
if (options.type) params.append("type", options.type);
|
|
103
|
+
if (options.limit) params.append("limit", String(options.limit));
|
|
104
|
+
const query = params.toString() ? `?${params.toString()}` : "";
|
|
105
|
+
return fetchWithAuth(`/channels${query}`, { signal });
|
|
106
106
|
},
|
|
107
107
|
|
|
108
108
|
/**
|
|
109
109
|
* Get channel by ID
|
|
110
110
|
*/
|
|
111
|
-
async get(
|
|
112
|
-
|
|
111
|
+
async get(
|
|
112
|
+
channelId: string,
|
|
113
|
+
signal?: AbortSignal,
|
|
114
|
+
): Promise<ApiResponse<Channel>> {
|
|
115
|
+
return fetchWithAuth(`/channels/${channelId}`, { signal });
|
|
113
116
|
},
|
|
114
117
|
|
|
115
118
|
/**
|
|
@@ -117,10 +120,10 @@ export const channelsApi = {
|
|
|
117
120
|
*/
|
|
118
121
|
async getOrCreateDM(
|
|
119
122
|
userId: string,
|
|
120
|
-
signal?: AbortSignal
|
|
123
|
+
signal?: AbortSignal,
|
|
121
124
|
): Promise<ApiResponse<Channel>> {
|
|
122
|
-
return fetchWithAuth(
|
|
123
|
-
method:
|
|
125
|
+
return fetchWithAuth("/channels/dm", {
|
|
126
|
+
method: "POST",
|
|
124
127
|
body: JSON.stringify({ user_id: userId }),
|
|
125
128
|
signal,
|
|
126
129
|
});
|
|
@@ -130,11 +133,16 @@ export const channelsApi = {
|
|
|
130
133
|
* Create channel
|
|
131
134
|
*/
|
|
132
135
|
async create(
|
|
133
|
-
data: {
|
|
134
|
-
|
|
136
|
+
data: {
|
|
137
|
+
name: string;
|
|
138
|
+
type?: string;
|
|
139
|
+
description?: string;
|
|
140
|
+
metadata?: Record<string, unknown>;
|
|
141
|
+
},
|
|
142
|
+
signal?: AbortSignal,
|
|
135
143
|
): Promise<ApiResponse<Channel>> {
|
|
136
|
-
return fetchWithAuth(
|
|
137
|
-
method:
|
|
144
|
+
return fetchWithAuth("/api/v1/channels", {
|
|
145
|
+
method: "POST",
|
|
138
146
|
body: JSON.stringify(data),
|
|
139
147
|
signal,
|
|
140
148
|
});
|
|
@@ -145,10 +153,10 @@ export const channelsApi = {
|
|
|
145
153
|
*/
|
|
146
154
|
async markAsRead(
|
|
147
155
|
channelId: string,
|
|
148
|
-
signal?: AbortSignal
|
|
156
|
+
signal?: AbortSignal,
|
|
149
157
|
): Promise<ApiResponse<{ unread_count: number }>> {
|
|
150
158
|
return fetchWithAuth(`/api/v1/channels/${channelId}/read`, {
|
|
151
|
-
method:
|
|
159
|
+
method: "POST",
|
|
152
160
|
signal,
|
|
153
161
|
});
|
|
154
162
|
},
|
|
@@ -158,9 +166,9 @@ export const channelsApi = {
|
|
|
158
166
|
*/
|
|
159
167
|
async getMembers(
|
|
160
168
|
channelId: string,
|
|
161
|
-
signal?: AbortSignal
|
|
169
|
+
signal?: AbortSignal,
|
|
162
170
|
): Promise<ApiResponse<{ members: UserSummary[] }>> {
|
|
163
|
-
return fetchWithAuth(`/
|
|
171
|
+
return fetchWithAuth(`/channels/${channelId}/members`, { signal });
|
|
164
172
|
},
|
|
165
173
|
|
|
166
174
|
/**
|
|
@@ -168,11 +176,15 @@ export const channelsApi = {
|
|
|
168
176
|
*/
|
|
169
177
|
async update(
|
|
170
178
|
channelId: string,
|
|
171
|
-
data: {
|
|
172
|
-
|
|
179
|
+
data: {
|
|
180
|
+
name?: string;
|
|
181
|
+
description?: string;
|
|
182
|
+
metadata?: Record<string, unknown>;
|
|
183
|
+
},
|
|
184
|
+
signal?: AbortSignal,
|
|
173
185
|
): Promise<ApiResponse<Channel>> {
|
|
174
186
|
return fetchWithAuth(`/api/v1/channels/${channelId}`, {
|
|
175
|
-
method:
|
|
187
|
+
method: "PATCH",
|
|
176
188
|
body: JSON.stringify(data),
|
|
177
189
|
signal,
|
|
178
190
|
});
|
|
@@ -186,13 +198,15 @@ export const messagesApi = {
|
|
|
186
198
|
async list(
|
|
187
199
|
channelId: string,
|
|
188
200
|
options: { limit?: number; before?: string } = {},
|
|
189
|
-
signal?: AbortSignal
|
|
201
|
+
signal?: AbortSignal,
|
|
190
202
|
): Promise<ApiResponse<MessagesResponse>> {
|
|
191
203
|
const params = new URLSearchParams();
|
|
192
|
-
if (options.limit) params.append(
|
|
193
|
-
if (options.before) params.append(
|
|
194
|
-
const query = params.toString() ? `?${params.toString()}` :
|
|
195
|
-
return fetchWithAuth(`/
|
|
204
|
+
if (options.limit) params.append("limit", String(options.limit));
|
|
205
|
+
if (options.before) params.append("before", options.before);
|
|
206
|
+
const query = params.toString() ? `?${params.toString()}` : "";
|
|
207
|
+
return fetchWithAuth(`/channels/${channelId}/messages${query}`, {
|
|
208
|
+
signal,
|
|
209
|
+
});
|
|
196
210
|
},
|
|
197
211
|
|
|
198
212
|
/**
|
|
@@ -200,11 +214,17 @@ export const messagesApi = {
|
|
|
200
214
|
*/
|
|
201
215
|
async send(
|
|
202
216
|
channelId: string,
|
|
203
|
-
data: {
|
|
204
|
-
|
|
217
|
+
data: {
|
|
218
|
+
content: string;
|
|
219
|
+
type?: string;
|
|
220
|
+
parent_id?: string;
|
|
221
|
+
metadata?: Record<string, unknown>;
|
|
222
|
+
file_ids?: string[];
|
|
223
|
+
},
|
|
224
|
+
signal?: AbortSignal,
|
|
205
225
|
): Promise<ApiResponse<Message>> {
|
|
206
|
-
return fetchWithAuth(`/
|
|
207
|
-
method:
|
|
226
|
+
return fetchWithAuth(`/channels/${channelId}/messages`, {
|
|
227
|
+
method: "POST",
|
|
208
228
|
body: JSON.stringify(data),
|
|
209
229
|
signal,
|
|
210
230
|
});
|
|
@@ -217,10 +237,10 @@ export const messagesApi = {
|
|
|
217
237
|
channelId: string,
|
|
218
238
|
messageId: string,
|
|
219
239
|
data: { content?: string; metadata?: Record<string, unknown> },
|
|
220
|
-
signal?: AbortSignal
|
|
240
|
+
signal?: AbortSignal,
|
|
221
241
|
): Promise<ApiResponse<Message>> {
|
|
222
|
-
return fetchWithAuth(`/
|
|
223
|
-
method:
|
|
242
|
+
return fetchWithAuth(`/channels/${channelId}/messages/${messageId}`, {
|
|
243
|
+
method: "PATCH",
|
|
224
244
|
body: JSON.stringify(data),
|
|
225
245
|
signal,
|
|
226
246
|
});
|
|
@@ -232,10 +252,10 @@ export const messagesApi = {
|
|
|
232
252
|
async delete(
|
|
233
253
|
channelId: string,
|
|
234
254
|
messageId: string,
|
|
235
|
-
signal?: AbortSignal
|
|
255
|
+
signal?: AbortSignal,
|
|
236
256
|
): Promise<ApiResponse<{ success: boolean }>> {
|
|
237
|
-
return fetchWithAuth(`/
|
|
238
|
-
method:
|
|
257
|
+
return fetchWithAuth(`/channels/${channelId}/messages/${messageId}`, {
|
|
258
|
+
method: "DELETE",
|
|
239
259
|
signal,
|
|
240
260
|
});
|
|
241
261
|
},
|
|
@@ -245,10 +265,10 @@ export const messagesApi = {
|
|
|
245
265
|
*/
|
|
246
266
|
async markDelivered(
|
|
247
267
|
channelId: string,
|
|
248
|
-
signal?: AbortSignal
|
|
268
|
+
signal?: AbortSignal,
|
|
249
269
|
): Promise<ApiResponse<{ success: boolean }>> {
|
|
250
|
-
return fetchWithAuth(`/
|
|
251
|
-
method:
|
|
270
|
+
return fetchWithAuth(`/channels/${channelId}/messages/delivered`, {
|
|
271
|
+
method: "POST",
|
|
252
272
|
signal,
|
|
253
273
|
});
|
|
254
274
|
},
|
|
@@ -258,10 +278,10 @@ export const messagesApi = {
|
|
|
258
278
|
*/
|
|
259
279
|
async markRead(
|
|
260
280
|
channelId: string,
|
|
261
|
-
signal?: AbortSignal
|
|
281
|
+
signal?: AbortSignal,
|
|
262
282
|
): Promise<ApiResponse<{ success: boolean }>> {
|
|
263
|
-
return fetchWithAuth(`/
|
|
264
|
-
method:
|
|
283
|
+
return fetchWithAuth(`/channels/${channelId}/messages/read`, {
|
|
284
|
+
method: "POST",
|
|
265
285
|
signal,
|
|
266
286
|
});
|
|
267
287
|
},
|
|
@@ -275,13 +295,16 @@ export const reactionsApi = {
|
|
|
275
295
|
channelId: string,
|
|
276
296
|
messageId: string,
|
|
277
297
|
emoji: string,
|
|
278
|
-
signal?: AbortSignal
|
|
298
|
+
signal?: AbortSignal,
|
|
279
299
|
): Promise<ApiResponse<{ reactions: ReactionSummary[] }>> {
|
|
280
|
-
return fetchWithAuth(
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
300
|
+
return fetchWithAuth(
|
|
301
|
+
`/channels/${channelId}/messages/${messageId}/reactions`,
|
|
302
|
+
{
|
|
303
|
+
method: "POST",
|
|
304
|
+
body: JSON.stringify({ emoji }),
|
|
305
|
+
signal,
|
|
306
|
+
},
|
|
307
|
+
);
|
|
285
308
|
},
|
|
286
309
|
|
|
287
310
|
/**
|
|
@@ -291,11 +314,11 @@ export const reactionsApi = {
|
|
|
291
314
|
channelId: string,
|
|
292
315
|
messageId: string,
|
|
293
316
|
emoji: string,
|
|
294
|
-
signal?: AbortSignal
|
|
317
|
+
signal?: AbortSignal,
|
|
295
318
|
): Promise<ApiResponse<{ reactions: ReactionSummary[] }>> {
|
|
296
319
|
return fetchWithAuth(
|
|
297
|
-
`/
|
|
298
|
-
{ method:
|
|
320
|
+
`/channels/${channelId}/messages/${messageId}/reactions/${encodeURIComponent(emoji)}`,
|
|
321
|
+
{ method: "DELETE", signal },
|
|
299
322
|
);
|
|
300
323
|
},
|
|
301
324
|
};
|
|
@@ -306,10 +329,10 @@ export const filesApi = {
|
|
|
306
329
|
*/
|
|
307
330
|
async getUploadUrl(
|
|
308
331
|
data: { file_name: string; file_type: string; file_size: number },
|
|
309
|
-
signal?: AbortSignal
|
|
332
|
+
signal?: AbortSignal,
|
|
310
333
|
): Promise<ApiResponse<UploadUrlResponse>> {
|
|
311
|
-
return fetchWithAuth(
|
|
312
|
-
method:
|
|
334
|
+
return fetchWithAuth("/files/upload-url", {
|
|
335
|
+
method: "POST",
|
|
313
336
|
body: JSON.stringify(data),
|
|
314
337
|
signal,
|
|
315
338
|
});
|
|
@@ -320,10 +343,10 @@ export const filesApi = {
|
|
|
320
343
|
*/
|
|
321
344
|
async confirm(
|
|
322
345
|
fileId: string,
|
|
323
|
-
signal?: AbortSignal
|
|
346
|
+
signal?: AbortSignal,
|
|
324
347
|
): Promise<ApiResponse<{ file: FileAttachment }>> {
|
|
325
|
-
return fetchWithAuth(
|
|
326
|
-
method:
|
|
348
|
+
return fetchWithAuth("/files", {
|
|
349
|
+
method: "POST",
|
|
327
350
|
body: JSON.stringify({ file_id: fileId }),
|
|
328
351
|
signal,
|
|
329
352
|
});
|
|
@@ -334,9 +357,9 @@ export const filesApi = {
|
|
|
334
357
|
*/
|
|
335
358
|
async getDownloadUrl(
|
|
336
359
|
fileId: string,
|
|
337
|
-
signal?: AbortSignal
|
|
360
|
+
signal?: AbortSignal,
|
|
338
361
|
): Promise<ApiResponse<{ url: string; expires_at: string }>> {
|
|
339
|
-
return fetchWithAuth(`/
|
|
362
|
+
return fetchWithAuth(`/files/${fileId}/download`, { signal });
|
|
340
363
|
},
|
|
341
364
|
};
|
|
342
365
|
|
|
@@ -346,16 +369,21 @@ export const usersApi = {
|
|
|
346
369
|
*/
|
|
347
370
|
async search(
|
|
348
371
|
query: string,
|
|
349
|
-
signal?: AbortSignal
|
|
372
|
+
signal?: AbortSignal,
|
|
350
373
|
): Promise<ApiResponse<{ users: UserSummary[] }>> {
|
|
351
|
-
return fetchWithAuth(`/
|
|
374
|
+
return fetchWithAuth(`/users/search?q=${encodeURIComponent(query)}`, {
|
|
375
|
+
signal,
|
|
376
|
+
});
|
|
352
377
|
},
|
|
353
378
|
|
|
354
379
|
/**
|
|
355
380
|
* Get user by ID
|
|
356
381
|
*/
|
|
357
|
-
async get(
|
|
358
|
-
|
|
382
|
+
async get(
|
|
383
|
+
userId: string,
|
|
384
|
+
signal?: AbortSignal,
|
|
385
|
+
): Promise<ApiResponse<UserSummary>> {
|
|
386
|
+
return fetchWithAuth(`/users/${userId}`, { signal });
|
|
359
387
|
},
|
|
360
388
|
};
|
|
361
389
|
|