@tanstack/angular-query-experimental 5.83.0 → 5.83.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/dist/create-base-query.d.ts +8 -0
- package/dist/create-base-query.mjs +74 -0
- package/dist/create-base-query.mjs.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.mjs +35 -0
- package/dist/index.mjs.map +1 -0
- package/dist/infinite-query-options.d.ts +44 -0
- package/dist/infinite-query-options.mjs +7 -0
- package/dist/infinite-query-options.mjs.map +1 -0
- package/dist/inject-infinite-query.d.ts +39 -0
- package/dist/inject-infinite-query.mjs +18 -0
- package/dist/inject-infinite-query.mjs.map +1 -0
- package/dist/inject-is-fetching.d.ts +21 -0
- package/dist/inject-is-fetching.mjs +31 -0
- package/dist/inject-is-fetching.mjs.map +1 -0
- package/dist/inject-is-mutating.d.ts +20 -0
- package/dist/inject-is-mutating.mjs +31 -0
- package/dist/inject-is-mutating.mjs.map +1 -0
- package/dist/inject-is-restoring.d.ts +24 -0
- package/dist/inject-is-restoring.mjs +24 -0
- package/dist/inject-is-restoring.mjs.map +1 -0
- package/dist/inject-mutation-state.d.ts +26 -0
- package/dist/inject-mutation-state.mjs +51 -0
- package/dist/inject-mutation-state.mjs.map +1 -0
- package/dist/inject-mutation.d.ts +22 -0
- package/dist/inject-mutation.mjs +79 -0
- package/dist/inject-mutation.mjs.map +1 -0
- package/dist/inject-queries.d.ts +76 -0
- package/dist/inject-queries.mjs +49 -0
- package/dist/inject-queries.mjs.map +1 -0
- package/dist/inject-query-client.d.ts +19 -0
- package/dist/inject-query-client.mjs +9 -0
- package/dist/inject-query-client.mjs.map +1 -0
- package/dist/inject-query.d.ts +126 -0
- package/dist/inject-query.mjs +14 -0
- package/dist/inject-query.mjs.map +1 -0
- package/dist/mutation-options.d.ts +38 -0
- package/dist/mutation-options.mjs +7 -0
- package/dist/mutation-options.mjs.map +1 -0
- package/dist/providers.d.ts +215 -0
- package/dist/providers.mjs +109 -0
- package/dist/providers.mjs.map +1 -0
- package/dist/query-options.d.ts +87 -0
- package/dist/query-options.mjs +7 -0
- package/dist/query-options.mjs.map +1 -0
- package/dist/signal-proxy.d.ts +11 -0
- package/dist/signal-proxy.mjs +29 -0
- package/dist/signal-proxy.mjs.map +1 -0
- package/dist/types.d.ts +89 -0
- package/dist/util/is-dev-mode/is-dev-mode.d.ts +1 -0
- package/package.json +9 -10
- package/build/index.d.ts +0 -821
- package/build/index.mjs +0 -622
- package/build/index.mjs.map +0 -1
- package/src/test-setup.ts +0 -7
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { assertInInjectionContext, inject, Injector, DestroyRef, NgZone, computed, signal, effect, untracked } from "@angular/core";
|
|
2
|
+
import { QueryClient, MutationObserver, noop, notifyManager, shouldThrowError } from "@tanstack/query-core";
|
|
3
|
+
import { signalProxy } from "./signal-proxy.mjs";
|
|
4
|
+
function injectMutation(injectMutationFn, options) {
|
|
5
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectMutation);
|
|
6
|
+
const injector = (options == null ? void 0 : options.injector) ?? inject(Injector);
|
|
7
|
+
const destroyRef = injector.get(DestroyRef);
|
|
8
|
+
const ngZone = injector.get(NgZone);
|
|
9
|
+
const queryClient = injector.get(QueryClient);
|
|
10
|
+
const optionsSignal = computed(injectMutationFn);
|
|
11
|
+
const observerSignal = (() => {
|
|
12
|
+
let instance = null;
|
|
13
|
+
return computed(() => {
|
|
14
|
+
return instance || (instance = new MutationObserver(queryClient, optionsSignal()));
|
|
15
|
+
});
|
|
16
|
+
})();
|
|
17
|
+
const mutateFnSignal = computed(() => {
|
|
18
|
+
const observer = observerSignal();
|
|
19
|
+
return (variables, mutateOptions) => {
|
|
20
|
+
observer.mutate(variables, mutateOptions).catch(noop);
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
const resultFromInitialOptionsSignal = computed(() => {
|
|
24
|
+
const observer = observerSignal();
|
|
25
|
+
return observer.getCurrentResult();
|
|
26
|
+
});
|
|
27
|
+
const resultFromSubscriberSignal = signal(null);
|
|
28
|
+
effect(
|
|
29
|
+
() => {
|
|
30
|
+
const observer = observerSignal();
|
|
31
|
+
const observerOptions = optionsSignal();
|
|
32
|
+
untracked(() => {
|
|
33
|
+
observer.setOptions(observerOptions);
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
injector
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
effect(
|
|
41
|
+
() => {
|
|
42
|
+
const observer = observerSignal();
|
|
43
|
+
untracked(() => {
|
|
44
|
+
const unsubscribe = ngZone.runOutsideAngular(
|
|
45
|
+
() => observer.subscribe(
|
|
46
|
+
notifyManager.batchCalls((state) => {
|
|
47
|
+
ngZone.run(() => {
|
|
48
|
+
if (state.isError && shouldThrowError(observer.options.throwOnError, [state.error])) {
|
|
49
|
+
ngZone.onError.emit(state.error);
|
|
50
|
+
throw state.error;
|
|
51
|
+
}
|
|
52
|
+
resultFromSubscriberSignal.set(state);
|
|
53
|
+
});
|
|
54
|
+
})
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
destroyRef.onDestroy(unsubscribe);
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
injector
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
const resultSignal = computed(() => {
|
|
65
|
+
const resultFromSubscriber = resultFromSubscriberSignal();
|
|
66
|
+
const resultFromInitialOptions = resultFromInitialOptionsSignal();
|
|
67
|
+
const result = resultFromSubscriber ?? resultFromInitialOptions;
|
|
68
|
+
return {
|
|
69
|
+
...result,
|
|
70
|
+
mutate: mutateFnSignal(),
|
|
71
|
+
mutateAsync: result.mutate
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
return signalProxy(resultSignal);
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
injectMutation
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=inject-mutation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-mutation.mjs","sources":["../src/inject-mutation.ts"],"sourcesContent":["import {\n DestroyRef,\n Injector,\n NgZone,\n assertInInjectionContext,\n computed,\n effect,\n inject,\n signal,\n untracked,\n} from '@angular/core'\nimport {\n MutationObserver,\n QueryClient,\n noop,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport { signalProxy } from './signal-proxy'\nimport type { DefaultError, MutationObserverResult } from '@tanstack/query-core'\nimport type { CreateMutateFunction, CreateMutationResult } from './types'\nimport type { CreateMutationOptions } from './mutation-options'\n\nexport interface InjectMutationOptions {\n /**\n * The `Injector` in which to create the mutation.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\n\n/**\n * Injects a mutation: an imperative function that can be invoked which typically performs server side effects.\n *\n * Unlike queries, mutations are not run automatically.\n * @param injectMutationFn - A function that returns mutation options.\n * @param options - Additional configuration\n * @returns The mutation.\n * @public\n */\nexport function injectMutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n>(\n injectMutationFn: () => CreateMutationOptions<\n TData,\n TError,\n TVariables,\n TContext\n >,\n options?: InjectMutationOptions,\n): CreateMutationResult<TData, TError, TVariables, TContext> {\n !options?.injector && assertInInjectionContext(injectMutation)\n const injector = options?.injector ?? inject(Injector)\n const destroyRef = injector.get(DestroyRef)\n const ngZone = injector.get(NgZone)\n const queryClient = injector.get(QueryClient)\n\n /**\n * computed() is used so signals can be inserted into the options\n * making it reactive. Wrapping options in a function ensures embedded expressions\n * are preserved and can keep being applied after signal changes\n */\n const optionsSignal = computed(injectMutationFn)\n\n const observerSignal = (() => {\n let instance: MutationObserver<TData, TError, TVariables, TContext> | null =\n null\n\n return computed(() => {\n return (instance ||= new MutationObserver(queryClient, optionsSignal()))\n })\n })()\n\n const mutateFnSignal = computed<\n CreateMutateFunction<TData, TError, TVariables, TContext>\n >(() => {\n const observer = observerSignal()\n return (variables, mutateOptions) => {\n observer.mutate(variables, mutateOptions).catch(noop)\n }\n })\n\n /**\n * Computed signal that gets result from mutation cache based on passed options\n */\n const resultFromInitialOptionsSignal = computed(() => {\n const observer = observerSignal()\n return observer.getCurrentResult()\n })\n\n /**\n * Signal that contains result set by subscriber\n */\n const resultFromSubscriberSignal = signal<MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > | null>(null)\n\n effect(\n () => {\n const observer = observerSignal()\n const observerOptions = optionsSignal()\n\n untracked(() => {\n observer.setOptions(observerOptions)\n })\n },\n {\n injector,\n },\n )\n\n effect(\n () => {\n // observer.trackResult is not used as this optimization is not needed for Angular\n const observer = observerSignal()\n\n untracked(() => {\n const unsubscribe = ngZone.runOutsideAngular(() =>\n observer.subscribe(\n notifyManager.batchCalls((state) => {\n ngZone.run(() => {\n if (\n state.isError &&\n shouldThrowError(observer.options.throwOnError, [state.error])\n ) {\n ngZone.onError.emit(state.error)\n throw state.error\n }\n\n resultFromSubscriberSignal.set(state)\n })\n }),\n ),\n )\n destroyRef.onDestroy(unsubscribe)\n })\n },\n {\n injector,\n },\n )\n\n const resultSignal = computed(() => {\n const resultFromSubscriber = resultFromSubscriberSignal()\n const resultFromInitialOptions = resultFromInitialOptionsSignal()\n\n const result = resultFromSubscriber ?? resultFromInitialOptions\n\n return {\n ...result,\n mutate: mutateFnSignal(),\n mutateAsync: result.mutate,\n }\n })\n\n return signalProxy(resultSignal) as CreateMutationResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n}\n"],"names":[],"mappings":";;;AAyCgB,SAAA,eAMd,kBAMA,SAC2D;AAC1D,IAAA,mCAAS,aAAY,yBAAyB,cAAc;AAC7D,QAAM,YAAW,mCAAS,aAAY,OAAO,QAAQ;AAC/C,QAAA,aAAa,SAAS,IAAI,UAAU;AACpC,QAAA,SAAS,SAAS,IAAI,MAAM;AAC5B,QAAA,cAAc,SAAS,IAAI,WAAW;AAOtC,QAAA,gBAAgB,SAAS,gBAAgB;AAE/C,QAAM,kBAAkB,MAAM;AAC5B,QAAI,WACF;AAEF,WAAO,SAAS,MAAM;AACpB,aAAQ,wBAAa,IAAI,iBAAiB,aAAa,eAAe;AAAA,IAAA,CACvE;AAAA,EAAA,GACA;AAEG,QAAA,iBAAiB,SAErB,MAAM;AACN,UAAM,WAAW,eAAe;AACzB,WAAA,CAAC,WAAW,kBAAkB;AACnC,eAAS,OAAO,WAAW,aAAa,EAAE,MAAM,IAAI;AAAA,IACtD;AAAA,EAAA,CACD;AAKK,QAAA,iCAAiC,SAAS,MAAM;AACpD,UAAM,WAAW,eAAe;AAChC,WAAO,SAAS,iBAAiB;AAAA,EAAA,CAClC;AAKK,QAAA,6BAA6B,OAKzB,IAAI;AAEd;AAAA,IACE,MAAM;AACJ,YAAM,WAAW,eAAe;AAChC,YAAM,kBAAkB,cAAc;AAEtC,gBAAU,MAAM;AACd,iBAAS,WAAW,eAAe;AAAA,MAAA,CACpC;AAAA,IACH;AAAA,IACA;AAAA,MACE;AAAA,IAAA;AAAA,EAEJ;AAEA;AAAA,IACE,MAAM;AAEJ,YAAM,WAAW,eAAe;AAEhC,gBAAU,MAAM;AACd,cAAM,cAAc,OAAO;AAAA,UAAkB,MAC3C,SAAS;AAAA,YACP,cAAc,WAAW,CAAC,UAAU;AAClC,qBAAO,IAAI,MAAM;AAEb,oBAAA,MAAM,WACN,iBAAiB,SAAS,QAAQ,cAAc,CAAC,MAAM,KAAK,CAAC,GAC7D;AACO,yBAAA,QAAQ,KAAK,MAAM,KAAK;AAC/B,wBAAM,MAAM;AAAA,gBAAA;AAGd,2CAA2B,IAAI,KAAK;AAAA,cAAA,CACrC;AAAA,YACF,CAAA;AAAA,UAAA;AAAA,QAEL;AACA,mBAAW,UAAU,WAAW;AAAA,MAAA,CACjC;AAAA,IACH;AAAA,IACA;AAAA,MACE;AAAA,IAAA;AAAA,EAEJ;AAEM,QAAA,eAAe,SAAS,MAAM;AAClC,UAAM,uBAAuB,2BAA2B;AACxD,UAAM,2BAA2B,+BAA+B;AAEhE,UAAM,SAAS,wBAAwB;AAEhC,WAAA;AAAA,MACL,GAAG;AAAA,MACH,QAAQ,eAAe;AAAA,MACvB,aAAa,OAAO;AAAA,IACtB;AAAA,EAAA,CACD;AAED,SAAO,YAAY,YAAY;AAMjC;"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Injector, Signal } from '@angular/core';
|
|
2
|
+
import { DefaultError, OmitKeyof, QueriesPlaceholderDataFunction, QueryFunction, QueryKey, QueryObserverOptions, QueryObserverResult, ThrowOnError } from '@tanstack/query-core';
|
|
3
|
+
type QueryObserverOptionsForCreateQueries<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = OmitKeyof<QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>, 'placeholderData'> & {
|
|
4
|
+
placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>;
|
|
5
|
+
};
|
|
6
|
+
type MAXIMUM_DEPTH = 20;
|
|
7
|
+
type SkipTokenForUseQueries = symbol;
|
|
8
|
+
type GetOptions<T> = T extends {
|
|
9
|
+
queryFnData: infer TQueryFnData;
|
|
10
|
+
error?: infer TError;
|
|
11
|
+
data: infer TData;
|
|
12
|
+
} ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData> : T extends {
|
|
13
|
+
queryFnData: infer TQueryFnData;
|
|
14
|
+
error?: infer TError;
|
|
15
|
+
} ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError> : T extends {
|
|
16
|
+
data: infer TData;
|
|
17
|
+
error?: infer TError;
|
|
18
|
+
} ? QueryObserverOptionsForCreateQueries<unknown, TError, TData> : T extends [infer TQueryFnData, infer TError, infer TData] ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData> : T extends [infer TQueryFnData, infer TError] ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError> : T extends [infer TQueryFnData] ? QueryObserverOptionsForCreateQueries<TQueryFnData> : T extends {
|
|
19
|
+
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> | SkipTokenForUseQueries;
|
|
20
|
+
select: (data: any) => infer TData;
|
|
21
|
+
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
22
|
+
} ? QueryObserverOptionsForCreateQueries<TQueryFnData, unknown extends TError ? DefaultError : TError, unknown extends TData ? TQueryFnData : TData, TQueryKey> : QueryObserverOptionsForCreateQueries;
|
|
23
|
+
type GetResults<T> = T extends {
|
|
24
|
+
queryFnData: any;
|
|
25
|
+
error?: infer TError;
|
|
26
|
+
data: infer TData;
|
|
27
|
+
} ? QueryObserverResult<TData, TError> : T extends {
|
|
28
|
+
queryFnData: infer TQueryFnData;
|
|
29
|
+
error?: infer TError;
|
|
30
|
+
} ? QueryObserverResult<TQueryFnData, TError> : T extends {
|
|
31
|
+
data: infer TData;
|
|
32
|
+
error?: infer TError;
|
|
33
|
+
} ? QueryObserverResult<TData, TError> : T extends [any, infer TError, infer TData] ? QueryObserverResult<TData, TError> : T extends [infer TQueryFnData, infer TError] ? QueryObserverResult<TQueryFnData, TError> : T extends [infer TQueryFnData] ? QueryObserverResult<TQueryFnData> : T extends {
|
|
34
|
+
queryFn?: QueryFunction<infer TQueryFnData, any> | SkipTokenForUseQueries;
|
|
35
|
+
select: (data: any) => infer TData;
|
|
36
|
+
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
37
|
+
} ? QueryObserverResult<unknown extends TData ? TQueryFnData : TData, unknown extends TError ? DefaultError : TError> : QueryObserverResult;
|
|
38
|
+
/**
|
|
39
|
+
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
export type QueriesOptions<T extends Array<any>, TResult extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<QueryObserverOptionsForCreateQueries> : T extends [] ? [] : T extends [infer Head] ? [...TResult, GetOptions<Head>] : T extends [infer Head, ...infer Tail] ? QueriesOptions<[
|
|
43
|
+
...Tail
|
|
44
|
+
], [
|
|
45
|
+
...TResult,
|
|
46
|
+
GetOptions<Head>
|
|
47
|
+
], [
|
|
48
|
+
...TDepth,
|
|
49
|
+
1
|
|
50
|
+
]> : ReadonlyArray<unknown> extends T ? T : T extends Array<QueryObserverOptionsForCreateQueries<infer TQueryFnData, infer TError, infer TData, infer TQueryKey>> ? Array<QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData, TQueryKey>> : Array<QueryObserverOptionsForCreateQueries>;
|
|
51
|
+
/**
|
|
52
|
+
* QueriesResults reducer recursively maps type param to results
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export type QueriesResults<T extends Array<any>, TResult extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<QueryObserverResult> : T extends [] ? [] : T extends [infer Head] ? [...TResult, GetResults<Head>] : T extends [infer Head, ...infer Tail] ? QueriesResults<[
|
|
56
|
+
...Tail
|
|
57
|
+
], [
|
|
58
|
+
...TResult,
|
|
59
|
+
GetResults<Head>
|
|
60
|
+
], [
|
|
61
|
+
...TDepth,
|
|
62
|
+
1
|
|
63
|
+
]> : T extends Array<QueryObserverOptionsForCreateQueries<infer TQueryFnData, infer TError, infer TData, any>> ? Array<QueryObserverResult<unknown extends TData ? TQueryFnData : TData, unknown extends TError ? DefaultError : TError>> : Array<QueryObserverResult>;
|
|
64
|
+
/**
|
|
65
|
+
* @param root0
|
|
66
|
+
* @param root0.queries
|
|
67
|
+
* @param root0.combine
|
|
68
|
+
* @param injector
|
|
69
|
+
* @param injector
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
export declare function injectQueries<T extends Array<any>, TCombinedResult = QueriesResults<T>>({ queries, ...options }: {
|
|
73
|
+
queries: Signal<[...QueriesOptions<T>]>;
|
|
74
|
+
combine?: (result: QueriesResults<T>) => TCombinedResult;
|
|
75
|
+
}, injector?: Injector): Signal<TCombinedResult>;
|
|
76
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { QueryClient, QueriesObserver, notifyManager } from "@tanstack/query-core";
|
|
2
|
+
import { assertInInjectionContext, runInInjectionContext, inject, Injector, DestroyRef, NgZone, computed, effect, signal } from "@angular/core";
|
|
3
|
+
import { injectIsRestoring } from "./inject-is-restoring.mjs";
|
|
4
|
+
function injectQueries({
|
|
5
|
+
queries,
|
|
6
|
+
...options
|
|
7
|
+
}, injector) {
|
|
8
|
+
!injector && assertInInjectionContext(injectQueries);
|
|
9
|
+
return runInInjectionContext(injector ?? inject(Injector), () => {
|
|
10
|
+
const destroyRef = inject(DestroyRef);
|
|
11
|
+
const ngZone = inject(NgZone);
|
|
12
|
+
const queryClient = inject(QueryClient);
|
|
13
|
+
const isRestoring = injectIsRestoring();
|
|
14
|
+
const defaultedQueries = computed(() => {
|
|
15
|
+
return queries().map((opts) => {
|
|
16
|
+
const defaultedOptions = queryClient.defaultQueryOptions(opts);
|
|
17
|
+
defaultedOptions._optimisticResults = isRestoring() ? "isRestoring" : "optimistic";
|
|
18
|
+
return defaultedOptions;
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
const observer = new QueriesObserver(
|
|
22
|
+
queryClient,
|
|
23
|
+
defaultedQueries(),
|
|
24
|
+
options
|
|
25
|
+
);
|
|
26
|
+
effect(() => {
|
|
27
|
+
observer.setQueries(
|
|
28
|
+
defaultedQueries(),
|
|
29
|
+
options
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
const [, getCombinedResult] = observer.getOptimisticResult(
|
|
33
|
+
defaultedQueries(),
|
|
34
|
+
options.combine
|
|
35
|
+
);
|
|
36
|
+
const result = signal(getCombinedResult());
|
|
37
|
+
effect(() => {
|
|
38
|
+
const unsubscribe = isRestoring() ? () => void 0 : ngZone.runOutsideAngular(
|
|
39
|
+
() => observer.subscribe(notifyManager.batchCalls(result.set))
|
|
40
|
+
);
|
|
41
|
+
destroyRef.onDestroy(unsubscribe);
|
|
42
|
+
});
|
|
43
|
+
return result;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
injectQueries
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=inject-queries.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-queries.mjs","sources":["../src/inject-queries.ts"],"sourcesContent":["import {\n QueriesObserver,\n QueryClient,\n notifyManager,\n} from '@tanstack/query-core'\nimport {\n DestroyRef,\n Injector,\n NgZone,\n assertInInjectionContext,\n computed,\n effect,\n inject,\n runInInjectionContext,\n signal,\n} from '@angular/core'\nimport { injectIsRestoring } from './inject-is-restoring'\nimport type { Signal } from '@angular/core'\nimport type {\n DefaultError,\n OmitKeyof,\n QueriesObserverOptions,\n QueriesPlaceholderDataFunction,\n QueryFunction,\n QueryKey,\n QueryObserverOptions,\n QueryObserverResult,\n ThrowOnError,\n} from '@tanstack/query-core'\n\n// This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function does not have a parameter\ntype QueryObserverOptionsForCreateQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = OmitKeyof<\n QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,\n 'placeholderData'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\n// Widen the type of the symbol to enable type inference even if skipToken is not immutable.\ntype SkipTokenForUseQueries = symbol\n\ntype GetOptions<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? QueryObserverOptionsForCreateQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? QueryObserverOptionsForCreateQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n select: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? QueryObserverOptionsForCreateQueries<\n TQueryFnData,\n unknown extends TError ? DefaultError : TError,\n unknown extends TData ? TQueryFnData : TData,\n TQueryKey\n >\n : // Fallback\n QueryObserverOptionsForCreateQueries\n\ntype GetResults<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? QueryObserverResult<TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? QueryObserverResult<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? QueryObserverResult<TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? QueryObserverResult<TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? QueryObserverResult<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? QueryObserverResult<TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n select: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? QueryObserverResult<\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : // Fallback\n QueryObserverResult\n\n/**\n * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n * @public\n */\nexport type QueriesOptions<\n T extends Array<any>,\n TResult extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<QueryObserverOptionsForCreateQueries>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResult, GetOptions<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesOptions<\n [...Tail],\n [...TResult, GetOptions<Head>],\n [...TDepth, 1]\n >\n : ReadonlyArray<unknown> extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends Array<\n QueryObserverOptionsForCreateQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >\n >\n ? Array<\n QueryObserverOptionsForCreateQueries<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >\n >\n : // Fallback\n Array<QueryObserverOptionsForCreateQueries>\n\n/**\n * QueriesResults reducer recursively maps type param to results\n * @public\n */\nexport type QueriesResults<\n T extends Array<any>,\n TResult extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<QueryObserverResult>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResult, GetResults<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesResults<\n [...Tail],\n [...TResult, GetResults<Head>],\n [...TDepth, 1]\n >\n : T extends Array<\n QueryObserverOptionsForCreateQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n any\n >\n >\n ? // Dynamic-size (homogenous) CreateQueryOptions array: map directly to array of results\n Array<\n QueryObserverResult<\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n >\n : // Fallback\n Array<QueryObserverResult>\n\n/**\n * @param root0\n * @param root0.queries\n * @param root0.combine\n * @param injector\n * @param injector\n * @public\n */\nexport function injectQueries<\n T extends Array<any>,\n TCombinedResult = QueriesResults<T>,\n>(\n {\n queries,\n ...options\n }: {\n queries: Signal<[...QueriesOptions<T>]>\n combine?: (result: QueriesResults<T>) => TCombinedResult\n },\n injector?: Injector,\n): Signal<TCombinedResult> {\n !injector && assertInInjectionContext(injectQueries)\n return runInInjectionContext(injector ?? inject(Injector), () => {\n const destroyRef = inject(DestroyRef)\n const ngZone = inject(NgZone)\n const queryClient = inject(QueryClient)\n const isRestoring = injectIsRestoring()\n\n const defaultedQueries = computed(() => {\n return queries().map((opts) => {\n const defaultedOptions = queryClient.defaultQueryOptions(opts)\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring()\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions as QueryObserverOptions\n })\n })\n\n const observer = new QueriesObserver<TCombinedResult>(\n queryClient,\n defaultedQueries(),\n options as QueriesObserverOptions<TCombinedResult>,\n )\n\n // Do not notify on updates because of changes in the options because\n // these changes should already be reflected in the optimistic result.\n effect(() => {\n observer.setQueries(\n defaultedQueries(),\n options as QueriesObserverOptions<TCombinedResult>,\n )\n })\n\n const [, getCombinedResult] = observer.getOptimisticResult(\n defaultedQueries(),\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n const result = signal(getCombinedResult() as any)\n\n effect(() => {\n const unsubscribe = isRestoring()\n ? () => undefined\n : ngZone.runOutsideAngular(() =>\n observer.subscribe(notifyManager.batchCalls(result.set)),\n )\n destroyRef.onDestroy(unsubscribe)\n })\n\n return result\n })\n}\n"],"names":[],"mappings":";;;AA6MO,SAAS,cAId;AAAA,EACE;AAAA,EACA,GAAG;AACL,GAIA,UACyB;AACxB,GAAA,YAAY,yBAAyB,aAAa;AACnD,SAAO,sBAAsB,YAAY,OAAO,QAAQ,GAAG,MAAM;AACzD,UAAA,aAAa,OAAO,UAAU;AAC9B,UAAA,SAAS,OAAO,MAAM;AACtB,UAAA,cAAc,OAAO,WAAW;AACtC,UAAM,cAAc,kBAAkB;AAEhC,UAAA,mBAAmB,SAAS,MAAM;AACtC,aAAO,QAAQ,EAAE,IAAI,CAAC,SAAS;AACvB,cAAA,mBAAmB,YAAY,oBAAoB,IAAI;AAE5C,yBAAA,qBAAqB,YAAY,IAC9C,gBACA;AAEG,eAAA;AAAA,MAAA,CACR;AAAA,IAAA,CACF;AAED,UAAM,WAAW,IAAI;AAAA,MACnB;AAAA,MACA,iBAAiB;AAAA,MACjB;AAAA,IACF;AAIA,WAAO,MAAM;AACF,eAAA;AAAA,QACP,iBAAiB;AAAA,QACjB;AAAA,MACF;AAAA,IAAA,CACD;AAED,UAAM,CAAG,EAAA,iBAAiB,IAAI,SAAS;AAAA,MACrC,iBAAiB;AAAA,MAChB,QAAoD;AAAA,IACvD;AAEM,UAAA,SAAS,OAAO,mBAA0B;AAEhD,WAAO,MAAM;AACX,YAAM,cAAc,YAAA,IAChB,MAAM,SACN,OAAO;AAAA,QAAkB,MACvB,SAAS,UAAU,cAAc,WAAW,OAAO,GAAG,CAAC;AAAA,MACzD;AACJ,iBAAW,UAAU,WAAW;AAAA,IAAA,CACjC;AAEM,WAAA;AAAA,EAAA,CACR;AACH;"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Injector, InjectOptions } from '@angular/core';
|
|
2
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
3
|
+
/**
|
|
4
|
+
* Injects a `QueryClient` instance and allows passing a custom injector.
|
|
5
|
+
* @param injectOptions - Type of the options argument to inject and optionally a custom injector.
|
|
6
|
+
* @returns The `QueryClient` instance.
|
|
7
|
+
* @public
|
|
8
|
+
* @deprecated Use `inject(QueryClient)` instead.
|
|
9
|
+
* If you need to get a `QueryClient` from a custom injector, use `injector.get(QueryClient)`.
|
|
10
|
+
*
|
|
11
|
+
*
|
|
12
|
+
* **Example**
|
|
13
|
+
* ```ts
|
|
14
|
+
* const queryClient = injectQueryClient();
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function injectQueryClient(injectOptions?: InjectOptions & {
|
|
18
|
+
injector?: Injector;
|
|
19
|
+
}): QueryClient;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { inject, Injector } from "@angular/core";
|
|
2
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
3
|
+
function injectQueryClient(injectOptions = {}) {
|
|
4
|
+
return (injectOptions.injector ?? inject(Injector)).get(QueryClient);
|
|
5
|
+
}
|
|
6
|
+
export {
|
|
7
|
+
injectQueryClient
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=inject-query-client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-query-client.mjs","sources":["../src/inject-query-client.ts"],"sourcesContent":["import { Injector, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport type { InjectOptions } from '@angular/core'\n\n/**\n * Injects a `QueryClient` instance and allows passing a custom injector.\n * @param injectOptions - Type of the options argument to inject and optionally a custom injector.\n * @returns The `QueryClient` instance.\n * @public\n * @deprecated Use `inject(QueryClient)` instead.\n * If you need to get a `QueryClient` from a custom injector, use `injector.get(QueryClient)`.\n *\n *\n * **Example**\n * ```ts\n * const queryClient = injectQueryClient();\n * ```\n */\nexport function injectQueryClient(\n injectOptions: InjectOptions & { injector?: Injector } = {},\n) {\n return (injectOptions.injector ?? inject(Injector)).get(QueryClient)\n}\n"],"names":[],"mappings":";;AAkBgB,SAAA,kBACd,gBAAyD,IACzD;AACA,UAAQ,cAAc,YAAY,OAAO,QAAQ,GAAG,IAAI,WAAW;AACrE;"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Injector } from '@angular/core';
|
|
2
|
+
import { DefaultError, QueryKey } from '@tanstack/query-core';
|
|
3
|
+
import { CreateQueryOptions, CreateQueryResult, DefinedCreateQueryResult } from './types.js';
|
|
4
|
+
import { DefinedInitialDataOptions, UndefinedInitialDataOptions } from './query-options.js';
|
|
5
|
+
export interface InjectQueryOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The `Injector` in which to create the query.
|
|
8
|
+
*
|
|
9
|
+
* If this is not provided, the current injection context will be used instead (via `inject`).
|
|
10
|
+
*/
|
|
11
|
+
injector?: Injector;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
15
|
+
*
|
|
16
|
+
* **Basic example**
|
|
17
|
+
* ```ts
|
|
18
|
+
* class ServiceOrComponent {
|
|
19
|
+
* query = injectQuery(() => ({
|
|
20
|
+
* queryKey: ['repoData'],
|
|
21
|
+
* queryFn: () =>
|
|
22
|
+
* this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
|
|
23
|
+
* }))
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
|
|
28
|
+
* In the example below, the query will be automatically enabled and executed when the filter signal changes
|
|
29
|
+
* to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
|
|
30
|
+
*
|
|
31
|
+
* **Reactive example**
|
|
32
|
+
* ```ts
|
|
33
|
+
* class ServiceOrComponent {
|
|
34
|
+
* filter = signal('')
|
|
35
|
+
*
|
|
36
|
+
* todosQuery = injectQuery(() => ({
|
|
37
|
+
* queryKey: ['todos', this.filter()],
|
|
38
|
+
* queryFn: () => fetchTodos(this.filter()),
|
|
39
|
+
* // Signals can be combined with expressions
|
|
40
|
+
* enabled: !!this.filter(),
|
|
41
|
+
* }))
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
* @param injectQueryFn - A function that returns query options.
|
|
45
|
+
* @param options - Additional configuration
|
|
46
|
+
* @returns The query result.
|
|
47
|
+
* @public
|
|
48
|
+
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
|
|
49
|
+
*/
|
|
50
|
+
export declare function injectQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(injectQueryFn: () => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, options?: InjectQueryOptions): DefinedCreateQueryResult<TData, TError>;
|
|
51
|
+
/**
|
|
52
|
+
* Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
53
|
+
*
|
|
54
|
+
* **Basic example**
|
|
55
|
+
* ```ts
|
|
56
|
+
* class ServiceOrComponent {
|
|
57
|
+
* query = injectQuery(() => ({
|
|
58
|
+
* queryKey: ['repoData'],
|
|
59
|
+
* queryFn: () =>
|
|
60
|
+
* this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
|
|
61
|
+
* }))
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
|
|
66
|
+
* In the example below, the query will be automatically enabled and executed when the filter signal changes
|
|
67
|
+
* to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
|
|
68
|
+
*
|
|
69
|
+
* **Reactive example**
|
|
70
|
+
* ```ts
|
|
71
|
+
* class ServiceOrComponent {
|
|
72
|
+
* filter = signal('')
|
|
73
|
+
*
|
|
74
|
+
* todosQuery = injectQuery(() => ({
|
|
75
|
+
* queryKey: ['todos', this.filter()],
|
|
76
|
+
* queryFn: () => fetchTodos(this.filter()),
|
|
77
|
+
* // Signals can be combined with expressions
|
|
78
|
+
* enabled: !!this.filter(),
|
|
79
|
+
* }))
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
* @param injectQueryFn - A function that returns query options.
|
|
83
|
+
* @param options - Additional configuration
|
|
84
|
+
* @returns The query result.
|
|
85
|
+
* @public
|
|
86
|
+
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
|
|
87
|
+
*/
|
|
88
|
+
export declare function injectQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(injectQueryFn: () => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, options?: InjectQueryOptions): CreateQueryResult<TData, TError>;
|
|
89
|
+
/**
|
|
90
|
+
* Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
91
|
+
*
|
|
92
|
+
* **Basic example**
|
|
93
|
+
* ```ts
|
|
94
|
+
* class ServiceOrComponent {
|
|
95
|
+
* query = injectQuery(() => ({
|
|
96
|
+
* queryKey: ['repoData'],
|
|
97
|
+
* queryFn: () =>
|
|
98
|
+
* this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
|
|
99
|
+
* }))
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
|
|
104
|
+
* In the example below, the query will be automatically enabled and executed when the filter signal changes
|
|
105
|
+
* to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
|
|
106
|
+
*
|
|
107
|
+
* **Reactive example**
|
|
108
|
+
* ```ts
|
|
109
|
+
* class ServiceOrComponent {
|
|
110
|
+
* filter = signal('')
|
|
111
|
+
*
|
|
112
|
+
* todosQuery = injectQuery(() => ({
|
|
113
|
+
* queryKey: ['todos', this.filter()],
|
|
114
|
+
* queryFn: () => fetchTodos(this.filter()),
|
|
115
|
+
* // Signals can be combined with expressions
|
|
116
|
+
* enabled: !!this.filter(),
|
|
117
|
+
* }))
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
* @param injectQueryFn - A function that returns query options.
|
|
121
|
+
* @param options - Additional configuration
|
|
122
|
+
* @returns The query result.
|
|
123
|
+
* @public
|
|
124
|
+
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
|
|
125
|
+
*/
|
|
126
|
+
export declare function injectQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(injectQueryFn: () => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>, options?: InjectQueryOptions): CreateQueryResult<TData, TError>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { QueryObserver } from "@tanstack/query-core";
|
|
2
|
+
import { assertInInjectionContext, runInInjectionContext, inject, Injector } from "@angular/core";
|
|
3
|
+
import { createBaseQuery } from "./create-base-query.mjs";
|
|
4
|
+
function injectQuery(injectQueryFn, options) {
|
|
5
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectQuery);
|
|
6
|
+
return runInInjectionContext(
|
|
7
|
+
(options == null ? void 0 : options.injector) ?? inject(Injector),
|
|
8
|
+
() => createBaseQuery(injectQueryFn, QueryObserver)
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
injectQuery
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=inject-query.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-query.mjs","sources":["../src/inject-query.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport {\n Injector,\n assertInInjectionContext,\n inject,\n runInInjectionContext,\n} from '@angular/core'\nimport { createBaseQuery } from './create-base-query'\nimport type { DefaultError, QueryKey } from '@tanstack/query-core'\nimport type {\n CreateQueryOptions,\n CreateQueryResult,\n DefinedCreateQueryResult,\n} from './types'\nimport type {\n DefinedInitialDataOptions,\n UndefinedInitialDataOptions,\n} from './query-options'\n\nexport interface InjectQueryOptions {\n /**\n * The `Injector` in which to create the query.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @public\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => DefinedInitialDataOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): DefinedCreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @public\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => UndefinedInitialDataOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): CreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @public\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => CreateQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): CreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @public\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery(\n injectQueryFn: () => CreateQueryOptions,\n options?: InjectQueryOptions,\n) {\n !options?.injector && assertInInjectionContext(injectQuery)\n return runInInjectionContext(options?.injector ?? inject(Injector), () =>\n createBaseQuery(injectQueryFn, QueryObserver),\n ) as unknown as CreateQueryResult\n}\n"],"names":[],"mappings":";;;AA6NgB,SAAA,YACd,eACA,SACA;AACC,IAAA,mCAAS,aAAY,yBAAyB,WAAW;AACnD,SAAA;AAAA,KAAsB,mCAAS,aAAY,OAAO,QAAQ;AAAA,IAAG,MAClE,gBAAgB,eAAe,aAAa;AAAA,EAC9C;AACF;"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { DefaultError, MutationObserverOptions, OmitKeyof } from '@tanstack/query-core';
|
|
2
|
+
/**
|
|
3
|
+
* Allows to share and re-use mutation options in a type-safe way.
|
|
4
|
+
*
|
|
5
|
+
* **Example**
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* export class QueriesService {
|
|
9
|
+
* private http = inject(HttpClient);
|
|
10
|
+
*
|
|
11
|
+
* updatePost(id: number) {
|
|
12
|
+
* return mutationOptions({
|
|
13
|
+
* mutationFn: (post: Post) => Promise.resolve(post),
|
|
14
|
+
* mutationKey: ["updatePost", id],
|
|
15
|
+
* onSuccess: (newPost) => {
|
|
16
|
+
* // ^? newPost: Post
|
|
17
|
+
* this.queryClient.setQueryData(["posts", id], newPost);
|
|
18
|
+
* },
|
|
19
|
+
* });
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* queries = inject(QueriesService)
|
|
24
|
+
* idSignal = new Signal(0);
|
|
25
|
+
* mutation = injectMutation(() => this.queries.updatePost(this.idSignal()))
|
|
26
|
+
*
|
|
27
|
+
* mutation.mutate({ title: 'New Title' })
|
|
28
|
+
* ```
|
|
29
|
+
* @param options - The mutation options.
|
|
30
|
+
* @returns Mutation options.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
export declare function mutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown>(options: MutationObserverOptions<TData, TError, TVariables, TContext>): CreateMutationOptions<TData, TError, TVariables, TContext>;
|
|
34
|
+
/**
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export interface CreateMutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown> extends OmitKeyof<MutationObserverOptions<TData, TError, TVariables, TContext>, '_defaulted'> {
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutation-options.mjs","sources":["../src/mutation-options.ts"],"sourcesContent":["import type {\n DefaultError,\n MutationObserverOptions,\n OmitKeyof,\n} from '@tanstack/query-core'\n\n/**\n * Allows to share and re-use mutation options in a type-safe way.\n *\n * **Example**\n *\n * ```ts\n * export class QueriesService {\n * private http = inject(HttpClient);\n *\n * updatePost(id: number) {\n * return mutationOptions({\n * mutationFn: (post: Post) => Promise.resolve(post),\n * mutationKey: [\"updatePost\", id],\n * onSuccess: (newPost) => {\n * // ^? newPost: Post\n * this.queryClient.setQueryData([\"posts\", id], newPost);\n * },\n * });\n * }\n * }\n *\n * queries = inject(QueriesService)\n * idSignal = new Signal(0);\n * mutation = injectMutation(() => this.queries.updatePost(this.idSignal()))\n *\n * mutation.mutate({ title: 'New Title' })\n * ```\n * @param options - The mutation options.\n * @returns Mutation options.\n * @public\n */\nexport function mutationOptions<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n>(\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n): CreateMutationOptions<TData, TError, TVariables, TContext> {\n return options\n}\n\n/**\n * @public\n */\nexport interface CreateMutationOptions<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends OmitKeyof<\n MutationObserverOptions<TData, TError, TVariables, TContext>,\n '_defaulted'\n > {}\n"],"names":[],"mappings":"AAqCO,SAAS,gBAMd,SAC4D;AACrD,SAAA;AACT;"}
|