From 3f4bf264617e995cf5aaffb309ac33cd49c93de0 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Wed, 24 Dec 2025 21:13:55 +0800 Subject: [PATCH] update server api client factory method and base client with token --- src/webapp/api-client/ApiClient.ts | 20 ++++++++++++-------- src/webapp/api-client/clients/BaseClient.ts | 12 ++++++++++-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/webapp/api-client/ApiClient.ts b/src/webapp/api-client/ApiClient.ts index 5963ec0a..c7c2de25 100644 --- a/src/webapp/api-client/ApiClient.ts +++ b/src/webapp/api-client/ApiClient.ts @@ -84,13 +84,16 @@ export class ApiClient { private feedClient: FeedClient; private notificationClient: NotificationClient; - constructor(private baseUrl: string) { - this.queryClient = new QueryClient(baseUrl); - this.cardClient = new CardClient(baseUrl); - this.collectionClient = new CollectionClient(baseUrl); - this.userClient = new UserClient(baseUrl); - this.feedClient = new FeedClient(baseUrl); - this.notificationClient = new NotificationClient(baseUrl); + constructor( + private baseUrl: string, + accessToken?: string, + ) { + this.queryClient = new QueryClient(baseUrl, accessToken); + this.cardClient = new CardClient(baseUrl, accessToken); + this.collectionClient = new CollectionClient(baseUrl, accessToken); + this.userClient = new UserClient(baseUrl, accessToken); + this.feedClient = new FeedClient(baseUrl, accessToken); + this.notificationClient = new NotificationClient(baseUrl, accessToken); } // Query operations - delegate to QueryClient @@ -329,9 +332,10 @@ export const createApiClient = () => { ); }; -export const createServerApiClient = () => { +export const createServerApiClient = (accessToken?: string) => { return new ApiClient( process.env.NEXT_PUBLIC_API_BASE_URL || 'http://127.0.0.1:3000', + accessToken, ); }; diff --git a/src/webapp/api-client/clients/BaseClient.ts b/src/webapp/api-client/clients/BaseClient.ts index 81bcf92f..cf4b0a29 100644 --- a/src/webapp/api-client/clients/BaseClient.ts +++ b/src/webapp/api-client/clients/BaseClient.ts @@ -1,7 +1,10 @@ import { ApiError, ApiErrorResponse } from '../errors'; export abstract class BaseClient { - constructor(protected baseUrl: string) {} + constructor( + protected baseUrl: string, + private accessToken?: string, // Add optional access token for server-side requests + ) {} protected async request( method: string, @@ -14,10 +17,15 @@ export abstract class BaseClient { 'Content-Type': 'application/json', }; + // Include access token as cookie header if provided (for server-side requests) + if (this.accessToken) { + headers.Cookie = `accessToken=${this.accessToken}`; + } + const config: RequestInit = { method, headers, - credentials: 'include', // Include cookies automatically (works for both client and server) + credentials: this.accessToken ? 'omit' : 'include', // Use 'omit' for server-side with manual cookies }; if (data && (method === 'POST' || method === 'PUT' || method === 'PATCH')) { -- 2.51.2