11// Import Third-party Dependencies
2- import { get } from "@openally/httpie" ;
32import { packument } from "@nodesecure/npm-registry-sdk" ;
43
54// Import Internal Dependencies
65import { 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
1012const kDefaultPlatform = "github.com" ;
1113const 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} ;
1517const kGitLabApiUrl = "https://gitlab.com" ;
1618export 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+
6882async 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
176188export interface BadgeOptions extends ResultOptions {
0 commit comments