From acf583f4df62766666ab41de40195be9dfb55369 Mon Sep 17 00:00:00 2001 From: GENTILHOMME Thomas Date: Mon, 23 Mar 2026 14:53:47 +0100 Subject: [PATCH] refactor: remove @openally/httpie with native Node.js fetch --- .gitignore | 1 + package.json | 6 +++--- src/index.ts | 30 +++++++++++++++++++++--------- test/badge.spec.ts | 2 +- test/result.spec.ts | 6 +++--- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 1790fbf..ecab8fd 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,4 @@ dist /temp +.claude diff --git a/package.json b/package.json index ab8998c..4de758d 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/index.ts b/src/index.ts index 4971270..af626c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; @@ -65,6 +67,18 @@ export interface ResultOptions { npmPackageVersion?: string; } +async function fetchJson( + url: URL, + init?: RequestInit +): Promise { + const response = await fetch(url, init); + if (!response.ok) { + throw new Error(response.statusText); + } + + return response.json() as Promise; +} + async function getNpmRepository( repository: string, version: string @@ -88,7 +102,7 @@ async function retrieveRepositoryOnGithub( owner: string, repo: string ): Promise { - const { data } = await get( + const data = await fetchJson( new URL(`/repos/${owner}/${repo}`, kGitHubApiUrl), kGitHubRequestOptions ); @@ -100,7 +114,7 @@ async function retrieveRepositoryOnGitLab( owner: string, repo: string ): Promise { - const { data } = await get( + const data = await fetchJson( new URL(`/api/v4/projects/${owner}%2F${repo}`, kGitLabApiUrl) ); @@ -166,11 +180,9 @@ export async function result( } } - const { data } = await get( + return fetchJson( new URL(`/projects/${platform}/${formattedRepository}`, API_URL) ); - - return data; } export interface BadgeOptions extends ResultOptions { diff --git a/test/badge.spec.ts b/test/badge.spec.ts index a471678..f37d682 100644 --- a/test/badge.spec.ts +++ b/test/badge.spec.ts @@ -8,7 +8,7 @@ import { setGlobalDispatcher, getGlobalDispatcher, type Interceptable -} from "@openally/httpie"; +} from "undici"; import isSvg from "is-svg"; import is from "@slimio/is"; diff --git a/test/result.spec.ts b/test/result.spec.ts index 9bd0605..1ca2482 100644 --- a/test/result.spec.ts +++ b/test/result.spec.ts @@ -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"; @@ -93,7 +93,7 @@ describe("#result() UT", () => { await assert.rejects( scorecard.result(expectedRepository), { - name: "HttpieOnHttpError", + name: "Error", message: "Not Found" } ); @@ -143,7 +143,7 @@ describe("#result() FT", () => { resolveOnVersionControl: false, resolveOnNpmRegistry: false }), { - name: "HttpieOnHttpError", + name: "Error", message: "Not Found" }); });