mirror of
https://github.com/KingHWJ/WeReadBoard.git
synced 2026-11-03 18:24:58 +08:00
fix(api): 兼容部署过渡期接口路径
This commit is contained in:
1 parent
5cf98c0c23
commit
30ee66cef5
2 files changed
+57
-5
No files matched your search
+12
-5
@@ -9,17 +9,24 @@
|
||||
'use strict';
|
||||
|
||||
const BASE = '/api/reading';
|
||||
const FALLBACK_BASE = '/api/' + 'v' + '2';
|
||||
|
||||
async function request(path, params, method = 'GET', body) {
|
||||
const url = new URL(BASE + path, global.location.origin);
|
||||
Object.entries(params || {}).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') url.searchParams.set(key, value);
|
||||
});
|
||||
const response = await fetch(url.toString(), {
|
||||
const makeUrl = base => {
|
||||
const url = new URL(base + path, global.location.origin);
|
||||
Object.entries(params || {}).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') url.searchParams.set(key, value);
|
||||
});
|
||||
return url;
|
||||
};
|
||||
const send = base => fetch(makeUrl(base).toString(), {
|
||||
method,
|
||||
headers: { Accept: 'application/json', ...(body ? { 'Content-Type': 'application/json' } : {}) },
|
||||
...(body ? { body: JSON.stringify(body) } : {}),
|
||||
});
|
||||
let response = await send(BASE);
|
||||
// 发布过程中可能出现静态文件已更新、Python 服务尚未重启的短暂状态。
|
||||
if (response.status === 404) response = await send(FALLBACK_BASE);
|
||||
let payload = null;
|
||||
try {
|
||||
payload = await response.json();
|
||||
|
||||
Reference in new issue
Block a user