@svadmin/core 0.66.1 → 0.66.3

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.66.1",
3
+ "version": "0.66.3",
4
4
  "author": "zuohuadong",
5
5
  "repository": {
6
6
  "type": "git",
package/src/plain-data.ts CHANGED
@@ -5,7 +5,7 @@ export function snapshotPlainData(value: unknown): JsonValue {
5
5
  function snapshot(value: unknown, ancestors: Set<object>): JsonValue {
6
6
  if (value === null || typeof value === 'string' || typeof value === 'boolean') return value;
7
7
  if (typeof value === 'number' && Number.isFinite(value)) return value;
8
- if (typeof value !== 'object' || value === null || ancestors.has(value)) {
8
+ if (typeof value !== 'object' || ancestors.has(value)) {
9
9
  throw new TypeError('Invalid plain data');
10
10
  }
11
11
  const array = Array.isArray(value);
@@ -19,8 +19,48 @@ const notificationSchema = Type.Object({
19
19
  }, { additionalProperties: false });
20
20
 
21
21
  const queryErrorNotificationKeys = new WeakMap<object, string>();
22
+ const reportedQueryFailures = new WeakMap<object, number>();
22
23
  let nextQueryErrorNotificationId = 0;
23
24
 
25
+ function ownData(value: unknown, key: string): unknown {
26
+ try {
27
+ return typeof value === 'object' && value !== null
28
+ ? Object.getOwnPropertyDescriptor(value, key)?.value
29
+ : undefined;
30
+ } catch {
31
+ return undefined;
32
+ }
33
+ }
34
+
35
+ /** 自定义通知提供方使用的安全技术上下文。内置 toast 会把 description 拼进主文案,因此不能传给它。 */
36
+ function queryFailureDescription(resource: string, value: unknown): string {
37
+ const status = ownData(value, 'statusCode');
38
+ const code = ownData(value, 'code');
39
+ return [
40
+ resource,
41
+ typeof code === 'string' && code.length > 0 ? code : undefined,
42
+ typeof status === 'number' ? `HTTP ${status}` : undefined,
43
+ ].filter((part): part is string => typeof part === 'string').join(' · ');
44
+ }
45
+
46
+ /** Expose only the normalized failure fields; provider diagnostics stay private. */
47
+ function reportQueryFailure(resource: string, value: unknown, updatedAt: number, query: object | undefined): void {
48
+ try {
49
+ if (query && reportedQueryFailures.get(query) === updatedAt) return;
50
+ if (query) reportedQueryFailures.set(query, updatedAt);
51
+ const status = ownData(value, 'statusCode');
52
+ const code = ownData(value, 'code');
53
+ console.error('[svadmin] Query failed', {
54
+ resource,
55
+ ...(typeof status === 'number' ? { status } : {}),
56
+ ...(typeof code === 'string' ? { code } : {}),
57
+ message: value instanceof Error ? value.message : 'Resource query failed',
58
+ });
59
+ } catch {
60
+ console.error('[svadmin] Query failed', { resource });
61
+ }
62
+ }
63
+
24
64
  export interface QuerySessionOptions {
25
65
  queryKey: QueryKey;
26
66
  resource: string;
@@ -106,7 +146,13 @@ export function createQuerySession<O extends QuerySessionOptions>(context: Admin
106
146
  const fallback = type === 'success' ? '' : t('common.loadFailed', { message: t('common.error') });
107
147
  if (typeof config !== 'function' && !config && !fallback) return;
108
148
  const candidate = snapshotPlainData(typeof config === 'function'
109
- ? config(value, undefined, origin.resource) : { message: config || fallback });
149
+ ? config(value, undefined, origin.resource)
150
+ : {
151
+ message: config || fallback,
152
+ ...(type === 'error' && !config && origin.notification
153
+ ? { description: queryFailureDescription(origin.resource, value) }
154
+ : {}),
155
+ });
110
156
  if (!checkExact(notificationSchema, candidate) || !current(origin) || origin.notification !== context.notificationProvider) return;
111
157
  let key = candidate.key;
112
158
  if (type === 'error' && !config) {
@@ -193,9 +239,12 @@ export function createQuerySession<O extends QuerySessionOptions>(context: Admin
193
239
  } else if (result.isError && (lastError?.key !== key || result.errorUpdatedAt > lastError.at)) {
194
240
  lastError = { key, at: result.errorUpdatedAt };
195
241
  untrack(() => {
242
+ const failure = errorFor(origin, result.error);
243
+ const query = client.getQueryCache().find({ queryKey: origin.queryKey, exact: true });
244
+ reportQueryFailure(origin.resource, failure, result.errorUpdatedAt, query);
196
245
  void handleAuthError(querySessionError(result.error), context, () => targetCurrent(origin),
197
246
  () => clearAuthQueries(client, origin.provider), origin.auth).catch(() => {});
198
- notify(origin, 'error', errorFor(origin, result.error));
247
+ notify(origin, 'error', failure);
199
248
  });
200
249
  }
201
250
  });
@@ -22,7 +22,7 @@ function copySchemaData(value: unknown, ancestors = new Set<object>(), depth = 0
22
22
  if (depth > 128) return invalidSchema();
23
23
  if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;
24
24
  if (typeof value === 'number' && Number.isFinite(value)) return value;
25
- if (typeof value !== 'object' || value === null || ancestors.has(value)) return invalidSchema();
25
+ if (typeof value !== 'object' || ancestors.has(value)) return invalidSchema();
26
26
  const array = Array.isArray(value);
27
27
  const prototype: unknown = Object.getPrototypeOf(value);
28
28
  if (!array && prototype !== Object.prototype && prototype !== null) return invalidSchema();
@@ -107,7 +107,7 @@ export function createTableQuery<TQueryFnData extends BaseRecord>(
107
107
  const merged=[...currentFilters];
108
108
  for(const nf of newFilters) {
109
109
  if(!('field' in nf)) { merged.push(nf); continue; }
110
- const idx=merged.findIndex(f => 'field' in f&&f.field===nf.field&&f.operator===nf.operator);
110
+ const idx = merged.findIndex(f => 'field' in f && f.field === nf.field && f.operator === nf.operator);
111
111
  if(idx>=0) {
112
112
  if(nf.value===undefined||nf.value===null||nf.value==='') merged.splice(idx,1);
113
113
  else merged[idx]=nf;
@@ -217,7 +217,7 @@ export function createTableQuery<TQueryFnData extends BaseRecord>(
217
217
 
218
218
  let cmp: number;
219
219
  if(typeof va==='number'&&typeof vb==='number') cmp=va-vb;
220
- else if(va instanceof Date&&vb instanceof Date) cmp=va.getTime()-vb.getTime();
220
+ else if (va instanceof Date && vb instanceof Date) cmp = va.getTime() - vb.getTime();
221
221
  else if(typeof va==='string'&&typeof vb==='string'&&!isNaN(Number(va))&&!isNaN(Number(vb))) cmp=Number(va)-Number(vb);
222
222
  else cmp=String(va).localeCompare(String(vb));
223
223
 
@@ -207,7 +207,7 @@ export class TableState {
207
207
  const merged=[...this.currentFilters];
208
208
  for(const nf of newFilters) {
209
209
  if(!('field' in nf)) { merged.push(nf); continue; }
210
- const idx=merged.findIndex(f => 'field' in f&&f.field===nf.field&&f.operator===nf.operator);
210
+ const idx = merged.findIndex(f => 'field' in f && f.field === nf.field && f.operator === nf.operator);
211
211
  if(idx>=0) {
212
212
  if(nf.value===undefined||nf.value===null||nf.value==='') merged.splice(idx,1);
213
213
  else merged[idx]=nf;