@svadmin/core 0.20.3 → 0.21.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svadmin/core",
3
- "version": "0.20.3",
3
+ "version": "0.21.0",
4
4
  "author": "zuohuadong",
5
5
  "repository": {
6
6
  "type": "git",
@@ -252,7 +252,7 @@ export function useForm<
252
252
  }
253
253
 
254
254
  // ─── Query (edit/clone mode) ────────────────────────────────────
255
- const query = (action === 'edit' || action === 'clone') && currentId != null
255
+ const query = (action === 'edit' || action === 'clone')
256
256
  ? createQuery(() => ({
257
257
  queryKey: [resource, 'one', currentId],
258
258
  queryFn: async () => {
@@ -16,6 +16,26 @@ import type {
16
16
  } from './types';
17
17
  import type { LiveMode, LiveEvent } from './live.svelte';
18
18
 
19
+ /**
20
+ * Extend a Tanstack Query result with additional properties.
21
+ * The query object is returned as-is, with extra properties accessible via Proxy.
22
+ */
23
+ function extendQuery<Q extends object, E extends Record<string, unknown>>(
24
+ query: Q,
25
+ extensions: () => E,
26
+ ): Q & E {
27
+ return new Proxy(query, {
28
+ get(target, prop, receiver) {
29
+ const ext = extensions();
30
+ if (prop in ext) return ext[prop as keyof E];
31
+ return Reflect.get(target, prop, receiver);
32
+ },
33
+ has(target, prop) {
34
+ return prop in extensions() || Reflect.has(target, prop);
35
+ },
36
+ }) as Q & E;
37
+ }
38
+
19
39
  // ─── useList ───────────────────────────────────────────────────
20
40
 
21
41
  export interface UseListOptions<TData extends BaseRecord = BaseRecord, TError = HttpError> {
@@ -82,10 +102,7 @@ export function useList<TData extends BaseRecord = BaseRecord, TError = HttpErro
82
102
  }
83
103
  });
84
104
 
85
- return {
86
- query,
87
- get overtime() { return overtime; }
88
- };
105
+ return extendQuery(query, () => ({ overtime }));
89
106
  }
90
107
 
91
108
  // ─── useOne ────────────────────────────────────────────────────
@@ -139,8 +156,9 @@ export function useOne<TData extends BaseRecord = BaseRecord, TError = HttpError
139
156
  }
140
157
  });
141
158
 
142
- return { query, get overtime() { return overtime; } };
159
+ return extendQuery(query, () => ({ overtime }));
143
160
  }
161
+
144
162
  // ─── useShow ──────────────────────────────────────────────────
145
163
  export function useShow<TData extends BaseRecord = BaseRecord, TError = HttpError>(
146
164
  options: UseOneOptions<TData, TError> = {}
@@ -155,12 +173,7 @@ export function useShow<TData extends BaseRecord = BaseRecord, TError = HttpErro
155
173
  get id() { return showId; },
156
174
  });
157
175
 
158
- return {
159
- get query() { return result.query; },
160
- get showId() { return showId; },
161
- setShowId,
162
- get overtime() { return result.overtime; },
163
- };
176
+ return extendQuery(result, () => ({ showId, setShowId }));
164
177
  }
165
178
 
166
179
  // ─── useMany ───────────────────────────────────────────────────
@@ -205,7 +218,7 @@ export function useMany<TData extends BaseRecord = BaseRecord, TError = HttpErro
205
218
  else if (query.isError) fireErrorNotification(options.errorNotification, 'Fetch failed', query.error);
206
219
  });
207
220
 
208
- return { query, get overtime() { return overtime; } };
221
+ return extendQuery(query, () => ({ overtime }));
209
222
  }
210
223
 
211
224
  export function useApiUrl(dataProviderName?: string): string {
@@ -83,7 +83,7 @@ export function useTable<
83
83
  ...restOptions
84
84
  });
85
85
 
86
- const query = tableQueryInfo.query;
86
+ const query = tableQueryInfo;
87
87
  const overtime = tableQueryInfo.overtime;
88
88
 
89
89
  function setSorters(newSorters: Sort[]) { currentSorters = newSorters; }
@@ -130,7 +130,7 @@ export class TableState<TData extends BaseRecord = BaseRecord, TError = HttpErro
130
130
  }
131
131
 
132
132
  /** Underlying TanStack Query result */
133
- get query() { return this._queryResult.query; }
133
+ get query() { return this._queryResult; }
134
134
 
135
135
  /** Table data rows */
136
136
  get data(): TData[] { return (this.query.data as GetListResult<TData> | undefined)?.data ?? []; }