rebuild request, fix editor

This commit is contained in:
KUN1007 2023-06-10 17:24:56 +08:00
parent be60b68410
commit 58c41a212c
3 changed files with 96 additions and 19 deletions

View file

@ -1,17 +1,17 @@
import { request } from '@/utils/request' import { fetchGet } from '@/utils/request'
import { KUNGalgameTopic } from './types/topic' import { KUNGalgameTopic } from './types/topic'
export async function getTopicApi(id: number) { export async function getTopicApi(id: number) {
return await request<KUNGalgameTopic>(`/topic/${id}`) return await fetchGet<KUNGalgameTopic>(`/topic/${id}`)
} }
export async function getTopicReplyApi(id: number) { export async function getTopicReplyApi(id: number) {
return await request<KUNGalgameTopic>(`/topic/reply/${id}`) return await fetchGet<KUNGalgameTopic>(`/topic/reply/${id}`)
} }
export async function getTopicCommentApi(id: number) { export async function getTopicCommentApi(id: number) {
return await request<KUNGalgameTopic>(`/topic/comment/${id}`) return await fetchGet<KUNGalgameTopic>(`/topic/comment/${id}`)
} }
// 获取指定范围内的帖子数据 // 获取指定范围内的帖子数据
@ -20,15 +20,9 @@ export async function getTopicRangeApi(
count: number count: number
): Promise<KUNGalgameTopic[]> { ): Promise<KUNGalgameTopic[]> {
const url = `http://127.0.0.1:10007/topic/topics/kun?start=${start}&count=${count}` const url = `http://127.0.0.1:10007/topic/topics/kun?start=${start}&count=${count}`
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
try { try {
const response = await request<KUNGalgameTopic[]>(url, options) const response = await fetchGet<KUNGalgameTopic[]>(url)
return response return response
} catch (error) { } catch (error) {
console.error('Error fetching topics:', error) console.error('Error fetching topics:', error)

View file

@ -12,6 +12,14 @@
} }
} }
s {
text-decoration: line-through;
}
* {
text-shadow: none;
}
:root { :root {
// textarea - css vars // textarea - css vars
--w-e-textarea-bg-color: var(--kungalgame-white); --w-e-textarea-bg-color: var(--kungalgame-white);

View file

@ -1,10 +1,85 @@
// request.ts type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
export async function request<T>( type FetchOptions = {
url: string, method: HttpMethod
options: RequestInit = {} headers?: Record<string, string>
): Promise<T> { body?: BodyInit
const response = await fetch(url, options)
const data = await response.json()
return data as T
} }
const fetchRequest = async <T>(
url: string,
options: FetchOptions
): Promise<T> => {
try {
const response = await fetch(url, options)
if (!response.ok) {
throw new Error('Request Error!')
}
const data: T = await response.json()
return data
} catch (error) {
throw new Error('Request Error!')
}
}
const fetchGet = async <T>(
url: string,
headers?: Record<string, string>
): Promise<T> => {
const options: FetchOptions = {
method: 'GET',
headers: headers,
}
return await fetchRequest<T>(url, options)
}
const fetchPost = async <T>(
url: string,
body?: Record<string, any>,
headers?: Record<string, string>
): Promise<T> => {
const options: FetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify(body),
}
return await fetchRequest<T>(url, options)
}
const fetchPut = async <T>(
url: string,
body?: Record<string, any>,
headers?: Record<string, string>
): Promise<T> => {
const options: FetchOptions = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify(body),
}
return await fetchRequest<T>(url, options)
}
const fetchDelete = async <T>(
url: string,
headers?: Record<string, string>
): Promise<T> => {
const options: FetchOptions = {
method: 'DELETE',
headers: headers,
}
return await fetchRequest<T>(url, options)
}
export { fetchGet, fetchPost, fetchPut, fetchDelete }