147 lines
6.0 KiB
TypeScript
147 lines
6.0 KiB
TypeScript
import type { User } from "~/context/AuthContext";
|
|
import type { Comment } from "~/types/comment";
|
|
import type { PaginatedResponse } from "~/types/PaginatedResponse";
|
|
import type { Post } from "~/types/post";
|
|
import type { Tag } from "~/types/tag";
|
|
|
|
const ENDPOINT = 'http://localhost:5259/api';
|
|
|
|
export class API {
|
|
|
|
private static async genericRequest(url: string, method: string, body: any = null, headers: Record<string, string> = {}): Promise<Response> {
|
|
const localHeaders = {
|
|
'Content-Type': 'application/json',
|
|
...headers,
|
|
};
|
|
const options: RequestInit = {
|
|
method,
|
|
headers: localHeaders,
|
|
};
|
|
if (body) {
|
|
options.body = JSON.stringify(body);
|
|
}
|
|
const response = await fetch(url, options);
|
|
return response;
|
|
}
|
|
|
|
private static async get(url: string, headers: Record<string, string> = {}) {
|
|
const response = await fetch(url);
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
return response.json();
|
|
}
|
|
|
|
public static async post(url: string, body: any, headers: Record<string, string> = {}): Promise<Response> {
|
|
return await API.genericRequest(url, 'POST', body, headers);
|
|
}
|
|
|
|
private static async patch(url: string, body: any, headers: Record<string, string> = {}): Promise<Response> {
|
|
return await API.genericRequest(url, 'PATCH', body, headers);
|
|
}
|
|
|
|
private static async delete(url: string, body: any, headers: Record<string, string> = {}): Promise<Response> {
|
|
return await API.genericRequest(url, 'DELETE', body, headers);
|
|
}
|
|
|
|
/* Tag APIs */
|
|
public static async fetchTags(page: number): Promise<PaginatedResponse<Tag>> {
|
|
const data = await API.get(`${ENDPOINT}/tags?page=${page}`);
|
|
return data
|
|
}
|
|
|
|
public static async createTag(name: string, tagType: string, user: User): Promise<Response> {
|
|
return await API.post(`${ENDPOINT}/tags`, { name, type: tagType }, { Authorization: `Bearer ${user.accessToken}`});
|
|
}
|
|
|
|
public static async updateTag(name: string, newType: string, user: User): Promise<Response> {
|
|
return await API.patch(`${ENDPOINT}/tags/${encodeURIComponent(name)}`, { type: newType }, { Authorization: `Bearer ${user.accessToken}`});
|
|
}
|
|
|
|
public static async deleteTag(name: string, user: User): Promise<Response> {
|
|
return await API.delete(`${ENDPOINT}/tags/${encodeURIComponent(name)}`, {}, { Authorization: `Bearer ${user.accessToken}`});
|
|
}
|
|
|
|
|
|
/* Post APIs */
|
|
public static async fetchPostById(id: number): Promise<Post|null> {
|
|
const data = await API.get(`${ENDPOINT}/posts/${id}`);
|
|
return data;
|
|
}
|
|
|
|
public static async fetchPosts(page: number, filter: string = ""): Promise<PaginatedResponse<Post>> {
|
|
const data = await API.get(`${ENDPOINT}/posts?page=${page}&query=${encodeURIComponent(filter)}`);
|
|
return data
|
|
}
|
|
|
|
public static async createPostRequest(
|
|
title: string, description: string,
|
|
tags: string[], filename: string,
|
|
fileMimeType: string, fileSize: number,
|
|
user: User
|
|
): Promise<Response> {
|
|
return await API.post(`${ENDPOINT}/posts`, {
|
|
title, description, tags, filename,
|
|
fileMimeType, fileSize
|
|
}, { Authorization: `Bearer ${user.accessToken}`})
|
|
}
|
|
|
|
public static async patchFileUploadUrl(uploadUrl: string, image: File, user: User): Promise<Response> {
|
|
return await fetch(uploadUrl, {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Content-Range': `bytes 0-${image.size - 1}/${image.size}`,
|
|
'Content-Type': 'application/octet-stream',
|
|
'Authorization': `Bearer ${user.accessToken}`,
|
|
},
|
|
body: image,
|
|
});
|
|
}
|
|
|
|
public static async updatePost(postId: number, title: string, description: string, tags: string[], user: User): Promise<Response> {
|
|
return await API.patch(`${ENDPOINT}/posts/${postId}`, { title, description, tags }, {Authorization: `Bearer ${user.accessToken}`});
|
|
}
|
|
|
|
public static async deletePost(postId: number, user: User): Promise<Response> {
|
|
return await API.delete(`${ENDPOINT}/posts/${postId}`, {}, {Authorization: `Bearer ${user.accessToken}`});
|
|
}
|
|
|
|
/* Comment APIs */
|
|
public static async fetchCommentsByPostId(postId: number, page: number): Promise<PaginatedResponse<Comment>> {
|
|
const data = await API.get(`${ENDPOINT}/posts/${postId}/comments?page=${page}`);
|
|
return data;
|
|
}
|
|
|
|
public static async postComment(postId: number, text: string, user: User): Promise<Response> {
|
|
return await API.post(`${ENDPOINT}/posts/${postId}/comments`, { text }, {Authorization: `Bearer ${user.accessToken}`});
|
|
}
|
|
|
|
public static async updateComment(postId: number, commentId: number, newText: string, user: User): Promise<Response> {
|
|
return await API.patch(`${ENDPOINT}/posts/${postId}/comments/${commentId}`, { text: newText }, {Authorization: `Bearer ${user.accessToken}`});
|
|
}
|
|
|
|
public static async deleteComment(postId: number, commentId: number, user: User): Promise<Response> {
|
|
return await API.delete(`${ENDPOINT}/posts/${postId}/comments/${commentId}`, {}, {Authorization: `Bearer ${user.accessToken}`});
|
|
}
|
|
|
|
|
|
|
|
/* Auth APIs */
|
|
public static async register(username: string, email: string, password: string): Promise<Response> {
|
|
return await API.post(`${ENDPOINT}/auth/register`, { username, email, password });
|
|
}
|
|
|
|
public static async login(email: string, password: string): Promise<Response> {
|
|
return await API.post(`${ENDPOINT}/auth/login`, { email, password });
|
|
}
|
|
|
|
public static async renewToken(refreshToken: string): Promise<Response> {
|
|
return await API.post(`${ENDPOINT}/auth/renew`, { refreshToken });
|
|
}
|
|
|
|
public static async revokeToken(refreshToken: string): Promise<Response> {
|
|
return await API.post(`${ENDPOINT}/auth/revoke`, { refreshToken });
|
|
}
|
|
}
|
|
|