@stacksjs/api 0.58.48 → 0.58.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/index.ts +114 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/api",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.58.
|
|
4
|
+
"version": "0.58.50",
|
|
5
5
|
"description": "The Stacks array utilities.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -40,7 +40,8 @@
|
|
|
40
40
|
],
|
|
41
41
|
"files": [
|
|
42
42
|
"README.md",
|
|
43
|
-
"dist"
|
|
43
|
+
"dist",
|
|
44
|
+
"src"
|
|
44
45
|
],
|
|
45
46
|
"scripts": {
|
|
46
47
|
"build": "bun --bun build.ts",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ofetch } from 'ofetch'
|
|
2
|
+
import type { FetchOptions } from 'ofetch'
|
|
3
|
+
interface Params {
|
|
4
|
+
[key: string]: any // Replace 'any' with more specific types if possible
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
type FetchResponse = string | Blob | ArrayBuffer | ReadableStream<Uint8Array>
|
|
8
|
+
|
|
9
|
+
const loading = ref(false)
|
|
10
|
+
const token = ref('')
|
|
11
|
+
|
|
12
|
+
export async function useHttpFetch(endpoint = '') {
|
|
13
|
+
let baseURL = '/'
|
|
14
|
+
|
|
15
|
+
if (endpoint)
|
|
16
|
+
baseURL = endpoint
|
|
17
|
+
|
|
18
|
+
async function post(url: string, params?: Params): Promise<any> {
|
|
19
|
+
const headers: any = { Accept: 'application/json' }
|
|
20
|
+
|
|
21
|
+
if (token.value)
|
|
22
|
+
headers.Authorization = `Bearer ${token.value}`
|
|
23
|
+
|
|
24
|
+
const parameters: FetchOptions = {
|
|
25
|
+
...params,
|
|
26
|
+
...{
|
|
27
|
+
headers,
|
|
28
|
+
parseResponse: JSON.parse,
|
|
29
|
+
method: 'POST',
|
|
30
|
+
baseURL,
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
loading.value = true
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
const result: string | FetchResponse | Blob | ArrayBuffer | ReadableStream<Uint8Array> = await ofetch(url, parameters)
|
|
38
|
+
|
|
39
|
+
loading.value = false
|
|
40
|
+
return result
|
|
41
|
+
}
|
|
42
|
+
catch (err: any) {
|
|
43
|
+
loading.value = false
|
|
44
|
+
|
|
45
|
+
throw err
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function get(url: string, params?: Params): Promise<any> {
|
|
50
|
+
const headers: any = { Accept: 'application/json' }
|
|
51
|
+
|
|
52
|
+
if (token.value)
|
|
53
|
+
headers.Authorization = `Bearer ${token.value}`
|
|
54
|
+
|
|
55
|
+
const parameters: FetchOptions = {
|
|
56
|
+
...params,
|
|
57
|
+
...{
|
|
58
|
+
headers,
|
|
59
|
+
parseResponse: JSON.parse,
|
|
60
|
+
method: 'GET',
|
|
61
|
+
baseURL,
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return await ofetch(url, parameters) as FetchResponse
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function patch(url: string, params?: Params): Promise<any> {
|
|
69
|
+
const headers: any = { Accept: 'application/json' }
|
|
70
|
+
|
|
71
|
+
if (token.value)
|
|
72
|
+
headers.Authorization = `Bearer ${token.value}`
|
|
73
|
+
|
|
74
|
+
const parameters: FetchOptions = {
|
|
75
|
+
...params,
|
|
76
|
+
...{
|
|
77
|
+
headers,
|
|
78
|
+
parseResponse: JSON.parse,
|
|
79
|
+
method: 'PATCH',
|
|
80
|
+
baseURL,
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
loading.value = true
|
|
85
|
+
|
|
86
|
+
return await ofetch(url, parameters) as FetchResponse
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function destroy(url: string, params?: Params): Promise<any> {
|
|
90
|
+
const headers: any = { Accept: 'application/json' }
|
|
91
|
+
|
|
92
|
+
if (token.value)
|
|
93
|
+
headers.Authorization = `Bearer ${token.value}`
|
|
94
|
+
|
|
95
|
+
const parameters: FetchOptions = {
|
|
96
|
+
...params,
|
|
97
|
+
...{
|
|
98
|
+
headers,
|
|
99
|
+
method: 'DELETE',
|
|
100
|
+
baseURL,
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
loading.value = true
|
|
105
|
+
|
|
106
|
+
return await ofetch(url, parameters) as FetchResponse
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function setToken(authToken: string) {
|
|
110
|
+
token.value = authToken
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { post, get, patch, destroy, baseURL, loading, token, setToken }
|
|
114
|
+
}
|