All files / apps/developer-portal/layers/github/app/utils github.ts

100% Statements 13/13
100% Branches 8/8
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2534x     26x 26x   11x     15x   15x   12x       34x 4x   4x   2x    
export const repoApiUrl = (repoUrl: string, resource: string): string | null => {
    let url: URL;
 
    try {
        url = new URL(repoUrl);
    } catch {
        return null;
    }
 
    const [owner, repo] = url.pathname.split("/").filter(Boolean);
 
    if (owner === undefined || repo === undefined) return null;
 
    return `${url.protocol}//api.${url.host}/repos/${owner}/${repo.replace(/\.git$/, "")}/${resource}`;
};
 
/** GitHub's own error text, such as the rate-limit notice, rather than the fetch client's status line. */
export const messageOf = (cause: unknown, fallback: string): string => {
    const data = (cause as { data?: { message?: unknown } } | null)?.data;
 
    if (typeof data?.message === "string") return data.message.slice(0, 300);
 
    return cause instanceof Error ? cause.message.slice(0, 300) : fallback;
};