|
| 1 | +const DOMAIN_URL = "https://raw.githubusercontent.com/ExpTechTW/API/refs/heads/main/resource/domain"; |
| 2 | + |
| 3 | +type Type = "lb" | "core"; |
| 4 | +type Service = "api" | "static"; |
| 5 | +type Region = "tpe1" | "khh1" | "tyo1" | "tnn1"; |
| 6 | + |
| 7 | +let urls: string[] = []; |
| 8 | + |
| 9 | +// 初始化:從遠端獲取端點列表 |
| 10 | +const init = async () => { |
| 11 | + const res = await fetch(DOMAIN_URL); |
| 12 | + const text = await res.text(); |
| 13 | + urls = text.match(/(?<=\* ).+/g) ?? []; |
| 14 | + return urls; |
| 15 | +}; |
| 16 | + |
| 17 | +// 取得所有 URL |
| 18 | +const getAll = () => urls; |
| 19 | + |
| 20 | +// 篩選 |
| 21 | +const get = (type?: Type, service?: Service, region?: Region | null) => |
| 22 | + urls.filter((u) => { |
| 23 | + if (type && !u.includes(`.${type}`)) return false; |
| 24 | + if (service && !u.startsWith(`${service}.`)) return false; |
| 25 | + if (region === null && /-\w+\./.test(u)) return false; // 只要主入口 |
| 26 | + if (region && !u.includes(`-${region}.`)) return false; |
| 27 | + return true; |
| 28 | + }); |
| 29 | + |
| 30 | +// 取單一端點 |
| 31 | +const getOne = (type: Type, service: Service, region?: Region | null) => |
| 32 | + get(type, service, region)[0]; |
| 33 | + |
| 34 | +// 取所有地區節點(排除主入口,健康檢查用) |
| 35 | +const getRegions = (type?: Type, service?: Service) => |
| 36 | + urls.filter((u) => { |
| 37 | + if (!/-\w+\./.test(u)) return false; // 排除主入口 |
| 38 | + if (type && !u.includes(`.${type}`)) return false; |
| 39 | + if (service && !u.startsWith(`${service}.`)) return false; |
| 40 | + return true; |
| 41 | + }); |
| 42 | + |
| 43 | +// ===== 使用範例 ===== |
| 44 | + |
| 45 | +(async () => { |
| 46 | + await init(); |
| 47 | + |
| 48 | + console.log("所有:", getAll()); |
| 49 | + console.log("LB API 主入口:", getOne("lb", "api", null)); |
| 50 | + console.log("Core API 東京:", getOne("core", "api", "tyo1")); |
| 51 | + console.log("LB Static 全部:", get("lb", "static")); |
| 52 | + console.log("所有 API:", get(undefined, "api")); |
| 53 | + console.log("所有 Core:", get("core")); |
| 54 | + console.log("所有地區節點:", getRegions()); |
| 55 | + console.log("LB API 地區節點:", getRegions("lb", "api")); |
| 56 | +})(); |
0 commit comments