1
- import { ofetch } from 'ofetch'
2
- import { ref } from 'vue'
1
+ import { type FetchOptions , ofetch } from 'ofetch'
3
2
4
3
interface Params {
5
4
[ key : string ] : any // Replace 'any' with more specific types if possible
6
5
}
7
6
8
- interface FetchRequestHeaders {
9
- 'Content-Type' ?: string
10
- Authorization ?: string
11
- Accept ?: string
12
- 'User-Agent' ?: string
13
- Referer ?: string
14
- Origin ?: string
15
- 'Cache-Control' ?: string
16
- Pragma ?: string
17
- Cookie ?: string
18
- 'If-Modified-Since' ?: string
19
- 'If-None-Match' ?: string
20
- 'X-Requested-With' ?: string
21
- 'Accept-Encoding' ?: string
22
- 'Accept-Language' ?: string
23
- [ key : string ] : string | undefined // To allow additional headers
24
- }
25
-
26
- interface FetchRequestParams {
27
- headers ?: FetchRequestHeaders
28
- method ?: string
29
- baseURL ?: string
30
- body ?: any // Optional, for request bodies like in POST or PUT requests
31
- mode ?: RequestMode // Optional, e.g., 'cors', 'no-cors', 'same-origin'
32
- credentials ?: RequestCredentials // Optional, e.g., 'include', 'same-origin', 'omit'
33
- cache ?: RequestCache // Optional, e.g., 'default', 'no-store', 'reload'
34
- redirect ?: RequestRedirect // Optional, e.g., 'follow', 'manual', 'error'
35
- referrerPolicy ?: ReferrerPolicy // Optional, e.g., 'no-referrer', 'origin'
36
- integrity ?: string // Optional, for subresource integrity
37
- keepalive ?: boolean // Optional, for whether the request should outlive the page
38
- signal ?: AbortSignal // Optional, to abort the request
39
- [ key : string ] : any // To allow additional parameters
7
+ interface ApiFetch {
8
+ get ( url : string , params ?: Params ) : Promise < FetchResponse >
9
+ post ( url : string , params ?: Params ) : Promise < FetchResponse >
10
+ destroy ( url : string , params ?: Params ) : Promise < FetchResponse >
11
+ patch ( url : string , params ?: Params ) : Promise < FetchResponse >
12
+ put ( url : string , params ?: Params ) : Promise < FetchResponse >
13
+ setToken ( authToken : string ) : void
14
+ baseURL : '/' | string
15
+ loading : boolean
16
+ token : string
40
17
}
41
18
42
19
type FetchResponse = string | Blob | ArrayBuffer | ReadableStream < Uint8Array >
43
20
44
- const loading = ref ( false )
45
- const token = ref ( '' )
21
+ let loading = false
22
+ let token = ''
46
23
const baseURL = '/'
47
24
48
- async function post ( url : string , params ?: Params ) : Promise < any > {
49
- const headers : FetchRequestHeaders = { Accept : 'application/json' }
25
+ async function get ( url : string , params ?: Params , headers ?: Headers ) : Promise < FetchResponse > {
26
+ if ( headers ) if ( token ) headers . set ( 'Authorization' , `Bearer ${ token } ` )
50
27
51
- if ( token . value ) headers . Authorization = `Bearer ${ token . value } `
28
+ return await ofetch ( url , { method : 'GET' , baseURL, params, headers } )
29
+ }
52
30
53
- const parameters : FetchRequestParams = {
54
- ...params ,
55
- ...{
56
- headers,
57
- parseResponse : JSON . parse ,
58
- method : 'POST' ,
59
- baseURL,
60
- } ,
61
- }
31
+ async function post ( url : string , params ?: Params , headers ?: Headers ) : Promise < any > {
32
+ if ( headers ) if ( token ) headers . set ( 'Authorization' , `Bearer ${ token } ` )
62
33
63
- loading . value = true
34
+ loading = true
64
35
65
36
try {
66
- const result : string | FetchResponse | Blob | ArrayBuffer | ReadableStream < Uint8Array > = await ofetch (
67
- url ,
68
- parameters ,
69
- )
37
+ const result : string | FetchResponse | Blob | ArrayBuffer | ReadableStream < Uint8Array > = await ofetch ( url , {
38
+ method : 'POST' ,
39
+ baseURL,
40
+ params,
41
+ headers,
42
+ } )
70
43
71
- loading . value = false
44
+ loading = false
72
45
return result
73
46
} catch ( err : any ) {
74
- loading . value = false
47
+ loading = false
75
48
76
49
throw err
77
50
}
78
51
}
79
52
80
- async function get ( url : string , params ?: Params ) : Promise < any > {
81
- const headers : FetchRequestHeaders = { Accept : 'application/json' }
53
+ async function patch ( url : string , params ?: Params , headers ?: Headers ) : Promise < FetchResponse > {
54
+ if ( headers ) if ( token ) headers . set ( 'Authorization' , `Bearer ${ token } ` )
82
55
83
- if ( token . value ) headers . Authorization = `Bearer ${ token . value } `
56
+ loading = true
84
57
85
- const parameters : FetchRequestParams = {
86
- ...params ,
87
- ...{
88
- headers,
89
- parseResponse : JSON . parse ,
90
- method : 'GET' ,
91
- baseURL,
92
- } ,
93
- }
94
-
95
- return ( await ofetch ( url , parameters ) ) as FetchResponse
58
+ return await ofetch ( url , {
59
+ method : 'PATCH' ,
60
+ baseURL,
61
+ params,
62
+ headers,
63
+ } )
96
64
}
97
65
98
- async function patch ( url : string , params ?: Params ) : Promise < any > {
99
- const headers : FetchRequestHeaders = { Accept : 'application/json' }
100
-
101
- if ( token . value ) headers . Authorization = `Bearer ${ token . value } `
102
-
103
- const parameters : FetchRequestParams = {
104
- ...params ,
105
- ...{
106
- headers,
107
- parseResponse : JSON . parse ,
108
- method : 'PATCH' ,
109
- baseURL,
110
- } ,
111
- }
66
+ async function put ( url : string , params ?: Params , headers ?: Headers ) : Promise < FetchResponse > {
67
+ if ( headers ) if ( token ) headers . set ( 'Authorization' , `Bearer ${ token } ` )
112
68
113
- loading . value = true
69
+ loading = true
114
70
115
- return ( await ofetch ( url , parameters ) ) as FetchResponse
71
+ return await ofetch ( url , {
72
+ method : 'PUT' ,
73
+ baseURL,
74
+ params,
75
+ headers,
76
+ } )
116
77
}
117
78
118
- async function destroy ( url : string , params ?: Params ) : Promise < any > {
119
- const headers : FetchRequestHeaders = { Accept : 'application/json' }
120
-
121
- if ( token . value ) headers . Authorization = `Bearer ${ token . value } `
79
+ async function destroy ( url : string , params ?: Params , headers ?: Headers ) : Promise < FetchResponse > {
80
+ if ( headers ) if ( token ) headers . set ( 'Authorization' , `Bearer ${ token } ` )
122
81
123
- const parameters : FetchRequestParams = {
82
+ const parameters = {
124
83
...params ,
125
84
...{
126
85
headers,
@@ -129,22 +88,28 @@ async function destroy(url: string, params?: Params): Promise<any> {
129
88
} ,
130
89
}
131
90
132
- loading . value = true
91
+ loading = true
133
92
134
- return ( await ofetch ( url , parameters ) ) as FetchResponse
93
+ return await ofetch ( url , {
94
+ method : 'DELETE' ,
95
+ baseURL,
96
+ params,
97
+ headers,
98
+ } )
135
99
}
136
100
137
101
function setToken ( authToken : string ) : void {
138
- token . value = authToken
102
+ token = authToken
139
103
}
140
104
141
- export const Fetch = {
142
- post,
143
- get,
144
- patch,
145
- destroy,
146
- baseURL,
147
- loading,
148
- token,
149
- setToken,
105
+ export const Fetch : ApiFetch = {
106
+ get : get ,
107
+ post : post ,
108
+ patch : patch ,
109
+ put : put ,
110
+ destroy : destroy ,
111
+ baseURL : baseURL ,
112
+ token : token ,
113
+ setToken : setToken ,
114
+ loading : loading ,
150
115
}
0 commit comments