This repository has no description
12 kB
397 lines
1import {
2 QueryClient,
3 CardClient,
4 CollectionClient,
5 UserClient,
6 FeedClient,
7 NotificationClient,
8} from './clients';
9import type {
10 // Request types
11 AddUrlToLibraryRequest,
12 AddCardToLibraryRequest,
13 AddCardToCollectionRequest,
14 UpdateNoteCardRequest,
15 UpdateUrlCardAssociationsRequest,
16 RemoveCardFromLibraryRequest,
17 RemoveCardFromCollectionRequest,
18 CreateCollectionRequest,
19 UpdateCollectionRequest,
20 DeleteCollectionRequest,
21 LoginWithAppPasswordRequest,
22 InitiateOAuthSignInRequest,
23 CompleteOAuthSignInRequest,
24 RefreshAccessTokenRequest,
25 GenerateExtensionTokensRequest,
26 GetMyUrlCardsParams,
27 GetCollectionPageParams,
28 GetCollectionPageByAtUriParams,
29 GetMyCollectionsParams,
30 GetGlobalFeedParams,
31 // Response types
32 AddUrlToLibraryResponse,
33 AddCardToLibraryResponse,
34 AddCardToCollectionResponse,
35 UpdateNoteCardResponse,
36 UpdateUrlCardAssociationsResponse,
37 RemoveCardFromLibraryResponse,
38 RemoveCardFromCollectionResponse,
39 CreateCollectionResponse,
40 UpdateCollectionResponse,
41 DeleteCollectionResponse,
42 LoginWithAppPasswordResponse,
43 InitiateOAuthSignInResponse,
44 CompleteOAuthSignInResponse,
45 RefreshAccessTokenResponse,
46 GenerateExtensionTokensResponse,
47 GetUrlMetadataResponse,
48 GetUrlCardViewResponse,
49 GetLibrariesForCardResponse,
50 GetCollectionPageResponse,
51 GetGlobalFeedResponse,
52 GetCollectionsResponse,
53 GetCollectionsParams,
54 GetUrlCardsParams,
55 GetUrlCardsResponse,
56 GetProfileResponse,
57 GetProfileParams,
58 GetUrlStatusForMyLibraryParams,
59 GetUrlStatusForMyLibraryResponse,
60 GetLibrariesForUrlParams,
61 GetLibrariesForUrlResponse,
62 GetNoteCardsForUrlParams,
63 GetNoteCardsForUrlResponse,
64 GetCollectionsForUrlParams,
65 GetCollectionsForUrlResponse,
66 GetSimilarUrlsForUrlParams,
67 GetSimilarUrlsForUrlResponse,
68 SemanticSearchUrlsParams,
69 SemanticSearchUrlsResponse,
70 SearchBskyPostsForUrlParams,
71 SearchBskyPostsForUrlResponse,
72 SearchAtProtoAccountsParams,
73 SearchAtProtoAccountsResponse,
74 SearchLeafletDocsForUrlParams,
75 SearchLeafletDocsForUrlResponse,
76 GetMyNotificationsParams,
77 GetMyNotificationsResponse,
78 GetUnreadNotificationCountResponse,
79 MarkNotificationsAsReadRequest,
80 MarkNotificationsAsReadResponse,
81 MarkAllNotificationsAsReadResponse,
82 GetGemActivityFeedParams,
83 SearchCollectionsParams,
84 GetOpenCollectionsWithContributorParams,
85 FollowTargetRequest,
86 FollowTargetResponse,
87} from '@semble/types';
88
89// Main API Client class using composition
90export class ApiClient {
91 private queryClient: QueryClient;
92 private cardClient: CardClient;
93 private collectionClient: CollectionClient;
94 private userClient: UserClient;
95 private feedClient: FeedClient;
96 private notificationClient: NotificationClient;
97
98 constructor(
99 private baseUrl: string,
100 accessToken?: string,
101 ) {
102 this.queryClient = new QueryClient(baseUrl, accessToken);
103 this.cardClient = new CardClient(baseUrl, accessToken);
104 this.collectionClient = new CollectionClient(baseUrl, accessToken);
105 this.userClient = new UserClient(baseUrl, accessToken);
106 this.feedClient = new FeedClient(baseUrl, accessToken);
107 this.notificationClient = new NotificationClient(baseUrl, accessToken);
108 }
109
110 // Query operations - delegate to QueryClient
111 async getUrlMetadata(url: string): Promise<GetUrlMetadataResponse> {
112 return this.queryClient.getUrlMetadata(url);
113 }
114
115 async getMyUrlCards(
116 params?: GetMyUrlCardsParams,
117 ): Promise<GetUrlCardsResponse> {
118 return this.queryClient.getMyUrlCards(params);
119 }
120
121 async getUrlCards(params: GetUrlCardsParams): Promise<GetUrlCardsResponse> {
122 return this.queryClient.getUserUrlCards(params);
123 }
124
125 async getUrlCardView(cardId: string): Promise<GetUrlCardViewResponse> {
126 return this.queryClient.getUrlCardView(cardId);
127 }
128
129 async getLibrariesForCard(
130 cardId: string,
131 ): Promise<GetLibrariesForCardResponse> {
132 return this.queryClient.getLibrariesForCard(cardId);
133 }
134
135 async getMyProfile(): Promise<GetProfileResponse> {
136 return this.queryClient.getMyProfile();
137 }
138
139 async getProfile(params: GetProfileParams): Promise<GetProfileResponse> {
140 return this.queryClient.getUserProfile(params);
141 }
142
143 async getCollectionPage(
144 collectionId: string,
145 params?: GetCollectionPageParams,
146 ): Promise<GetCollectionPageResponse> {
147 return this.queryClient.getCollectionPage(collectionId, params);
148 }
149
150 async getCollectionPageByAtUri(
151 params: GetCollectionPageByAtUriParams,
152 ): Promise<GetCollectionPageResponse> {
153 return this.queryClient.getCollectionPageByAtUri(params);
154 }
155
156 async getMyCollections(
157 params?: GetMyCollectionsParams,
158 ): Promise<GetCollectionsResponse> {
159 return this.queryClient.getMyCollections(params);
160 }
161
162 async getCollections(
163 params: GetCollectionsParams,
164 ): Promise<GetCollectionsResponse> {
165 return this.queryClient.getUserCollections(params);
166 }
167
168 async getOpenCollectionsWithContributor(
169 params: GetOpenCollectionsWithContributorParams,
170 ): Promise<GetCollectionsResponse> {
171 return this.queryClient.getOpenCollectionsWithContributor(params);
172 }
173
174 async getUrlStatusForMyLibrary(
175 params: GetUrlStatusForMyLibraryParams,
176 ): Promise<GetUrlStatusForMyLibraryResponse> {
177 return this.queryClient.getUrlStatusForMyLibrary(params);
178 }
179
180 async getLibrariesForUrl(
181 params: GetLibrariesForUrlParams,
182 ): Promise<GetLibrariesForUrlResponse> {
183 return this.queryClient.getLibrariesForUrl(params);
184 }
185
186 async getNoteCardsForUrl(
187 params: GetNoteCardsForUrlParams,
188 ): Promise<GetNoteCardsForUrlResponse> {
189 return this.queryClient.getNoteCardsForUrl(params);
190 }
191
192 async getCollectionsForUrl(
193 params: GetCollectionsForUrlParams,
194 ): Promise<GetCollectionsForUrlResponse> {
195 return this.queryClient.getCollectionsForUrl(params);
196 }
197
198 async getSimilarUrlsForUrl(
199 params: GetSimilarUrlsForUrlParams,
200 ): Promise<GetSimilarUrlsForUrlResponse> {
201 return this.queryClient.getSimilarUrlsForUrl(params);
202 }
203
204 async semanticSearchUrls(
205 params: SemanticSearchUrlsParams,
206 ): Promise<SemanticSearchUrlsResponse> {
207 return this.queryClient.semanticSearchUrls(params);
208 }
209
210 async searchBskyPosts(
211 params: SearchBskyPostsForUrlParams,
212 ): Promise<SearchBskyPostsForUrlResponse> {
213 return this.queryClient.searchBskyPosts(params);
214 }
215
216 async searchAtProtoAccounts(
217 params: SearchAtProtoAccountsParams,
218 ): Promise<SearchAtProtoAccountsResponse> {
219 return this.queryClient.searchAtProtoAccounts(params);
220 }
221
222 async searchLeafletDocs(
223 params: SearchLeafletDocsForUrlParams,
224 ): Promise<SearchLeafletDocsForUrlResponse> {
225 return this.queryClient.searchLeafletDocs(params);
226 }
227
228 // Card operations - delegate to CardClient
229 async addUrlToLibrary(
230 request: AddUrlToLibraryRequest,
231 ): Promise<AddUrlToLibraryResponse> {
232 return this.cardClient.addUrlToLibrary(request);
233 }
234
235 async addCardToLibrary(
236 request: AddCardToLibraryRequest,
237 ): Promise<AddCardToLibraryResponse> {
238 return this.cardClient.addCardToLibrary(request);
239 }
240
241 async addCardToCollection(
242 request: AddCardToCollectionRequest,
243 ): Promise<AddCardToCollectionResponse> {
244 return this.cardClient.addCardToCollection(request);
245 }
246
247 async updateNoteCard(
248 request: UpdateNoteCardRequest,
249 ): Promise<UpdateNoteCardResponse> {
250 return this.cardClient.updateNoteCard(request);
251 }
252
253 async updateUrlCardAssociations(
254 request: UpdateUrlCardAssociationsRequest,
255 ): Promise<UpdateUrlCardAssociationsResponse> {
256 return this.cardClient.updateUrlCardAssociations(request);
257 }
258
259 async removeCardFromLibrary(
260 request: RemoveCardFromLibraryRequest,
261 ): Promise<RemoveCardFromLibraryResponse> {
262 return this.cardClient.removeCardFromLibrary(request);
263 }
264
265 async removeCardFromCollection(
266 request: RemoveCardFromCollectionRequest,
267 ): Promise<RemoveCardFromCollectionResponse> {
268 return this.cardClient.removeCardFromCollection(request);
269 }
270
271 // Collection operations - delegate to CollectionClient
272 async createCollection(
273 request: CreateCollectionRequest,
274 ): Promise<CreateCollectionResponse> {
275 return this.collectionClient.createCollection(request);
276 }
277
278 async updateCollection(
279 request: UpdateCollectionRequest,
280 ): Promise<UpdateCollectionResponse> {
281 return this.collectionClient.updateCollection(request);
282 }
283
284 async deleteCollection(
285 request: DeleteCollectionRequest,
286 ): Promise<DeleteCollectionResponse> {
287 return this.collectionClient.deleteCollection(request);
288 }
289
290 async searchCollections(
291 params?: SearchCollectionsParams,
292 ): Promise<GetCollectionsResponse> {
293 return this.collectionClient.searchCollections(params);
294 }
295
296 // User operations - delegate to UserClient
297 async loginWithAppPassword(
298 request: LoginWithAppPasswordRequest,
299 ): Promise<LoginWithAppPasswordResponse> {
300 return this.userClient.loginWithAppPassword(request);
301 }
302
303 async initiateOAuthSignIn(
304 request?: InitiateOAuthSignInRequest,
305 ): Promise<InitiateOAuthSignInResponse> {
306 return this.userClient.initiateOAuthSignIn(request);
307 }
308
309 async completeOAuthSignIn(
310 request: CompleteOAuthSignInRequest,
311 ): Promise<CompleteOAuthSignInResponse> {
312 return this.userClient.completeOAuthSignIn(request);
313 }
314
315 async refreshAccessToken(
316 request: RefreshAccessTokenRequest,
317 ): Promise<RefreshAccessTokenResponse> {
318 return this.userClient.refreshAccessToken(request);
319 }
320
321 async generateExtensionTokens(
322 request?: GenerateExtensionTokensRequest,
323 ): Promise<GenerateExtensionTokensResponse> {
324 return this.userClient.generateExtensionTokens(request);
325 }
326
327 async logout(): Promise<{ success: boolean; message: string }> {
328 return this.userClient.logout();
329 }
330
331 async followTarget(
332 request: FollowTargetRequest,
333 ): Promise<FollowTargetResponse> {
334 return this.userClient.followTarget(request);
335 }
336
337 async unfollowTarget(
338 targetId: string,
339 targetType: 'USER' | 'COLLECTION',
340 ): Promise<void> {
341 return this.userClient.unfollowTarget(targetId, targetType);
342 }
343
344 // Feed operations - delegate to FeedClient
345 async getGlobalFeed(
346 params?: GetGlobalFeedParams,
347 ): Promise<GetGlobalFeedResponse> {
348 return this.feedClient.getGlobalFeed(params);
349 }
350
351 async getGemsActivityFeed(
352 params?: GetGemActivityFeedParams,
353 ): Promise<GetGlobalFeedResponse> {
354 return this.feedClient.getGemsActivityFeed(params);
355 }
356
357 // Notification operations - delegate to NotificationClient
358 async getMyNotifications(
359 params?: GetMyNotificationsParams,
360 ): Promise<GetMyNotificationsResponse> {
361 return this.notificationClient.getMyNotifications(params);
362 }
363
364 async getUnreadNotificationCount(): Promise<GetUnreadNotificationCountResponse> {
365 return this.notificationClient.getUnreadNotificationCount();
366 }
367
368 async markNotificationsAsRead(
369 request: MarkNotificationsAsReadRequest,
370 ): Promise<MarkNotificationsAsReadResponse> {
371 return this.notificationClient.markNotificationsAsRead(request);
372 }
373
374 async markAllNotificationsAsRead(): Promise<MarkAllNotificationsAsReadResponse> {
375 return this.notificationClient.markAllNotificationsAsRead();
376 }
377}
378
379// Re-export types for convenience
380export * from '@semble/types';
381
382// Factory functions for different client types
383export const createApiClient = () => {
384 return new ApiClient(
385 process.env.NEXT_PUBLIC_API_BASE_URL || 'http://127.0.0.1:3000',
386 );
387};
388
389export const createServerApiClient = (accessToken?: string) => {
390 return new ApiClient(
391 process.env.NEXT_PUBLIC_API_BASE_URL || 'http://127.0.0.1:3000',
392 accessToken,
393 );
394};
395
396// Default client instance for backward compatibility
397export const apiClient = createApiClient();