@libs-ui/services-http-request 0.2.355-8 → 0.2.356-0
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/README.md +153 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,3 +1,154 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Http Request Service
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Service trung gian cho HTTP requests trong Angular. Gọi bất kỳ method nào của một service khác thông qua config object, hỗ trợ caching 3 tầng (memory / localStorage / IndexedDB) và auto-detect Observable vs Promise. Kèm bộ **API Mockup** (`returnListObject`, `returnDetailObject`) để phát triển frontend không cần backend.
|
|
4
|
+
|
|
5
|
+
## Tính năng
|
|
6
|
+
|
|
7
|
+
- ✅ `callApi()` — gọi method bất kỳ của service qua config, không cần inject trực tiếp
|
|
8
|
+
- ✅ Caching 3 tầng: `service` (memory Map) / `local-store` (localStorage) / `indexDB` (persist + TTL)
|
|
9
|
+
- ✅ Auto-detect Observable/Promise — không cần convert thủ công
|
|
10
|
+
- ✅ `convertResponseData` — transform response trước khi trả về (cache lưu raw)
|
|
11
|
+
- ✅ `updateArguments()` — tự động cập nhật page params cho infinite scroll / load more
|
|
12
|
+
- ✅ **API Mockup**: `returnListObject`, `returnDetailObject`, `getConfigListDataDemo`, `pagingToList`
|
|
13
|
+
|
|
14
|
+
## Cài đặt
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @libs-ui/services-http-request
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Import
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
// Service chính
|
|
24
|
+
import { LibsUiHttpRequestService } from '@libs-ui/services-http-request';
|
|
25
|
+
|
|
26
|
+
// API Mockup (dev/test)
|
|
27
|
+
import { returnListObject, returnDetailObject, getConfigListDataDemo, pagingToList } from '@libs-ui/services-http-request';
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start — callApi()
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { Component, inject } from '@angular/core';
|
|
34
|
+
import { LibsUiHttpRequestService } from '@libs-ui/services-http-request';
|
|
35
|
+
|
|
36
|
+
@Component({ standalone: true, ... })
|
|
37
|
+
export class FeatureComponent {
|
|
38
|
+
private httpReq = inject(LibsUiHttpRequestService);
|
|
39
|
+
|
|
40
|
+
async loadData() {
|
|
41
|
+
// Gọi UserService.getUsers(page=1) — không cần inject UserService
|
|
42
|
+
const result = await this.httpReq.callApi<IHttpResponse<User[]>>({
|
|
43
|
+
serviceClass: UserService, // Angular DI resolve tự động
|
|
44
|
+
functionName: 'getUsers', // ten method
|
|
45
|
+
argumentsValue: [1], // arguments truyền vào method
|
|
46
|
+
});
|
|
47
|
+
console.log(result.data); // User[]
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Caching
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
// 3 loại cache: service (memory) | local-store (localStorage) | indexDB (persist + TTL)
|
|
56
|
+
const result = await httpReq.callApi({
|
|
57
|
+
serviceClass: UserService,
|
|
58
|
+
functionName: 'getUsers',
|
|
59
|
+
argumentsValue: [],
|
|
60
|
+
keyCache: 'USER_LIST_UNIQUE_KEY', // UUID cố định — KHÔNG random
|
|
61
|
+
cacheType: 'service', // memory, nhanh nhất
|
|
62
|
+
// cacheType: 'local-store', // persist qua session
|
|
63
|
+
// cacheType: 'indexDB', // persist + timeCache phút
|
|
64
|
+
timeCache: 30, // 30 phút (chỉ áp dụng cho indexDB)
|
|
65
|
+
clearCache: false, // true = xóa cache trước khi gọi
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API Mockup — Dev Mode
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { returnListObject, returnDetailObject } from '@libs-ui/services-http-request';
|
|
73
|
+
import { UtilsHttpParamsRequest } from '@libs-ui/utils';
|
|
74
|
+
|
|
75
|
+
const mockData = [
|
|
76
|
+
{ id: 1, name: 'Alice' },
|
|
77
|
+
{ id: 2, name: 'Bob' },
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
// List với phân trang
|
|
81
|
+
const listSvc = returnListObject(mockData, undefined, { delay: 300 });
|
|
82
|
+
const params = new UtilsHttpParamsRequest({ fromObject: { page: '1', per_page: '10' } });
|
|
83
|
+
const paged = await listSvc.listPaging(params);
|
|
84
|
+
// → { code: 200, data: [...], paging: { page, total_pages, total_items, per_page } }
|
|
85
|
+
|
|
86
|
+
// Detail lookup by key
|
|
87
|
+
const detailParams = new UtilsHttpParamsRequest({ fromObject: { keys: '1', fieldKey: 'id' } });
|
|
88
|
+
const detailSvc = returnDetailObject(detailParams, mockData);
|
|
89
|
+
const detail = await detailSvc.detailByData();
|
|
90
|
+
// → { code: 200, data: { id: 1, name: 'Alice' } }
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Infinite Scroll — updateArguments()
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
let argumentsValue = [new HttpParams().set('page', '1').set('per_page', '20')];
|
|
97
|
+
let pagingStore: IPaging;
|
|
98
|
+
let isLoadedAll = false;
|
|
99
|
+
|
|
100
|
+
async function loadMore() {
|
|
101
|
+
isLoadedAll = httpReq.updateArguments(
|
|
102
|
+
argumentsValue,
|
|
103
|
+
{ paging: pagingStore },
|
|
104
|
+
pagingStore,
|
|
105
|
+
searchKeyword,
|
|
106
|
+
true,
|
|
107
|
+
isLoadedAll,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
if (!isLoadedAll) {
|
|
111
|
+
const res = await httpReq.callApi({ ..., argumentsValue });
|
|
112
|
+
pagingStore = res.paging;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## API — Public Methods
|
|
118
|
+
|
|
119
|
+
| Method | Returns | Mô tả |
|
|
120
|
+
| --------------------------------------------------------------------- | --------------------- | -------------------------------------------------------------------- |
|
|
121
|
+
| `callApi<T>(config)` | `Promise<T>` | Gọi method của service theo config, có cache tự động |
|
|
122
|
+
| `updateArguments(args, data, paging, search, online, loaded, guide?)` | `boolean` | Cập nhật argumentsValue cho load more. Trả về `true` khi hết dữ liệu |
|
|
123
|
+
| `fakeResponsePagingApi()` | `{ paging: IPaging }` | Tạo fake paging (fallback) — `page:0, total_pages:1` |
|
|
124
|
+
|
|
125
|
+
## API — IHttpRequestConfig
|
|
126
|
+
|
|
127
|
+
| Field | Type | Required | Mô tả |
|
|
128
|
+
| --------------------- | ----------------------------------------- | ----------------------- | ------------------------------------------ |
|
|
129
|
+
| `serviceClass` | `Type<T>` | Một trong hai | Class service — Angular DI resolve tự động |
|
|
130
|
+
| `objectInstance` | `T` | Một trong hai | Object instance — không qua DI |
|
|
131
|
+
| `functionName` | `keyof T` | ✅ | Tên method cần gọi |
|
|
132
|
+
| `argumentsValue` | `Array<any>` | ✅ | Arguments truyền vào method |
|
|
133
|
+
| `cacheType` | `'service' \| 'local-store' \| 'indexDB'` | ❌ | Loại cache |
|
|
134
|
+
| `keyCache` | `string` | Bắt buộc nếu dùng cache | Cache key (UUID cố định) |
|
|
135
|
+
| `timeCache` | `number` | ❌ | Phút — chỉ áp dụng cho `indexDB` |
|
|
136
|
+
| `clearCache` | `boolean` | ❌ | Xóa cache trước khi gọi |
|
|
137
|
+
| `convertResponseData` | `(res) => any` | ❌ | Transform response trước khi trả về |
|
|
138
|
+
|
|
139
|
+
## API Mockup Functions
|
|
140
|
+
|
|
141
|
+
| Function | Returns | Mô tả |
|
|
142
|
+
| ---------------------------------------------- | -------------------------------------- | ------------------------------------------------- |
|
|
143
|
+
| `returnListObject(data?, fn?, opts?)` | `{ list, listObservable, listPaging }` | Mock list API từ array tĩnh hoặc dynamic function |
|
|
144
|
+
| `returnDetailObject(params, data?, fn?, ...)` | `{ detailByData, detailByFunction }` | Mock detail API — lookup theo key |
|
|
145
|
+
| `getConfigListDataDemo(params?, schema?, fn?)` | `{ list, detailByData }` | Tạo 3015 items mock sẵn |
|
|
146
|
+
| `pagingToList(data, page, perPage)` | `IHttpResponse<T>` | Phân trang mảng data |
|
|
147
|
+
|
|
148
|
+
## Lưu ý quan trọng
|
|
149
|
+
|
|
150
|
+
- **`keyCache` phải là UUID cố định** — không dùng biến ngẫu nhiên hay timestamp. Cùng key = cache hit.
|
|
151
|
+
- **`serviceClass` vs `objectInstance`**: dùng `serviceClass` khi service được DI quản lý; `objectInstance` khi cần mock hoặc service không injectable.
|
|
152
|
+
- **Cache key** được tạo từ `keyCache + MD5(argumentsValue)` — cùng `keyCache` nhưng args khác = cache key khác.
|
|
153
|
+
- **Observable auto-wrap**: method có thể trả về Observable hay Promise — service tự detect và wrap bằng `lastValueFrom()`.
|
|
154
|
+
- **`convertResponseData`** chạy cả khi đọc từ cache — cache luôn lưu raw response.
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libs-ui/services-http-request",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.356-0",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"@angular/common": ">=18.0.0",
|
|
6
6
|
"@angular/core": ">=18.0.0",
|
|
7
|
-
"@libs-ui/interfaces-types": "0.2.
|
|
8
|
-
"@libs-ui/utils": "0.2.
|
|
7
|
+
"@libs-ui/interfaces-types": "0.2.356-0",
|
|
8
|
+
"@libs-ui/utils": "0.2.356-0",
|
|
9
9
|
"rxjs": "~7.8.0"
|
|
10
10
|
},
|
|
11
11
|
"sideEffects": false,
|