|
1 | | -import { withBasePath, withLionUrl } from "@/lion/utils/base"; |
| 1 | +import { apiRequest } from "@/composables/useApiRequest"; |
| 2 | +import { withLionUrl } from "@/lion/utils/base"; |
2 | 3 |
|
3 | | -const postJson = (url: string, data: any) => { |
4 | | - return fetch(url, { |
| 4 | +export interface SuggestionUser { |
| 5 | + id: string; |
| 6 | + name: string; |
| 7 | + username: string; |
| 8 | +} |
| 9 | + |
| 10 | +export interface SuggestionUserPage { |
| 11 | + count?: number; |
| 12 | + next?: string | null; |
| 13 | + previous?: string | null; |
| 14 | + results: SuggestionUser[]; |
| 15 | +} |
| 16 | + |
| 17 | +export interface ShareApiResponse { |
| 18 | + success?: boolean; |
| 19 | + message?: string; |
| 20 | + id?: string; |
| 21 | + verify_code?: string; |
| 22 | + [key: string]: unknown; |
| 23 | +} |
| 24 | + |
| 25 | +export interface ShareSessionResponse extends ShareApiResponse { |
| 26 | + action_permission?: { |
| 27 | + value?: string; |
| 28 | + }; |
| 29 | + session?: { |
| 30 | + id?: string; |
| 31 | + [key: string]: unknown; |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +export interface LionRequestAuth { |
| 36 | + ticket?: string; |
| 37 | + token?: string; |
| 38 | +} |
| 39 | + |
| 40 | +const withRequestAuth = (url: string, auth?: LionRequestAuth) => { |
| 41 | + if (!auth?.ticket && !auth?.token) return url; |
| 42 | + const target = new URL(url, window.location.origin); |
| 43 | + if (auth.ticket) target.searchParams.set("ticket", auth.ticket); |
| 44 | + if (auth.token) target.searchParams.set("token", auth.token); |
| 45 | + return target.toString(); |
| 46 | +}; |
| 47 | + |
| 48 | +const parseResponse = async <T>(response: Response): Promise<T> => { |
| 49 | + const text = await response.text(); |
| 50 | + let data: unknown = null; |
| 51 | + |
| 52 | + if (text) { |
| 53 | + try { |
| 54 | + data = JSON.parse(text); |
| 55 | + } catch { |
| 56 | + data = text; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (!response.ok) { |
| 61 | + const message = |
| 62 | + data && typeof data === "object" && "message" in data |
| 63 | + ? String((data as { message?: unknown }).message || "") |
| 64 | + : text; |
| 65 | + throw new Error(message || `HTTP ${response.status}`); |
| 66 | + } |
| 67 | + |
| 68 | + return data as T; |
| 69 | +}; |
| 70 | + |
| 71 | +const postJson = async <T>(url: string, data: unknown): Promise<T> => { |
| 72 | + const response = await fetch(url, { |
5 | 73 | method: "POST", |
6 | 74 | credentials: "include", |
7 | 75 | headers: { |
8 | 76 | "Content-Type": "application/json" |
9 | 77 | }, |
10 | 78 | body: JSON.stringify(data) |
11 | 79 | }); |
12 | | -}; |
13 | 80 |
|
14 | | -const getSuggestionUsers = (query: string) => { |
15 | | - const url = new URL(withBasePath("/api/v1/users/users/suggestions/"), window.location.origin); |
16 | | - url.searchParams.set("search", query); |
17 | | - return fetch(url.toString(), { credentials: "include" }); |
| 81 | + return await parseResponse<T>(response); |
18 | 82 | }; |
19 | 83 |
|
20 | | -const createShareURL = (data: any, endpointUrl?: string) => postJson(withLionUrl("/api/share/", endpointUrl), data); |
21 | | -const getShareSession = (id: string, data: any, endpointUrl?: string) => |
22 | | - postJson(withLionUrl(`/api/share/${id}/`, endpointUrl), data); |
23 | | -const removeShareUser = (data: any, endpointUrl?: string) => |
24 | | - postJson(withLionUrl("/api/share/remove/", endpointUrl), data); |
| 84 | +const getSuggestionUsers = (query: string, page = 1, limit = 10) => |
| 85 | + apiRequest<SuggestionUserPage | SuggestionUser[]>({ |
| 86 | + method: "GET", |
| 87 | + path: "/api/v1/users/users/suggestions/", |
| 88 | + query: { |
| 89 | + search: query, |
| 90 | + page, |
| 91 | + limit |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | +const createShareURL = (data: unknown, endpointUrl?: string, auth?: LionRequestAuth) => |
| 96 | + postJson<ShareApiResponse>(withRequestAuth(withLionUrl("/api/share/", endpointUrl), auth), data); |
| 97 | +const getShareSession = (id: string, data: unknown, endpointUrl?: string, auth?: LionRequestAuth) => |
| 98 | + postJson<ShareSessionResponse>(withRequestAuth(withLionUrl(`/api/share/${id}/`, endpointUrl), auth), data); |
| 99 | +const removeShareUser = (data: unknown, endpointUrl?: string, auth?: LionRequestAuth) => |
| 100 | + postJson<ShareApiResponse>(withRequestAuth(withLionUrl("/api/share/remove/", endpointUrl), auth), data); |
25 | 101 |
|
26 | 102 | export { createShareURL, getShareSession, getSuggestionUsers, removeShareUser }; |
0 commit comments