@mmstack/resource 19.0.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/LICENSE +21 -0
- package/README.md +111 -0
- package/fesm2022/mmstack-resource.mjs +1049 -0
- package/fesm2022/mmstack-resource.mjs.map +1 -0
- package/index.d.ts +1 -0
- package/lib/mutation-resource.d.ts +80 -0
- package/lib/public_api.d.ts +3 -0
- package/lib/query-resource.d.ts +92 -0
- package/lib/util/cache/cache.d.ts +177 -0
- package/lib/util/cache/cache.interceptor.d.ts +39 -0
- package/lib/util/cache/index.d.ts +2 -0
- package/lib/util/cache/public_api.d.ts +2 -0
- package/lib/util/circuit-breaker.d.ts +74 -0
- package/lib/util/dedupe.interceptor.d.ts +50 -0
- package/lib/util/equality.d.ts +3 -0
- package/lib/util/has-slow-connection.d.ts +1 -0
- package/lib/util/index.d.ts +9 -0
- package/lib/util/persist.d.ts +3 -0
- package/lib/util/public_api.d.ts +3 -0
- package/lib/util/refresh.d.ts +3 -0
- package/lib/util/retry-on-error.d.ts +6 -0
- package/lib/util/url-with-params.d.ts +2 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Miha Mulec
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# @mmstack/resource
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@mmstack/resource)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
`@mmstack/resource` is an Angular library that provides powerful, signal-based primitives for managing asynchronous data fetching and mutations. It builds upon Angular's `httpResource` and offers features like caching, retries, refresh intervals, circuit breakers, and request deduplication, all while maintaining a fine-grained reactive graph. It's inspired by libraries like TanStack Query, but aims for a more Angular-idiomatic and signal-centric approach.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Signal-Based:** Fully integrates with Angular's signal system for efficient change detection and reactivity.
|
|
11
|
+
- **Caching:** Built-in caching with configurable TTL (Time To Live) and stale-while-revalidate behavior. Supports custom cache key generation and respects HTTP caching headers.
|
|
12
|
+
- **Retries:** Automatic retries on failure with configurable backoff strategies.
|
|
13
|
+
- **Refresh Intervals:** Automatically refetch data at specified intervals.
|
|
14
|
+
- **Circuit Breaker:** Protects your application from cascading failures by temporarily disabling requests to failing endpoints.
|
|
15
|
+
- **Request Deduplication:** Avoids making multiple identical requests concurrently.
|
|
16
|
+
- **Mutations:** Provides a dedicated `mutationResource` for handling data modifications, with callbacks for `onMutate`, `onError`, `onSuccess`, and `onSettled`.
|
|
17
|
+
- **Prefetching:** Allows you to prefetch data into the cache, improving perceived performance.
|
|
18
|
+
- **Extensible:** Designed to be modular and extensible. You can easily add your own custom features or integrate with other libraries.
|
|
19
|
+
- **TypeScript Support:** A strong focus on typesafety
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
1. Install mmstack-resource
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @mmstack/primitives
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
2. Initialize the QueryCache & interceptors (optional)
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
33
|
+
import { ApplicationConfig } from '@angular/core';
|
|
34
|
+
import { createCacheInterceptor, createDedupeRequestsInterceptor, provideQueryCache } from '@mmstack/resource';
|
|
35
|
+
|
|
36
|
+
export const appConfig: ApplicationConfig = {
|
|
37
|
+
providers: [
|
|
38
|
+
// ..other providers
|
|
39
|
+
provideQueryCache(),
|
|
40
|
+
provideHttpClient(withInterceptors([createCacheInterceptor(), createDedupeRequestsInterceptor()])),
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
3. Use it :)
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { Injectable, isDevMode, untracked } from '@angular/core';
|
|
49
|
+
import { createCircuitBreaker, mutationResource, queryResource } from '@mmstack/resource';
|
|
50
|
+
|
|
51
|
+
type Post = {
|
|
52
|
+
userId: number;
|
|
53
|
+
id: number;
|
|
54
|
+
title: string;
|
|
55
|
+
body: string;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
@Injectable({
|
|
59
|
+
providedIn: 'root',
|
|
60
|
+
})
|
|
61
|
+
export class PostsService {
|
|
62
|
+
private readonly endpoint = 'https://jsonplaceholder.typicode.com/posts';
|
|
63
|
+
private readonly cb = createCircuitBreaker();
|
|
64
|
+
readonly posts = queryResource<Post[]>(
|
|
65
|
+
() => ({
|
|
66
|
+
url: this.endpoint,
|
|
67
|
+
}),
|
|
68
|
+
{
|
|
69
|
+
keepPrevious: true, // keep data between requests
|
|
70
|
+
refresh: 5 * 60 * 1000, // refresh every 5 minutes
|
|
71
|
+
circuitBreaker: this.cb, // use shared circuit breaker use true if not sharing
|
|
72
|
+
retry: 3, // retry 3 times on error using default backoff
|
|
73
|
+
onError: (err) => {
|
|
74
|
+
if (!isDevMode()) return;
|
|
75
|
+
console.error(err);
|
|
76
|
+
}, // log errors in dev mode
|
|
77
|
+
defaultValue: [],
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
private readonly createPostResource = mutationResource(
|
|
82
|
+
() => ({
|
|
83
|
+
url: this.endpoint,
|
|
84
|
+
method: 'POST',
|
|
85
|
+
}),
|
|
86
|
+
{
|
|
87
|
+
circuitBreaker: this.cb, // use shared circuit breaker use true if not sharing
|
|
88
|
+
onMutate: (post: Post) => {
|
|
89
|
+
const prev = untracked(this.posts.value);
|
|
90
|
+
this.posts.set([...prev, post]); // optimistically update
|
|
91
|
+
return prev;
|
|
92
|
+
},
|
|
93
|
+
onError: (err, prev) => {
|
|
94
|
+
if (isDevMode()) console.error(err);
|
|
95
|
+
this.posts.set(prev); // rollback on error
|
|
96
|
+
},
|
|
97
|
+
onSuccess: (next) => {
|
|
98
|
+
this.posts.update((posts) => posts.map((p) => (p.id === next.id ? next : p))); // replace with value from server
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
createPost(post: Post) {
|
|
104
|
+
this.createPostResource.mutate({ body: post }); // send the request
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## In-depth
|
|
110
|
+
|
|
111
|
+
For an in-depth explanation of the primitives & how they work check out this article: [Fun-grained Reactivity in Angular: Part 3 - Resources](https://dev.to/mihamulec/fun-grained-reactivity-in-angular-part-3-client-side-http-57g4)
|