Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ dist


/temp
.claude
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
"@slimio/is": "^2.0.0",
"@types/node": "^25.0.3",
"is-svg": "^6.0.0",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"undici": "^7.0.0"
},
"dependencies": {
"@nodesecure/npm-registry-sdk": "4.5.1",
"@openally/httpie": "^1.0.0"
"@nodesecure/npm-registry-sdk": "4.5.1"
}
}
30 changes: 21 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Import Third-party Dependencies
import { get } from "@openally/httpie";
import { packument } from "@nodesecure/npm-registry-sdk";

// Import Internal Dependencies
import { repositoryFromUrl } from "./utils/repositoryFromUrl.ts";
import type { GitHubRepository, GitLabProject } from "./types.ts";
import type {
GitHubRepository,
GitLabProject
} from "./types.ts";

// CONSTANTS
const kDefaultPlatform = "github.com";
const kGitHubApiUrl = "https://api.github.com";
const kGitHubRequestOptions = {
authorization: process.env.GITHUB_TOKEN ?? ""
const kGitHubRequestOptions: RequestInit = {
headers: { Authorization: process.env.GITHUB_TOKEN ?? "" }
};
const kGitLabApiUrl = "https://gitlab.com";
export const API_URL = "https://api.securityscorecards.dev";
Expand Down Expand Up @@ -65,6 +67,18 @@ export interface ResultOptions {
npmPackageVersion?: string;
}

async function fetchJson<T>(
url: URL,
init?: RequestInit
): Promise<T> {
const response = await fetch(url, init);
if (!response.ok) {
throw new Error(response.statusText);
}

return response.json() as Promise<T>;
}

async function getNpmRepository(
repository: string,
version: string
Expand All @@ -88,7 +102,7 @@ async function retrieveRepositoryOnGithub(
owner: string,
repo: string
): Promise<string> {
const { data } = await get<GitHubRepository>(
const data = await fetchJson<GitHubRepository>(
new URL(`/repos/${owner}/${repo}`, kGitHubApiUrl),
kGitHubRequestOptions
);
Expand All @@ -100,7 +114,7 @@ async function retrieveRepositoryOnGitLab(
owner: string,
repo: string
): Promise<string> {
const { data } = await get<GitLabProject>(
const data = await fetchJson<GitLabProject>(
new URL(`/api/v4/projects/${owner}%2F${repo}`, kGitLabApiUrl)
);

Expand Down Expand Up @@ -166,11 +180,9 @@ export async function result(
}
}

const { data } = await get<ScorecardResult>(
return fetchJson<ScorecardResult>(
new URL(`/projects/${platform}/${formattedRepository}`, API_URL)
);

return data;
}

export interface BadgeOptions extends ResultOptions {
Expand Down
2 changes: 1 addition & 1 deletion test/badge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
setGlobalDispatcher,
getGlobalDispatcher,
type Interceptable
} from "@openally/httpie";
} from "undici";
import isSvg from "is-svg";
import is from "@slimio/is";

Expand Down
6 changes: 3 additions & 3 deletions test/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type Interceptable,
getGlobalDispatcher,
setGlobalDispatcher
} from "@openally/httpie";
} from "undici";
import is from "@slimio/is";
import * as npmRegistrySdk from "@nodesecure/npm-registry-sdk";

Expand Down Expand Up @@ -93,7 +93,7 @@ describe("#result() UT", () => {
await assert.rejects(
scorecard.result(expectedRepository),
{
name: "HttpieOnHttpError",
name: "Error",
message: "Not Found"
}
);
Expand Down Expand Up @@ -143,7 +143,7 @@ describe("#result() FT", () => {
resolveOnVersionControl: false,
resolveOnNpmRegistry: false
}), {
name: "HttpieOnHttpError",
name: "Error",
message: "Not Found"
});
});
Expand Down
Loading