Skip to content

Commit 7d5cf91

Browse files
committed
change signature of predefined updaters
1 parent 57932bd commit 7d5cf91

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

examples/vite/src/examples/main/example.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ export const editArticleMutation = builder
4747
{
4848
type: 'articles',
4949
optimistic: true,
50-
updater: 'update-body-by-id',
50+
updater: 'update-with-body-by-id',
5151
},
5252
(ctx) => ({
5353
type: 'article',
5454
id: ctx.vars.params.id,
5555
optimistic: true,
56-
updater: 'merge-body',
56+
updater: 'merge-with-body',
5757
}),
5858
)
5959
.withMiddleware(async (ctx, next) => {
@@ -70,5 +70,5 @@ export const editArticleMutation = builder
7070
export const deleteArticleMutation = builder.withMethod('delete').withPath('/articles/:id').withUpdates({
7171
type: 'articles',
7272
optimistic: true,
73-
updater: 'delete-params-by-id',
73+
updater: 'delete-with-params-by-id',
7474
});

packages/tanstack-query-builder/src/tags/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ export type QueryUpdater<TVars = unknown, TData = unknown, TErr = unknown, TTarg
3939
| QueryUpdaterFn<TVars, TData, TErr, TTarget>
4040
| PredefinedUpdater<TVars, TData, TErr, TTarget>;
4141

42+
type FlatPredefinedOp = 'clear' | 'merge' | 'replace';
43+
type DeepPredefinedOp = 'create' | 'update' | 'upsert' | 'delete' | 'switch';
44+
4245
export type PredefinedUpdater<TVars = unknown, TData = unknown, TErr = unknown, TTarget = unknown> =
43-
| `clear-${UpdaterSelector<TVars>}`
44-
| `merge-${UpdaterSelector<TVars>}`
45-
| `replace-${UpdaterSelector<TVars>}`
46-
| `${'create' | 'update' | 'upsert' | 'delete' | 'switch'}-${UpdaterSelector<TVars>}-by-${KeyOfTarget<TTarget>}`;
46+
| `${FlatPredefinedOp}-with-${UpdaterSelector<TVars>}`
47+
| `${DeepPredefinedOp}-with-${UpdaterSelector<TVars>}-by-${KeyOfTarget<TTarget>}`;
4748

4849
type UpdaterSelector<TVars> = 'data' | 'vars' | Extract<KeysOfValue<TVars, Record<string, unknown> | undefined>, string>;
4950
type KeyOfTarget<TTarget> = KeyOfItem<TTarget> & string & {};

packages/tanstack-query-builder/src/tags/updateTags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function updateTags({
3838

3939
const updater = typeof tag === 'object' && tag.updater;
4040
if (!updater) continue;
41-
const updaterFn = getUpdater(updater, tag);
41+
const updaterFn = getUpdater(updater);
4242
if (!updaterFn) continue;
4343

4444
/**

packages/tanstack-query-builder/src/tags/updaters.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import type { QueryTagObject, QueryUpdater, QueryUpdaterFn } from './types';
1+
import type { QueryUpdater, QueryUpdaterFn } from './types';
22

33
export function getUpdater<TVars = unknown, TData = unknown, TErr = unknown, TTarget = unknown>(
44
updater: QueryUpdater<TVars, TData, TErr, TTarget>,
5-
tag: QueryTagObject,
65
): QueryUpdaterFn<TVars, TData, TErr, TTarget> | undefined {
76
if (typeof updater === 'function') return updater;
87

9-
const [_, updaterType, updaterKey, __, byKey] = updater.match(/^(\w+)-(\w+)(-by-(\w+))?$/) || [];
8+
const [_, updaterType, updaterKey, __, byKey] = updater.match(/^(\w+)-with-(\w+)(-by-(\w+))?$/) || [];
109

1110
const getFromCtx = (ctx: any) => {
1211
if (updaterKey === 'data') return ctx.data;

website/docs/tags.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const deleteArticleMutation = builder
9797
.withUpdates({
9898
type: "articles",
9999
// highlight-next-line
100-
updater: "delete-params-by-id",
100+
updater: "delete-with-params-by-id",
101101
});
102102
```
103103

@@ -106,15 +106,15 @@ const deleteArticleMutation = builder
106106
Explanation
107107
</summary>
108108

109-
The predefined functions are referred as a string. This string has a format of `<operation>-<context>-by-<field>` which can be broken down as follows:
109+
The predefined functions are referred as a string. This string has a format of `<operation>-with-<context>-by-<field>` which can be broken down as follows:
110110

111111
- `<operation>`: The operation to be performed. This can be one of `clear, merge, replace, create, update, upsert, delete, switch`.
112112
- `<context>`: The body of the update that will be used in the operation. This can be one of `data, vars, body, params, search, meta`
113113
- `<field>`: The field to be used in the context to match with the data in the cache. This is usually a unique identifier. This can be any field in the context.
114114

115115
Some examples:
116116

117-
- `delete-params-by-id`: remove the item from a list, where `params.id` matches the `id` field of the item.
118-
- `merge-body-by-id`: merge the `body` to the item in a list, where `body.id` matches the `id` field of the item.
117+
- `delete-with-params-by-id`: remove the item from a list, where `params.id` matches the `id` field of the item.
118+
- `merge-with-body-by-id`: merge the `body` to the item in a list, where `body.id` matches the `id` field of the item.
119119

120120
</details>

0 commit comments

Comments
 (0)