1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-pushy/lib/endpoint.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-10-28 14:36:04 +08:00
import { logger } from './utils';
2020-07-28 23:15:42 +08:00
2023-10-28 14:36:04 +08:00
let currentEndpoint = 'https://update.react-native.cn/api';
let backupEndpoints: string[] = ['https://update.reactnative.cn/api'];
let backupEndpointsQueryUrl: string | null = null;
2020-07-28 23:15:42 +08:00
2023-10-28 14:36:04 +08:00
export async function updateBackupEndpoints() {
if (backupEndpointsQueryUrl) {
2020-07-28 23:15:42 +08:00
try {
const resp = await fetch(backupEndpointsQueryUrl);
2023-10-28 14:36:04 +08:00
const remoteEndpoints = await resp.json();
if (Array.isArray(remoteEndpoints)) {
backupEndpoints = Array.from(
new Set([...backupEndpoints, ...remoteEndpoints]),
);
logger('fetch remote endpoints:', remoteEndpoints);
logger('merged backup endpoints:', backupEndpoints);
}
2020-07-28 23:15:42 +08:00
} catch (e) {
2023-10-28 14:36:04 +08:00
logger('fetch remote endpoints failed');
2020-07-28 23:15:42 +08:00
}
}
2023-10-28 14:36:04 +08:00
return backupEndpoints;
2020-07-28 23:15:42 +08:00
}
export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
return `${endpoint}/checkUpdate/${APPKEY}`;
}
2023-08-31 19:24:00 +08:00
/**
* @param {string} main - The main api endpoint
* @param {string[]} [backups] - The back up endpoints.
* @param {string} [backupQueryUrl] - An url that return a json file containing an array of endpoint.
* like: ["https://backup.api/1", "https://backup.api/2"]
*/
export function setCustomEndpoints({
main,
backups,
backupQueryUrl,
}: {
main: string;
backups?: string[];
backupQueryUrl?: string;
}) {
2020-07-28 23:34:06 +08:00
currentEndpoint = main;
backupEndpointsQueryUrl = null;
2020-07-28 23:15:42 +08:00
if (Array.isArray(backups) && backups.length > 0) {
backupEndpoints = backups;
}
2020-07-28 23:34:06 +08:00
if (typeof backupQueryUrl === 'string') {
backupEndpointsQueryUrl = backupQueryUrl;
}
2020-07-28 23:15:42 +08:00
}