Vue CURD Class

Vue CURD Class

vue 专属的 curd 类

在之前 Angular 版本上面作了少量修改,但是默认依然返回 Observable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import type http from "@/libraries/request";
import { of, Observable, from } from "rxjs";
import { map, filter, mergeMap } from "rxjs/operators";

import type { CommonResult, PageResult } from "@/models/public_api";

type httpClient = typeof http;
type joinPathParamsType = Parameters<typeof joinPath>[0];

const env = import.meta.env;

const handleError = (error: Error) => {
if (env.MODE !== "prod") {
console.error(error);
}
};

export function isNonNull<T>(value: T): value is NonNullable<T> {
return value != null || value !== undefined;
}

export function joinPath(params?: {
[key: string]: string | undefined | null;
}): string {
let path = "";
if (!params) {
return path;
} else if (Object.prototype.toString.call(params) === "[object Object]") {
for (const key in params) {
const value = params[key];
path = `${path}${value}/`;
}
return path;
} else {
return path;
}
}

export class CurdFactoryService {
/**
*
* @param queryParams url ? 查询参数
* @param params url 路径参数 id、code 拼接
*/
_fetch = async <T = never, Q = never, P extends joinPathParamsType = never>(
queryParams?: Q,
params?: P
) => {
const url = `${this.baseUrl}${joinPath(params)}`;
return (
this.http.get(url, { params: queryParams }) as Promise<CommonResult<T>>
)
.then((res) => {
if (res) {
return res;
} else {
return null;
}
})
.catch((error) => {
handleError(error);
return null;
});
};
/**
*
* @desc 获取携带分页信息的数据
* @param queryParams url ? 查询参数
* @param params url 路径参数 id、code 拼接
*/
_fetchSlice = async <
T = never,
Q = never,
P extends joinPathParamsType = never
>(
queryParams?: Q,
params?: P
) => {
const url = `${this.baseUrl}${joinPath(params)}`;
return (
this.http.get(url, { params: queryParams }) as Promise<
CommonResult<PageResult<T[]>>
>
)
.then((res) => {
if (res) {
return res;
} else {
return null;
}
})
.catch((error) => {
handleError(error);
return null;
});
};
/**
*
* @desc 获取携带分页信息的所有分页数据
* @param queryParams url ? 查询参数
* @param params url 路径参数 id、code 拼接
*/
_fetchAllSlice = <T = never, Q = never, P extends joinPathParamsType = never>(
queryParams: Q & { limit: number; offset: number },
params?: P
) => {
const part = (
_queryParams: typeof queryParams,
_params?: typeof params
): Observable<T[]> => {
return from(this._fetchSlice<T, Q, P>(_queryParams, _params)).pipe(
filter(isNonNull),
mergeMap((res) => {
const { results: previousResults, count } = res.data;
const { limit, offset } = _queryParams;
const nextOffset = Number(limit) + Number(offset);
if (nextOffset <= count - 1) {
return part({ ...queryParams, offset: nextOffset }).pipe(
map((results) => {
return [...previousResults, ...results];
})
);
} else {
return of(previousResults);
}
})
);
};
return part(queryParams, params);
};
/**
*
* @param body 请求体
* @param params url 路径参数 id、code 拼接
*/
_post = async <T = never, B = never, P extends joinPathParamsType = never>(
body?: B,
params?: P
) => {
const url = `${this.baseUrl}${joinPath(params)}`;
return (this.http.post(url, body) as Promise<CommonResult<T>>)
.then((res) => {
if (res) {
return res;
} else {
return null;
}
})
.catch((error) => {
handleError(error);
return null;
});
};
/**
*
* @param body 请求体
* @param params url 路径参数 id、code 拼接
*/
_postForm = async <
T = never,
B = never,
P extends joinPathParamsType = never
>(
body?: B,
params?: P
) => {
const url = `${this.baseUrl}${joinPath(params)}`;
return (this.http.postForm(url, body) as Promise<CommonResult<T>>)
.then((res) => {
if (res) {
return res;
} else {
return null;
}
})
.catch((error) => {
handleError(error);
return null;
});
};
/**
*
* @param body 请求体
* @param params url 路径参数 id、code 拼接
*/
_patch = async <T = never, B = never, P extends joinPathParamsType = never>(
body?: B,
params?: P
) => {
const url = `${this.baseUrl}${joinPath(params)}`;
return (this.http.patch(url, body) as Promise<CommonResult<T>>)
.then((res) => {
if (res) {
return res;
} else {
return null;
}
})
.catch((error) => {
handleError(error);
return null;
});
};
/**
*
* @param body 请求体
* @param params url 路径参数 id、code 拼接
*/
_put = async <T = never, B = never, P extends joinPathParamsType = never>(
body?: B,
params?: P
) => {
const url = `${this.baseUrl}${joinPath(params)}`;
return (this.http.put(url, body) as Promise<CommonResult<T>>)
.then((res) => {
if (res) {
return res;
} else {
return null;
}
})
.catch((error) => {
handleError(error);
return null;
});
};
/**
*
* @param body 请求体
* @param params url 路径参数 id、code 拼接
*/
_delete = async <T = never, P extends joinPathParamsType = never>(
params?: P
) => {
const url = `${this.baseUrl}${joinPath(params)}`;
return (this.http.delete(url) as Promise<CommonResult<T>>)
.then((res) => {
if (res) {
return res;
} else {
return null;
}
})
.catch((error) => {
handleError(error);
return null;
});
};

constructor(private http: httpClient, private baseUrl: string | null) {}
}

如何使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { CurdFactoryService } from '@/apis/curd-factory';
import http from '@/libraries/request';

const host = "https://api.tedding.dev";
const api = "/api/get-xxx";
const AAA = new CurdFactoryService(http, `${host}${api}`);

const fetchAAA = (params: any) => {
AAA._fetch<any, typeof params>(params);
};
const postAAA = (body) => {
AAA._post(body);
};
const postFormAAA = (body) => {
AAA._postForm(body);
};
const fetchSliceAAA = (params) => {
AAA._fetchSlice(params);
};

Vue CURD Class
https://tedding.dev/2023/06/06/1888f402630.html
作者
TED.DING
发布于
2023年6月6日
许可协议