Commit 8a5497cc by liimin

refactor(utils): 优化参数处理逻辑并移除废弃函数

- 移除废弃的通用参数处理函数 processParams,使用具体的处理函数替代
- 在签名模块中使用 Map 缓存参数处理策略,避免重复创建
- 统一各模块使用具体的参数处理函数,提高代码可读性
1 parent 7668fda9
import { processParams, ArrayProcessMode } from '../utils'; import { processParamsAsObjectIndex } from '../utils';
/** /**
* 构建完整URL * 构建完整URL
...@@ -16,7 +16,7 @@ export function buildURL(baseURL: string = '', url: string = '', params: Record< ...@@ -16,7 +16,7 @@ export function buildURL(baseURL: string = '', url: string = '', params: Record<
// if ((method.toUpperCase() === 'GET' || method.toUpperCase() === 'DELETE') && Object.keys(params).length) { // if ((method.toUpperCase() === 'GET' || method.toUpperCase() === 'DELETE') && Object.keys(params).length) {
if (Object.keys(params).length) { if (Object.keys(params).length) {
// 使用通用参数处理函数,采用对象索引模式处理数组,支持Java后端 // 使用通用参数处理函数,采用对象索引模式处理数组,支持Java后端
const processedParams = processParams(params, ArrayProcessMode.OBJECT_INDEX) as Array<{key: string, value: string}>; const processedParams = processParamsAsObjectIndex(params) as Array<{key: string, value: string}>;
// 构建查询字符串 // 构建查询字符串
const queryParts = processedParams.map(({ key, value }) => const queryParts = processedParams.map(({ key, value }) =>
`${encodeURIComponent(key)}=${encodeURIComponent(value)}` `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
......
import CryptoJS from 'crypto-js'; import CryptoJS from 'crypto-js';
import { stringify } from './libs';
import { processGetSignatureParams, processPostSignatureParams } from './utils'; import { processGetSignatureParams, processPostSignatureParams } from './utils';
export interface SignatureOptions { export interface SignatureOptions {
...@@ -83,6 +82,18 @@ function buildSignatureString(processedParams: Record<string, any>, noSignAttr: ...@@ -83,6 +82,18 @@ function buildSignatureString(processedParams: Record<string, any>, noSignAttr:
} }
/** /**
* 参数处理策略映射
* 避免每次调用generateSignature时都创建新的Map对象
*/
const PARAM_PROCESSORS = new Map([
['GET', processGetSignatureParams],
['DELETE', processGetSignatureParams],
['POST', processPostSignatureParams],
['PUT', processPostSignatureParams],
['PATCH', processPostSignatureParams]
]);
/**
* 生成签名 * 生成签名
* @param options 签名参数 * @param options 签名参数
* @returns 签名信息 * @returns 签名信息
...@@ -111,11 +122,10 @@ export function generateSignature(options: SignatureOptions): SignatureResult { ...@@ -111,11 +122,10 @@ export function generateSignature(options: SignatureOptions): SignatureResult {
...params ...params
}; };
// 根据请求方法使用相应的参数处理函数 // 根据请求方法选择相应的参数处理策略
const normalizedMethod = method.toUpperCase(); const normalizedMethod = method.toUpperCase();
const processedParams = normalizedMethod === 'GET' const processor = PARAM_PROCESSORS.get(normalizedMethod) || processPostSignatureParams;
? processGetSignatureParams(allParams) const processedParams = processor(allParams);
: processPostSignatureParams(allParams);
// 构建签名字符串 // 构建签名字符串
const stringToSign = buildSignatureString(processedParams, noSignAttr); const stringToSign = buildSignatureString(processedParams, noSignAttr);
......
...@@ -159,35 +159,12 @@ export function processParamsAsObjectIndex(params: Record<string, any>): Array<{ ...@@ -159,35 +159,12 @@ export function processParamsAsObjectIndex(params: Record<string, any>): Array<{
} }
/** /**
* 通用参数处理函数,支持不同的数组处理模式
* @param params 原始参数对象
* @param arrayMode 数组处理模式
* @returns 处理后的参数对象或键值对数组
* @deprecated 建议使用具体的处理函数:processParamsAsJson, processParamsAsIndex, processParamsAsObjectIndex
*/
export function processParams(
params: Record<string, any>,
arrayMode: ArrayProcessMode = ArrayProcessMode.JSON
): Record<string, string> | Array<{key: string, value: string}> {
if (arrayMode === ArrayProcessMode.INDEX) {
return processParamsAsIndex(params);
}
else if (arrayMode === ArrayProcessMode.OBJECT_INDEX) {
return processParamsAsObjectIndex(params);
}
else {
return processParamsAsJson(params);
}
}
/**
* 处理GET请求的签名参数,将数组展开为 key[index]=value 格式 * 处理GET请求的签名参数,将数组展开为 key[index]=value 格式
* @param params 原始参数对象 * @param params 原始参数对象
* @returns 处理后的参数对象 * @returns 处理后的参数对象
*/ */
export function processGetSignatureParams(params: Record<string, any>): Record<string, string> { export function processGetSignatureParams(params: Record<string, any>): Record<string, string> {
const processedResult = processParams(params, ArrayProcessMode.OBJECT_INDEX); const processedResult = processParamsAsObjectIndex(params);
// OBJECT_INDEX 模式返回的是数组,需要转换为对象 // OBJECT_INDEX 模式返回的是数组,需要转换为对象
const result: Record<string, string> = {}; const result: Record<string, string> = {};
...@@ -203,7 +180,7 @@ export function processGetSignatureParams(params: Record<string, any>): Record<s ...@@ -203,7 +180,7 @@ export function processGetSignatureParams(params: Record<string, any>): Record<s
* @returns 处理后的参数对象 * @returns 处理后的参数对象
*/ */
export function processPostSignatureParams(params: Record<string, any>): Record<string, string> { export function processPostSignatureParams(params: Record<string, any>): Record<string, string> {
const processedResult = processParams(params, ArrayProcessMode.JSON); const processedResult = processParamsAsJson(params);
// JSON 模式直接返回对象 // JSON 模式直接返回对象
return processedResult as Record<string, string>; return processedResult as Record<string, string>;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!