feat: migrate ChKSz API and support API key authentication
This commit is contained in:
+13
-1
@@ -7,8 +7,20 @@ function getProtocol(url) {
|
||||
}
|
||||
|
||||
function fetchJSON(url) {
|
||||
let targetUrl = url;
|
||||
try {
|
||||
const { API } = require('./config');
|
||||
const parsed = new URL(url);
|
||||
if (!parsed.searchParams.has('apikey') && API?.apikey && parsed.hostname.includes('chksz.')) {
|
||||
parsed.searchParams.set('apikey', API.apikey);
|
||||
targetUrl = parsed.toString();
|
||||
}
|
||||
} catch {}
|
||||
return new Promise((resolve, reject) => {
|
||||
getProtocol(url).get(url, (res) => {
|
||||
getProtocol(targetUrl).get(targetUrl, (res) => {
|
||||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
||||
return fetchJSON(res.headers.location).then(resolve).catch(reject);
|
||||
}
|
||||
let data = '';
|
||||
res.on('data', chunk => data += chunk);
|
||||
res.on('end', () => {
|
||||
|
||||
Reference in New Issue
Block a user