-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathinstance.ts
More file actions
53 lines (48 loc) · 1.19 KB
/
instance.ts
File metadata and controls
53 lines (48 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { callRpc } from '@/utils/rpc'
import { isErr } from '@bus/result'
declare global {
interface Window {
__BASE_URL__?: string
}
}
interface ResponseWithDetail {
ok: boolean
detail?: string
}
interface FetchOptionsWithSignal {
signal?: AbortSignal
}
interface FetchOptions<B extends object = any> {
url: string
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
data?: B
responseType?: string
headers?: Record<string, string>
credentials?: 'omit' | 'same-origin' | 'include'
mode?: 'cors' | 'no-cors' | 'same-origin'
cache?:
| 'default'
| 'no-store'
| 'reload'
| 'no-cache'
| 'force-cache'
| 'only-if-cached'
params?: Record<string, string | number | boolean | undefined | null>
}
export async function fetchAPI<T = any, B extends object = any>(
config: FetchOptions<B>,
_options?: Partial<FetchOptionsWithSignal>,
): Promise<T & ResponseWithDetail> {
const request = {
url: config.url,
method: config.method,
params: config.params,
body: config.data,
}
const result = await callRpc('api_query', request)
if (isErr(result)) {
throw new Error(result.error)
}
const response = result.value.data
return response
}