mobx-tanstack-query-api 0.41.0 → 0.42.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/cli.cjs +36 -18
- package/cli.cjs.map +1 -1
- package/cli.d.ts +4 -3
- package/cli.js +36 -18
- package/cli.js.map +1 -1
- package/index.cjs +518 -161
- package/index.cjs.map +1 -1
- package/index.d.ts +195 -16
- package/index.js +520 -163
- package/index.js.map +1 -1
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -2,14 +2,12 @@ import { hashKey } from "@tanstack/query-core";
|
|
|
2
2
|
export * from "@tanstack/query-core";
|
|
3
3
|
import { callFunction } from "yummies/common";
|
|
4
4
|
import { makeObservable, observable, runInAction, comparer, computed, reaction, action } from "mobx";
|
|
5
|
-
import {
|
|
5
|
+
import { InfiniteQuery, Mutation, Query, QueryClient } from "mobx-tanstack-query";
|
|
6
6
|
import { getMobxAdministration, lazyObserve } from "yummies/mobx";
|
|
7
7
|
import { stringify } from "qs";
|
|
8
|
-
class
|
|
8
|
+
class EndpointInfiniteQuery extends InfiniteQuery {
|
|
9
9
|
/**
|
|
10
|
-
* Creates `
|
|
11
|
-
*
|
|
12
|
-
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#constructor)
|
|
10
|
+
* Creates `EndpointInfiniteQuery` instance.
|
|
13
11
|
*/
|
|
14
12
|
constructor(endpoint, inputQueryClient, queryOptionsInput) {
|
|
15
13
|
const isQueryOptionsInputFn = typeof queryOptionsInput === "function";
|
|
@@ -20,6 +18,7 @@ class EndpointQuery extends Query {
|
|
|
20
18
|
params,
|
|
21
19
|
onDone: onDoneInput,
|
|
22
20
|
queryClient: overridedQueryClient,
|
|
21
|
+
mergePageParam,
|
|
23
22
|
...queryOptions
|
|
24
23
|
} = unpackedQueryOptionsInput;
|
|
25
24
|
const queryClient = overridedQueryClient ?? inputQueryClient;
|
|
@@ -27,7 +26,9 @@ class EndpointQuery extends Query {
|
|
|
27
26
|
params: null,
|
|
28
27
|
dynamicOptions: void 0,
|
|
29
28
|
response: null,
|
|
30
|
-
uniqKey: unpackedQueryOptionsInput.uniqKey
|
|
29
|
+
uniqKey: unpackedQueryOptionsInput.uniqKey,
|
|
30
|
+
transform: transformResponse,
|
|
31
|
+
mergePageParam
|
|
31
32
|
};
|
|
32
33
|
if (!isQueryOptionsInputFn && typeof params !== "function") {
|
|
33
34
|
if ("params" in unpackedQueryOptionsInput) {
|
|
@@ -40,7 +41,9 @@ class EndpointQuery extends Query {
|
|
|
40
41
|
makeObservable(_observableData, {
|
|
41
42
|
params: observable.ref,
|
|
42
43
|
response: observable.ref,
|
|
43
|
-
dynamicOptions: observable
|
|
44
|
+
dynamicOptions: observable,
|
|
45
|
+
transform: observable.ref,
|
|
46
|
+
mergePageParam: observable.ref
|
|
44
47
|
});
|
|
45
48
|
const onDone = onDoneInput && ((...args) => {
|
|
46
49
|
onDoneInput(...args);
|
|
@@ -51,7 +54,7 @@ class EndpointQuery extends Query {
|
|
|
51
54
|
queryClient,
|
|
52
55
|
meta: endpoint.toQueryMeta(queryOptions.meta),
|
|
53
56
|
options: () => {
|
|
54
|
-
const builtOptions =
|
|
57
|
+
const builtOptions = buildInfiniteOptionsFromParams(
|
|
55
58
|
endpoint,
|
|
56
59
|
_observableData.params,
|
|
57
60
|
_observableData.uniqKey
|
|
@@ -74,7 +77,13 @@ class EndpointQuery extends Query {
|
|
|
74
77
|
_observableData.params = params2;
|
|
75
78
|
}
|
|
76
79
|
});
|
|
77
|
-
|
|
80
|
+
const mergedParams = mergeInfiniteQueryPageParam(
|
|
81
|
+
params2,
|
|
82
|
+
ctx.pageParam ?? queryOptions.initialPageParam,
|
|
83
|
+
ctx,
|
|
84
|
+
_observableData.mergePageParam
|
|
85
|
+
);
|
|
86
|
+
let requestParams = mergedParams.requestParams;
|
|
78
87
|
if (requestParams) {
|
|
79
88
|
if (!requestParams.signal) {
|
|
80
89
|
requestParams.signal = ctx.signal;
|
|
@@ -83,14 +92,14 @@ class EndpointQuery extends Query {
|
|
|
83
92
|
requestParams = { signal: ctx.signal };
|
|
84
93
|
}
|
|
85
94
|
const fixedInput = {
|
|
86
|
-
...
|
|
95
|
+
...mergedParams,
|
|
87
96
|
requestParams
|
|
88
97
|
};
|
|
89
98
|
const response = await endpoint.request(fixedInput);
|
|
90
99
|
runInAction(() => {
|
|
91
100
|
_observableData.response = response;
|
|
92
101
|
});
|
|
93
|
-
return await
|
|
102
|
+
return await _observableData.transform?.(response) ?? response.data;
|
|
94
103
|
}
|
|
95
104
|
});
|
|
96
105
|
this.endpoint = endpoint;
|
|
@@ -103,6 +112,8 @@ class EndpointQuery extends Query {
|
|
|
103
112
|
() => {
|
|
104
113
|
let outDynamicOptions;
|
|
105
114
|
let outParams;
|
|
115
|
+
let outTransform;
|
|
116
|
+
let outMergePageParam;
|
|
106
117
|
let uniqKey2;
|
|
107
118
|
if (isQueryOptionsInputFn) {
|
|
108
119
|
const result = queryOptionsInput();
|
|
@@ -115,9 +126,14 @@ class EndpointQuery extends Query {
|
|
|
115
126
|
onInit,
|
|
116
127
|
enableOnDemand,
|
|
117
128
|
uniqKey: _uniqKey,
|
|
129
|
+
transform,
|
|
130
|
+
mergePageParam: mergePageParam2,
|
|
131
|
+
queryClient: queryClient2,
|
|
118
132
|
...dynamicOptions
|
|
119
133
|
} = result;
|
|
120
134
|
uniqKey2 = _uniqKey;
|
|
135
|
+
outTransform = transform;
|
|
136
|
+
outMergePageParam = mergePageParam2;
|
|
121
137
|
if ("params" in result) {
|
|
122
138
|
outParams = result.params;
|
|
123
139
|
} else {
|
|
@@ -127,22 +143,30 @@ class EndpointQuery extends Query {
|
|
|
127
143
|
} else if ("params" in unpackedQueryOptionsInput) {
|
|
128
144
|
outParams = unpackedQueryOptionsInput.params;
|
|
129
145
|
uniqKey2 = unpackedQueryOptionsInput.uniqKey;
|
|
146
|
+
outTransform = unpackedQueryOptionsInput.transform;
|
|
147
|
+
outMergePageParam = unpackedQueryOptionsInput.mergePageParam;
|
|
130
148
|
} else {
|
|
131
149
|
outParams = {};
|
|
132
150
|
uniqKey2 = unpackedQueryOptionsInput.uniqKey;
|
|
151
|
+
outTransform = unpackedQueryOptionsInput.transform;
|
|
152
|
+
outMergePageParam = unpackedQueryOptionsInput.mergePageParam;
|
|
133
153
|
}
|
|
134
154
|
return {
|
|
135
155
|
params: callFunction(outParams),
|
|
136
156
|
dynamicOptions: outDynamicOptions,
|
|
137
|
-
uniqKey: uniqKey2
|
|
157
|
+
uniqKey: uniqKey2,
|
|
158
|
+
transform: outTransform,
|
|
159
|
+
mergePageParam: outMergePageParam
|
|
138
160
|
};
|
|
139
161
|
},
|
|
140
|
-
({ params: params2, dynamicOptions, uniqKey: uniqKey2 }) => {
|
|
162
|
+
({ params: params2, dynamicOptions, uniqKey: uniqKey2, transform, mergePageParam: mergePageParam2 }) => {
|
|
141
163
|
runInAction(() => {
|
|
142
164
|
_observableData.initialized = true;
|
|
143
165
|
_observableData.params = params2;
|
|
144
166
|
_observableData.dynamicOptions = dynamicOptions;
|
|
145
167
|
_observableData.uniqKey = uniqKey2;
|
|
168
|
+
_observableData.transform = transform;
|
|
169
|
+
_observableData.mergePageParam = mergePageParam2;
|
|
146
170
|
});
|
|
147
171
|
},
|
|
148
172
|
{
|
|
@@ -166,75 +190,59 @@ class EndpointQuery extends Query {
|
|
|
166
190
|
}
|
|
167
191
|
_observableData;
|
|
168
192
|
/**
|
|
169
|
-
* Current endpoint params used
|
|
170
|
-
*
|
|
171
|
-
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#params)
|
|
193
|
+
* Current base endpoint params used for `queryKey`.
|
|
172
194
|
*/
|
|
173
195
|
get params() {
|
|
174
196
|
return this._observableData.params;
|
|
175
197
|
}
|
|
176
198
|
/**
|
|
177
199
|
* Last raw HTTP response returned by endpoint.
|
|
178
|
-
*
|
|
179
|
-
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#response)
|
|
180
200
|
*/
|
|
181
201
|
get response() {
|
|
182
202
|
return this._observableData.response;
|
|
183
203
|
}
|
|
184
204
|
/**
|
|
185
|
-
* Updates query options and optionally params.
|
|
186
|
-
*
|
|
187
|
-
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#update)
|
|
205
|
+
* Updates query options and optionally base params.
|
|
188
206
|
*/
|
|
189
207
|
update(updateParams) {
|
|
190
208
|
if ("params" in updateParams) {
|
|
191
209
|
const { params, ...options } = updateParams;
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
210
|
+
if (this._observableData) {
|
|
211
|
+
runInAction(() => {
|
|
212
|
+
this._observableData.params = params;
|
|
213
|
+
});
|
|
214
|
+
return super.update({
|
|
215
|
+
...buildInfiniteOptionsFromParams(
|
|
216
|
+
this.endpoint,
|
|
217
|
+
params,
|
|
218
|
+
this._observableData.uniqKey
|
|
219
|
+
),
|
|
220
|
+
...options
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return super.update(options);
|
|
224
|
+
}
|
|
225
|
+
if (this._observableData) {
|
|
204
226
|
return super.update({
|
|
205
|
-
...
|
|
227
|
+
...buildInfiniteOptionsFromParams(
|
|
206
228
|
this.endpoint,
|
|
207
229
|
this._observableData.params,
|
|
208
230
|
this._observableData.uniqKey
|
|
209
231
|
),
|
|
210
232
|
...updateParams
|
|
211
233
|
});
|
|
212
|
-
} else {
|
|
213
|
-
return super.update(updateParams);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Refetches query when params are initialized.
|
|
218
|
-
*
|
|
219
|
-
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#refetch)
|
|
220
|
-
*/
|
|
221
|
-
refetch(options) {
|
|
222
|
-
if (this.params) {
|
|
223
|
-
return super.refetch(options);
|
|
224
234
|
}
|
|
225
|
-
return
|
|
235
|
+
return super.update(updateParams);
|
|
226
236
|
}
|
|
227
237
|
/**
|
|
228
|
-
* Sets params and starts query execution.
|
|
229
|
-
*
|
|
230
|
-
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#start)
|
|
238
|
+
* Sets base params and starts the infinite query execution.
|
|
231
239
|
*/
|
|
232
240
|
async start(params) {
|
|
233
241
|
runInAction(() => {
|
|
234
242
|
this._observableData.params = params;
|
|
235
243
|
});
|
|
236
244
|
return await super.start(
|
|
237
|
-
|
|
245
|
+
buildInfiniteOptionsFromParams(
|
|
238
246
|
this.endpoint,
|
|
239
247
|
params,
|
|
240
248
|
this._observableData.uniqKey
|
|
@@ -246,13 +254,60 @@ class EndpointQuery extends Query {
|
|
|
246
254
|
runInAction(() => {
|
|
247
255
|
this._observableData.params = void 0;
|
|
248
256
|
this._observableData.dynamicOptions = void 0;
|
|
257
|
+
this._observableData.transform = void 0;
|
|
258
|
+
this._observableData.mergePageParam = void 0;
|
|
249
259
|
});
|
|
250
260
|
}
|
|
251
261
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
262
|
+
function ensureObjectPageParam(pageParam, mergePageParam) {
|
|
263
|
+
if (typeof pageParam === "object" && pageParam !== null && !Array.isArray(pageParam)) {
|
|
264
|
+
return pageParam;
|
|
265
|
+
}
|
|
266
|
+
throw new Error(
|
|
267
|
+
`[mobx-tanstack-query-api] "${mergePageParam}" mergePageParam expects an object pageParam. Use a custom mergePageParam function for primitive page params.`
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
function mergeInfiniteQueryPageParam(params, pageParam, ctx, mergePageParam) {
|
|
271
|
+
if (pageParam == null || !mergePageParam) {
|
|
272
|
+
return params;
|
|
273
|
+
}
|
|
274
|
+
if (typeof mergePageParam === "function") {
|
|
275
|
+
return mergePageParam(params, pageParam, ctx) || {};
|
|
276
|
+
}
|
|
277
|
+
const objectPageParam = ensureObjectPageParam(pageParam, mergePageParam);
|
|
278
|
+
switch (mergePageParam) {
|
|
279
|
+
case "params":
|
|
280
|
+
return {
|
|
281
|
+
...params,
|
|
282
|
+
...objectPageParam
|
|
283
|
+
};
|
|
284
|
+
case "body":
|
|
285
|
+
return {
|
|
286
|
+
...params,
|
|
287
|
+
body: {
|
|
288
|
+
...params.body,
|
|
289
|
+
...objectPageParam
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
case "query":
|
|
293
|
+
return {
|
|
294
|
+
...params,
|
|
295
|
+
query: {
|
|
296
|
+
...params.query,
|
|
297
|
+
...objectPageParam
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
case "headers":
|
|
301
|
+
return {
|
|
302
|
+
...params,
|
|
303
|
+
headers: {
|
|
304
|
+
...params.headers,
|
|
305
|
+
...objectPageParam
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const buildInfiniteOptionsFromParams = (endpoint, params, uniqKey) => {
|
|
256
311
|
const { requiredParams } = endpoint.configuration;
|
|
257
312
|
let hasRequiredParams = false;
|
|
258
313
|
if (requiredParams.length > 0) {
|
|
@@ -262,99 +317,9 @@ const buildOptionsFromParams = (endpoint, params, uniqKey) => {
|
|
|
262
317
|
}
|
|
263
318
|
return {
|
|
264
319
|
enabled: hasRequiredParams,
|
|
265
|
-
queryKey: endpoint.
|
|
320
|
+
queryKey: endpoint.toInfiniteQueryKey(params || {}, uniqKey)
|
|
266
321
|
};
|
|
267
322
|
};
|
|
268
|
-
class EndpointInfiniteQuery extends InfiniteQuery {
|
|
269
|
-
constructor(endpoint, queryClient, queryOptionsInput) {
|
|
270
|
-
const {
|
|
271
|
-
uniqKey,
|
|
272
|
-
transform: transformResponse,
|
|
273
|
-
params: paramsFn,
|
|
274
|
-
...queryOptions
|
|
275
|
-
} = typeof queryOptionsInput === "function" ? queryOptionsInput() : queryOptionsInput;
|
|
276
|
-
super({
|
|
277
|
-
...queryOptions,
|
|
278
|
-
queryClient,
|
|
279
|
-
meta: endpoint.toQueryMeta(queryOptions.meta),
|
|
280
|
-
options: (query) => {
|
|
281
|
-
const extraOptions = {};
|
|
282
|
-
let willEnableManually;
|
|
283
|
-
let params;
|
|
284
|
-
const pageParam = query.options.initialPageParam;
|
|
285
|
-
if (typeof queryOptionsInput === "function") {
|
|
286
|
-
Object.assign(extraOptions, queryOptionsInput());
|
|
287
|
-
params = paramsFn(pageParam);
|
|
288
|
-
willEnableManually = false;
|
|
289
|
-
} else {
|
|
290
|
-
willEnableManually = queryOptionsInput.enabled === false;
|
|
291
|
-
params = paramsFn(pageParam);
|
|
292
|
-
}
|
|
293
|
-
const builtOptions = buildOptionsFromParams(endpoint, params, uniqKey);
|
|
294
|
-
let isEnabled = false;
|
|
295
|
-
if (willEnableManually) ;
|
|
296
|
-
else {
|
|
297
|
-
isEnabled = builtOptions.enabled;
|
|
298
|
-
}
|
|
299
|
-
return {
|
|
300
|
-
...query.options,
|
|
301
|
-
...builtOptions,
|
|
302
|
-
// ...dynamicOuterOptions,
|
|
303
|
-
enabled: isEnabled,
|
|
304
|
-
...extraOptions
|
|
305
|
-
};
|
|
306
|
-
},
|
|
307
|
-
queryFn: async (ctx) => {
|
|
308
|
-
const params = paramsFn(
|
|
309
|
-
ctx.pageParam ?? queryOptions.initialPageParam
|
|
310
|
-
);
|
|
311
|
-
runInAction(() => {
|
|
312
|
-
this.response = null;
|
|
313
|
-
this.params = params;
|
|
314
|
-
});
|
|
315
|
-
let requestParams = params.request;
|
|
316
|
-
if (requestParams) {
|
|
317
|
-
if (!requestParams.signal) {
|
|
318
|
-
requestParams.signal = ctx.signal;
|
|
319
|
-
}
|
|
320
|
-
} else {
|
|
321
|
-
requestParams = { signal: ctx.signal };
|
|
322
|
-
}
|
|
323
|
-
const fixedInput = {
|
|
324
|
-
...params,
|
|
325
|
-
request: requestParams
|
|
326
|
-
};
|
|
327
|
-
const response = await endpoint.request(fixedInput);
|
|
328
|
-
runInAction(() => {
|
|
329
|
-
this.response = response;
|
|
330
|
-
});
|
|
331
|
-
return await transformResponse?.(response) ?? response.data;
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
this.endpoint = endpoint;
|
|
335
|
-
this.uniqKey = uniqKey;
|
|
336
|
-
observable.ref(this, "response");
|
|
337
|
-
observable.ref(this, "params");
|
|
338
|
-
makeObservable(this);
|
|
339
|
-
}
|
|
340
|
-
response = null;
|
|
341
|
-
params = null;
|
|
342
|
-
uniqKey;
|
|
343
|
-
update({
|
|
344
|
-
params,
|
|
345
|
-
...options
|
|
346
|
-
}) {
|
|
347
|
-
return super.update({
|
|
348
|
-
...buildOptionsFromParams(this.endpoint, params, this.uniqKey),
|
|
349
|
-
...options
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
async start(params) {
|
|
353
|
-
return await super.start(
|
|
354
|
-
buildOptionsFromParams(this.endpoint, params, this.uniqKey)
|
|
355
|
-
);
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
323
|
class EndpointMutation extends Mutation {
|
|
359
324
|
/**
|
|
360
325
|
* Creates `EndpointMutation` instance.
|
|
@@ -425,6 +390,266 @@ class EndpointMutation extends Mutation {
|
|
|
425
390
|
});
|
|
426
391
|
}
|
|
427
392
|
}
|
|
393
|
+
class EndpointQuery extends Query {
|
|
394
|
+
/**
|
|
395
|
+
* Creates `EndpointQuery` instance.
|
|
396
|
+
*
|
|
397
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#constructor)
|
|
398
|
+
*/
|
|
399
|
+
constructor(endpoint, inputQueryClient, queryOptionsInput) {
|
|
400
|
+
const isQueryOptionsInputFn = typeof queryOptionsInput === "function";
|
|
401
|
+
const unpackedQueryOptionsInput = isQueryOptionsInputFn ? queryOptionsInput() : queryOptionsInput;
|
|
402
|
+
const {
|
|
403
|
+
uniqKey,
|
|
404
|
+
transform: transformResponse,
|
|
405
|
+
params,
|
|
406
|
+
onDone: onDoneInput,
|
|
407
|
+
queryClient: overridedQueryClient,
|
|
408
|
+
...queryOptions
|
|
409
|
+
} = unpackedQueryOptionsInput;
|
|
410
|
+
const queryClient = overridedQueryClient ?? inputQueryClient;
|
|
411
|
+
const _observableData = {
|
|
412
|
+
params: null,
|
|
413
|
+
dynamicOptions: void 0,
|
|
414
|
+
response: null,
|
|
415
|
+
uniqKey: unpackedQueryOptionsInput.uniqKey
|
|
416
|
+
};
|
|
417
|
+
if (!isQueryOptionsInputFn && typeof params !== "function") {
|
|
418
|
+
if ("params" in unpackedQueryOptionsInput) {
|
|
419
|
+
_observableData.params = params;
|
|
420
|
+
} else {
|
|
421
|
+
_observableData.params = {};
|
|
422
|
+
}
|
|
423
|
+
_observableData.initialized = true;
|
|
424
|
+
}
|
|
425
|
+
makeObservable(_observableData, {
|
|
426
|
+
params: observable.ref,
|
|
427
|
+
response: observable.ref,
|
|
428
|
+
dynamicOptions: observable
|
|
429
|
+
});
|
|
430
|
+
const onDone = onDoneInput && ((...args) => {
|
|
431
|
+
onDoneInput(...args);
|
|
432
|
+
});
|
|
433
|
+
super({
|
|
434
|
+
...queryOptions,
|
|
435
|
+
onDone,
|
|
436
|
+
queryClient,
|
|
437
|
+
meta: endpoint.toQueryMeta(queryOptions.meta),
|
|
438
|
+
options: () => {
|
|
439
|
+
const builtOptions = buildOptionsFromParams(
|
|
440
|
+
endpoint,
|
|
441
|
+
_observableData.params,
|
|
442
|
+
_observableData.uniqKey
|
|
443
|
+
);
|
|
444
|
+
let isEnabled = !!_observableData.initialized && builtOptions.enabled;
|
|
445
|
+
if (typeof queryOptionsInput !== "function" && queryOptionsInput.enabled === false) {
|
|
446
|
+
isEnabled = false;
|
|
447
|
+
}
|
|
448
|
+
return {
|
|
449
|
+
...builtOptions,
|
|
450
|
+
enabled: isEnabled,
|
|
451
|
+
..._observableData.dynamicOptions
|
|
452
|
+
};
|
|
453
|
+
},
|
|
454
|
+
queryFn: async (ctx) => {
|
|
455
|
+
const params2 = endpoint.getParamsFromContext(ctx);
|
|
456
|
+
runInAction(() => {
|
|
457
|
+
_observableData.response = null;
|
|
458
|
+
if (!comparer.structural(params2, _observableData.params)) {
|
|
459
|
+
_observableData.params = params2;
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
let requestParams = params2.requestParams;
|
|
463
|
+
if (requestParams) {
|
|
464
|
+
if (!requestParams.signal) {
|
|
465
|
+
requestParams.signal = ctx.signal;
|
|
466
|
+
}
|
|
467
|
+
} else {
|
|
468
|
+
requestParams = { signal: ctx.signal };
|
|
469
|
+
}
|
|
470
|
+
const fixedInput = {
|
|
471
|
+
...params2,
|
|
472
|
+
requestParams
|
|
473
|
+
};
|
|
474
|
+
const response = await endpoint.request(fixedInput);
|
|
475
|
+
runInAction(() => {
|
|
476
|
+
_observableData.response = response;
|
|
477
|
+
});
|
|
478
|
+
return await transformResponse?.(response) ?? response.data;
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
this.endpoint = endpoint;
|
|
482
|
+
const parentAtom = getMobxAdministration(this);
|
|
483
|
+
computed.struct(this, "params");
|
|
484
|
+
computed.struct(this, "response");
|
|
485
|
+
makeObservable(this);
|
|
486
|
+
if (isQueryOptionsInputFn || typeof params === "function") {
|
|
487
|
+
const createParamsReaction = () => reaction(
|
|
488
|
+
() => {
|
|
489
|
+
let outDynamicOptions;
|
|
490
|
+
let outParams;
|
|
491
|
+
let uniqKey2;
|
|
492
|
+
if (isQueryOptionsInputFn) {
|
|
493
|
+
const result = queryOptionsInput();
|
|
494
|
+
const {
|
|
495
|
+
params: params2,
|
|
496
|
+
abortSignal,
|
|
497
|
+
select,
|
|
498
|
+
onDone: onDone2,
|
|
499
|
+
onError,
|
|
500
|
+
onInit,
|
|
501
|
+
enableOnDemand,
|
|
502
|
+
uniqKey: _uniqKey,
|
|
503
|
+
...dynamicOptions
|
|
504
|
+
} = result;
|
|
505
|
+
uniqKey2 = _uniqKey;
|
|
506
|
+
if ("params" in result) {
|
|
507
|
+
outParams = result.params;
|
|
508
|
+
} else {
|
|
509
|
+
outParams = {};
|
|
510
|
+
}
|
|
511
|
+
outDynamicOptions = Object.keys(dynamicOptions).length > 0 ? dynamicOptions : void 0;
|
|
512
|
+
} else if ("params" in unpackedQueryOptionsInput) {
|
|
513
|
+
outParams = unpackedQueryOptionsInput.params;
|
|
514
|
+
uniqKey2 = unpackedQueryOptionsInput.uniqKey;
|
|
515
|
+
} else {
|
|
516
|
+
outParams = {};
|
|
517
|
+
uniqKey2 = unpackedQueryOptionsInput.uniqKey;
|
|
518
|
+
}
|
|
519
|
+
return {
|
|
520
|
+
params: callFunction(outParams),
|
|
521
|
+
dynamicOptions: outDynamicOptions,
|
|
522
|
+
uniqKey: uniqKey2
|
|
523
|
+
};
|
|
524
|
+
},
|
|
525
|
+
({ params: params2, dynamicOptions, uniqKey: uniqKey2 }) => {
|
|
526
|
+
runInAction(() => {
|
|
527
|
+
_observableData.initialized = true;
|
|
528
|
+
_observableData.params = params2;
|
|
529
|
+
_observableData.dynamicOptions = dynamicOptions;
|
|
530
|
+
_observableData.uniqKey = uniqKey2;
|
|
531
|
+
});
|
|
532
|
+
},
|
|
533
|
+
{
|
|
534
|
+
fireImmediately: true
|
|
535
|
+
}
|
|
536
|
+
);
|
|
537
|
+
if (this.features.lazy) {
|
|
538
|
+
lazyObserve({
|
|
539
|
+
property: parentAtom.values_.get("_result"),
|
|
540
|
+
onStart: createParamsReaction,
|
|
541
|
+
onEnd: (disposeFn) => disposeFn()
|
|
542
|
+
});
|
|
543
|
+
} else {
|
|
544
|
+
this.abortController.signal.addEventListener(
|
|
545
|
+
"abort",
|
|
546
|
+
createParamsReaction()
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
this._observableData = _observableData;
|
|
551
|
+
}
|
|
552
|
+
_observableData;
|
|
553
|
+
/**
|
|
554
|
+
* Current endpoint params used by this query.
|
|
555
|
+
*
|
|
556
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#params)
|
|
557
|
+
*/
|
|
558
|
+
get params() {
|
|
559
|
+
return this._observableData.params;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Last raw HTTP response returned by endpoint.
|
|
563
|
+
*
|
|
564
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#response)
|
|
565
|
+
*/
|
|
566
|
+
get response() {
|
|
567
|
+
return this._observableData.response;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Updates query options and optionally params.
|
|
571
|
+
*
|
|
572
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#update)
|
|
573
|
+
*/
|
|
574
|
+
update(updateParams) {
|
|
575
|
+
if ("params" in updateParams) {
|
|
576
|
+
const { params, ...options } = updateParams;
|
|
577
|
+
runInAction(() => {
|
|
578
|
+
this._observableData.params = params;
|
|
579
|
+
});
|
|
580
|
+
return super.update({
|
|
581
|
+
...buildOptionsFromParams(
|
|
582
|
+
this.endpoint,
|
|
583
|
+
params,
|
|
584
|
+
this._observableData.uniqKey
|
|
585
|
+
),
|
|
586
|
+
...options
|
|
587
|
+
});
|
|
588
|
+
} else if (this._observableData) {
|
|
589
|
+
return super.update({
|
|
590
|
+
...buildOptionsFromParams(
|
|
591
|
+
this.endpoint,
|
|
592
|
+
this._observableData.params,
|
|
593
|
+
this._observableData.uniqKey
|
|
594
|
+
),
|
|
595
|
+
...updateParams
|
|
596
|
+
});
|
|
597
|
+
} else {
|
|
598
|
+
return super.update(updateParams);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Refetches query when params are initialized.
|
|
603
|
+
*
|
|
604
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#refetch)
|
|
605
|
+
*/
|
|
606
|
+
refetch(options) {
|
|
607
|
+
if (this.params) {
|
|
608
|
+
return super.refetch(options);
|
|
609
|
+
}
|
|
610
|
+
return Promise.resolve(this.queryObserver.getCurrentResult());
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Sets params and starts query execution.
|
|
614
|
+
*
|
|
615
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoint-queries/#start)
|
|
616
|
+
*/
|
|
617
|
+
async start(params) {
|
|
618
|
+
runInAction(() => {
|
|
619
|
+
this._observableData.params = params;
|
|
620
|
+
});
|
|
621
|
+
return await super.start(
|
|
622
|
+
buildOptionsFromParams(
|
|
623
|
+
this.endpoint,
|
|
624
|
+
params,
|
|
625
|
+
this._observableData.uniqKey
|
|
626
|
+
)
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
handleDestroy() {
|
|
630
|
+
super.handleDestroy();
|
|
631
|
+
runInAction(() => {
|
|
632
|
+
this._observableData.params = void 0;
|
|
633
|
+
this._observableData.dynamicOptions = void 0;
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
const getParamsFromContext = (ctx) => {
|
|
638
|
+
return ctx.queryKey.at(-2) || {};
|
|
639
|
+
};
|
|
640
|
+
const buildOptionsFromParams = (endpoint, params, uniqKey) => {
|
|
641
|
+
const { requiredParams } = endpoint.configuration;
|
|
642
|
+
let hasRequiredParams = false;
|
|
643
|
+
if (requiredParams.length > 0) {
|
|
644
|
+
hasRequiredParams = !!params && requiredParams.every((param) => param in params);
|
|
645
|
+
} else {
|
|
646
|
+
hasRequiredParams = !!params;
|
|
647
|
+
}
|
|
648
|
+
return {
|
|
649
|
+
enabled: hasRequiredParams,
|
|
650
|
+
queryKey: endpoint.toQueryKey(params || {}, uniqKey)
|
|
651
|
+
};
|
|
652
|
+
};
|
|
428
653
|
const emptyStatusCodesSet = /* @__PURE__ */ new Set([204, 205, 304]);
|
|
429
654
|
class HttpResponse {
|
|
430
655
|
constructor(originalResponse, request) {
|
|
@@ -488,6 +713,65 @@ function isContractOptionEnabled(option, key) {
|
|
|
488
713
|
return option === true || typeof option === "object" && option !== null && option[key] === true;
|
|
489
714
|
}
|
|
490
715
|
class Endpoint {
|
|
716
|
+
/**
|
|
717
|
+
* Unique runtime identifier of the endpoint instance.
|
|
718
|
+
*
|
|
719
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#endpointid)
|
|
720
|
+
*/
|
|
721
|
+
endpointId;
|
|
722
|
+
/**
|
|
723
|
+
* Mutable presets used by helper factory methods like `toMutation()`.
|
|
724
|
+
*
|
|
725
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#presets)
|
|
726
|
+
*/
|
|
727
|
+
presets = {
|
|
728
|
+
mutations: {}
|
|
729
|
+
};
|
|
730
|
+
/**
|
|
731
|
+
* Type-only helper that exposes endpoint params shape.
|
|
732
|
+
*
|
|
733
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#__params)
|
|
734
|
+
*/
|
|
735
|
+
__params;
|
|
736
|
+
/**
|
|
737
|
+
* Type-only helper that exposes endpoint response shape.
|
|
738
|
+
*
|
|
739
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#__response)
|
|
740
|
+
*/
|
|
741
|
+
__response;
|
|
742
|
+
/**
|
|
743
|
+
* Custom metadata attached to the endpoint configuration.
|
|
744
|
+
*
|
|
745
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#meta)
|
|
746
|
+
*/
|
|
747
|
+
meta;
|
|
748
|
+
/**
|
|
749
|
+
* Endpoint configuration generated from the contract/codegen layer.
|
|
750
|
+
*
|
|
751
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#configuration)
|
|
752
|
+
*/
|
|
753
|
+
configuration;
|
|
754
|
+
/**
|
|
755
|
+
* Query client used by query and mutation helpers.
|
|
756
|
+
*
|
|
757
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#queryclient)
|
|
758
|
+
*/
|
|
759
|
+
queryClient;
|
|
760
|
+
/**
|
|
761
|
+
* HTTP client used to build URLs and execute requests.
|
|
762
|
+
*
|
|
763
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#httpclient)
|
|
764
|
+
*/
|
|
765
|
+
httpClient;
|
|
766
|
+
validateParams = false;
|
|
767
|
+
validateData = false;
|
|
768
|
+
throwParams = false;
|
|
769
|
+
throwData = false;
|
|
770
|
+
/**
|
|
771
|
+
* Creates a callable `Endpoint` instance.
|
|
772
|
+
*
|
|
773
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#constructor)
|
|
774
|
+
*/
|
|
491
775
|
constructor(configuration, queryClient, httpClient) {
|
|
492
776
|
this.configuration = configuration;
|
|
493
777
|
this.queryClient = queryClient;
|
|
@@ -512,43 +796,77 @@ class Endpoint {
|
|
|
512
796
|
});
|
|
513
797
|
return callable;
|
|
514
798
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
__response;
|
|
521
|
-
meta;
|
|
522
|
-
validateParams = false;
|
|
523
|
-
validateData = false;
|
|
524
|
-
throwParams = false;
|
|
525
|
-
throwData = false;
|
|
799
|
+
/**
|
|
800
|
+
* Returns a fully resolved request URL for the provided params.
|
|
801
|
+
*
|
|
802
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#getfullurl)
|
|
803
|
+
*/
|
|
526
804
|
getFullUrl(...args) {
|
|
527
805
|
const params = this.configuration.params(args[0] ?? {});
|
|
528
806
|
return this.httpClient.buildUrl(params);
|
|
529
807
|
}
|
|
808
|
+
/**
|
|
809
|
+
* Returns the resolved request path without the base URL.
|
|
810
|
+
*
|
|
811
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#getpath)
|
|
812
|
+
*/
|
|
530
813
|
getPath(...args) {
|
|
531
814
|
const params = this.configuration.params(args[0] ?? {});
|
|
532
815
|
return params.path;
|
|
533
816
|
}
|
|
817
|
+
/**
|
|
818
|
+
* Extracts endpoint params from TanStack Query function context.
|
|
819
|
+
*
|
|
820
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#getparamsfromcontext)
|
|
821
|
+
*/
|
|
534
822
|
getParamsFromContext(ctx) {
|
|
535
823
|
return ctx.queryKey.at(-2) || {};
|
|
536
824
|
}
|
|
825
|
+
/**
|
|
826
|
+
* Tags declared for the endpoint.
|
|
827
|
+
*
|
|
828
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#tags)
|
|
829
|
+
*/
|
|
537
830
|
get tags() {
|
|
538
831
|
return this.configuration.tags;
|
|
539
832
|
}
|
|
833
|
+
/**
|
|
834
|
+
* Path segments declared for the endpoint.
|
|
835
|
+
*
|
|
836
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#path)
|
|
837
|
+
*/
|
|
540
838
|
get path() {
|
|
541
839
|
return this.configuration.path;
|
|
542
840
|
}
|
|
841
|
+
/**
|
|
842
|
+
* Slash-joined path declaration for the endpoint.
|
|
843
|
+
*
|
|
844
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#pathdeclaration)
|
|
845
|
+
*/
|
|
543
846
|
get pathDeclaration() {
|
|
544
847
|
return this.path.join("/");
|
|
545
848
|
}
|
|
849
|
+
/**
|
|
850
|
+
* Operation identifier from the endpoint configuration.
|
|
851
|
+
*
|
|
852
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#operationid)
|
|
853
|
+
*/
|
|
546
854
|
get operationId() {
|
|
547
855
|
return this.configuration.operationId;
|
|
548
856
|
}
|
|
857
|
+
/**
|
|
858
|
+
* Optional endpoint group.
|
|
859
|
+
*
|
|
860
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#group)
|
|
861
|
+
*/
|
|
549
862
|
get group() {
|
|
550
863
|
return this.configuration.group;
|
|
551
864
|
}
|
|
865
|
+
/**
|
|
866
|
+
* Optional endpoint namespace.
|
|
867
|
+
*
|
|
868
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#namespace)
|
|
869
|
+
*/
|
|
552
870
|
get namespace() {
|
|
553
871
|
return this.configuration.namespace;
|
|
554
872
|
}
|
|
@@ -587,6 +905,11 @@ class Endpoint {
|
|
|
587
905
|
}
|
|
588
906
|
}
|
|
589
907
|
}
|
|
908
|
+
/**
|
|
909
|
+
* Performs the HTTP request and optionally validates contracts.
|
|
910
|
+
*
|
|
911
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#request)
|
|
912
|
+
*/
|
|
590
913
|
async request(...args) {
|
|
591
914
|
const rawParams = args[0] ?? {};
|
|
592
915
|
const contract = this.configuration.contract;
|
|
@@ -613,6 +936,11 @@ class Endpoint {
|
|
|
613
936
|
}
|
|
614
937
|
return response;
|
|
615
938
|
}
|
|
939
|
+
/**
|
|
940
|
+
* Builds query metadata payload enriched with endpoint fields.
|
|
941
|
+
*
|
|
942
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#toquerymeta)
|
|
943
|
+
*/
|
|
616
944
|
toQueryMeta = (meta) => ({
|
|
617
945
|
...meta,
|
|
618
946
|
tags: this.tags,
|
|
@@ -622,6 +950,11 @@ class Endpoint {
|
|
|
622
950
|
endpointId: this.endpointId,
|
|
623
951
|
endpointQuery: true
|
|
624
952
|
});
|
|
953
|
+
/**
|
|
954
|
+
* Builds a stable TanStack Query key for a regular query.
|
|
955
|
+
*
|
|
956
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#toquerykey)
|
|
957
|
+
*/
|
|
625
958
|
toQueryKey(params, uniqKey) {
|
|
626
959
|
return [
|
|
627
960
|
...this.configuration.path,
|
|
@@ -630,6 +963,11 @@ class Endpoint {
|
|
|
630
963
|
callFunction(uniqKey)
|
|
631
964
|
];
|
|
632
965
|
}
|
|
966
|
+
/**
|
|
967
|
+
* Builds a stable TanStack Query key for an infinite query.
|
|
968
|
+
*
|
|
969
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#toinfinitequerykey)
|
|
970
|
+
*/
|
|
633
971
|
toInfiniteQueryKey(params, uniqKey) {
|
|
634
972
|
return [
|
|
635
973
|
{ infiniteQuery: true },
|
|
@@ -639,6 +977,11 @@ class Endpoint {
|
|
|
639
977
|
callFunction(uniqKey)
|
|
640
978
|
];
|
|
641
979
|
}
|
|
980
|
+
/**
|
|
981
|
+
* Invalidates the exact query produced by this endpoint.
|
|
982
|
+
*
|
|
983
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#invalidatequery)
|
|
984
|
+
*/
|
|
642
985
|
invalidateQuery(...args) {
|
|
643
986
|
this.queryClient.invalidateQueries(
|
|
644
987
|
{
|
|
@@ -649,6 +992,11 @@ class Endpoint {
|
|
|
649
992
|
args[2]
|
|
650
993
|
);
|
|
651
994
|
}
|
|
995
|
+
/**
|
|
996
|
+
* Creates an `EndpointMutation` bound to this endpoint.
|
|
997
|
+
*
|
|
998
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#tomutation)
|
|
999
|
+
*/
|
|
652
1000
|
toMutation(options) {
|
|
653
1001
|
return new EndpointMutation(
|
|
654
1002
|
this,
|
|
@@ -659,6 +1007,11 @@ class Endpoint {
|
|
|
659
1007
|
}
|
|
660
1008
|
);
|
|
661
1009
|
}
|
|
1010
|
+
/**
|
|
1011
|
+
* Creates an `EndpointQuery` bound to this endpoint.
|
|
1012
|
+
*
|
|
1013
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#toquery)
|
|
1014
|
+
*/
|
|
662
1015
|
toQuery(options) {
|
|
663
1016
|
return new EndpointQuery(
|
|
664
1017
|
this,
|
|
@@ -666,12 +1019,13 @@ class Endpoint {
|
|
|
666
1019
|
options
|
|
667
1020
|
);
|
|
668
1021
|
}
|
|
1022
|
+
/**
|
|
1023
|
+
* Creates an `EndpointInfiniteQuery` bound to this endpoint.
|
|
1024
|
+
*
|
|
1025
|
+
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query-api/endpoints/#toinfinitequery)
|
|
1026
|
+
*/
|
|
669
1027
|
toInfiniteQuery(options) {
|
|
670
|
-
return new EndpointInfiniteQuery(
|
|
671
|
-
this,
|
|
672
|
-
this.queryClient,
|
|
673
|
-
options
|
|
674
|
-
);
|
|
1028
|
+
return new EndpointInfiniteQuery(this, this.queryClient, options);
|
|
675
1029
|
}
|
|
676
1030
|
}
|
|
677
1031
|
class EndpointQueryClient extends QueryClient {
|
|
@@ -933,15 +1287,18 @@ class HttpClient {
|
|
|
933
1287
|
export {
|
|
934
1288
|
ContentType,
|
|
935
1289
|
Endpoint,
|
|
1290
|
+
EndpointInfiniteQuery,
|
|
936
1291
|
EndpointMutation,
|
|
937
1292
|
EndpointQuery,
|
|
938
1293
|
EndpointQueryClient,
|
|
939
1294
|
HttpClient,
|
|
940
1295
|
HttpResponse,
|
|
1296
|
+
buildInfiniteOptionsFromParams,
|
|
941
1297
|
buildOptionsFromParams,
|
|
942
1298
|
emptyStatusCodesSet,
|
|
943
1299
|
getParamsFromContext,
|
|
944
1300
|
isHttpBadResponse,
|
|
945
|
-
isHttpResponse
|
|
1301
|
+
isHttpResponse,
|
|
1302
|
+
mergeInfiniteQueryPageParam
|
|
946
1303
|
};
|
|
947
1304
|
//# sourceMappingURL=index.js.map
|