mirror of
https://github.com/fatedier/frp.git
synced 2026-03-19 00:09:14 +08:00
server: add client registry with dashboard support (#5115)
This commit is contained in:
50
web/frps/src/api/http.ts
Normal file
50
web/frps/src/api/http.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// http.ts - Base HTTP client
|
||||
|
||||
class HTTPError extends Error {
|
||||
status: number
|
||||
statusText: string
|
||||
|
||||
constructor(status: number, statusText: string, message?: string) {
|
||||
super(message || statusText)
|
||||
this.status = status
|
||||
this.statusText = statusText
|
||||
}
|
||||
}
|
||||
|
||||
async function request<T>(url: string, options: RequestInit = {}): Promise<T> {
|
||||
const defaultOptions: RequestInit = {
|
||||
credentials: 'include',
|
||||
}
|
||||
|
||||
const response = await fetch(url, { ...defaultOptions, ...options })
|
||||
|
||||
if (!response.ok) {
|
||||
throw new HTTPError(response.status, response.statusText, `HTTP ${response.status}`)
|
||||
}
|
||||
|
||||
// Handle empty response (e.g. 204 No Content)
|
||||
if (response.status === 204) {
|
||||
return {} as T
|
||||
}
|
||||
|
||||
return response.json()
|
||||
}
|
||||
|
||||
export const http = {
|
||||
get: <T>(url: string, options?: RequestInit) => request<T>(url, { ...options, method: 'GET' }),
|
||||
post: <T>(url: string, body?: any, options?: RequestInit) =>
|
||||
request<T>(url, {
|
||||
...options,
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...options?.headers },
|
||||
body: JSON.stringify(body)
|
||||
}),
|
||||
put: <T>(url: string, body?: any, options?: RequestInit) =>
|
||||
request<T>(url, {
|
||||
...options,
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', ...options?.headers },
|
||||
body: JSON.stringify(body)
|
||||
}),
|
||||
delete: <T>(url: string, options?: RequestInit) => request<T>(url, { ...options, method: 'DELETE' }),
|
||||
}
|
||||
Reference in New Issue
Block a user