mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-12 02:06:37 +08:00
fix(access-logs): 修复概览域名筛选参数 hosts[] 被 Gin 忽略
Axios 默认序列化为 hosts[]=,Gin QueryArray("hosts") 读不到导致筛选失效;
后端兼容 hosts/hosts[],前端改为重复键序列化。
This commit is contained in:
@@ -28,6 +28,10 @@ const apiClient = axios.create({
|
||||
withCredentials: apiConfig.withCredentials,
|
||||
// Fail fast on slash redirect loops between Next dev proxy and Gin legacy routes.
|
||||
maxRedirects: 5,
|
||||
// Gin QueryArray("hosts") expects hosts=a&hosts=b, not hosts[]=a.
|
||||
paramsSerializer: {
|
||||
indexes: null,
|
||||
},
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Serialize query params for Gin-compatible repeated array keys.
|
||||
* Axios default uses hosts[]= which Gin QueryArray("hosts") ignores.
|
||||
*/
|
||||
export function serializeSearchParams(
|
||||
params?: Record<string, unknown>,
|
||||
): string {
|
||||
if (!params) return '';
|
||||
const search = new URLSearchParams();
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
if (value === undefined || value === null || value === '') continue;
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) {
|
||||
if (item === undefined || item === null || item === '') continue;
|
||||
search.append(key, String(item));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
search.append(key, String(value));
|
||||
}
|
||||
return search.toString();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { serializeSearchParams } from '@/lib/services/core/search-params';
|
||||
|
||||
describe('serializeSearchParams', () => {
|
||||
it('serializes hosts arrays as repeated keys for Gin QueryArray', () => {
|
||||
const query = serializeSearchParams({
|
||||
hours: 168,
|
||||
hosts: ['gist.arctel.de'],
|
||||
});
|
||||
expect(query).toBe('hours=168&hosts=gist.arctel.de');
|
||||
});
|
||||
|
||||
it('serializes multiple hosts without bracket notation', () => {
|
||||
const query = serializeSearchParams({
|
||||
hosts: ['a.example', 'b.example'],
|
||||
});
|
||||
expect(query).toBe('hosts=a.example&hosts=b.example');
|
||||
expect(query).not.toContain('hosts[]');
|
||||
expect(query).not.toContain('hosts%5B%5D');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user