mobx-tanstack-query-api 0.2.0 → 0.2.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/package.json
CHANGED
|
@@ -7,13 +7,15 @@ import { AnyEndpoint } from './endpoint.types.js';
|
|
|
7
7
|
export declare class EndpointQuery<TEndpoint extends AnyEndpoint, TQueryFnData = TEndpoint['__response']['data'], TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData> extends Query<TQueryFnData, TError, TData, TQueryData> {
|
|
8
8
|
private endpoint;
|
|
9
9
|
response: TEndpoint['__response'] | null;
|
|
10
|
-
params: TEndpoint['__params'] | null;
|
|
11
10
|
private uniqKey?;
|
|
11
|
+
private _observableData;
|
|
12
12
|
constructor(endpoint: AnyEndpoint, queryClient: EndpointQueryClient, queryOptionsInput: EndpointQueryOptions<TEndpoint, TQueryFnData, TError, TData, TQueryData> | (() => EndpointQueryFlattenOptions<TEndpoint, TQueryFnData, TError, TData, TQueryData>));
|
|
13
|
+
get params(): MaybeFalsy<TEndpoint["__params"]>;
|
|
13
14
|
update({ params, ...options }: Omit<QueryUpdateOptionsAllVariants<TQueryFnData, TError, TData, TQueryData>, ExcludedQueryKeys> & {
|
|
14
15
|
params?: MaybeFalsy<TEndpoint['__params']>;
|
|
15
16
|
}): void;
|
|
16
17
|
start(params: MaybeFalsy<TEndpoint['__params']>): Promise<QueryObserverResult<TData, TError>>;
|
|
18
|
+
destroy(): void;
|
|
17
19
|
}
|
|
18
20
|
export declare const getParamsFromContext: (ctx: QueryFunctionContext<any, any>) => AnyEndpoint["__params"];
|
|
19
21
|
export declare const buildOptionsFromParams: (endpoint: AnyEndpoint, params: MaybeFalsy<AnyObject>, uniqKey: Maybe<EndpointQueryUniqKey>) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-query.d.ts","sourceRoot":"","sources":["../../src/runtime/endpoint-query.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"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I,CACL,6BAA6B,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,CAAC,EACtE,iBAAiB,CAClB,GAAG;QACF,MAAM,CAAC,EAAE,UAAU,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;KAC5C;IAUK,KAAK,CACT,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,GACxC,OAAO,CAAC,mBAAmB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAS9C,OAAO,IAAI,IAAI;CAOhB;AAED,eAAO,MAAM,oBAAoB,GAAI,KAAK,oBAAoB,CAAC,GAAG,EAAE,GAAG,CAAC,KAChC,WAAW,CAAC,UAAU,CAC7D,CAAC;AAEF,eAAO,MAAM,sBAAsB,GACjC,UAAU,WAAW,EACrB,QAAQ,UAAU,CAAC,SAAS,CAAC,EAC7B,SAAS,KAAK,CAAC,oBAAoB,CAAC,KACnC;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,GAAG,EAAE,CAAA;CAarC,CAAC"}
|
|
@@ -1,61 +1,70 @@
|
|
|
1
|
-
import { makeObservable, observable, runInAction } from 'mobx';
|
|
1
|
+
import { action, comparer, computed, makeObservable, observable, reaction, runInAction, } from 'mobx';
|
|
2
2
|
import { Query } from 'mobx-tanstack-query';
|
|
3
|
+
import { callFunction } from 'yummies/common';
|
|
3
4
|
export class EndpointQuery extends Query {
|
|
4
5
|
endpoint;
|
|
5
6
|
response = null;
|
|
6
|
-
params = null;
|
|
7
7
|
uniqKey;
|
|
8
|
+
_observableData;
|
|
8
9
|
constructor(endpoint, queryClient, queryOptionsInput) {
|
|
9
10
|
const { uniqKey, transform: transformResponse, ...queryOptions } = typeof queryOptionsInput === 'function'
|
|
10
11
|
? queryOptionsInput()
|
|
11
12
|
: queryOptionsInput;
|
|
13
|
+
const _observableData = {
|
|
14
|
+
params: null,
|
|
15
|
+
dynamicOptions: undefined,
|
|
16
|
+
};
|
|
17
|
+
makeObservable(_observableData, {
|
|
18
|
+
params: observable.ref,
|
|
19
|
+
dynamicOptions: observable,
|
|
20
|
+
});
|
|
21
|
+
const disposeFn = reaction(() => {
|
|
22
|
+
if (typeof queryOptionsInput === 'function') {
|
|
23
|
+
const { params, abortSignal, select, onDone, onError, onInit, enableOnDemand, ...dynamicOptions } = queryOptionsInput();
|
|
24
|
+
return {
|
|
25
|
+
params,
|
|
26
|
+
dynamicOptions: Object.keys(dynamicOptions).length > 0
|
|
27
|
+
? dynamicOptions
|
|
28
|
+
: undefined,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return {
|
|
33
|
+
params: callFunction(queryOptionsInput.params),
|
|
34
|
+
dynamicOptions: undefined,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}, action(({ params, dynamicOptions }) => {
|
|
38
|
+
_observableData.params = params;
|
|
39
|
+
_observableData.dynamicOptions = dynamicOptions;
|
|
40
|
+
}), {
|
|
41
|
+
fireImmediately: true,
|
|
42
|
+
});
|
|
12
43
|
super({
|
|
13
44
|
...queryOptions,
|
|
14
45
|
queryClient,
|
|
15
46
|
meta: endpoint.toQueryMeta(queryOptions.meta),
|
|
16
47
|
options: (query) => {
|
|
17
|
-
const
|
|
18
|
-
let willEnableManually;
|
|
19
|
-
let params;
|
|
20
|
-
if (typeof queryOptionsInput === 'function') {
|
|
21
|
-
const { params: dynamicParams, ...dynamicOptions } = queryOptionsInput();
|
|
22
|
-
Object.assign(extraOptions, dynamicOptions);
|
|
23
|
-
params = dynamicParams;
|
|
24
|
-
willEnableManually = false;
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
const { params: dynamicParams, ...otherOptions } = queryOptionsInput;
|
|
28
|
-
willEnableManually = otherOptions.enabled === false;
|
|
29
|
-
params = dynamicParams == null ? {} : dynamicParams();
|
|
30
|
-
}
|
|
31
|
-
const builtOptions = buildOptionsFromParams(endpoint, params, uniqKey);
|
|
48
|
+
const builtOptions = buildOptionsFromParams(endpoint, _observableData.params, uniqKey);
|
|
32
49
|
// const dynamicOuterOptions = getDynamicOptions?.(query);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// isEnabled = dynamicOuterOptions.enabled;
|
|
37
|
-
// }
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
// const outerDynamicEnabled =
|
|
41
|
-
// dynamicOuterOptions?.enabled == null ||
|
|
42
|
-
// !!dynamicOuterOptions.enabled;
|
|
43
|
-
// isEnabled = builtOptions.enabled && outerDynamicEnabled;
|
|
44
|
-
isEnabled = builtOptions.enabled;
|
|
45
|
-
}
|
|
50
|
+
const isEnabled = typeof queryOptionsInput !== 'function' &&
|
|
51
|
+
queryOptionsInput.enabled !== false &&
|
|
52
|
+
builtOptions.enabled;
|
|
46
53
|
return {
|
|
47
54
|
...query.options,
|
|
48
55
|
...builtOptions,
|
|
49
56
|
// ...dynamicOuterOptions,
|
|
50
57
|
enabled: isEnabled,
|
|
51
|
-
...
|
|
58
|
+
..._observableData.dynamicOptions,
|
|
52
59
|
};
|
|
53
60
|
},
|
|
54
61
|
queryFn: async (ctx) => {
|
|
55
62
|
const params = getParamsFromContext(ctx);
|
|
56
63
|
runInAction(() => {
|
|
57
64
|
this.response = null;
|
|
58
|
-
|
|
65
|
+
if (!comparer.structural(params, _observableData.params)) {
|
|
66
|
+
_observableData.params = params;
|
|
67
|
+
}
|
|
59
68
|
});
|
|
60
69
|
let requestParams = params.request;
|
|
61
70
|
if (requestParams) {
|
|
@@ -80,18 +89,36 @@ export class EndpointQuery extends Query {
|
|
|
80
89
|
this.endpoint = endpoint;
|
|
81
90
|
this.uniqKey = uniqKey;
|
|
82
91
|
observable.ref(this, 'response');
|
|
83
|
-
|
|
92
|
+
computed.struct(this, 'params');
|
|
84
93
|
makeObservable(this);
|
|
94
|
+
this._observableData = _observableData;
|
|
95
|
+
this.abortController.signal.addEventListener('abort', disposeFn);
|
|
96
|
+
}
|
|
97
|
+
get params() {
|
|
98
|
+
return this._observableData.params;
|
|
85
99
|
}
|
|
86
100
|
update({ params, ...options }) {
|
|
101
|
+
runInAction(() => {
|
|
102
|
+
this._observableData.params = params;
|
|
103
|
+
});
|
|
87
104
|
return super.update({
|
|
88
105
|
...buildOptionsFromParams(this.endpoint, params, this.uniqKey),
|
|
89
106
|
...options,
|
|
90
107
|
});
|
|
91
108
|
}
|
|
92
109
|
async start(params) {
|
|
110
|
+
runInAction(() => {
|
|
111
|
+
this._observableData.params = params;
|
|
112
|
+
});
|
|
93
113
|
return await super.start(buildOptionsFromParams(this.endpoint, params, this.uniqKey));
|
|
94
114
|
}
|
|
115
|
+
destroy() {
|
|
116
|
+
super.destroy();
|
|
117
|
+
runInAction(() => {
|
|
118
|
+
this._observableData.params = undefined;
|
|
119
|
+
this._observableData.dynamicOptions = undefined;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
95
122
|
}
|
|
96
123
|
export const getParamsFromContext = (ctx) => {
|
|
97
124
|
return (ctx.queryKey.at(-2) || {});
|
|
@@ -103,9 +130,6 @@ export const buildOptionsFromParams = (endpoint, params, uniqKey) => {
|
|
|
103
130
|
hasRequiredParams =
|
|
104
131
|
!!params && requiredParams.every((param) => param in params);
|
|
105
132
|
}
|
|
106
|
-
else {
|
|
107
|
-
hasRequiredParams = !!params;
|
|
108
|
-
}
|
|
109
133
|
return {
|
|
110
134
|
enabled: hasRequiredParams,
|
|
111
135
|
queryKey: endpoint.toQueryKey(params || {}, uniqKey),
|