init
This commit is contained in:
commit
fcec12e5ba
78 files changed
+9569
No files matched your search
@@ -0,0 +1,40 @@
|
||||
interface ApiEnvelope<T> {
|
||||
data: T | null
|
||||
code: number
|
||||
message?: string | string[]
|
||||
}
|
||||
|
||||
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const res = await fetch(path, {
|
||||
...init,
|
||||
credentials: 'same-origin',
|
||||
headers: { 'Content-Type': 'application/json', ...(init?.headers ?? {}) },
|
||||
})
|
||||
|
||||
if (res.status === 204) return undefined as T
|
||||
|
||||
const body = (await res.json()) as ApiEnvelope<T>
|
||||
|
||||
if (!res.ok) {
|
||||
const message = Array.isArray(body.message) ? body.message.join(', ') : body.message
|
||||
throw new Error(message ?? `Request failed with status ${res.status}`)
|
||||
}
|
||||
|
||||
return body.data as T
|
||||
}
|
||||
|
||||
export function toQuery(params: Record<string, string | number | undefined | null>): string {
|
||||
const sp = new URLSearchParams()
|
||||
for (const [k, v] of Object.entries(params)) {
|
||||
if (v !== undefined && v !== null && v !== '') sp.set(k, String(v))
|
||||
}
|
||||
const s = sp.toString()
|
||||
return s ? `?${s}` : ''
|
||||
}
|
||||
|
||||
export const apiClient = {
|
||||
get: <T>(path: string) => request<T>(path),
|
||||
post: <T>(path: string, body?: unknown) => request<T>(path, { method: 'POST', body: JSON.stringify(body) }),
|
||||
patch: <T>(path: string, body?: unknown) => request<T>(path, { method: 'PATCH', body: JSON.stringify(body) }),
|
||||
delete: (path: string) => request<void>(path, { method: 'DELETE' }),
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { createAuthClient } from 'better-auth/vue'
|
||||
|
||||
export const authClient = createAuthClient({
|
||||
baseURL: 'http://localhost:3333',
|
||||
plugins: [],
|
||||
})
|
||||
Reference in new issue
Block a user