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