create-web-kit 25.728.953 → 25.728.1414

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.
@@ -1,204 +0,0 @@
1
- /**
2
- * HTTP 请求类封装(强类型,无 any)
3
- * 配合 React Query 使用,简化超时和重试逻辑
4
- */
5
-
6
- import { toast } from "sonner";
7
-
8
- interface RequestConfig {
9
- baseURL?: string;
10
- headers?: Record<string, string>;
11
- }
12
-
13
- interface RequestOptions extends RequestInit {
14
- params?: Record<string, string | number | boolean>;
15
- data?: unknown; // 用于传递请求体
16
- }
17
-
18
- interface ApiResponse<T> {
19
- data: T;
20
- status?: number;
21
- ok?: boolean;
22
- code?: number; // 后端业务状态码
23
- msg?: string | null; // 后端消息
24
- }
25
-
26
- // 后端 API 响应结构
27
- interface BackendResponse<T> {
28
- code: number;
29
- msg: string | null;
30
- data: T;
31
- }
32
-
33
- export class HttpClient {
34
- private baseURL: string;
35
- private defaultHeaders: Record<string, string>;
36
-
37
- constructor(config: RequestConfig = {}) {
38
- this.baseURL = config.baseURL || "";
39
- this.defaultHeaders = {
40
- "Content-Type": "application/json",
41
- ...config.headers,
42
- };
43
- }
44
- updateToken(token: string) {
45
- // this.defaultHeaders['Authorization'] = `Bearer ${token}`;
46
- this.defaultHeaders["token"] = `${token}`;
47
- }
48
-
49
- private buildURL(url: string, params?: Record<string, unknown>): string {
50
- let fullURL = url.startsWith("http") ? url : `${this.baseURL}${url}`;
51
-
52
- if (params) {
53
- const searchParams = new URLSearchParams();
54
- Object.entries(params).forEach(([key, value]) => {
55
- if (value !== null && value !== undefined) {
56
- searchParams.append(key, String(value));
57
- }
58
- });
59
- const paramString = searchParams.toString();
60
- if (paramString) {
61
- fullURL += `${fullURL.includes("?") ? "&" : "?"}${paramString}`;
62
- }
63
- }
64
-
65
- return fullURL;
66
- }
67
-
68
- async request<T>(
69
- url: string,
70
- options: RequestOptions = {}
71
- ): Promise<ApiResponse<T>> {
72
- const { params, data, ...fetchOptions } = options;
73
-
74
- const fullURL = this.buildURL(url, params);
75
-
76
- const headers: HeadersInit = {
77
- ...this.defaultHeaders,
78
- ...(fetchOptions.headers || {}),
79
- };
80
-
81
- let body = fetchOptions.body;
82
-
83
- // 如果提供了 data,优先使用
84
- if (data !== undefined) {
85
- if (data instanceof FormData) {
86
- body = data;
87
- Reflect.deleteProperty(headers, "Content-Type"); // FormData 不需要手动设置 Content-Type
88
- } else {
89
- body = JSON.stringify(data);
90
- }
91
- }
92
- try {
93
- const response = await fetch(fullURL, {
94
- ...fetchOptions,
95
- headers,
96
- body,
97
- });
98
- const contentType = response.headers.get("content-type");
99
- let responseData: T;
100
- if (contentType?.includes("application/json")) {
101
- const jsonResponse: BackendResponse<T> = await response.json();
102
- if (jsonResponse.code == 401) {
103
- // 清除本地存储
104
- localStorage.clear();
105
-
106
- // 退出用户状态
107
- if (typeof window !== "undefined") {
108
- // 动态导入store避免循环依赖
109
- import("@/store")
110
- .then(({ useStore }) => {
111
- const { logout, openLoginDialog } = useStore.getState();
112
- logout();
113
- openLoginDialog();
114
- })
115
- .catch(console.error);
116
- }
117
-
118
- toast.error("登录信息已过期,请重新登录");
119
- }
120
- return jsonResponse;
121
- } else if (contentType?.startsWith("text/")) {
122
- responseData = (await response.text()) as T;
123
- } else {
124
- responseData = (await response.blob()) as T;
125
- }
126
-
127
- return {
128
- data: responseData,
129
- status: response.status,
130
- ok: response.ok,
131
- };
132
- } catch (error: unknown) {
133
- if (error instanceof Error) {
134
- throw new Error(error.message);
135
- }
136
- throw new Error("网络请求失败");
137
- }
138
- }
139
-
140
- // GET 请求
141
- get<T>(
142
- url: string,
143
- params?: Record<string, string | number | boolean>,
144
- options?: Omit<RequestOptions, "params">
145
- ) {
146
- return this.request<T>(url, { ...options, method: "GET", params });
147
- }
148
-
149
- // POST 请求
150
- post<T = unknown, B = unknown>(
151
- url: string,
152
- data?: B,
153
- options?: Omit<RequestOptions, "data">
154
- ) {
155
- return this.request<T>(url, { ...options, method: "POST", data });
156
- }
157
-
158
- // PUT 请求
159
- put<T = unknown, B = unknown>(
160
- url: string,
161
- data?: B,
162
- options?: Omit<RequestOptions, "data">
163
- ) {
164
- return this.request<T>(url, { ...options, method: "PUT", data });
165
- }
166
-
167
- // PATCH 请求
168
- patch<T = unknown, B = unknown>(
169
- url: string,
170
- data?: B,
171
- options?: Omit<RequestOptions, "data">
172
- ) {
173
- return this.request<T>(url, { ...options, method: "PATCH", data });
174
- }
175
-
176
- // DELETE 请求
177
- delete<T = unknown>(url: string, options?: RequestOptions) {
178
- return this.request<T>(url, { ...options, method: "DELETE" });
179
- }
180
-
181
- // 上传文件
182
- upload<T = unknown>(
183
- url: string,
184
- formData: FormData,
185
- options?: Omit<RequestOptions, "data" | "body">
186
- ) {
187
- return this.request<T>(url, { ...options, method: "POST", data: formData });
188
- }
189
- }
190
-
191
- // 创建默认实例
192
- export const http = new HttpClient({
193
- baseURL: `${process.env.NEXT_PUBLIC_API_URL || "/api"}`,
194
- });
195
-
196
- // 快捷导出函数
197
- export const get = http.get.bind(http);
198
- export const post = http.post.bind(http);
199
- export const put = http.put.bind(http);
200
- export const patch = http.patch.bind(http);
201
- export const del = http.delete.bind(http);
202
- export const upload = http.upload.bind(http);
203
-
204
- export default http;
@@ -1,12 +0,0 @@
1
- interface ShowProps {
2
- when: boolean;
3
- fallback?: React.ReactNode;
4
- }
5
-
6
- export default function Show({
7
- children,
8
- when,
9
- fallback = null,
10
- }: React.PropsWithChildren<ShowProps>) {
11
- return when ? children : fallback;
12
- }
@@ -1,17 +0,0 @@
1
- "use client";
2
-
3
- import { ThemeProvider as NextThemesProvider } from "next-themes";
4
- import * as React from "react";
5
-
6
- export function ThemeProvider({ children }: React.PropsWithChildren) {
7
- return (
8
- <NextThemesProvider
9
- attribute="class"
10
- defaultTheme="system"
11
- enableSystem
12
- disableTransitionOnChange
13
- >
14
- {children}
15
- </NextThemesProvider>
16
- );
17
- }
@@ -1,12 +0,0 @@
1
- import { defineConfig } from "vite";
2
- import vue from "@vitejs/plugin-vue";
3
- import { resolve } from "path";
4
-
5
- export default defineConfig({
6
- plugins: [vue()],
7
- resolve: {
8
- alias: {
9
- "@": resolve(__dirname, "src"),
10
- },
11
- },
12
- });