miaoda-react-devkit 0.1.1-beta.5 → 0.1.1-beta.7

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/dist/index.cjs CHANGED
@@ -22,6 +22,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
22
22
  // src/index.ts
23
23
  var src_exports = {};
24
24
  __export(src_exports, {
25
+ DEFAULT_AUTH_URL_PATTERNS: () => DEFAULT_AUTH_URL_PATTERNS,
26
+ DEFAULT_AUTH_WHITELIST_CODES: () => DEFAULT_AUTH_WHITELIST_CODES,
25
27
  DEFAULT_CONSOLE_FILTER_KEYWORDS: () => DEFAULT_CONSOLE_FILTER_KEYWORDS,
26
28
  DEFAULT_IGNORE_URLS: () => DEFAULT_IGNORE_URLS,
27
29
  DEFAULT_PGREST_URL_PATTERNS: () => DEFAULT_PGREST_URL_PATTERNS,
@@ -116,7 +118,30 @@ function getFetchRequestArgBody(fetchArgs = []) {
116
118
 
117
119
  // src/capture/pgrest.ts
118
120
  var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
119
- var DEFAULT_PGREST_WHITELIST_CODES = [];
121
+ var DEFAULT_PGREST_WHITELIST_CODES = [
122
+ "22P02",
123
+ // 无效的文本表示(如类型转换失败)
124
+ "42703",
125
+ // 未定义的列
126
+ "42P01",
127
+ // 未定义的表
128
+ "42501",
129
+ // 权限不足
130
+ "23503",
131
+ // 外键约束失败
132
+ "42P17",
133
+ // 对象已存在
134
+ "22008",
135
+ // 日期时间字段值超出范围
136
+ "23505",
137
+ // 唯一键冲突
138
+ "PGRST116",
139
+ // 期望单行但返回多行
140
+ "PGRST200",
141
+ // 外键关系查找失败
142
+ "PGRST204"
143
+ // 响应中找不到列
144
+ ];
120
145
  function isPgRestRequest(url, patterns) {
121
146
  return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
122
147
  }
@@ -141,6 +166,32 @@ function parsePgRestErrorBody(body) {
141
166
  };
142
167
  }
143
168
 
169
+ // src/capture/auth.ts
170
+ var DEFAULT_AUTH_URL_PATTERNS = [/\/auth\/v1\//];
171
+ var DEFAULT_AUTH_WHITELIST_CODES = ["unexpected_failure"];
172
+ function isAuthRequest(url, patterns) {
173
+ return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
174
+ }
175
+ function parseAuthErrorBody(body) {
176
+ if (!body) return null;
177
+ let parsed;
178
+ try {
179
+ parsed = JSON.parse(body);
180
+ } catch {
181
+ return null;
182
+ }
183
+ if (typeof parsed !== "object") return null;
184
+ if (parsed === null) return null;
185
+ const p = parsed;
186
+ if (typeof p["code"] !== "string") return null;
187
+ if (typeof p["message"] !== "string") return null;
188
+ return {
189
+ code: p["code"],
190
+ message: p["message"],
191
+ requestId: typeof p["requestId"] === "string" ? p["requestId"] : null
192
+ };
193
+ }
194
+
144
195
  // src/capture/network.ts
145
196
  var BODY_TRUNCATE_LIMIT = 2e3;
146
197
  var DEFAULT_IGNORE_URLS = [
@@ -241,6 +292,8 @@ function createNetworkObserver(opts, subscribers) {
241
292
  const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
242
293
  const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
243
294
  const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
295
+ const authPatterns = opts.authUrlPatterns ?? DEFAULT_AUTH_URL_PATTERNS;
296
+ const authWhitelistCodes = opts.authWhitelistCodes ?? DEFAULT_AUTH_WHITELIST_CODES;
244
297
  function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
245
298
  if (statusCode == null) return;
246
299
  if (!isPgRestRequest(url, pgRestPatterns)) return;
@@ -261,6 +314,26 @@ function createNetworkObserver(opts, subscribers) {
261
314
  };
262
315
  for (const sub of subscribers) sub.onPgRestWhitelistError(info);
263
316
  }
317
+ function tryDispatchAuthWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
318
+ if (statusCode == null) return;
319
+ if (!isAuthRequest(url, authPatterns)) return;
320
+ const authError = parseAuthErrorBody(responseBody);
321
+ if (!authError) return;
322
+ if (authWhitelistCodes.length > 0 && !authWhitelistCodes.includes(authError.code)) return;
323
+ const allBreadcrumbs = (0, import_core3.getIsolationScope)().getScopeData().breadcrumbs;
324
+ const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
325
+ const info = {
326
+ type: requestType,
327
+ method,
328
+ url,
329
+ statusCode,
330
+ authError,
331
+ requestBody,
332
+ responseBody,
333
+ precedingBreadcrumbs
334
+ };
335
+ for (const sub of subscribers) sub.onAuthWhitelistError(info);
336
+ }
264
337
  function makeHandler(requestType) {
265
338
  return (info) => {
266
339
  for (const sub of subscribers) void sub.onNetworkRequest(info);
@@ -272,6 +345,14 @@ function createNetworkObserver(opts, subscribers) {
272
345
  info.requestBody,
273
346
  info.responseBody
274
347
  );
348
+ tryDispatchAuthWhitelistError(
349
+ requestType,
350
+ info.method,
351
+ info.url,
352
+ info.statusCode,
353
+ info.requestBody,
354
+ info.responseBody
355
+ );
275
356
  };
276
357
  }
277
358
  return {
@@ -380,54 +461,8 @@ var PostMessageSubscriber = class {
380
461
  };
381
462
  this.send(msg, "*");
382
463
  }
383
- onNetworkRequest({ method, url, statusCode, requestBody, responseBody, isError, cause }) {
384
- const isClientError = statusCode != null && statusCode >= 400 && statusCode < 500;
385
- if (!isError && !isClientError) return;
386
- const urlPath = extractUrlPath(url);
387
- if (isError) {
388
- const causeDesc = cause != null ? String(cause) : "NetworkError";
389
- const errorMessage = `${method} ${urlPath} failed: ${causeDesc}`;
390
- const msg = {
391
- type: "GLOBAL_ERROR",
392
- message: errorMessage,
393
- error: cause != null ? { name: cause.name, message: cause.message, stack: cause.stack } : null,
394
- errorData: {
395
- name: cause?.name ?? "NetworkError",
396
- message: cause?.message ?? causeDesc,
397
- stack: cause?.stack ?? null,
398
- fileName: url,
399
- lineNumber: 0,
400
- columnNumber: 0,
401
- httpPath: urlPath,
402
- httpStatus: 0,
403
- httpMethod: method,
404
- requestBody: requestBody ?? "",
405
- responseBody: ""
406
- }
407
- };
408
- this.send(msg, "*");
409
- } else {
410
- const errorMessage = `${method} ${urlPath} -> ${statusCode}`;
411
- const msg = {
412
- type: "GLOBAL_ERROR",
413
- message: errorMessage,
414
- error: null,
415
- errorData: {
416
- name: "HTTPClientError",
417
- message: errorMessage,
418
- stack: null,
419
- fileName: url,
420
- lineNumber: 0,
421
- columnNumber: 0,
422
- httpPath: urlPath,
423
- httpStatus: statusCode,
424
- httpMethod: method,
425
- requestBody: requestBody ?? "",
426
- responseBody: responseBody ?? ""
427
- }
428
- };
429
- this.send(msg, "*");
430
- }
464
+ // postMessage 场景不需要上报 network 日志
465
+ onNetworkRequest(_info) {
431
466
  }
432
467
  onPgRestWhitelistError({ type, method, url, statusCode, pgRestError, requestBody, responseBody }) {
433
468
  const urlPath = extractUrlPath(url);
@@ -457,6 +492,33 @@ var PostMessageSubscriber = class {
457
492
  };
458
493
  this.send(msg, "*");
459
494
  }
495
+ onAuthWhitelistError({ type, method, url, statusCode, authError, requestBody, responseBody }) {
496
+ const urlPath = extractUrlPath(url);
497
+ const code = authError.code;
498
+ const errorMessage = `${method} ${urlPath} -> ${statusCode} [Auth:${code}]`;
499
+ const msg = {
500
+ type: "GLOBAL_ERROR",
501
+ message: errorMessage,
502
+ error: null,
503
+ errorData: {
504
+ name: "AuthWhitelistError",
505
+ message: authError.message,
506
+ stack: null,
507
+ fileName: url,
508
+ lineNumber: 0,
509
+ columnNumber: 0,
510
+ httpPath: urlPath,
511
+ httpStatus: statusCode,
512
+ httpMethod: method,
513
+ requestBody: requestBody ?? "",
514
+ responseBody,
515
+ authCode: code,
516
+ authRequestId: authError.requestId ?? void 0,
517
+ requestType: type
518
+ }
519
+ };
520
+ this.send(msg, "*");
521
+ }
460
522
  };
461
523
  function createPostMessageSubscriber(send) {
462
524
  return new PostMessageSubscriber(send);
@@ -472,11 +534,15 @@ function createNoopSubscriber() {
472
534
  onNetworkRequest() {
473
535
  },
474
536
  onPgRestWhitelistError() {
537
+ },
538
+ onAuthWhitelistError() {
475
539
  }
476
540
  };
477
541
  }
478
542
  // Annotate the CommonJS export names for ESM import in node:
479
543
  0 && (module.exports = {
544
+ DEFAULT_AUTH_URL_PATTERNS,
545
+ DEFAULT_AUTH_WHITELIST_CODES,
480
546
  DEFAULT_CONSOLE_FILTER_KEYWORDS,
481
547
  DEFAULT_IGNORE_URLS,
482
548
  DEFAULT_PGREST_URL_PATTERNS,
package/dist/index.d.cts CHANGED
@@ -24,6 +24,29 @@ interface IPgRestErrorBody {
24
24
  hint: string | null;
25
25
  }
26
26
 
27
+ /** Supabase Auth 响应检测与错误体解析。 */
28
+ /**
29
+ * 默认匹配 Supabase Auth 的标准路径前缀。
30
+ * 可通过 ISentryCaptureOptions.authUrlPatterns 覆盖。
31
+ */
32
+ declare const DEFAULT_AUTH_URL_PATTERNS: Array<string | RegExp>;
33
+ /**
34
+ * 白名单 Auth error code 列表。
35
+ * 命中白名单的响应才触发 onAuthWhitelistError。
36
+ *
37
+ * TODO: 根据业务需要补充具体 code。格式示例:
38
+ * - 'email_not_confirmed'(邮箱未确认)
39
+ * - 'invalid_email'(邮箱格式无效)
40
+ * - 'unexpected_failure'(未知失败)
41
+ */
42
+ declare const DEFAULT_AUTH_WHITELIST_CODES: string[];
43
+ /** Supabase Auth 标准错误响应体结构(HTTP 4xx/5xx 时返回)。 */
44
+ interface IAuthErrorBody {
45
+ requestId: string | null;
46
+ code: string;
47
+ message: string;
48
+ }
49
+
27
50
  /** Sentry 捕获模块共享类型定义。 */
28
51
 
29
52
  interface IErrorCaptureInfo {
@@ -92,6 +115,25 @@ interface IPgRestWhitelistErrorInfo {
92
115
  */
93
116
  precedingBreadcrumbs: Breadcrumb[];
94
117
  }
118
+ /**
119
+ * 捕获到 Auth 白名单错误时的事件数据。
120
+ * 由 network observer 在响应体符合 Auth 结构且 code 命中白名单时构造并派发。
121
+ */
122
+ interface IAuthWhitelistErrorInfo {
123
+ /** 请求类型 */
124
+ type: 'fetch' | 'xhr';
125
+ method: string;
126
+ url: string;
127
+ statusCode: number;
128
+ /** 解析后的 Auth 标准错误体 */
129
+ authError: IAuthErrorBody;
130
+ /** 原始请求体文本 */
131
+ requestBody: string | undefined;
132
+ /** 原始响应体文本 */
133
+ responseBody: string;
134
+ /** 本次请求触发前 scope 中已积累的 breadcrumb 链路,用于还原用户操作路径。 */
135
+ precedingBreadcrumbs: Breadcrumb[];
136
+ }
95
137
  /** SentryCapture 的初始化选项,仅包含过滤与端点配置;事件处理通过 subscribers 注入。 */
96
138
  interface ISentryCaptureOptions {
97
139
  /**
@@ -126,6 +168,15 @@ interface ISentryCaptureOptions {
126
168
  * 未配置时取全部(受 Sentry `maxBreadcrumbs` 上限约束,默认最多 100 条)。
127
169
  */
128
170
  precedingBreadcrumbsLimit?: number;
171
+ /**
172
+ * 匹配 Auth 服务端点的 URL 模式(可选)。
173
+ * 命中模式的请求会额外做 Auth 响应体解析,检查是否触发白名单错误事件。
174
+ */
175
+ authUrlPatterns?: Array<string | RegExp>;
176
+ /**
177
+ * Auth error 白名单 error code 列表(可选,默认为 `DEFAULT_AUTH_WHITELIST_CODES`)。
178
+ */
179
+ authWhitelistCodes?: string[];
129
180
  }
130
181
 
131
182
  /** 捕获事件订阅者接口与消息类型定义。 */
@@ -151,6 +202,11 @@ interface ICaptureSubscriber {
151
202
  * 适用于需要对特定数据库业务错误(如唯一键冲突、JWT 过期)做专项处理的场景。
152
203
  */
153
204
  onPgRestWhitelistError(info: IPgRestWhitelistErrorInfo): void;
205
+ /**
206
+ * 请求命中 Auth 端点,且响应体 code 字段在白名单中时触发。
207
+ * 适用于需要对特定认证错误(如邮箱未确认、格式无效)做专项处理的场景。
208
+ */
209
+ onAuthWhitelistError(info: IAuthWhitelistErrorInfo): void;
154
210
  }
155
211
  /** postMessage 发送函数,可注入替换(便于测试)。 */
156
212
  interface GlobalErrorMessage {
@@ -178,6 +234,9 @@ interface GlobalErrorMessage {
178
234
  pgRestDetails?: string;
179
235
  pgRestHint?: string;
180
236
  requestType?: 'fetch' | 'xhr';
237
+ /** AuthWhitelistError 专属字段 */
238
+ authCode?: string;
239
+ authRequestId?: string;
181
240
  };
182
241
  }
183
242
  type PostMessageFn = (message: GlobalErrorMessage, targetOrigin: string) => void;
@@ -235,4 +294,4 @@ declare function createPostMessageSubscriber(send?: PostMessageFn): ICaptureSubs
235
294
  /** 创建一个什么都不做的订阅者,方便测试时按需 spy 或替换单个方法。 */
236
295
  declare function createNoopSubscriber(): ICaptureSubscriber;
237
296
 
238
- export { DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS, DEFAULT_PGREST_URL_PATTERNS, DEFAULT_PGREST_WHITELIST_CODES, type GlobalErrorMessage, type ICaptureSubscriber, type IConsoleCaptureInfo, type IErrorCaptureInfo, type INetworkRequestInfo, type IPgRestErrorBody, type IPgRestWhitelistErrorInfo, type ISentryCaptureOptions, type PostMessageFn, SentryCapture, createNoopSubscriber, createPostMessageSubscriber };
297
+ export { DEFAULT_AUTH_URL_PATTERNS, DEFAULT_AUTH_WHITELIST_CODES, DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS, DEFAULT_PGREST_URL_PATTERNS, DEFAULT_PGREST_WHITELIST_CODES, type GlobalErrorMessage, type IAuthErrorBody, type IAuthWhitelistErrorInfo, type ICaptureSubscriber, type IConsoleCaptureInfo, type IErrorCaptureInfo, type INetworkRequestInfo, type IPgRestErrorBody, type IPgRestWhitelistErrorInfo, type ISentryCaptureOptions, type PostMessageFn, SentryCapture, createNoopSubscriber, createPostMessageSubscriber };
package/dist/index.d.ts CHANGED
@@ -24,6 +24,29 @@ interface IPgRestErrorBody {
24
24
  hint: string | null;
25
25
  }
26
26
 
27
+ /** Supabase Auth 响应检测与错误体解析。 */
28
+ /**
29
+ * 默认匹配 Supabase Auth 的标准路径前缀。
30
+ * 可通过 ISentryCaptureOptions.authUrlPatterns 覆盖。
31
+ */
32
+ declare const DEFAULT_AUTH_URL_PATTERNS: Array<string | RegExp>;
33
+ /**
34
+ * 白名单 Auth error code 列表。
35
+ * 命中白名单的响应才触发 onAuthWhitelistError。
36
+ *
37
+ * TODO: 根据业务需要补充具体 code。格式示例:
38
+ * - 'email_not_confirmed'(邮箱未确认)
39
+ * - 'invalid_email'(邮箱格式无效)
40
+ * - 'unexpected_failure'(未知失败)
41
+ */
42
+ declare const DEFAULT_AUTH_WHITELIST_CODES: string[];
43
+ /** Supabase Auth 标准错误响应体结构(HTTP 4xx/5xx 时返回)。 */
44
+ interface IAuthErrorBody {
45
+ requestId: string | null;
46
+ code: string;
47
+ message: string;
48
+ }
49
+
27
50
  /** Sentry 捕获模块共享类型定义。 */
28
51
 
29
52
  interface IErrorCaptureInfo {
@@ -92,6 +115,25 @@ interface IPgRestWhitelistErrorInfo {
92
115
  */
93
116
  precedingBreadcrumbs: Breadcrumb[];
94
117
  }
118
+ /**
119
+ * 捕获到 Auth 白名单错误时的事件数据。
120
+ * 由 network observer 在响应体符合 Auth 结构且 code 命中白名单时构造并派发。
121
+ */
122
+ interface IAuthWhitelistErrorInfo {
123
+ /** 请求类型 */
124
+ type: 'fetch' | 'xhr';
125
+ method: string;
126
+ url: string;
127
+ statusCode: number;
128
+ /** 解析后的 Auth 标准错误体 */
129
+ authError: IAuthErrorBody;
130
+ /** 原始请求体文本 */
131
+ requestBody: string | undefined;
132
+ /** 原始响应体文本 */
133
+ responseBody: string;
134
+ /** 本次请求触发前 scope 中已积累的 breadcrumb 链路,用于还原用户操作路径。 */
135
+ precedingBreadcrumbs: Breadcrumb[];
136
+ }
95
137
  /** SentryCapture 的初始化选项,仅包含过滤与端点配置;事件处理通过 subscribers 注入。 */
96
138
  interface ISentryCaptureOptions {
97
139
  /**
@@ -126,6 +168,15 @@ interface ISentryCaptureOptions {
126
168
  * 未配置时取全部(受 Sentry `maxBreadcrumbs` 上限约束,默认最多 100 条)。
127
169
  */
128
170
  precedingBreadcrumbsLimit?: number;
171
+ /**
172
+ * 匹配 Auth 服务端点的 URL 模式(可选)。
173
+ * 命中模式的请求会额外做 Auth 响应体解析,检查是否触发白名单错误事件。
174
+ */
175
+ authUrlPatterns?: Array<string | RegExp>;
176
+ /**
177
+ * Auth error 白名单 error code 列表(可选,默认为 `DEFAULT_AUTH_WHITELIST_CODES`)。
178
+ */
179
+ authWhitelistCodes?: string[];
129
180
  }
130
181
 
131
182
  /** 捕获事件订阅者接口与消息类型定义。 */
@@ -151,6 +202,11 @@ interface ICaptureSubscriber {
151
202
  * 适用于需要对特定数据库业务错误(如唯一键冲突、JWT 过期)做专项处理的场景。
152
203
  */
153
204
  onPgRestWhitelistError(info: IPgRestWhitelistErrorInfo): void;
205
+ /**
206
+ * 请求命中 Auth 端点,且响应体 code 字段在白名单中时触发。
207
+ * 适用于需要对特定认证错误(如邮箱未确认、格式无效)做专项处理的场景。
208
+ */
209
+ onAuthWhitelistError(info: IAuthWhitelistErrorInfo): void;
154
210
  }
155
211
  /** postMessage 发送函数,可注入替换(便于测试)。 */
156
212
  interface GlobalErrorMessage {
@@ -178,6 +234,9 @@ interface GlobalErrorMessage {
178
234
  pgRestDetails?: string;
179
235
  pgRestHint?: string;
180
236
  requestType?: 'fetch' | 'xhr';
237
+ /** AuthWhitelistError 专属字段 */
238
+ authCode?: string;
239
+ authRequestId?: string;
181
240
  };
182
241
  }
183
242
  type PostMessageFn = (message: GlobalErrorMessage, targetOrigin: string) => void;
@@ -235,4 +294,4 @@ declare function createPostMessageSubscriber(send?: PostMessageFn): ICaptureSubs
235
294
  /** 创建一个什么都不做的订阅者,方便测试时按需 spy 或替换单个方法。 */
236
295
  declare function createNoopSubscriber(): ICaptureSubscriber;
237
296
 
238
- export { DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS, DEFAULT_PGREST_URL_PATTERNS, DEFAULT_PGREST_WHITELIST_CODES, type GlobalErrorMessage, type ICaptureSubscriber, type IConsoleCaptureInfo, type IErrorCaptureInfo, type INetworkRequestInfo, type IPgRestErrorBody, type IPgRestWhitelistErrorInfo, type ISentryCaptureOptions, type PostMessageFn, SentryCapture, createNoopSubscriber, createPostMessageSubscriber };
297
+ export { DEFAULT_AUTH_URL_PATTERNS, DEFAULT_AUTH_WHITELIST_CODES, DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS, DEFAULT_PGREST_URL_PATTERNS, DEFAULT_PGREST_WHITELIST_CODES, type GlobalErrorMessage, type IAuthErrorBody, type IAuthWhitelistErrorInfo, type ICaptureSubscriber, type IConsoleCaptureInfo, type IErrorCaptureInfo, type INetworkRequestInfo, type IPgRestErrorBody, type IPgRestWhitelistErrorInfo, type ISentryCaptureOptions, type PostMessageFn, SentryCapture, createNoopSubscriber, createPostMessageSubscriber };
package/dist/index.js CHANGED
@@ -86,7 +86,30 @@ function getFetchRequestArgBody(fetchArgs = []) {
86
86
 
87
87
  // src/capture/pgrest.ts
88
88
  var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
89
- var DEFAULT_PGREST_WHITELIST_CODES = [];
89
+ var DEFAULT_PGREST_WHITELIST_CODES = [
90
+ "22P02",
91
+ // 无效的文本表示(如类型转换失败)
92
+ "42703",
93
+ // 未定义的列
94
+ "42P01",
95
+ // 未定义的表
96
+ "42501",
97
+ // 权限不足
98
+ "23503",
99
+ // 外键约束失败
100
+ "42P17",
101
+ // 对象已存在
102
+ "22008",
103
+ // 日期时间字段值超出范围
104
+ "23505",
105
+ // 唯一键冲突
106
+ "PGRST116",
107
+ // 期望单行但返回多行
108
+ "PGRST200",
109
+ // 外键关系查找失败
110
+ "PGRST204"
111
+ // 响应中找不到列
112
+ ];
90
113
  function isPgRestRequest(url, patterns) {
91
114
  return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
92
115
  }
@@ -111,6 +134,32 @@ function parsePgRestErrorBody(body) {
111
134
  };
112
135
  }
113
136
 
137
+ // src/capture/auth.ts
138
+ var DEFAULT_AUTH_URL_PATTERNS = [/\/auth\/v1\//];
139
+ var DEFAULT_AUTH_WHITELIST_CODES = ["unexpected_failure"];
140
+ function isAuthRequest(url, patterns) {
141
+ return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
142
+ }
143
+ function parseAuthErrorBody(body) {
144
+ if (!body) return null;
145
+ let parsed;
146
+ try {
147
+ parsed = JSON.parse(body);
148
+ } catch {
149
+ return null;
150
+ }
151
+ if (typeof parsed !== "object") return null;
152
+ if (parsed === null) return null;
153
+ const p = parsed;
154
+ if (typeof p["code"] !== "string") return null;
155
+ if (typeof p["message"] !== "string") return null;
156
+ return {
157
+ code: p["code"],
158
+ message: p["message"],
159
+ requestId: typeof p["requestId"] === "string" ? p["requestId"] : null
160
+ };
161
+ }
162
+
114
163
  // src/capture/network.ts
115
164
  var BODY_TRUNCATE_LIMIT = 2e3;
116
165
  var DEFAULT_IGNORE_URLS = [
@@ -211,6 +260,8 @@ function createNetworkObserver(opts, subscribers) {
211
260
  const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
212
261
  const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
213
262
  const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
263
+ const authPatterns = opts.authUrlPatterns ?? DEFAULT_AUTH_URL_PATTERNS;
264
+ const authWhitelistCodes = opts.authWhitelistCodes ?? DEFAULT_AUTH_WHITELIST_CODES;
214
265
  function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
215
266
  if (statusCode == null) return;
216
267
  if (!isPgRestRequest(url, pgRestPatterns)) return;
@@ -231,6 +282,26 @@ function createNetworkObserver(opts, subscribers) {
231
282
  };
232
283
  for (const sub of subscribers) sub.onPgRestWhitelistError(info);
233
284
  }
285
+ function tryDispatchAuthWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
286
+ if (statusCode == null) return;
287
+ if (!isAuthRequest(url, authPatterns)) return;
288
+ const authError = parseAuthErrorBody(responseBody);
289
+ if (!authError) return;
290
+ if (authWhitelistCodes.length > 0 && !authWhitelistCodes.includes(authError.code)) return;
291
+ const allBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs;
292
+ const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
293
+ const info = {
294
+ type: requestType,
295
+ method,
296
+ url,
297
+ statusCode,
298
+ authError,
299
+ requestBody,
300
+ responseBody,
301
+ precedingBreadcrumbs
302
+ };
303
+ for (const sub of subscribers) sub.onAuthWhitelistError(info);
304
+ }
234
305
  function makeHandler(requestType) {
235
306
  return (info) => {
236
307
  for (const sub of subscribers) void sub.onNetworkRequest(info);
@@ -242,6 +313,14 @@ function createNetworkObserver(opts, subscribers) {
242
313
  info.requestBody,
243
314
  info.responseBody
244
315
  );
316
+ tryDispatchAuthWhitelistError(
317
+ requestType,
318
+ info.method,
319
+ info.url,
320
+ info.statusCode,
321
+ info.requestBody,
322
+ info.responseBody
323
+ );
245
324
  };
246
325
  }
247
326
  return {
@@ -350,54 +429,8 @@ var PostMessageSubscriber = class {
350
429
  };
351
430
  this.send(msg, "*");
352
431
  }
353
- onNetworkRequest({ method, url, statusCode, requestBody, responseBody, isError, cause }) {
354
- const isClientError = statusCode != null && statusCode >= 400 && statusCode < 500;
355
- if (!isError && !isClientError) return;
356
- const urlPath = extractUrlPath(url);
357
- if (isError) {
358
- const causeDesc = cause != null ? String(cause) : "NetworkError";
359
- const errorMessage = `${method} ${urlPath} failed: ${causeDesc}`;
360
- const msg = {
361
- type: "GLOBAL_ERROR",
362
- message: errorMessage,
363
- error: cause != null ? { name: cause.name, message: cause.message, stack: cause.stack } : null,
364
- errorData: {
365
- name: cause?.name ?? "NetworkError",
366
- message: cause?.message ?? causeDesc,
367
- stack: cause?.stack ?? null,
368
- fileName: url,
369
- lineNumber: 0,
370
- columnNumber: 0,
371
- httpPath: urlPath,
372
- httpStatus: 0,
373
- httpMethod: method,
374
- requestBody: requestBody ?? "",
375
- responseBody: ""
376
- }
377
- };
378
- this.send(msg, "*");
379
- } else {
380
- const errorMessage = `${method} ${urlPath} -> ${statusCode}`;
381
- const msg = {
382
- type: "GLOBAL_ERROR",
383
- message: errorMessage,
384
- error: null,
385
- errorData: {
386
- name: "HTTPClientError",
387
- message: errorMessage,
388
- stack: null,
389
- fileName: url,
390
- lineNumber: 0,
391
- columnNumber: 0,
392
- httpPath: urlPath,
393
- httpStatus: statusCode,
394
- httpMethod: method,
395
- requestBody: requestBody ?? "",
396
- responseBody: responseBody ?? ""
397
- }
398
- };
399
- this.send(msg, "*");
400
- }
432
+ // postMessage 场景不需要上报 network 日志
433
+ onNetworkRequest(_info) {
401
434
  }
402
435
  onPgRestWhitelistError({ type, method, url, statusCode, pgRestError, requestBody, responseBody }) {
403
436
  const urlPath = extractUrlPath(url);
@@ -427,6 +460,33 @@ var PostMessageSubscriber = class {
427
460
  };
428
461
  this.send(msg, "*");
429
462
  }
463
+ onAuthWhitelistError({ type, method, url, statusCode, authError, requestBody, responseBody }) {
464
+ const urlPath = extractUrlPath(url);
465
+ const code = authError.code;
466
+ const errorMessage = `${method} ${urlPath} -> ${statusCode} [Auth:${code}]`;
467
+ const msg = {
468
+ type: "GLOBAL_ERROR",
469
+ message: errorMessage,
470
+ error: null,
471
+ errorData: {
472
+ name: "AuthWhitelistError",
473
+ message: authError.message,
474
+ stack: null,
475
+ fileName: url,
476
+ lineNumber: 0,
477
+ columnNumber: 0,
478
+ httpPath: urlPath,
479
+ httpStatus: statusCode,
480
+ httpMethod: method,
481
+ requestBody: requestBody ?? "",
482
+ responseBody,
483
+ authCode: code,
484
+ authRequestId: authError.requestId ?? void 0,
485
+ requestType: type
486
+ }
487
+ };
488
+ this.send(msg, "*");
489
+ }
430
490
  };
431
491
  function createPostMessageSubscriber(send) {
432
492
  return new PostMessageSubscriber(send);
@@ -442,10 +502,14 @@ function createNoopSubscriber() {
442
502
  onNetworkRequest() {
443
503
  },
444
504
  onPgRestWhitelistError() {
505
+ },
506
+ onAuthWhitelistError() {
445
507
  }
446
508
  };
447
509
  }
448
510
  export {
511
+ DEFAULT_AUTH_URL_PATTERNS,
512
+ DEFAULT_AUTH_WHITELIST_CODES,
449
513
  DEFAULT_CONSOLE_FILTER_KEYWORDS,
450
514
  DEFAULT_IGNORE_URLS,
451
515
  DEFAULT_PGREST_URL_PATTERNS,
@@ -89,7 +89,30 @@ function getFetchRequestArgBody(fetchArgs = []) {
89
89
 
90
90
  // src/capture/pgrest.ts
91
91
  var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
92
- var DEFAULT_PGREST_WHITELIST_CODES = [];
92
+ var DEFAULT_PGREST_WHITELIST_CODES = [
93
+ "22P02",
94
+ // 无效的文本表示(如类型转换失败)
95
+ "42703",
96
+ // 未定义的列
97
+ "42P01",
98
+ // 未定义的表
99
+ "42501",
100
+ // 权限不足
101
+ "23503",
102
+ // 外键约束失败
103
+ "42P17",
104
+ // 对象已存在
105
+ "22008",
106
+ // 日期时间字段值超出范围
107
+ "23505",
108
+ // 唯一键冲突
109
+ "PGRST116",
110
+ // 期望单行但返回多行
111
+ "PGRST200",
112
+ // 外键关系查找失败
113
+ "PGRST204"
114
+ // 响应中找不到列
115
+ ];
93
116
  function isPgRestRequest(url, patterns) {
94
117
  return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
95
118
  }
@@ -114,6 +137,32 @@ function parsePgRestErrorBody(body) {
114
137
  };
115
138
  }
116
139
 
140
+ // src/capture/auth.ts
141
+ var DEFAULT_AUTH_URL_PATTERNS = [/\/auth\/v1\//];
142
+ var DEFAULT_AUTH_WHITELIST_CODES = ["unexpected_failure"];
143
+ function isAuthRequest(url, patterns) {
144
+ return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
145
+ }
146
+ function parseAuthErrorBody(body) {
147
+ if (!body) return null;
148
+ let parsed;
149
+ try {
150
+ parsed = JSON.parse(body);
151
+ } catch {
152
+ return null;
153
+ }
154
+ if (typeof parsed !== "object") return null;
155
+ if (parsed === null) return null;
156
+ const p = parsed;
157
+ if (typeof p["code"] !== "string") return null;
158
+ if (typeof p["message"] !== "string") return null;
159
+ return {
160
+ code: p["code"],
161
+ message: p["message"],
162
+ requestId: typeof p["requestId"] === "string" ? p["requestId"] : null
163
+ };
164
+ }
165
+
117
166
  // src/capture/network.ts
118
167
  var BODY_TRUNCATE_LIMIT = 2e3;
119
168
  var DEFAULT_IGNORE_URLS = [
@@ -214,6 +263,8 @@ function createNetworkObserver(opts, subscribers) {
214
263
  const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
215
264
  const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
216
265
  const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
266
+ const authPatterns = opts.authUrlPatterns ?? DEFAULT_AUTH_URL_PATTERNS;
267
+ const authWhitelistCodes = opts.authWhitelistCodes ?? DEFAULT_AUTH_WHITELIST_CODES;
217
268
  function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
218
269
  if (statusCode == null) return;
219
270
  if (!isPgRestRequest(url, pgRestPatterns)) return;
@@ -234,6 +285,26 @@ function createNetworkObserver(opts, subscribers) {
234
285
  };
235
286
  for (const sub of subscribers) sub.onPgRestWhitelistError(info);
236
287
  }
288
+ function tryDispatchAuthWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
289
+ if (statusCode == null) return;
290
+ if (!isAuthRequest(url, authPatterns)) return;
291
+ const authError = parseAuthErrorBody(responseBody);
292
+ if (!authError) return;
293
+ if (authWhitelistCodes.length > 0 && !authWhitelistCodes.includes(authError.code)) return;
294
+ const allBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs;
295
+ const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
296
+ const info = {
297
+ type: requestType,
298
+ method,
299
+ url,
300
+ statusCode,
301
+ authError,
302
+ requestBody,
303
+ responseBody,
304
+ precedingBreadcrumbs
305
+ };
306
+ for (const sub of subscribers) sub.onAuthWhitelistError(info);
307
+ }
237
308
  function makeHandler(requestType) {
238
309
  return (info) => {
239
310
  for (const sub of subscribers) void sub.onNetworkRequest(info);
@@ -245,6 +316,14 @@ function createNetworkObserver(opts, subscribers) {
245
316
  info.requestBody,
246
317
  info.responseBody
247
318
  );
319
+ tryDispatchAuthWhitelistError(
320
+ requestType,
321
+ info.method,
322
+ info.url,
323
+ info.statusCode,
324
+ info.requestBody,
325
+ info.responseBody
326
+ );
248
327
  };
249
328
  }
250
329
  return {
@@ -353,54 +432,8 @@ var PostMessageSubscriber = class {
353
432
  };
354
433
  this.send(msg, "*");
355
434
  }
356
- onNetworkRequest({ method, url, statusCode, requestBody, responseBody, isError, cause }) {
357
- const isClientError = statusCode != null && statusCode >= 400 && statusCode < 500;
358
- if (!isError && !isClientError) return;
359
- const urlPath = extractUrlPath(url);
360
- if (isError) {
361
- const causeDesc = cause != null ? String(cause) : "NetworkError";
362
- const errorMessage = `${method} ${urlPath} failed: ${causeDesc}`;
363
- const msg = {
364
- type: "GLOBAL_ERROR",
365
- message: errorMessage,
366
- error: cause != null ? { name: cause.name, message: cause.message, stack: cause.stack } : null,
367
- errorData: {
368
- name: cause?.name ?? "NetworkError",
369
- message: cause?.message ?? causeDesc,
370
- stack: cause?.stack ?? null,
371
- fileName: url,
372
- lineNumber: 0,
373
- columnNumber: 0,
374
- httpPath: urlPath,
375
- httpStatus: 0,
376
- httpMethod: method,
377
- requestBody: requestBody ?? "",
378
- responseBody: ""
379
- }
380
- };
381
- this.send(msg, "*");
382
- } else {
383
- const errorMessage = `${method} ${urlPath} -> ${statusCode}`;
384
- const msg = {
385
- type: "GLOBAL_ERROR",
386
- message: errorMessage,
387
- error: null,
388
- errorData: {
389
- name: "HTTPClientError",
390
- message: errorMessage,
391
- stack: null,
392
- fileName: url,
393
- lineNumber: 0,
394
- columnNumber: 0,
395
- httpPath: urlPath,
396
- httpStatus: statusCode,
397
- httpMethod: method,
398
- requestBody: requestBody ?? "",
399
- responseBody: responseBody ?? ""
400
- }
401
- };
402
- this.send(msg, "*");
403
- }
435
+ // postMessage 场景不需要上报 network 日志
436
+ onNetworkRequest(_info) {
404
437
  }
405
438
  onPgRestWhitelistError({ type, method, url, statusCode, pgRestError, requestBody, responseBody }) {
406
439
  const urlPath = extractUrlPath(url);
@@ -430,6 +463,33 @@ var PostMessageSubscriber = class {
430
463
  };
431
464
  this.send(msg, "*");
432
465
  }
466
+ onAuthWhitelistError({ type, method, url, statusCode, authError, requestBody, responseBody }) {
467
+ const urlPath = extractUrlPath(url);
468
+ const code = authError.code;
469
+ const errorMessage = `${method} ${urlPath} -> ${statusCode} [Auth:${code}]`;
470
+ const msg = {
471
+ type: "GLOBAL_ERROR",
472
+ message: errorMessage,
473
+ error: null,
474
+ errorData: {
475
+ name: "AuthWhitelistError",
476
+ message: authError.message,
477
+ stack: null,
478
+ fileName: url,
479
+ lineNumber: 0,
480
+ columnNumber: 0,
481
+ httpPath: urlPath,
482
+ httpStatus: statusCode,
483
+ httpMethod: method,
484
+ requestBody: requestBody ?? "",
485
+ responseBody,
486
+ authCode: code,
487
+ authRequestId: authError.requestId ?? void 0,
488
+ requestType: type
489
+ }
490
+ };
491
+ this.send(msg, "*");
492
+ }
433
493
  };
434
494
  function createPostMessageSubscriber(send) {
435
495
  return new PostMessageSubscriber(send);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miaoda-react-devkit",
3
- "version": "0.1.1-beta.5",
3
+ "version": "0.1.1-beta.7",
4
4
  "description": "React admin 应用开发工具集",
5
5
  "license": "MIT",
6
6
  "type": "module",