logs removed from api.ts
This commit is contained in:
parent
325fe9735b
commit
a11194831d
@ -1,8 +1,8 @@
|
||||
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000/api/v1';
|
||||
|
||||
// Debug logging
|
||||
console.log('API_BASE_URL:', API_BASE_URL);
|
||||
console.log('VITE_API_URL env:', import.meta.env.VITE_API_URL);
|
||||
// console.log('API_BASE_URL:', API_BASE_URL);
|
||||
// console.log('VITE_API_URL env:', import.meta.env.VITE_API_URL);
|
||||
|
||||
// Helper function to get auth headers
|
||||
function getAuthHeaders(): HeadersInit {
|
||||
@ -10,18 +10,18 @@ function getAuthHeaders(): HeadersInit {
|
||||
const headers: HeadersInit = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
// Enhanced fetch wrapper
|
||||
async function authFetch(url: string, options: RequestInit = {}): Promise<Response> {
|
||||
const headers = getAuthHeaders();
|
||||
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
@ -29,14 +29,14 @@ async function authFetch(url: string, options: RequestInit = {}): Promise<Respon
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
// Handle 401 unauthorized
|
||||
if (response.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
window.location.href = '/auth';
|
||||
}
|
||||
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -142,7 +142,7 @@ export interface UpdateArticleDto {
|
||||
}
|
||||
|
||||
export async function fetchArticles(params: FindArticlesParams = {}): Promise<ArticlesResponse> {
|
||||
console.log('fetchArticles called with params:', params, 'API_BASE_URL:', API_BASE_URL);
|
||||
// console.log('fetchArticles called with params:', params, 'API_BASE_URL:', API_BASE_URL);
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
// Convert parameters to proper types for URLSearchParams
|
||||
@ -157,17 +157,17 @@ export async function fetchArticles(params: FindArticlesParams = {}): Promise<Ar
|
||||
});
|
||||
|
||||
const url = `${API_BASE_URL}/articles?${searchParams}`;
|
||||
console.log('Fetching from:', url);
|
||||
// console.log('Fetching from:', url);
|
||||
|
||||
const response = await authFetch(url);
|
||||
console.log('Response status:', response.status, 'ok:', response.ok);
|
||||
// console.log('Response status:', response.status, 'ok:', response.ok);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch articles');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log('Response data:', data);
|
||||
// console.log('Response data:', data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@ -355,7 +355,7 @@ export interface UpdateLiveBlogDto {
|
||||
// Live Blog API Functions
|
||||
export async function fetchLiveBlogs(params: FindLiveBlogsParams = {}): Promise<LiveBlogsResponse> {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
if (typeof value === 'number') {
|
||||
@ -375,8 +375,8 @@ export async function fetchLiveBlogs(params: FindLiveBlogsParams = {}): Promise<
|
||||
|
||||
export async function fetchLiveBlogBySlug(slugOrId: string): Promise<LiveBlog> {
|
||||
const isUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(slugOrId);
|
||||
const endpoint = isUuid
|
||||
? `${API_BASE_URL}/live-blogs/${slugOrId}`
|
||||
const endpoint = isUuid
|
||||
? `${API_BASE_URL}/live-blogs/${slugOrId}`
|
||||
: `${API_BASE_URL}/live-blogs/slug/${slugOrId}`;
|
||||
const response = await authFetch(endpoint);
|
||||
if (!response.ok) {
|
||||
@ -550,12 +550,12 @@ export async function login(dto: LoginDto): Promise<AuthResponse> {
|
||||
},
|
||||
body: JSON.stringify(dto),
|
||||
});
|
||||
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || 'Login failed');
|
||||
}
|
||||
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
@ -567,12 +567,12 @@ export async function register(dto: RegisterDto): Promise<AuthResponse> {
|
||||
},
|
||||
body: JSON.stringify(dto),
|
||||
});
|
||||
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || 'Registration failed');
|
||||
}
|
||||
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
@ -580,11 +580,11 @@ export async function getProfile(): Promise<User> {
|
||||
const response = await authFetch(`${API_BASE_URL}/users/profile`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch profile');
|
||||
}
|
||||
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
@ -701,14 +701,14 @@ function mapBackendComment(comment: BackendComment): Comment {
|
||||
|
||||
export async function fetchComments(params: FindCommentsParams = {}): Promise<CommentsResponse> {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
|
||||
// Map parentCommentId to parentId for backend compatibility
|
||||
const backendParams = { ...params };
|
||||
if (backendParams.parentCommentId) {
|
||||
backendParams.parentId = backendParams.parentCommentId;
|
||||
delete backendParams.parentCommentId;
|
||||
}
|
||||
|
||||
|
||||
Object.entries(backendParams).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
if (typeof value === 'number') {
|
||||
@ -723,11 +723,11 @@ export async function fetchComments(params: FindCommentsParams = {}): Promise<Co
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch comments');
|
||||
}
|
||||
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
|
||||
const mappedData = (data as BackendComment[]).map(mapBackendComment);
|
||||
|
||||
|
||||
return {
|
||||
data: mappedData,
|
||||
total: mappedData.length,
|
||||
@ -742,7 +742,7 @@ export async function createComment(dto: CreateCommentDto): Promise<Comment> {
|
||||
liveBlogId: dto.liveBlogId,
|
||||
parentId: dto.parentCommentId,
|
||||
};
|
||||
|
||||
|
||||
const response = await authFetch(`${API_BASE_URL}/comments`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(backendDto),
|
||||
@ -750,9 +750,9 @@ export async function createComment(dto: CreateCommentDto): Promise<Comment> {
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to create comment');
|
||||
}
|
||||
|
||||
|
||||
const comment = await response.json() as BackendComment;
|
||||
|
||||
|
||||
// Map backend response to frontend interface
|
||||
return mapBackendComment(comment);
|
||||
}
|
||||
@ -793,7 +793,7 @@ export async function getReactionCounts(
|
||||
commentId?: string
|
||||
): Promise<ReactionCounts> {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
|
||||
if (articleId) searchParams.append('articleId', articleId);
|
||||
if (liveBlogId) searchParams.append('liveBlogId', liveBlogId);
|
||||
if (commentId) searchParams.append('commentId', commentId);
|
||||
@ -812,7 +812,7 @@ export async function getUserReaction(
|
||||
commentId?: string
|
||||
): Promise<{ type: string | null }> {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
|
||||
if (articleId) searchParams.append('articleId', articleId);
|
||||
if (liveBlogId) searchParams.append('liveBlogId', liveBlogId);
|
||||
if (commentId) searchParams.append('commentId', commentId);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user