This repository has no description
16 kB
560 lines
1import {
2 QueryClient,
3 CardClient,
4 CollectionClient,
5 ConnectionClient,
6 UserClient,
7 FeedClient,
8 NotificationClient,
9} from './clients';
10import type {
11 // Request types
12 AddUrlToLibraryRequest,
13 AddCardToLibraryRequest,
14 AddCardToCollectionRequest,
15 UpdateNoteCardRequest,
16 UpdateUrlCardAssociationsRequest,
17 RemoveCardFromLibraryRequest,
18 RemoveCardFromCollectionRequest,
19 CreateCollectionRequest,
20 UpdateCollectionRequest,
21 DeleteCollectionRequest,
22 LoginWithAppPasswordRequest,
23 InitiateOAuthSignInRequest,
24 CompleteOAuthSignInRequest,
25 RefreshAccessTokenRequest,
26 GenerateExtensionTokensRequest,
27 GetUrlMetadataParams,
28 GetMyUrlCardsParams,
29 GetCollectionPageParams,
30 GetCollectionPageByAtUriParams,
31 GetMyCollectionsParams,
32 GetGlobalFeedParams,
33 GetFollowingFeedParams,
34 // Response types
35 AddUrlToLibraryResponse,
36 AddCardToLibraryResponse,
37 AddCardToCollectionResponse,
38 UpdateNoteCardResponse,
39 UpdateUrlCardAssociationsResponse,
40 RemoveCardFromLibraryResponse,
41 RemoveCardFromCollectionResponse,
42 CreateCollectionResponse,
43 UpdateCollectionResponse,
44 DeleteCollectionResponse,
45 LoginWithAppPasswordResponse,
46 InitiateOAuthSignInResponse,
47 CompleteOAuthSignInResponse,
48 RefreshAccessTokenResponse,
49 GenerateExtensionTokensResponse,
50 GetUrlMetadataResponse,
51 GetUrlCardViewResponse,
52 GetLibrariesForCardResponse,
53 GetCollectionPageResponse,
54 GetGlobalFeedResponse,
55 GetCollectionsResponse,
56 GetCollectionsParams,
57 GetUrlCardsParams,
58 GetUrlCardsResponse,
59 GetProfileResponse,
60 GetProfileParams,
61 GetUrlStatusForMyLibraryParams,
62 GetUrlStatusForMyLibraryResponse,
63 GetLibrariesForUrlParams,
64 GetLibrariesForUrlResponse,
65 GetNoteCardsForUrlParams,
66 GetNoteCardsForUrlResponse,
67 GetCollectionsForUrlParams,
68 GetCollectionsForUrlResponse,
69 GetSimilarUrlsForUrlParams,
70 GetSimilarUrlsForUrlResponse,
71 SemanticSearchUrlsParams,
72 SemanticSearchUrlsResponse,
73 SearchBskyPostsForUrlParams,
74 SearchBskyPostsForUrlResponse,
75 SearchAtProtoAccountsParams,
76 SearchAtProtoAccountsResponse,
77 SearchLeafletDocsForUrlParams,
78 SearchLeafletDocsForUrlResponse,
79 GetMyNotificationsParams,
80 GetMyNotificationsResponse,
81 GetUnreadNotificationCountResponse,
82 MarkNotificationsAsReadRequest,
83 MarkNotificationsAsReadResponse,
84 MarkAllNotificationsAsReadResponse,
85 GetGemActivityFeedParams,
86 SearchCollectionsParams,
87 GetOpenCollectionsWithContributorParams,
88 FollowTargetRequest,
89 FollowTargetResponse,
90 GetFollowingUsersParams,
91 GetFollowingUsersResponse,
92 GetFollowersParams,
93 GetFollowersResponse,
94 GetFollowingCollectionsParams,
95 GetFollowingCollectionsResponse,
96 GetCollectionFollowersParams,
97 GetCollectionFollowersResponse,
98 GetFollowingCountParams,
99 GetFollowersCountParams,
100 GetFollowingCollectionsCountParams,
101 GetCollectionFollowersCountParams,
102 GetFollowCountResponse,
103 GetCollectionContributorsParams,
104 GetCollectionContributorsResponse,
105 // Connection types
106 CreateConnectionResponse,
107 UpdateConnectionRequest,
108 UpdateConnectionResponse,
109 DeleteConnectionRequest,
110 DeleteConnectionResponse,
111 GetConnectionsParams,
112 GetConnectionsResponse,
113 GetConnectionsForUrlParams,
114 GetConnectionsForUrlResponse,
115 ConnectionType,
116 // Search types
117 SearchUrlsParams,
118 SearchUrlsResponse,
119 // Graph types
120 GetGraphDataParams,
121 GetGraphDataResponse,
122 GetUrlGraphDataParams,
123} from '@semble/types';
124
125// Main API Client class using composition
126export class ApiClient {
127 private queryClient: QueryClient;
128 private cardClient: CardClient;
129 private collectionClient: CollectionClient;
130 private connectionClient: ConnectionClient;
131 private userClient: UserClient;
132 private feedClient: FeedClient;
133 private notificationClient: NotificationClient;
134
135 constructor(
136 private baseUrl: string,
137 accessToken?: string,
138 ) {
139 this.queryClient = new QueryClient(baseUrl, accessToken);
140 this.cardClient = new CardClient(baseUrl, accessToken);
141 this.collectionClient = new CollectionClient(baseUrl, accessToken);
142 this.connectionClient = new ConnectionClient(baseUrl, accessToken);
143 this.userClient = new UserClient(baseUrl, accessToken);
144 this.feedClient = new FeedClient(baseUrl, accessToken);
145 this.notificationClient = new NotificationClient(baseUrl, accessToken);
146 }
147
148 // Query operations - delegate to QueryClient
149 async getUrlMetadata(
150 params: GetUrlMetadataParams,
151 ): Promise<GetUrlMetadataResponse> {
152 return this.queryClient.getUrlMetadata(params);
153 }
154
155 async getMyUrlCards(
156 params?: GetMyUrlCardsParams,
157 ): Promise<GetUrlCardsResponse> {
158 return this.queryClient.getMyUrlCards(params);
159 }
160
161 async getUrlCards(params: GetUrlCardsParams): Promise<GetUrlCardsResponse> {
162 return this.queryClient.getUserUrlCards(params);
163 }
164
165 async getUrlCardView(cardId: string): Promise<GetUrlCardViewResponse> {
166 return this.queryClient.getUrlCardView(cardId);
167 }
168
169 async getLibrariesForCard(
170 cardId: string,
171 ): Promise<GetLibrariesForCardResponse> {
172 return this.queryClient.getLibrariesForCard(cardId);
173 }
174
175 async getMyProfile(params?: {
176 includeStats?: boolean;
177 }): Promise<GetProfileResponse> {
178 return this.queryClient.getMyProfile(params);
179 }
180
181 async getProfile(params: GetProfileParams): Promise<GetProfileResponse> {
182 return this.queryClient.getUserProfile(params);
183 }
184
185 async getCollectionPage(
186 collectionId: string,
187 params?: GetCollectionPageParams,
188 ): Promise<GetCollectionPageResponse> {
189 return this.queryClient.getCollectionPage(collectionId, params);
190 }
191
192 async getCollectionPageByAtUri(
193 params: GetCollectionPageByAtUriParams,
194 ): Promise<GetCollectionPageResponse> {
195 return this.queryClient.getCollectionPageByAtUri(params);
196 }
197
198 async getMyCollections(
199 params?: GetMyCollectionsParams,
200 ): Promise<GetCollectionsResponse> {
201 return this.queryClient.getMyCollections(params);
202 }
203
204 async getCollections(
205 params: GetCollectionsParams,
206 ): Promise<GetCollectionsResponse> {
207 return this.queryClient.getUserCollections(params);
208 }
209
210 async getOpenCollectionsWithContributor(
211 params: GetOpenCollectionsWithContributorParams,
212 ): Promise<GetCollectionsResponse> {
213 return this.queryClient.getOpenCollectionsWithContributor(params);
214 }
215
216 async getUrlStatusForMyLibrary(
217 params: GetUrlStatusForMyLibraryParams,
218 ): Promise<GetUrlStatusForMyLibraryResponse> {
219 return this.queryClient.getUrlStatusForMyLibrary(params);
220 }
221
222 async getLibrariesForUrl(
223 params: GetLibrariesForUrlParams,
224 ): Promise<GetLibrariesForUrlResponse> {
225 return this.queryClient.getLibrariesForUrl(params);
226 }
227
228 async getNoteCardsForUrl(
229 params: GetNoteCardsForUrlParams,
230 ): Promise<GetNoteCardsForUrlResponse> {
231 return this.queryClient.getNoteCardsForUrl(params);
232 }
233
234 async getCollectionsForUrl(
235 params: GetCollectionsForUrlParams,
236 ): Promise<GetCollectionsForUrlResponse> {
237 return this.queryClient.getCollectionsForUrl(params);
238 }
239
240 async getSimilarUrlsForUrl(
241 params: GetSimilarUrlsForUrlParams,
242 ): Promise<GetSimilarUrlsForUrlResponse> {
243 return this.queryClient.getSimilarUrlsForUrl(params);
244 }
245
246 async semanticSearchUrls(
247 params: SemanticSearchUrlsParams,
248 ): Promise<SemanticSearchUrlsResponse> {
249 return this.queryClient.semanticSearchUrls(params);
250 }
251
252 async searchBskyPosts(
253 params: SearchBskyPostsForUrlParams,
254 ): Promise<SearchBskyPostsForUrlResponse> {
255 return this.queryClient.searchBskyPosts(params);
256 }
257
258 async searchAtProtoAccounts(
259 params: SearchAtProtoAccountsParams,
260 ): Promise<SearchAtProtoAccountsResponse> {
261 return this.queryClient.searchAtProtoAccounts(params);
262 }
263
264 async searchLeafletDocs(
265 params: SearchLeafletDocsForUrlParams,
266 ): Promise<SearchLeafletDocsForUrlResponse> {
267 return this.queryClient.searchLeafletDocs(params);
268 }
269
270 // Follow query operations - delegate to QueryClient
271 async getFollowingUsers(
272 params: GetFollowingUsersParams,
273 ): Promise<GetFollowingUsersResponse> {
274 return this.queryClient.getFollowingUsers(params);
275 }
276
277 async getFollowers(
278 params: GetFollowersParams,
279 ): Promise<GetFollowersResponse> {
280 return this.queryClient.getFollowers(params);
281 }
282
283 async getFollowingCollections(
284 params: GetFollowingCollectionsParams,
285 ): Promise<GetFollowingCollectionsResponse> {
286 return this.queryClient.getFollowingCollections(params);
287 }
288
289 async getCollectionFollowers(
290 params: GetCollectionFollowersParams,
291 ): Promise<GetCollectionFollowersResponse> {
292 return this.queryClient.getCollectionFollowers(params);
293 }
294
295 async getFollowingCount(
296 params: GetFollowingCountParams,
297 ): Promise<GetFollowCountResponse> {
298 return this.queryClient.getFollowingCount(params);
299 }
300
301 async getFollowersCount(
302 params: GetFollowersCountParams,
303 ): Promise<GetFollowCountResponse> {
304 return this.queryClient.getFollowersCount(params);
305 }
306
307 async getFollowingCollectionsCount(
308 params: GetFollowingCollectionsCountParams,
309 ): Promise<GetFollowCountResponse> {
310 return this.queryClient.getFollowingCollectionsCount(params);
311 }
312
313 async getCollectionFollowersCount(
314 params: GetCollectionFollowersCountParams,
315 ): Promise<GetFollowCountResponse> {
316 return this.queryClient.getCollectionFollowersCount(params);
317 }
318
319 async getCollectionContributors(
320 params: GetCollectionContributorsParams,
321 ): Promise<GetCollectionContributorsResponse> {
322 return this.queryClient.getCollectionContributors(params);
323 }
324
325 // Card operations - delegate to CardClient
326 async addUrlToLibrary(
327 request: AddUrlToLibraryRequest,
328 ): Promise<AddUrlToLibraryResponse> {
329 return this.cardClient.addUrlToLibrary(request);
330 }
331
332 async addCardToLibrary(
333 request: AddCardToLibraryRequest,
334 ): Promise<AddCardToLibraryResponse> {
335 return this.cardClient.addCardToLibrary(request);
336 }
337
338 async addCardToCollection(
339 request: AddCardToCollectionRequest,
340 ): Promise<AddCardToCollectionResponse> {
341 return this.cardClient.addCardToCollection(request);
342 }
343
344 async updateNoteCard(
345 request: UpdateNoteCardRequest,
346 ): Promise<UpdateNoteCardResponse> {
347 return this.cardClient.updateNoteCard(request);
348 }
349
350 async updateUrlCardAssociations(
351 request: UpdateUrlCardAssociationsRequest,
352 ): Promise<UpdateUrlCardAssociationsResponse> {
353 return this.cardClient.updateUrlCardAssociations(request);
354 }
355
356 async removeCardFromLibrary(
357 request: RemoveCardFromLibraryRequest,
358 ): Promise<RemoveCardFromLibraryResponse> {
359 return this.cardClient.removeCardFromLibrary(request);
360 }
361
362 async removeCardFromCollection(
363 request: RemoveCardFromCollectionRequest,
364 ): Promise<RemoveCardFromCollectionResponse> {
365 return this.cardClient.removeCardFromCollection(request);
366 }
367
368 // Collection operations - delegate to CollectionClient
369 async createCollection(
370 request: CreateCollectionRequest,
371 ): Promise<CreateCollectionResponse> {
372 return this.collectionClient.createCollection(request);
373 }
374
375 async updateCollection(
376 request: UpdateCollectionRequest,
377 ): Promise<UpdateCollectionResponse> {
378 return this.collectionClient.updateCollection(request);
379 }
380
381 async deleteCollection(
382 request: DeleteCollectionRequest,
383 ): Promise<DeleteCollectionResponse> {
384 return this.collectionClient.deleteCollection(request);
385 }
386
387 async searchCollections(
388 params?: SearchCollectionsParams,
389 ): Promise<GetCollectionsResponse> {
390 return this.collectionClient.searchCollections(params);
391 }
392
393 // User operations - delegate to UserClient
394 async loginWithAppPassword(
395 request: LoginWithAppPasswordRequest,
396 ): Promise<LoginWithAppPasswordResponse> {
397 return this.userClient.loginWithAppPassword(request);
398 }
399
400 async initiateOAuthSignIn(
401 request?: InitiateOAuthSignInRequest,
402 ): Promise<InitiateOAuthSignInResponse> {
403 return this.userClient.initiateOAuthSignIn(request);
404 }
405
406 async completeOAuthSignIn(
407 request: CompleteOAuthSignInRequest,
408 ): Promise<CompleteOAuthSignInResponse> {
409 return this.userClient.completeOAuthSignIn(request);
410 }
411
412 async refreshAccessToken(
413 request: RefreshAccessTokenRequest,
414 ): Promise<RefreshAccessTokenResponse> {
415 return this.userClient.refreshAccessToken(request);
416 }
417
418 async generateExtensionTokens(
419 request?: GenerateExtensionTokensRequest,
420 ): Promise<GenerateExtensionTokensResponse> {
421 return this.userClient.generateExtensionTokens(request);
422 }
423
424 async logout(): Promise<{ success: boolean; message: string }> {
425 return this.userClient.logout();
426 }
427
428 async followTarget(
429 request: FollowTargetRequest,
430 ): Promise<FollowTargetResponse> {
431 return this.userClient.followTarget(request);
432 }
433
434 async unfollowTarget(
435 targetId: string,
436 targetType: 'USER' | 'COLLECTION',
437 ): Promise<void> {
438 return this.userClient.unfollowTarget(targetId, targetType);
439 }
440
441 // Feed operations - delegate to FeedClient
442 async getGlobalFeed(
443 params?: GetGlobalFeedParams,
444 ): Promise<GetGlobalFeedResponse> {
445 return this.feedClient.getGlobalFeed(params);
446 }
447
448 async getGemsActivityFeed(
449 params?: GetGemActivityFeedParams,
450 ): Promise<GetGlobalFeedResponse> {
451 return this.feedClient.getGemsActivityFeed(params);
452 }
453
454 async getFollowingFeed(
455 params?: GetFollowingFeedParams,
456 ): Promise<GetGlobalFeedResponse> {
457 return this.feedClient.getFollowingFeed(params);
458 }
459
460 // Notification operations - delegate to NotificationClient
461 async getMyNotifications(
462 params?: GetMyNotificationsParams,
463 ): Promise<GetMyNotificationsResponse> {
464 return this.notificationClient.getMyNotifications(params);
465 }
466
467 async getUnreadNotificationCount(): Promise<GetUnreadNotificationCountResponse> {
468 return this.notificationClient.getUnreadNotificationCount();
469 }
470
471 async markNotificationsAsRead(
472 request: MarkNotificationsAsReadRequest,
473 ): Promise<MarkNotificationsAsReadResponse> {
474 return this.notificationClient.markNotificationsAsRead(request);
475 }
476
477 async markAllNotificationsAsRead(): Promise<MarkAllNotificationsAsReadResponse> {
478 return this.notificationClient.markAllNotificationsAsRead();
479 }
480
481 // Connection operations
482 async getConnections(
483 params: GetConnectionsParams,
484 ): Promise<GetConnectionsResponse> {
485 return this.queryClient.getConnections(params);
486 }
487
488 async createConnection(params: {
489 sourceUrl: string;
490 targetUrl: string;
491 connectionType?: ConnectionType;
492 note?: string;
493 }): Promise<CreateConnectionResponse> {
494 return this.connectionClient.createConnection(params);
495 }
496
497 async updateConnection(
498 request: UpdateConnectionRequest,
499 ): Promise<UpdateConnectionResponse> {
500 return this.connectionClient.updateConnection(request);
501 }
502
503 async deleteConnection(
504 request: DeleteConnectionRequest,
505 ): Promise<DeleteConnectionResponse> {
506 return this.connectionClient.deleteConnection(request);
507 }
508
509 async getConnectionsForUrl(
510 params: GetConnectionsForUrlParams,
511 ): Promise<GetConnectionsForUrlResponse> {
512 return this.queryClient.getConnectionsForUrl(params);
513 }
514
515 // Search operations
516 async searchUrls(params: SearchUrlsParams): Promise<SearchUrlsResponse> {
517 return this.queryClient.searchUrls(params);
518 }
519
520 // Graph operations
521 async getGraphData(
522 params?: GetGraphDataParams,
523 ): Promise<GetGraphDataResponse> {
524 return this.queryClient.getGraphData(params);
525 }
526
527 async getUserGraphData(params: {
528 identifier: string;
529 page?: number;
530 limit?: number;
531 }): Promise<GetGraphDataResponse> {
532 return this.queryClient.getUserGraphData(params);
533 }
534
535 async getUrlGraphData(
536 params: GetUrlGraphDataParams,
537 ): Promise<GetGraphDataResponse> {
538 return this.queryClient.getUrlGraphData(params);
539 }
540}
541
542// Re-export types for convenience
543export * from '@semble/types';
544
545// Factory functions for different client types
546export const createApiClient = () => {
547 return new ApiClient(
548 process.env.NEXT_PUBLIC_API_BASE_URL || 'http://127.0.0.1:3000',
549 );
550};
551
552export const createServerApiClient = (accessToken?: string) => {
553 return new ApiClient(
554 process.env.NEXT_PUBLIC_API_BASE_URL || 'http://127.0.0.1:3000',
555 accessToken,
556 );
557};
558
559// Default client instance for backward compatibility
560export const apiClient = createApiClient();