|
@@ -1,15 +1,25 @@
|
|
|
+import { useUserStore } from '@/store/user'
|
|
|
import { fetch } from '@tauri-apps/plugin-http'
|
|
|
|
|
|
interface RequestOptions {
|
|
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE'
|
|
|
url: string
|
|
|
+ query?: Record<string, string>
|
|
|
+ noAuth?: boolean
|
|
|
headers?: Record<string, string>
|
|
|
body?: unknown
|
|
|
}
|
|
|
|
|
|
-export const request = async ({ method, url, headers, body }: RequestOptions) => {
|
|
|
+export const request = async ({ method, url, headers, body, noAuth, query }: RequestOptions) => {
|
|
|
+ if (!noAuth) {
|
|
|
+ const { getToken } = useUserStore()
|
|
|
+ headers['Access-Token'] = getToken
|
|
|
+ }
|
|
|
+ const reqPath = new URL(url, import.meta.env.VITE_BASE_URL || '')
|
|
|
+ if (query) Object.keys(query).forEach((key) => reqPath.searchParams.append(key, query[key]))
|
|
|
+
|
|
|
try {
|
|
|
- const response = await fetch(url, {
|
|
|
+ const response = await fetch(reqPath.toString(), {
|
|
|
method,
|
|
|
headers,
|
|
|
body: body ? JSON.stringify(body) : undefined,
|