Commit d83421bc by liimin

fix(defaults): 仅在启用sentry时检查dsn必填参数

refactor(utils): 将generateUUID函数提取到utils模块
feat: 导出generateUUID函数供外部使用
chore: 更新项目名称为glsk-toolkit并添加开发依赖
1 parent e5eda7b3
{
"name": "glsk-fetch",
"name": "glsk-toolkit",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "glsk-fetch",
"name": "glsk-toolkit",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
......@@ -21,8 +21,10 @@
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.4",
"@types/crypto-js": "^4.2.2",
"cross-env": "^10.0.0",
"jest": "^29.7.0",
"rollup": "^4.12.0",
"rollup-plugin-analyzer": "^4.0.0",
"rollup-plugin-polyfill-node": "^0.13.0",
"tslib": "^2.8.1",
"typescript": "^5.9.2"
......@@ -1679,6 +1681,12 @@
"integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==",
"dev": true
},
"node_modules/@epic-web/invariant": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@epic-web/invariant/-/invariant-1.0.0.tgz",
"integrity": "sha512-lrTPqgvfFQtR/eY/qkIzp98OGdNJu0m5ji3q/nJI8v3SXkRKEnWiOxMmbvcSoAIzv/cGiuvRy57k4suKQSAdwA==",
"dev": true
},
"node_modules/@istanbuljs/load-nyc-config": {
"version": "1.1.0",
"resolved": "https://registry.npmmirror.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz",
......@@ -3258,6 +3266,23 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/cross-env": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/cross-env/-/cross-env-10.0.0.tgz",
"integrity": "sha512-aU8qlEK/nHYtVuN4p7UQgAwVljzMg8hB4YK5ThRqD2l/ziSnryncPNn7bMLt5cFYsKVKBh8HqLqyCoTupEUu7Q==",
"dev": true,
"dependencies": {
"@epic-web/invariant": "^1.0.0",
"cross-spawn": "^7.0.6"
},
"bin": {
"cross-env": "dist/bin/cross-env.js",
"cross-env-shell": "dist/bin/cross-env-shell.js"
},
"engines": {
"node": ">=20"
}
},
"node_modules/cross-spawn": {
"version": "7.0.6",
"resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.6.tgz",
......@@ -5162,6 +5187,15 @@
"fsevents": "~2.3.2"
}
},
"node_modules/rollup-plugin-analyzer": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/rollup-plugin-analyzer/-/rollup-plugin-analyzer-4.0.0.tgz",
"integrity": "sha512-LL9GEt3bkXp6Wa19SNR5MWcvHNMvuTFYg+eYBZN2OIFhSWN+pEJUQXEKu5BsOeABob3x9PDaLKW7w5iOJnsESQ==",
"dev": true,
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/rollup-plugin-polyfill-node": {
"version": "0.13.0",
"resolved": "https://registry.npmmirror.com/rollup-plugin-polyfill-node/-/rollup-plugin-polyfill-node-0.13.0.tgz",
......
......@@ -43,10 +43,9 @@ class Defaults implements RequestConfig {
*/
constructor(config: Partial<RequestConfig> = {}) {
// 检查必填参数
if (!config.sentry?.dsn) {
throw new Error("sentry.dsn is required for initialization");
if (config.sentry?.enable && !config.sentry?.dsn) {
throw new Error("sentry.dsn is required when sentry is enabled");
}
// 基础配置
this.baseURL = config.baseURL || "";
this.timeout = config.timeout || 5000;
......
......@@ -2,11 +2,12 @@ import { Request } from './core/request';
import Defaults from './core/defaults';
import * as Sentry from './monitoring/sentry';
import { simpleGet } from './utils/http';
import { generateUUID } from './utils';
import { initAbortControllerPolyfill } from './utils/polyfill';
// 初始化 AbortController polyfill(如果需要的话)
initAbortControllerPolyfill();
export { Request, Defaults, Sentry, simpleGet };
export { Request, Defaults, Sentry, simpleGet, generateUUID };
export type { RequestConfig, ResponseData, RequestHeaders } from './types';
export type { SentryOptions, ErrorReportOptions } from './monitoring/sentry';
\ No newline at end of file
import CryptoJS from 'crypto-js';
import { processGetSignatureParams, processPostSignatureParams } from '../utils';
import { processGetSignatureParams, processPostSignatureParams, generateUUID } from '../utils';
export interface SignatureOptions {
appId: string;
......@@ -17,33 +17,6 @@ export interface SignatureResult {
}
/**
* 生成UUID v4格式的随机字符串
* 使用更高效的实现方式
* @returns {string} UUID v4格式字符串
*/
function generateUUID(): string {
// 使用crypto API如果可用,否则回退到Math.random
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
// 设置版本号 (4) 和变体位
bytes[6] = (bytes[6] & 0x0f) | 0x40; // 版本4
bytes[8] = (bytes[8] & 0x3f) | 0x80; // 变体位
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
}
// 回退到原有实现
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
/**
* 验证签名参数
* @param options 签名选项
*/
......
......@@ -184,4 +184,31 @@ export function processPostSignatureParams(params: Record<string, any>): Record<
// JSON 模式直接返回对象
return processedResult as Record<string, string>;
}
/**
* 生成UUID v4格式的随机字符串
* 使用更高效的实现方式
* @returns {string} UUID v4格式字符串
*/
export function generateUUID(): string {
// 使用crypto API如果可用,否则回退到Math.random
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
// 设置版本号 (4) 和变体位
bytes[6] = (bytes[6] & 0x0f) | 0x40; // 版本4
bytes[8] = (bytes[8] & 0x3f) | 0x80; // 变体位
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
}
// 回退到原有实现
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!