forked from MathMan05/JankClient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
32 lines (29 loc) · 812 Bytes
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const handleEndpoint = (url = "", isAPI = false) => {
let parsed = new URL(url).toString()
if (parsed.endsWith("/")) parsed = parsed.slice(0, -1)
if (!/\/v\d+$/.test(parsed) && isAPI) parsed += "/v9"
return parsed
}
module.exports.handleEndpoint = handleEndpoint
const getAPIURLs = async str => {
if (str.at(-1) != "/") str += "/"
let api
try {
const info = await fetch(str + "/.well-known/spacebar").then(x => x.json())
api = info.api
} catch {
return false
}
try {
const info = await fetch(api + "/policies/instance/domains").then(x => x.json())
return {
api: handleEndpoint(info.apiEndpoint, true),
gateway: handleEndpoint(info.gateway),
cdn: handleEndpoint(info.cdn),
wellknown: handleEndpoint(str)
}
} catch {
return false
}
}
module.exports.getAPIURLs = getAPIURLs