Refactor fetchMentions function to improve token handling and debug output
All checks were successful
Deploy / deploy (push) Successful in 1m19s

This commit is contained in:
Adrian Altner 2026-04-22 02:41:14 +02:00
parent f436df55b0
commit 1a3e4bf64a

View file

@ -30,12 +30,15 @@ interface FetchResult {
} }
async function fetchMentions(target: string): Promise<FetchResult> { async function fetchMentions(target: string): Promise<FetchResult> {
const importMetaToken = import.meta.env.WEBMENTION_TOKEN; // Bracket notation prevents Vite from statically replacing at build time.
const processToken = typeof process !== 'undefined' ? process.env.WEBMENTION_TOKEN : undefined; const envKey = 'WEBMENTION_TOKEN';
const token = importMetaToken || processToken; const processToken =
const iLen = typeof importMetaToken === 'string' ? importMetaToken.length : 0; typeof process !== 'undefined' ? (process.env as Record<string, string | undefined>)[envKey] : undefined;
const importMetaToken = (import.meta.env as Record<string, string | undefined>)[envKey];
const token = processToken || importMetaToken;
const pLen = typeof processToken === 'string' ? processToken.length : 0; const pLen = typeof processToken === 'string' ? processToken.length : 0;
if (!token) return { mentions: [], debug: `no-token(iml=${iLen},pe=${pLen})` }; const iLen = typeof importMetaToken === 'string' ? importMetaToken.length : 0;
if (!token) return { mentions: [], debug: `no-token(pe=${pLen},iml=${iLen})` };
const withSlash = target.endsWith('/') ? target : `${target}/`; const withSlash = target.endsWith('/') ? target : `${target}/`;
const withoutSlash = target.replace(/\/+$/, ''); const withoutSlash = target.replace(/\/+$/, '');
const fetchOne = async (t: string) => { const fetchOne = async (t: string) => {