修改底层请求逻辑,封装请求方法

This commit is contained in:
rain
2026-07-10 14:11:10 +08:00
parent 6050508144
commit 6129a9221a
66 changed files with 5861 additions and 451 deletions
+15 -12
View File
@@ -1,7 +1,7 @@
const assert = require('assert');
const StorageUtil = require('../utils/StorageUtil.js');
const FormUtil = require('../utils/FormUtil.js');
const RequestUtil = require('../utils/RequestUtil.js');
const AxiosRequestUtil = require('../utils/AxiosRequestUtil.js');
assert.strictEqual(FormUtil.trimOrUndefined(' 张三 '), '张三');
assert.strictEqual(FormUtil.trimOrUndefined(' '), undefined);
@@ -27,19 +27,20 @@ StorageUtil.remove(storage, 'token');
assert.strictEqual(StorageUtil.read(storage, 'token'), null);
const calls = [];
const requester = RequestUtil.createRequester({
const requester = AxiosRequestUtil.createRequester({
baseUrl: 'https://test-genealogy-api.ddxcjp.cn',
clientId: 'pc-client',
tokenHeaderName: 'Authorization',
getToken: () => 'token-x',
fetchImpl: async (url, options) => {
calls.push({ url, options });
axiosInstance: {
request: async (config) => {
calls.push(config);
return {
ok: true,
status: 200,
json: async () => ({ code: 200, data: { ok: true } })
};
return {
status: 200,
data: { code: 200, data: { ok: true } }
};
}
}
});
@@ -47,8 +48,10 @@ requester('GET', '/genealogy/pc/auth/profile', {
query: { keyword: '汤 氏' }
}).then((data) => {
assert.deepStrictEqual(data, { ok: true });
assert.strictEqual(calls[0].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/profile?keyword=%E6%B1%A4+%E6%B0%8F');
assert.strictEqual(calls[0].options.headers.clientid, 'pc-client');
assert.strictEqual(calls[0].options.headers.Authorization, 'Bearer token-x');
assert.strictEqual(calls[0].method, 'get');
assert.strictEqual(calls[0].url, '/genealogy/pc/auth/profile');
assert.deepStrictEqual(calls[0].params, { keyword: '汤 氏' });
assert.strictEqual(calls[0].headers.clientid, 'pc-client');
assert.strictEqual(calls[0].headers.Authorization, 'Bearer token-x');
console.log('utils tests passed');
});