Skip to content

Commit afacc73

Browse files
committed
feat: support 5.90.2 tanstack query core; fix: transformError feature work
1 parent 72ad6ae commit afacc73

File tree

8 files changed

+84
-55
lines changed

8 files changed

+84
-55
lines changed

.changeset/chatty-months-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": patch
3+
---
4+
5+
fixed `transformError` query/mutation feature

.changeset/fair-readers-say.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": minor
3+
---
4+
5+
support new version `@tanstack/query-core` (`5.90.2`)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"url": "git://github.com/js2me/mobx-tanstack-query"
4949
},
5050
"peerDependencies": {
51-
"@tanstack/query-core": "^5.81.5",
51+
"@tanstack/query-core": "^5.90.2",
5252
"mobx": "^6.12.4"
5353
},
5454
"dependencies": {

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/inifinite-query.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,7 @@ export class InfiniteQuery<
369369
this.start = this.start.bind(this);
370370

371371
originalQueryProperties.forEach((property) => {
372-
if (this[property]) return;
373-
if (property === 'error' && this.features.transformError) {
374-
Object.defineProperty(this, property, {
375-
get: () => this.features.transformError!(this.result[property]),
376-
});
377-
} else {
372+
if (!this[property]) {
378373
Object.defineProperty(this, property, {
379374
get: () => this.result[property],
380375
});

src/mutation.ts

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {
33
MutationObserver,
44
MutationObserverOptions,
55
MutationObserverResult,
6-
MutationOptions,
76
MutationObserverBaseResult,
87
MutationStatus,
8+
MutateOptions,
99
} from '@tanstack/query-core';
1010
import { action, makeObservable, observable } from 'mobx';
1111
import { lazyObserve } from 'yummies/mobx';
@@ -42,37 +42,51 @@ export class Mutation<
4242
TData = unknown,
4343
TVariables = void,
4444
TError = DefaultError,
45-
TContext = unknown,
45+
TOnMutateResult = unknown,
4646
>
4747
extends Destroyable
4848
implements
4949
Disposable,
5050
Pick<
51-
MutationObserverBaseResult<TData, TError, TVariables, TContext>,
51+
MutationObserverBaseResult<TData, TError, TVariables, TOnMutateResult>,
5252
(typeof originalMutationProperties)[number]
5353
>
5454
{
5555
protected queryClient: AnyQueryClient;
5656

57-
mutationOptions: MutationObserverOptions<TData, TError, TVariables, TContext>;
58-
mutationObserver: MutationObserver<TData, TError, TVariables, TContext>;
57+
mutationOptions: MutationObserverOptions<
58+
TData,
59+
TError,
60+
TVariables,
61+
TOnMutateResult
62+
>;
63+
mutationObserver: MutationObserver<
64+
TData,
65+
TError,
66+
TVariables,
67+
TOnMutateResult
68+
>;
5969

60-
result: MutationObserverResult<TData, TError, TVariables, TContext>;
70+
result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>;
6171

6272
protected features: MutationFeatures;
6373

6474
protected settledListeners: MutationSettledListener<
6575
TData,
6676
TError,
6777
TVariables,
68-
TContext
78+
TOnMutateResult
6979
>[];
7080
protected errorListeners: MutationErrorListener<
7181
TError,
7282
TVariables,
73-
TContext
83+
TOnMutateResult
84+
>[];
85+
protected doneListeners: MutationDoneListener<
86+
TData,
87+
TVariables,
88+
TOnMutateResult
7489
>[];
75-
protected doneListeners: MutationDoneListener<TData, TVariables, TContext>[];
7690

7791
private _observerSubscription?: VoidFunction;
7892
private hooks?: QueryClientHooks;
@@ -120,14 +134,19 @@ export class Mutation<
120134
*/
121135
status!: MutationStatus;
122136

123-
context!: TContext | undefined;
137+
context!: TOnMutateResult | undefined;
124138
failureCount!: number;
125139
failureReason!: TError | null;
126140
isPaused!: boolean;
127141
submittedAt!: number;
128142

129143
constructor(
130-
protected config: MutationConfig<TData, TVariables, TError, TContext>,
144+
protected config: MutationConfig<
145+
TData,
146+
TVariables,
147+
TError,
148+
TOnMutateResult
149+
>,
131150
) {
132151
super(config.abortSignal);
133152

@@ -166,15 +185,9 @@ export class Mutation<
166185
this.start = this.start.bind(this);
167186

168187
originalMutationProperties.forEach((property) => {
169-
if (property === 'error' && this.features.transformError) {
170-
Object.defineProperty(this, property, {
171-
get: () => this.features.transformError!(this.result[property]),
172-
});
173-
} else {
174-
Object.defineProperty(this, property, {
175-
get: () => this.result[property],
176-
});
177-
}
188+
Object.defineProperty(this, property, {
189+
get: () => this.result[property],
190+
});
178191
});
179192

180193
makeObservable(this);
@@ -185,7 +198,7 @@ export class Mutation<
185198
TData,
186199
TError,
187200
TVariables,
188-
TContext
201+
TOnMutateResult
189202
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
190203
// @ts-expect-error
191204
>(queryClient, {
@@ -273,7 +286,7 @@ export class Mutation<
273286

274287
async mutate(
275288
variables: TVariables,
276-
options?: MutationOptions<TData, TError, TVariables, TContext>,
289+
options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,
277290
) {
278291
if (this.features.lazy) {
279292
let error: any;
@@ -299,7 +312,7 @@ export class Mutation<
299312

300313
async start(
301314
variables: TVariables,
302-
options?: MutationOptions<TData, TError, TVariables, TContext>,
315+
options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,
303316
) {
304317
return await this.mutate(variables, options);
305318
}
@@ -308,10 +321,14 @@ export class Mutation<
308321
* Modify this result so it matches the tanstack query result.
309322
*/
310323
private updateResult(
311-
result: MutationObserverResult<TData, TError, TVariables, TContext>,
324+
result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
312325
) {
313326
this.result = result || {};
314327

328+
if (this.features.transformError && this.result.error) {
329+
this.result.error = this.features.transformError(this.result.error);
330+
}
331+
315332
if (result.isSuccess && !result.error) {
316333
this.doneListeners.forEach((fn) =>
317334
fn(result.data!, result.variables!, result.context),
@@ -330,16 +347,25 @@ export class Mutation<
330347
}
331348

332349
onSettled(
333-
listener: MutationSettledListener<TData, TError, TVariables, TContext>,
350+
listener: MutationSettledListener<
351+
TData,
352+
TError,
353+
TVariables,
354+
TOnMutateResult
355+
>,
334356
): void {
335357
this.settledListeners.push(listener);
336358
}
337359

338-
onDone(listener: MutationDoneListener<TData, TVariables, TContext>): void {
360+
onDone(
361+
listener: MutationDoneListener<TData, TVariables, TOnMutateResult>,
362+
): void {
339363
this.doneListeners.push(listener);
340364
}
341365

342-
onError(listener: MutationErrorListener<TError, TVariables, TContext>): void {
366+
onError(
367+
listener: MutationErrorListener<TError, TVariables, TOnMutateResult>,
368+
): void {
343369
this.errorListeners.push(listener);
344370
}
345371

@@ -370,5 +396,5 @@ export class MobxMutation<
370396
TData = unknown,
371397
TVariables = void,
372398
TError = DefaultError,
373-
TContext = unknown,
374-
> extends Mutation<TData, TVariables, TError, TContext> {}
399+
TOnMutateResult = unknown,
400+
> extends Mutation<TData, TVariables, TError, TOnMutateResult> {}

src/preset/create-mutation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export type CreateMutationParams<
99
TData = unknown,
1010
TVariables = void,
1111
TError = DefaultError,
12-
TContext = unknown,
12+
TOnMutateResult = unknown,
1313
> = Omit<
14-
MutationConfig<TData, TVariables, TError, TContext>,
14+
MutationConfig<TData, TVariables, TError, TOnMutateResult>,
1515
'queryClient' | 'mutationFn'
1616
> & {
1717
queryClient?: QueryClient;
@@ -21,10 +21,10 @@ export const createMutation = <
2121
TData = unknown,
2222
TVariables = void,
2323
TError = DefaultError,
24-
TContext = unknown,
24+
TOnMutateResult = unknown,
2525
>(
26-
fn: MutationConfig<TData, TVariables, TError, TContext>['mutationFn'],
27-
params?: CreateMutationParams<TData, TVariables, TError, TContext>,
26+
fn: MutationConfig<TData, TVariables, TError, TOnMutateResult>['mutationFn'],
27+
params?: CreateMutationParams<TData, TVariables, TError, TOnMutateResult>,
2828
) => {
2929
return new Mutation({
3030
...params,

src/query.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,9 @@ export class Query<
325325
this.start = this.start.bind(this);
326326

327327
originalQueryProperties.forEach((property) => {
328-
if (property === 'error' && this.features.transformError) {
329-
Object.defineProperty(this, property, {
330-
get: () => this.features.transformError!(this.result[property]),
331-
});
332-
} else {
333-
Object.defineProperty(this, property, {
334-
get: () => this.result[property],
335-
});
336-
}
328+
Object.defineProperty(this, property, {
329+
get: () => this.result[property],
330+
});
337331
});
338332

339333
makeObservable(this);
@@ -597,6 +591,10 @@ export class Query<
597591
private updateResult(result: QueryObserverResult<TData, TError>) {
598592
this._result = result;
599593

594+
if (this.features.transformError && this._result.error) {
595+
this._result.error = this.features.transformError(this._result.error);
596+
}
597+
600598
if (result.isSuccess && !result.error && result.fetchStatus === 'idle') {
601599
this.doneListeners.forEach((fn) => fn(result.data!, void 0));
602600
} else if (result.error) {

0 commit comments

Comments
 (0)