feat(framework): config H5 URL query 身份来源
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import type { ChannelIdentity, IdentitySource } from '../identity.ts';
|
||||
|
||||
/** H5 模式:从 location.search 解析渠道身份覆盖。URLSearchParams 在浏览器/Cocos/Node 均可用。 */
|
||||
export function queryStringSource(search: string): IdentitySource {
|
||||
return () => {
|
||||
const q = new URLSearchParams(search);
|
||||
const out: Partial<ChannelIdentity> = {};
|
||||
const agentid = q.get('agentid'); if (agentid) out.agentid = agentid;
|
||||
const channelid = q.get('channelid'); if (channelid) out.channelid = channelid;
|
||||
const marketid = q.get('marketid'); if (marketid) out.marketid = marketid;
|
||||
const version = q.get('version'); if (version) out.version = version;
|
||||
return out;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { queryStringSource } from '../../../YouleNexus/assets/framework/config/sources/query-string.ts';
|
||||
|
||||
test('解析 agentid/channelid/marketid/version', () => {
|
||||
const src = queryStringSource('?agentid=A1&channelid=C1&marketid=2&version=9.9');
|
||||
assert.deepEqual(src(), { agentid: 'A1', channelid: 'C1', marketid: '2', version: '9.9' });
|
||||
});
|
||||
|
||||
test('缺省键不出现在结果里', () => {
|
||||
const src = queryStringSource('?agentid=A1');
|
||||
assert.deepEqual(src(), { agentid: 'A1' });
|
||||
});
|
||||
|
||||
test('空 query → 空对象', () => {
|
||||
assert.deepEqual(queryStringSource('')(), {});
|
||||
});
|
||||
|
||||
test('不含前导 ? 也能解析', () => {
|
||||
assert.deepEqual(queryStringSource('agentid=A1')(), { agentid: 'A1' });
|
||||
});
|
||||
Reference in New Issue
Block a user