ngx-api-cache 20.0.1 → 20.0.2
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 +67 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ This project was generated using [Angular CLI](https://github.com/angular/angula
|
|
|
4
4
|
|
|
5
5
|
## Description
|
|
6
6
|
|
|
7
|
-
This is a very lightweight library for
|
|
7
|
+
This is a very lightweight library for api-cache.
|
|
8
8
|
|
|
9
9
|
This library does not require third party dependencies
|
|
10
10
|
|
|
@@ -13,7 +13,7 @@ This library does not require third party dependencies
|
|
|
13
13
|
1. Install
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
npm i ngx-api-cache
|
|
16
|
+
npm i ngx-api-cache
|
|
17
17
|
```
|
|
18
18
|
OR
|
|
19
19
|
|
|
@@ -21,24 +21,76 @@ This library does not require third party dependencies
|
|
|
21
21
|
yarn add ngx-api-cache
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
2.
|
|
24
|
+
2. Provide HttpClient to your App:
|
|
25
25
|
|
|
26
26
|
```bash
|
|
27
|
-
|
|
27
|
+
import { provideHttpClient } from '@angular/common/http';
|
|
28
|
+
|
|
29
|
+
// In your app config:
|
|
30
|
+
providers: [
|
|
31
|
+
provideHttpClient(), // ← required
|
|
32
|
+
// ... other providers
|
|
33
|
+
]
|
|
28
34
|
```
|
|
29
35
|
|
|
30
|
-
3.
|
|
36
|
+
3. Inject NgxApiCacheService in any standalone component or service:
|
|
31
37
|
|
|
32
38
|
```bash
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
|
|
40
|
+
const apiCache = inject(NgxApiCacheService);
|
|
41
|
+
const result: CacheResult<User> = apiCache.get('/api/user');
|
|
42
|
+
if you want, you can also add destroyRef: apiCache.get('/api/user', destroyRef);
|
|
43
|
+
|
|
38
44
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
The returned result object gives you:
|
|
46
|
+
data: a readonly signal with the response (or null)
|
|
47
|
+
loading: a readonly signal indicating if a request is in progress
|
|
48
|
+
error: a readonly signal with the error (or null)
|
|
49
|
+
refresh(): re-fetches data from the API (bypassing cache)
|
|
50
|
+
patch(updater): lets you update the cached value directly (e.g., for optimistic UI)
|
|
43
51
|
```
|
|
44
|
-
|
|
52
|
+
4. Example for template:
|
|
53
|
+
|
|
54
|
+
```html
|
|
55
|
+
@if (result.loading(); as loading) {
|
|
56
|
+
<p>Loading... {{ loading }}</p>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@if (result.error(); as error) {
|
|
60
|
+
<div class="error">
|
|
61
|
+
<p>Failed to load data.</p>
|
|
62
|
+
<button (click)="result.refresh()">Retry</button>
|
|
63
|
+
</div>
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@if (result.data(); as data) {
|
|
67
|
+
<div class="content">
|
|
68
|
+
<h2>{{ data.name }}</h2>
|
|
69
|
+
<p>{{ data.position }}</p>
|
|
70
|
+
<button (click)="result.refresh()">Refresh</button>
|
|
71
|
+
</div>
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
5. Example for patch method:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
// 1) Edit. After user edits their name in a form:
|
|
79
|
+
userResult.patch(user => ({
|
|
80
|
+
...user,
|
|
81
|
+
name: 'Alex Updated'
|
|
82
|
+
}));
|
|
83
|
+
|
|
84
|
+
// 2) Delete. In your component:
|
|
85
|
+
deleteTodo(id: number) {
|
|
86
|
+
todosResult.patch(todos => todos.filter(todo => todo.id !== id));
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## 💸 Support project
|
|
91
|
+
|
|
92
|
+
[](https://andrew-dev283.github.io/andrew-dev.github.io/)
|
|
93
|
+
|
|
94
|
+
OR
|
|
95
|
+
|
|
96
|
+
https://boosty.to/hq_dev
|