@hlw-uni/mp-vue 2.1.51 → 2.1.54
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/dist/app.d.ts +2 -20
- package/dist/composables/device/index.d.ts +5 -70
- package/dist/composables/index.d.ts +5 -14
- package/dist/composables/navigator/index.d.ts +15 -39
- package/dist/composables/request/client.d.ts +21 -0
- package/dist/composables/request/index.d.ts +6 -0
- package/dist/composables/request/service.d.ts +30 -0
- package/dist/composables/{http → request}/types.d.ts +0 -3
- package/dist/composables/share/index.d.ts +7 -66
- package/dist/composables/theme/appearance.d.ts +3 -2
- package/dist/composables/theme/index.d.ts +5 -7
- package/dist/composables/utils/index.d.ts +25 -32
- package/dist/hlw.d.ts +2 -4
- package/dist/index.d.ts +2 -3
- package/dist/index.js +485 -1269
- package/dist/index.mjs +484 -1268
- package/package.json +2 -1
- package/src/app.ts +3 -143
- package/src/components/hlw-ad/index.vue +2 -2
- package/src/components/hlw-page/index.vue +1 -1
- package/src/composables/device/index.ts +110 -83
- package/src/composables/index.ts +9 -39
- package/src/composables/navigator/index.ts +77 -77
- package/src/composables/request/client.ts +204 -0
- package/src/composables/request/index.ts +15 -0
- package/src/composables/request/service.ts +54 -0
- package/src/composables/{http → request}/types.ts +0 -4
- package/src/composables/share/index.ts +64 -168
- package/src/composables/theme/appearance.ts +3 -2
- package/src/composables/theme/index.ts +11 -28
- package/src/composables/theme/palette.ts +22 -4
- package/src/composables/utils/index.ts +131 -95
- package/src/hlw.ts +6 -14
- package/src/index.ts +2 -3
- package/dist/composables/_internal/unwrap.d.ts +0 -15
- package/dist/composables/ad/index.d.ts +0 -78
- package/dist/composables/algo/index.d.ts +0 -7
- package/dist/composables/algo/uuid.d.ts +0 -17
- package/dist/composables/color/index.d.ts +0 -8
- package/dist/composables/contact/index.d.ts +0 -32
- package/dist/composables/format/index.d.ts +0 -9
- package/dist/composables/http/client.d.ts +0 -66
- package/dist/composables/http/index.d.ts +0 -8
- package/dist/composables/loading/index.d.ts +0 -7
- package/dist/composables/page-meta/index.d.ts +0 -18
- package/dist/composables/storage/index.d.ts +0 -16
- package/dist/composables/validate/index.d.ts +0 -12
- package/dist/error.d.ts +0 -1
- package/src/composables/_internal/unwrap.ts +0 -19
- package/src/composables/ad/index.ts +0 -412
- package/src/composables/algo/index.ts +0 -7
- package/src/composables/algo/uuid.ts +0 -27
- package/src/composables/color/index.ts +0 -44
- package/src/composables/contact/index.ts +0 -92
- package/src/composables/format/index.ts +0 -48
- package/src/composables/http/client.ts +0 -237
- package/src/composables/http/index.ts +0 -8
- package/src/composables/http/useRequest.ts +0 -107
- package/src/composables/loading/index.ts +0 -23
- package/src/composables/page-meta/index.ts +0 -49
- package/src/composables/storage/index.ts +0 -76
- package/src/composables/validate/index.ts +0 -58
- package/src/error.ts +0 -5
- /package/dist/composables/{http → request}/adapters/alist.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/base.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/cos.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/index.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/oss.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/qiniu.d.ts +0 -0
- /package/src/composables/{http → request}/adapters/alist.ts +0 -0
- /package/src/composables/{http → request}/adapters/base.ts +0 -0
- /package/src/composables/{http → request}/adapters/cos.ts +0 -0
- /package/src/composables/{http → request}/adapters/index.ts +0 -0
- /package/src/composables/{http → request}/adapters/oss.ts +0 -0
- /package/src/composables/{http → request}/adapters/qiniu.ts +0 -0
|
@@ -1,104 +1,104 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* 使用:
|
|
5
|
-
* const { navigate } = useRouter();
|
|
6
|
-
* navigate(tool.jump_type, tool.jump_value);
|
|
7
|
-
*
|
|
8
|
-
* 未装 vue-router 也能用;命名风格对齐 Vue Router,熟悉的 push/replace/back
|
|
9
|
-
* 会在后续迭代中补齐。
|
|
2
|
+
* useNavigate - uni-app 跳转工具
|
|
10
3
|
*/
|
|
11
|
-
|
|
12
|
-
/** 已知的跳转类型;保留 `(string & {})` 允许任意自定义值向下兼容 */
|
|
13
4
|
export type NavigateType =
|
|
14
5
|
| "navigateTo"
|
|
15
6
|
| "redirectTo"
|
|
16
7
|
| "switchTab"
|
|
17
8
|
| "reLaunch"
|
|
18
|
-
| "
|
|
9
|
+
| "navigateBack"
|
|
19
10
|
| "miniprogram"
|
|
11
|
+
| "webview"
|
|
20
12
|
| (string & {});
|
|
21
13
|
|
|
22
14
|
export interface NavigateOptions {
|
|
23
|
-
/** 失败回调,收到 uni 返回的错误信息 */
|
|
24
|
-
onFail?: (errMsg: string) => void;
|
|
25
|
-
/** 是否禁用默认的 Toast 错误提示,默认 false */
|
|
26
15
|
silent?: boolean;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
16
|
+
onFail?: (message: string) => void;
|
|
17
|
+
delta?: number;
|
|
18
|
+
path?: string;
|
|
30
19
|
envVersion?: "develop" | "trial" | "release";
|
|
31
|
-
|
|
32
|
-
extraData?: Record<string, unknown> | null;
|
|
20
|
+
extraData?: Record<string, unknown>;
|
|
33
21
|
}
|
|
34
22
|
|
|
35
|
-
|
|
36
|
-
* 根据 type + value 调用对应的 uni API,屏蔽参数差异。
|
|
37
|
-
*
|
|
38
|
-
* 对应关系:
|
|
39
|
-
* - `navigateTo`(默认) → uni.navigateTo
|
|
40
|
-
* - `redirectTo` → uni.redirectTo
|
|
41
|
-
* - `switchTab` → uni.switchTab(value 必须是 tabBar 页)
|
|
42
|
-
* - `reLaunch` → uni.reLaunch
|
|
43
|
-
* - `webview` → 默认 Toast 提示,上层需接入 web-view 承载页
|
|
44
|
-
* - `miniprogram` → uni.navigateToMiniProgram,value 格式 `appid:path`
|
|
45
|
-
*/
|
|
46
|
-
function doNavigate(type: NavigateType, value: string, options: NavigateOptions = {}): void {
|
|
47
|
-
const { onFail, silent = false } = options;
|
|
23
|
+
type UniFail = { errMsg?: string };
|
|
48
24
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return;
|
|
25
|
+
function fail(message: string, options: NavigateOptions = {}) {
|
|
26
|
+
if (!options.silent) {
|
|
27
|
+
uni.showToast({ title: message, icon: "none" });
|
|
53
28
|
}
|
|
29
|
+
options.onFail?.(message);
|
|
30
|
+
}
|
|
54
31
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
onFail?.(msg);
|
|
32
|
+
function failHandler(target: string, options: NavigateOptions = {}) {
|
|
33
|
+
return (error?: UniFail) => {
|
|
34
|
+
fail(error?.errMsg || `无法跳转:${target}`, options);
|
|
59
35
|
};
|
|
36
|
+
}
|
|
60
37
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
38
|
+
function navigate(type: NavigateType = "navigateTo", url = "", options: NavigateOptions = {}) {
|
|
39
|
+
if (type === "navigateBack") {
|
|
40
|
+
uni.navigateBack({ delta: options.delta || 1, fail: failHandler("返回上一页", options) });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!url) {
|
|
45
|
+
fail("跳转目标未配置", options);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const onFail = failHandler(url, options);
|
|
50
|
+
|
|
51
|
+
if (type === "redirectTo") {
|
|
52
|
+
uni.redirectTo({ url, fail: onFail });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (type === "switchTab") {
|
|
57
|
+
uni.switchTab({ url, fail: onFail });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (type === "reLaunch") {
|
|
62
|
+
uni.reLaunch({ url, fail: onFail });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (type === "miniprogram") {
|
|
67
|
+
const openMiniProgram = uni.navigateToMiniProgram as
|
|
68
|
+
| ((options: UniApp.NavigateToMiniProgramOptions) => void)
|
|
69
|
+
| undefined;
|
|
70
|
+
|
|
71
|
+
if (!openMiniProgram) {
|
|
72
|
+
fail("当前平台不支持打开小程序", options);
|
|
73
|
+
return;
|
|
85
74
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
75
|
+
|
|
76
|
+
openMiniProgram({
|
|
77
|
+
appId: url,
|
|
78
|
+
path: options.path || "",
|
|
79
|
+
envVersion: options.envVersion || "release",
|
|
80
|
+
extraData: options.extraData,
|
|
81
|
+
fail: onFail,
|
|
82
|
+
});
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (type === "webview") {
|
|
87
|
+
fail(`H5:${url}`, options);
|
|
88
|
+
return;
|
|
90
89
|
}
|
|
90
|
+
|
|
91
|
+
uni.navigateTo({ url, fail: onFail });
|
|
91
92
|
}
|
|
92
93
|
|
|
93
|
-
|
|
94
|
-
* 路由 composable:返回跳转方法集合。
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* const { navigate } = useRouter();
|
|
98
|
-
* navigate("switchTab", "/pages/index/index");
|
|
99
|
-
*/
|
|
100
|
-
export function useRouter() {
|
|
94
|
+
export function useNavigate() {
|
|
101
95
|
return {
|
|
102
|
-
navigate
|
|
96
|
+
navigate,
|
|
97
|
+
to: (url: string, options?: NavigateOptions) => navigate("navigateTo", url, options),
|
|
98
|
+
redirect: (url: string, options?: NavigateOptions) => navigate("redirectTo", url, options),
|
|
99
|
+
tab: (url: string, options?: NavigateOptions) => navigate("switchTab", url, options),
|
|
100
|
+
reLaunch: (url: string, options?: NavigateOptions) => navigate("reLaunch", url, options),
|
|
101
|
+
back: (delta = 1, options: NavigateOptions = {}) => navigate("navigateBack", "", { ...options, delta }),
|
|
102
|
+
miniProgram: (appId: string, options?: NavigateOptions) => navigate("miniprogram", appId, options),
|
|
103
103
|
};
|
|
104
104
|
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { ref } from "vue";
|
|
2
|
+
import type {
|
|
3
|
+
ApiResponse,
|
|
4
|
+
ErrorInterceptor,
|
|
5
|
+
RequestConfig,
|
|
6
|
+
RequestInterceptor,
|
|
7
|
+
ResponseInterceptor,
|
|
8
|
+
UploadConfig,
|
|
9
|
+
UploadResult,
|
|
10
|
+
} from "./types";
|
|
11
|
+
import { getAdapter } from "./adapters";
|
|
12
|
+
|
|
13
|
+
export interface RequestClient {
|
|
14
|
+
request<T = unknown>(config: RequestConfig): Promise<ApiResponse<T>>;
|
|
15
|
+
get<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>>;
|
|
16
|
+
post<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>>;
|
|
17
|
+
put<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>>;
|
|
18
|
+
del<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>>;
|
|
19
|
+
upload(config: UploadConfig): Promise<UploadResult>;
|
|
20
|
+
setBaseURL(url: string): void;
|
|
21
|
+
resolveServiceUrl(namespace: string, url: string, servicePrefix?: string): string;
|
|
22
|
+
onRequest(fn: RequestInterceptor): () => void;
|
|
23
|
+
onResponse<T = unknown>(fn: ResponseInterceptor<T>): () => void;
|
|
24
|
+
onError(fn: ErrorInterceptor): () => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class UniRequestClient implements RequestClient {
|
|
28
|
+
private reqInterceptors: RequestInterceptor[] = [];
|
|
29
|
+
private resInterceptors: ResponseInterceptor[] = [];
|
|
30
|
+
private errInterceptors: ErrorInterceptor[] = [];
|
|
31
|
+
private baseURL = "";
|
|
32
|
+
private defaultHeaders: Record<string, string> = { "Content-Type": "application/json" };
|
|
33
|
+
|
|
34
|
+
setBaseURL(url: string): void {
|
|
35
|
+
this.baseURL = url;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
onRequest(fn: RequestInterceptor): () => void {
|
|
39
|
+
this.reqInterceptors.push(fn);
|
|
40
|
+
return () => this.remove(this.reqInterceptors, fn);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
onResponse<T = unknown>(fn: ResponseInterceptor<T>): () => void {
|
|
44
|
+
const item = fn as ResponseInterceptor;
|
|
45
|
+
this.resInterceptors.push(item);
|
|
46
|
+
return () => this.remove(this.resInterceptors, item);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
onError(fn: ErrorInterceptor): () => void {
|
|
50
|
+
this.errInterceptors.push(fn);
|
|
51
|
+
return () => this.remove(this.errInterceptors, fn);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async request<T = unknown>(config: RequestConfig): Promise<ApiResponse<T>> {
|
|
55
|
+
let cfg: RequestConfig = {
|
|
56
|
+
method: "GET",
|
|
57
|
+
...config,
|
|
58
|
+
headers: { ...this.defaultHeaders, ...config.headers },
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
for (const fn of this.reqInterceptors) {
|
|
62
|
+
cfg = await fn(cfg);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const res = await this.send<T>(this.resolveUrl(cfg.url), cfg);
|
|
67
|
+
for (const fn of this.resInterceptors) {
|
|
68
|
+
const next = await fn(res as ApiResponse<unknown>);
|
|
69
|
+
if (next !== undefined) return next as ApiResponse<T>;
|
|
70
|
+
}
|
|
71
|
+
return res;
|
|
72
|
+
} catch (e) {
|
|
73
|
+
const err = e as Error;
|
|
74
|
+
for (const fn of this.errInterceptors) {
|
|
75
|
+
await fn(err);
|
|
76
|
+
}
|
|
77
|
+
throw err;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>> {
|
|
82
|
+
return this.request<T>({ url, method: "GET", data });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
post<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>> {
|
|
86
|
+
return this.request<T>({ url, method: "POST", data });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
put<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>> {
|
|
90
|
+
return this.request<T>({ url, method: "PUT", data });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
del<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>> {
|
|
94
|
+
return this.request<T>({ url, method: "DELETE", data });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
upload(config: UploadConfig): Promise<UploadResult> {
|
|
98
|
+
const fileName = config.fileName ?? config.filePath.split("/").pop() ?? "file";
|
|
99
|
+
const server = config.type === "local" && config.url ? config.url : config.server;
|
|
100
|
+
const formData = config.type === "local"
|
|
101
|
+
? (config.credentials ?? {})
|
|
102
|
+
: getAdapter(config.type).buildFormData({
|
|
103
|
+
filePath: config.filePath,
|
|
104
|
+
fileName,
|
|
105
|
+
credentials: config.credentials,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
uni.uploadFile({
|
|
110
|
+
url: server,
|
|
111
|
+
filePath: config.filePath,
|
|
112
|
+
name: "file",
|
|
113
|
+
formData: formData as unknown as UniNamespace.UploadFileOption["formData"],
|
|
114
|
+
header: config.header as Record<string, string>,
|
|
115
|
+
success: (res) => {
|
|
116
|
+
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
117
|
+
reject(new Error(`上传失败: ${res.statusCode}`));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
const body = JSON.parse(res.data);
|
|
123
|
+
resolve({
|
|
124
|
+
code: body.code ?? 1,
|
|
125
|
+
msg: body.msg ?? body.message ?? "上传成功",
|
|
126
|
+
data: body.data ?? "",
|
|
127
|
+
});
|
|
128
|
+
} catch {
|
|
129
|
+
resolve({ code: 1, msg: "上传成功", data: res.data });
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
fail: (err) => reject(new Error(err.errMsg || "上传失败")),
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
resolveServiceUrl(namespace: string, url: string, servicePrefix = ""): string {
|
|
138
|
+
if (isAbsolute(url)) return url;
|
|
139
|
+
|
|
140
|
+
const ns = namespace.replace(/^\/+|\/+$/g, "").replace(/\//g, ".");
|
|
141
|
+
const prefixValue = servicePrefix.replace(/^\/+|\/+$/g, "");
|
|
142
|
+
const path = url.startsWith("/") ? url : `/${url}`;
|
|
143
|
+
const prefix = [prefixValue, ns].filter(Boolean).join("/");
|
|
144
|
+
|
|
145
|
+
return prefix ? `/${prefix}${path}` : path;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private resolveUrl(url: string): string {
|
|
149
|
+
if (isAbsolute(url)) return url;
|
|
150
|
+
return `${this.baseURL}${url}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private send<T>(url: string, cfg: RequestConfig): Promise<ApiResponse<T>> {
|
|
154
|
+
return new Promise((resolve, reject) => {
|
|
155
|
+
uni.request({
|
|
156
|
+
url,
|
|
157
|
+
method: cfg.method,
|
|
158
|
+
data: cfg.data as UniNamespace.RequestOptions["data"],
|
|
159
|
+
header: cfg.headers as Record<string, string>,
|
|
160
|
+
timeout: cfg.timeout,
|
|
161
|
+
success: (res) => {
|
|
162
|
+
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
163
|
+
resolve(res.data as ApiResponse<T>);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const body = res.data as Record<string, unknown> | undefined;
|
|
168
|
+
reject(new Error(String(body?.info ?? body?.message ?? `请求失败: ${res.statusCode}`)));
|
|
169
|
+
},
|
|
170
|
+
fail: (err) => reject(new Error(err.errMsg || "网络请求失败")),
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private remove<T>(items: T[], item: T): void {
|
|
176
|
+
const index = items.indexOf(item);
|
|
177
|
+
if (index > -1) items.splice(index, 1);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function isAbsolute(url: string): boolean {
|
|
182
|
+
return /^(https?:)?\/\//.test(url) || url.startsWith("file://");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const requestClient = new UniRequestClient();
|
|
186
|
+
|
|
187
|
+
export function useRequest(): RequestClient {
|
|
188
|
+
return requestClient;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function useUpload() {
|
|
192
|
+
const uploading = ref(false);
|
|
193
|
+
|
|
194
|
+
async function upload(options: UploadConfig): Promise<UploadResult> {
|
|
195
|
+
uploading.value = true;
|
|
196
|
+
try {
|
|
197
|
+
return await requestClient.upload(options);
|
|
198
|
+
} finally {
|
|
199
|
+
uploading.value = false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return { uploading, upload };
|
|
204
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { useRequest, useUpload } from "./client";
|
|
2
|
+
export { BaseService, ServiceNamespace, ServicePrefix } from "./service";
|
|
3
|
+
export type { RequestClient } from "./client";
|
|
4
|
+
export type {
|
|
5
|
+
ApiResponse,
|
|
6
|
+
PageResult,
|
|
7
|
+
RequestConfig,
|
|
8
|
+
RequestInterceptor,
|
|
9
|
+
ResponseInterceptor,
|
|
10
|
+
ErrorInterceptor,
|
|
11
|
+
UploadConfig,
|
|
12
|
+
UploadResult,
|
|
13
|
+
} from "./types";
|
|
14
|
+
export type { ServiceNamespaceOptions, ServicePrefixOptions, ServiceRequestConfig } from "./service";
|
|
15
|
+
export * from "./adapters";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useRequest } from "./client";
|
|
2
|
+
import type { ApiResponse, RequestConfig } from "./types";
|
|
3
|
+
|
|
4
|
+
export type ServiceRequestConfig = Omit<RequestConfig, "url"> & {
|
|
5
|
+
url: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export interface ServiceNamespaceOptions {
|
|
9
|
+
namespace: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ServicePrefixOptions {
|
|
13
|
+
prefix: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class BaseService {
|
|
17
|
+
declare namespace: string;
|
|
18
|
+
declare servicePrefix: string;
|
|
19
|
+
|
|
20
|
+
request<T = unknown>(options: ServiceRequestConfig): Promise<ApiResponse<T>> {
|
|
21
|
+
return useRequest().request<T>({
|
|
22
|
+
...options,
|
|
23
|
+
url: useRequest().resolveServiceUrl(this.namespace ?? "", options.url, this.servicePrefix ?? ""),
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>> {
|
|
28
|
+
return this.request<T>({ url, method: "GET", data });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
post<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>> {
|
|
32
|
+
return this.request<T>({ url, method: "POST", data });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
put<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>> {
|
|
36
|
+
return this.request<T>({ url, method: "PUT", data });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
del<T = unknown>(url: string, data?: unknown): Promise<ApiResponse<T>> {
|
|
40
|
+
return this.request<T>({ url, method: "DELETE", data });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function ServiceNamespace(value: string | ServiceNamespaceOptions) {
|
|
45
|
+
return function (target: { prototype: { namespace?: string } }) {
|
|
46
|
+
target.prototype.namespace = typeof value === "string" ? value : value.namespace;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function ServicePrefix(value: string | ServicePrefixOptions) {
|
|
51
|
+
return function (target: { prototype: { servicePrefix?: string } }) {
|
|
52
|
+
target.prototype.servicePrefix = typeof value === "string" ? value : value.prefix;
|
|
53
|
+
};
|
|
54
|
+
}
|