Angular CURD Abstract Class

Angular CURD Abstract Class

RESTful Api 一次性生成所有的可能的 Http Action,简化coding 时间

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
import { catchError, map, filter, mergeMap } from "rxjs/operators";
import { HttpClient } from "@angular/common/http";
import { of, Observable } from "rxjs";

interface CommonResult<T> {
code: number;
success: boolean;
msg: string;
data: T;
}

interface Result<T> {
count: number;
next: string;
previous: string;
results: T;
}

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

export abstract class CurdFactoryService {
/** 基础接口url */
abstract baseUrl: string;

private 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;
}
}

/**
*
* @param queryParams url ? 查询参数
* @param params url 路径参数 id、code 拼接
*/
_fetch = <T, Q = any, P extends any = any>(queryParams?: Q, params?: P) => {
const url = `${this.baseUrl}${this.joinPath(params)}`;
return this.http.get<CommonResult<T>>(url, queryParams).pipe(
map((res) => {
if (res) {
return res;
} else {
return null;
}
}),
catchError((error) => {
console.error(error);
return of(null);
})
);
};
/**
*
* @desc 获取携带分页信息的数据
* @param queryParams url ? 查询参数
* @param params url 路径参数 id、code 拼接
*/
_fetchSlice = <T, Q = any, P extends any = any>(
queryParams?: Q,
params?: P
) => {
const url = `${this.baseUrl}${this.joinPath(params)}`;
return this.http.get<CommonResult<Result<T[]>>>(url, queryParams).pipe(
map((res) => {
if (res) {
return res;
} else {
return null;
}
}),
catchError((error) => {
console.error(error);
return of(null);
})
);
};
/**
*
* @desc 获取携带分页信息的所有分页数据
* @param queryParams url ? 查询参数
* @param params url 路径参数 id、code 拼接
*/
_fetchAllSlice = <T, Q = any, P extends any = any>(
queryParams: Q & { limit: number; offset: number },
params?: P
) => {
const part = (
_queryParams: typeof queryParams,
_params?: typeof params
): Observable<T[]> => {
return this._fetchSlice<T, Q, P>(_queryParams, _params).pipe(
filter(isNonNull),
map((res) => res.data),
mergeMap((data) => {
const { results: previousResults, count } = 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 = <T, B = any, P extends any = any>(body?: B, params?: P) => {
const url = `${this.baseUrl}${this.joinPath(params)}`;
return this.http.post<CommonResult<T>>(url, body, params).pipe(
map((res) => {
if (res) {
return res;
} else {
return null;
}
}),
catchError((error) => {
console.error(error);
return of(null);
})
);
};
/**
*
* @param body 请求体
* @param params url 路径参数 id、code 拼接
*/
_patch = <T, B = any, P extends any = any>(body?: B, params?: P) => {
const url = `${this.baseUrl}${this.joinPath(params)}`;
return this.http.patch<CommonResult<T>>(url, body).pipe(
map((res) => {
if (res) {
return res;
} else {
return null;
}
}),
catchError((error) => {
console.error(error);
return of(null);
})
);
};
/**
*
* @param body 请求体
* @param params url 路径参数 id、code 拼接
*/
_delete = <T, P extends any = any>(params?: P) => {
const url = `${this.baseUrl}${this.joinPath(params)}`;
return this.http.delete<CommonResult<T>>(url).pipe(
map((res) => {
if (res) {
return res;
} else {
return null;
}
}),
catchError((error) => {
console.error(error);
return of(null);
})
);
};

constructor(private http: HttpClient) {}

如何使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { Injectable } from "@angular/core";
import { CurdFactoryService } from "@core";
import { HttpClient } from "@angular/common/http";
import { APIS } from "@configure";

@Injectable({
providedIn: "root",
})
export class DoctorAppConfigService extends CurdFactoryService {
baseUrl: string;
constructor(http: _HttpClient) {
super(http);
this.baseUrl = APIS.doctorPromotion;
}
}
/** 也可以在此扩展特有的方法 **/
funcA = () => {};
funcB = () => {};
funcC = () => {};

还可以这样用

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
import { Injectable } from "@angular/core";
import { CurdFactoryService } from "@core";
import { HttpClient } from "@angular/common/http";
import { APIS } from "@configure";

class AAA extends CurdFactoryService {
baseUrl: string;
constructor(http: _HttpClient) {
super(http);
this.baseUrl = APIS.AAA;
}
}

class BBB extends CurdFactoryService {
baseUrl: string;
constructor(http: _HttpClient) {
super(http);
this.baseUrl = APIS.BBB;
}
}

class CCC extends CurdFactoryService {
baseUrl: string;
constructor(http: _HttpClient) {
super(http);
this.baseUrl = APIS.CCC;
}
}
class DDD extends CurdFactoryService {
baseUrl: string;
constructor(http: _HttpClient) {
super(http);
this.baseUrl = APIS.DDD;
}
}

@Injectable({
providedIn: "root",
})
export class UnionService {
/** API 1 */
AAAService = new AAA(this.http);
/** API 2 */
BBBService = new BBB(this.http);
/** API 3 */
CCCService = new CCC(this.http);
/** API 4 */
DDDService = new DDD(this.http);
constructor(private http: _HttpClient) {}
}

Angular CURD Abstract Class
https://tedding.dev/2023/03/15/186e2f0a090.html
作者
TED.DING
发布于
2023年3月15日
许可协议