Skip to content

Commit bee3e48

Browse files
authored
refactor: remove @openally/httpie with native Node.js fetch (#144)
1 parent 5025622 commit bee3e48

5 files changed

Lines changed: 29 additions & 16 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,4 @@ dist
105105

106106

107107
/temp
108+
.claude

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@
5151
"@slimio/is": "^2.0.0",
5252
"@types/node": "^25.0.3",
5353
"is-svg": "^6.0.0",
54-
"typescript": "^5.3.3"
54+
"typescript": "^5.3.3",
55+
"undici": "^7.0.0"
5556
},
5657
"dependencies": {
57-
"@nodesecure/npm-registry-sdk": "4.5.1",
58-
"@openally/httpie": "^1.0.0"
58+
"@nodesecure/npm-registry-sdk": "4.5.1"
5959
}
6060
}

src/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
// Import Third-party Dependencies
2-
import { get } from "@openally/httpie";
32
import { packument } from "@nodesecure/npm-registry-sdk";
43

54
// Import Internal Dependencies
65
import { repositoryFromUrl } from "./utils/repositoryFromUrl.ts";
7-
import type { GitHubRepository, GitLabProject } from "./types.ts";
6+
import type {
7+
GitHubRepository,
8+
GitLabProject
9+
} from "./types.ts";
810

911
// CONSTANTS
1012
const kDefaultPlatform = "github.com";
1113
const kGitHubApiUrl = "https://api.github.com";
12-
const kGitHubRequestOptions = {
13-
authorization: process.env.GITHUB_TOKEN ?? ""
14+
const kGitHubRequestOptions: RequestInit = {
15+
headers: { Authorization: process.env.GITHUB_TOKEN ?? "" }
1416
};
1517
const kGitLabApiUrl = "https://gitlab.com";
1618
export const API_URL = "https://api.securityscorecards.dev";
@@ -65,6 +67,18 @@ export interface ResultOptions {
6567
npmPackageVersion?: string;
6668
}
6769

70+
async function fetchJson<T>(
71+
url: URL,
72+
init?: RequestInit
73+
): Promise<T> {
74+
const response = await fetch(url, init);
75+
if (!response.ok) {
76+
throw new Error(response.statusText);
77+
}
78+
79+
return response.json() as Promise<T>;
80+
}
81+
6882
async function getNpmRepository(
6983
repository: string,
7084
version: string
@@ -88,7 +102,7 @@ async function retrieveRepositoryOnGithub(
88102
owner: string,
89103
repo: string
90104
): Promise<string> {
91-
const { data } = await get<GitHubRepository>(
105+
const data = await fetchJson<GitHubRepository>(
92106
new URL(`/repos/${owner}/${repo}`, kGitHubApiUrl),
93107
kGitHubRequestOptions
94108
);
@@ -100,7 +114,7 @@ async function retrieveRepositoryOnGitLab(
100114
owner: string,
101115
repo: string
102116
): Promise<string> {
103-
const { data } = await get<GitLabProject>(
117+
const data = await fetchJson<GitLabProject>(
104118
new URL(`/api/v4/projects/${owner}%2F${repo}`, kGitLabApiUrl)
105119
);
106120

@@ -166,11 +180,9 @@ export async function result(
166180
}
167181
}
168182

169-
const { data } = await get<ScorecardResult>(
183+
return fetchJson<ScorecardResult>(
170184
new URL(`/projects/${platform}/${formattedRepository}`, API_URL)
171185
);
172-
173-
return data;
174186
}
175187

176188
export interface BadgeOptions extends ResultOptions {

test/badge.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
setGlobalDispatcher,
99
getGlobalDispatcher,
1010
type Interceptable
11-
} from "@openally/httpie";
11+
} from "undici";
1212
import isSvg from "is-svg";
1313
import is from "@slimio/is";
1414

test/result.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type Interceptable,
99
getGlobalDispatcher,
1010
setGlobalDispatcher
11-
} from "@openally/httpie";
11+
} from "undici";
1212
import is from "@slimio/is";
1313
import * as npmRegistrySdk from "@nodesecure/npm-registry-sdk";
1414

@@ -93,7 +93,7 @@ describe("#result() UT", () => {
9393
await assert.rejects(
9494
scorecard.result(expectedRepository),
9595
{
96-
name: "HttpieOnHttpError",
96+
name: "Error",
9797
message: "Not Found"
9898
}
9999
);
@@ -143,7 +143,7 @@ describe("#result() FT", () => {
143143
resolveOnVersionControl: false,
144144
resolveOnNpmRegistry: false
145145
}), {
146-
name: "HttpieOnHttpError",
146+
name: "Error",
147147
message: "Not Found"
148148
});
149149
});

0 commit comments

Comments
 (0)