@angular-architects/ngrx-toolkit 19.3.0 → 19.4.1
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/fesm2022/angular-architects-ngrx-toolkit.mjs +158 -30
- package/fesm2022/angular-architects-ngrx-toolkit.mjs.map +1 -1
- package/index.d.ts +3 -1
- package/lib/mutation/http-mutation.d.ts +86 -0
- package/lib/mutation/mutation.d.ts +20 -0
- package/lib/mutation/rx-mutation.d.ts +76 -0
- package/lib/with-mutations.d.ts +1 -16
- package/package.json +1 -1
- package/lib/rx-mutation.d.ts +0 -58
package/index.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export { withLocalStorage, withSessionStorage, } from './lib/storage-sync/featur
|
|
|
22
22
|
export { SyncConfig, withStorageSync, } from './lib/storage-sync/with-storage-sync';
|
|
23
23
|
export { emptyFeature, withConditional } from './lib/with-conditional';
|
|
24
24
|
export { withFeatureFactory } from './lib/with-feature-factory';
|
|
25
|
-
export * from './lib/rx-mutation';
|
|
25
|
+
export * from './lib/mutation/rx-mutation';
|
|
26
26
|
export * from './lib/with-mutations';
|
|
27
27
|
export { concatOp, exhaustOp, mergeOp, switchOp, } from './lib/flattening-operator';
|
|
28
|
+
export * from './lib/mutation/http-mutation';
|
|
29
|
+
export { rxMutation } from './lib/mutation/rx-mutation';
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { HttpContext, HttpHeaders, HttpParams, HttpProgressEvent } from '@angular/common/http';
|
|
2
|
+
import { Signal } from '@angular/core';
|
|
3
|
+
import { Mutation } from './mutation';
|
|
4
|
+
import { RxMutationOptions } from './rx-mutation';
|
|
5
|
+
export type HttpMutationRequest = {
|
|
6
|
+
url: string;
|
|
7
|
+
method: string;
|
|
8
|
+
body?: unknown;
|
|
9
|
+
headers?: HttpHeaders | Record<string, string | string[]>;
|
|
10
|
+
context?: HttpContext;
|
|
11
|
+
reportProgress?: boolean;
|
|
12
|
+
params?: HttpParams | Record<string, string | number | boolean | ReadonlyArray<string | number | boolean>>;
|
|
13
|
+
withCredentials?: boolean;
|
|
14
|
+
credentials?: RequestCredentials;
|
|
15
|
+
keepalive?: boolean;
|
|
16
|
+
priority?: RequestPriority;
|
|
17
|
+
cache?: RequestCache;
|
|
18
|
+
mode?: RequestMode;
|
|
19
|
+
redirect?: RequestRedirect;
|
|
20
|
+
transferCache?: {
|
|
21
|
+
includeHeaders?: string[];
|
|
22
|
+
} | boolean;
|
|
23
|
+
};
|
|
24
|
+
export type HttpMutationOptions<Parameter, Result> = Omit<RxMutationOptions<Parameter, NoInfer<Result>>, 'operation'> & {
|
|
25
|
+
request: (param: Parameter) => HttpMutationRequest;
|
|
26
|
+
parse?: (response: unknown) => Result;
|
|
27
|
+
};
|
|
28
|
+
export type HttpMutation<Parameter, Result> = Mutation<Parameter, Result> & {
|
|
29
|
+
uploadProgress: Signal<HttpProgressEvent | undefined>;
|
|
30
|
+
downloadProgress: Signal<HttpProgressEvent | undefined>;
|
|
31
|
+
headers: Signal<HttpHeaders | undefined>;
|
|
32
|
+
statusCode: Signal<string | undefined>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Creates an HTTP mutation.
|
|
36
|
+
*
|
|
37
|
+
* export type Params = {
|
|
38
|
+
* value: number;
|
|
39
|
+
* };
|
|
40
|
+
*
|
|
41
|
+
* export type CounterResponse = {
|
|
42
|
+
* // httpbin.org echos the request using the
|
|
43
|
+
* // json property
|
|
44
|
+
* json: { counter: number };
|
|
45
|
+
* };
|
|
46
|
+
*
|
|
47
|
+
* const simpleSaveUser = httpMutation({
|
|
48
|
+
* request: (userData: AddUserEntry) => ({
|
|
49
|
+
* url: 'api/users',
|
|
50
|
+
* body: userData,
|
|
51
|
+
* }),
|
|
52
|
+
* parse: Boolean,
|
|
53
|
+
* })
|
|
54
|
+
*
|
|
55
|
+
* const saveUser = httpMutation({
|
|
56
|
+
* request: (p: Params) => ({
|
|
57
|
+
* url: `https://httpbin.org/post`,
|
|
58
|
+
* method: 'POST',
|
|
59
|
+
* body: { counter: p.value },
|
|
60
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
61
|
+
* }),
|
|
62
|
+
* onSuccess: (response: CounterResponse) => {
|
|
63
|
+
* console.log('Counter sent to server:', response);
|
|
64
|
+
* },
|
|
65
|
+
* onError: (error) => {
|
|
66
|
+
* console.error('Failed to send counter:', error);
|
|
67
|
+
* },
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* ...
|
|
71
|
+
*
|
|
72
|
+
* const result = await this.saveUser({ value: 17 });
|
|
73
|
+
* if (result.status === 'success') {
|
|
74
|
+
* console.log('Successfully saved to server:', result.value);
|
|
75
|
+
* }
|
|
76
|
+
* else if (result.status === 'error') {
|
|
77
|
+
* console.log('Failed to save:', result.error);
|
|
78
|
+
* }
|
|
79
|
+
* else {
|
|
80
|
+
* console.log('Operation aborted');
|
|
81
|
+
* }
|
|
82
|
+
*
|
|
83
|
+
* @param options The options for the HTTP mutation.
|
|
84
|
+
* @returns The HTTP mutation.
|
|
85
|
+
*/
|
|
86
|
+
export declare function httpMutation<Parameter, Result>(optionsOrRequest: HttpMutationOptions<Parameter, Result> | ((param: Parameter) => HttpMutationRequest)): HttpMutation<Parameter, Result>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Signal } from '@angular/core';
|
|
2
|
+
export type MutationResult<Result> = {
|
|
3
|
+
status: 'success';
|
|
4
|
+
value: Result;
|
|
5
|
+
} | {
|
|
6
|
+
status: 'error';
|
|
7
|
+
error: unknown;
|
|
8
|
+
} | {
|
|
9
|
+
status: 'aborted';
|
|
10
|
+
};
|
|
11
|
+
export type MutationStatus = 'idle' | 'pending' | 'error' | 'success';
|
|
12
|
+
export type Mutation<Parameter, Result> = {
|
|
13
|
+
(params: Parameter): Promise<MutationResult<Result>>;
|
|
14
|
+
status: Signal<MutationStatus>;
|
|
15
|
+
value: Signal<Result | undefined>;
|
|
16
|
+
isPending: Signal<boolean>;
|
|
17
|
+
isSuccess: Signal<boolean>;
|
|
18
|
+
error: Signal<unknown>;
|
|
19
|
+
hasValue(): this is Mutation<Exclude<Parameter, undefined>, Result>;
|
|
20
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Injector } from '@angular/core';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { FlatteningOperator } from '../flattening-operator';
|
|
4
|
+
import { Mutation } from './mutation';
|
|
5
|
+
export type Operation<Parameter, Result> = (param: Parameter) => Result;
|
|
6
|
+
export interface RxMutationOptions<Parameter, Result> {
|
|
7
|
+
operation: Operation<Parameter, Observable<Result>>;
|
|
8
|
+
onSuccess?: (result: Result, param: Parameter) => void;
|
|
9
|
+
onError?: (error: unknown, param: Parameter) => void;
|
|
10
|
+
operator?: FlatteningOperator;
|
|
11
|
+
injector?: Injector;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Creates a mutation that leverages RxJS.
|
|
15
|
+
*
|
|
16
|
+
* For each mutation the following options can be defined:
|
|
17
|
+
* - `operation`: A function that defines the mutation logic. It returns an Observable.
|
|
18
|
+
* - `onSuccess`: A callback that is called when the mutation is successful.
|
|
19
|
+
* - `onError`: A callback that is called when the mutation fails.
|
|
20
|
+
* - `operator`: An optional wrapper of an RxJS flattening operator. By default `concat` sematics are used.
|
|
21
|
+
* - `injector`: An optional Angular injector to use for dependency injection.
|
|
22
|
+
*
|
|
23
|
+
* The `operation` is the only mandatory option.
|
|
24
|
+
*
|
|
25
|
+
* The returned mutation can be called as an async function and returns a Promise.
|
|
26
|
+
* This promise informs about whether the mutation was successful, failed, or aborted
|
|
27
|
+
* (due to switchMap or exhaustMap semantics).
|
|
28
|
+
*
|
|
29
|
+
* The mutation also provides several Signals such as error, status or isPending (see below).
|
|
30
|
+
*
|
|
31
|
+
* Example usage without Store:
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const counterSignal = signal(0);
|
|
35
|
+
*
|
|
36
|
+
* const increment = rxMutation({
|
|
37
|
+
* operation: (param: Param) => {
|
|
38
|
+
* return calcSum(this.counterSignal(), param.value);
|
|
39
|
+
* },
|
|
40
|
+
* operator: concatOp,
|
|
41
|
+
* onSuccess: (result) => {
|
|
42
|
+
* this.counterSignal.set(result);
|
|
43
|
+
* },
|
|
44
|
+
* onError: (error) => {
|
|
45
|
+
* console.error('Error occurred:', error);
|
|
46
|
+
* },
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* const error = increment.error;
|
|
50
|
+
* const isPending = increment.isPending;
|
|
51
|
+
* const status = increment.status;
|
|
52
|
+
* const value = increment.value;
|
|
53
|
+
* const hasValue = increment.hasValue;
|
|
54
|
+
*
|
|
55
|
+
* async function incrementCounter() {
|
|
56
|
+
* const result = await increment({ value: 1 });
|
|
57
|
+
* if (result.status === 'success') {
|
|
58
|
+
* console.log('Success:', result.value);
|
|
59
|
+
* }
|
|
60
|
+
* if (result.status === 'error') {
|
|
61
|
+
* console.log('Error:', result.error);
|
|
62
|
+
* }
|
|
63
|
+
* if (result.status === 'aborted') {
|
|
64
|
+
* console.log('Operation aborted');
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* function calcSum(a: number, b: number): Observable<number> {
|
|
69
|
+
* return of(result).pipe(delay(500));
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @param options
|
|
74
|
+
* @returns the actual mutation function along tracking data as properties/methods
|
|
75
|
+
*/
|
|
76
|
+
export declare function rxMutation<Parameter, Result>(optionsOrOperation: RxMutationOptions<Parameter, Result> | Operation<Parameter, Observable<Result>>): Mutation<Parameter, Result>;
|
package/lib/with-mutations.d.ts
CHANGED
|
@@ -1,22 +1,7 @@
|
|
|
1
1
|
import { Signal } from '@angular/core';
|
|
2
2
|
import { EmptyFeatureResult, SignalStoreFeature, SignalStoreFeatureResult, StateSignals, WritableStateSource } from '@ngrx/signals';
|
|
3
|
-
|
|
4
|
-
(params: P): Promise<MutationResult<R>>;
|
|
5
|
-
status: Signal<MutationStatus>;
|
|
6
|
-
isPending: Signal<boolean>;
|
|
7
|
-
error: Signal<unknown>;
|
|
8
|
-
};
|
|
3
|
+
import { Mutation, MutationStatus } from './mutation/mutation';
|
|
9
4
|
type MutationsDictionary = Record<string, Mutation<any, any>>;
|
|
10
|
-
export type MutationResult<T> = {
|
|
11
|
-
status: 'success';
|
|
12
|
-
value: T;
|
|
13
|
-
} | {
|
|
14
|
-
status: 'error';
|
|
15
|
-
error: unknown;
|
|
16
|
-
} | {
|
|
17
|
-
status: 'aborted';
|
|
18
|
-
};
|
|
19
|
-
export type MutationStatus = 'idle' | 'pending' | 'error' | 'success';
|
|
20
5
|
export type MethodsDictionary = Record<string, Function>;
|
|
21
6
|
type NamedMutationProps<T extends MutationsDictionary> = {
|
|
22
7
|
[Prop in keyof T as `${Prop & string}IsPending`]: Signal<boolean>;
|
package/package.json
CHANGED
package/lib/rx-mutation.d.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { Injector } from '@angular/core';
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
3
|
-
import { FlatteningOperator } from './flattening-operator';
|
|
4
|
-
import { Mutation } from './with-mutations';
|
|
5
|
-
export type Func<P, R> = (params: P) => R;
|
|
6
|
-
export interface RxMutationOptions<P, R> {
|
|
7
|
-
operation: Func<P, Observable<R>>;
|
|
8
|
-
onSuccess?: (result: R, params: P) => void;
|
|
9
|
-
onError?: (error: unknown, params: P) => void;
|
|
10
|
-
operator?: FlatteningOperator;
|
|
11
|
-
injector?: Injector;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Creates a mutation that leverages RxJS.
|
|
15
|
-
*
|
|
16
|
-
* For each mutation the following options can be defined:
|
|
17
|
-
* - `operation`: A function that defines the mutation logic. It returns an Observable.
|
|
18
|
-
* - `onSuccess`: A callback that is called when the mutation is successful.
|
|
19
|
-
* - `onError`: A callback that is called when the mutation fails.
|
|
20
|
-
* - `operator`: An optional wrapper of an RxJS flattening operator. By default `concat` sematics are used.
|
|
21
|
-
* - `injector`: An optional Angular injector to use for dependency injection.
|
|
22
|
-
*
|
|
23
|
-
* The `operation` is the only mandatory option.
|
|
24
|
-
*
|
|
25
|
-
* ```typescript
|
|
26
|
-
* export type Params = {
|
|
27
|
-
* value: number;
|
|
28
|
-
* };
|
|
29
|
-
*
|
|
30
|
-
* export const CounterStore = signalStore(
|
|
31
|
-
* { providedIn: 'root' },
|
|
32
|
-
* withState({ counter: 0 }),
|
|
33
|
-
* withMutations((store) => ({
|
|
34
|
-
* increment: rxMutation({
|
|
35
|
-
* operation: (params: Params) => {
|
|
36
|
-
* return calcSum(store.counter(), params.value);
|
|
37
|
-
* },
|
|
38
|
-
* operator: concatOp,
|
|
39
|
-
* onSuccess: (result) => {
|
|
40
|
-
* console.log('result', result);
|
|
41
|
-
* patchState(store, { counter: result });
|
|
42
|
-
* },
|
|
43
|
-
* onError: (error) => {
|
|
44
|
-
* console.error('Error occurred:', error);
|
|
45
|
-
* },
|
|
46
|
-
* }),
|
|
47
|
-
* })),
|
|
48
|
-
* );
|
|
49
|
-
*
|
|
50
|
-
* function calcSum(a: number, b: number): Observable<number> {
|
|
51
|
-
* return of(a + b);
|
|
52
|
-
* }
|
|
53
|
-
* ```
|
|
54
|
-
*
|
|
55
|
-
* @param options
|
|
56
|
-
* @returns
|
|
57
|
-
*/
|
|
58
|
-
export declare function rxMutation<P, R>(options: RxMutationOptions<P, R>): Mutation<P, R>;
|