Skip to content

Commit 0eb3cd6

Browse files
feat: resolveOnNpmRegistry option (#54)
* feat: resolveOnNpmRegistry option * test: add functional tests * refactor(error): use cause
1 parent a4bc947 commit 0eb3cd6

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const data = await scorecard.result("NodeSecure/scanner");
4747
console.log(data);
4848
```
4949

50-
You can also provide a custom `platform` with the **options** payload:
50+
You can provide a custom `platform` with the **options** payload:
5151

5252
```ts
5353
const data = await scorecard.result("NodeSecure/scanner", {
@@ -56,6 +56,14 @@ const data = await scorecard.result("NodeSecure/scanner", {
5656
console.log(data);
5757
```
5858

59+
You can disable `resolveOnNpmRegistry` option which is `true` by default.
60+
61+
```ts
62+
const data = await scorecard.result("NodeSecure/scanner", {
63+
resolveOnNpmRegistry: false, // default to true
64+
});
65+
console.log(data);
66+
```
5967
Options are described with the following TypeScript interface:
6068

6169
```ts
@@ -65,6 +73,11 @@ export interface IResultOptions {
6573
* @default github.com
6674
*/
6775
platform?: string;
76+
/**
77+
* @description Try to resolve the given repository on the NPM registry if its not found on the given platform.
78+
* @default true
79+
*/
80+
resolveOnNpmRegistry?: boolean;
6881
}
6982
```
7083

src/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export interface IResultOptions {
4242
* @default github.com
4343
*/
4444
platform?: string;
45+
/**
46+
* @description Try to resolve the given repository on the NPM registry if its not found on the given platform.
47+
* @default true
48+
*/
49+
resolveOnNpmRegistry?: boolean;
4550
}
4651

4752
async function getNpmRepository(repository: string): Promise<string> {
@@ -67,7 +72,10 @@ export async function result(
6772
options: IResultOptions = {}
6873
): Promise<ScorecardResult> {
6974
let formattedRepository = repository;
70-
const { platform = kDefaultPlatform } = options;
75+
const {
76+
platform = kDefaultPlatform,
77+
resolveOnNpmRegistry = true
78+
} = options;
7179
const [owner, repo] = repository.replace("@", "").split("/");
7280

7381
try {
@@ -83,14 +91,20 @@ export async function result(
8391
const data = await response.json() as any;
8492
formattedRepository = data.full_name;
8593
}
86-
catch {
87-
// If the repository is not found, we try to retrieve it from the NPM registry
88-
// i.e: if given repository is "@nodesecure/cli"
94+
catch (error) {
95+
if (!resolveOnNpmRegistry) {
96+
throw new Error("Invalid repository, cannot find it on GitHub", {
97+
cause: error
98+
});
99+
}
100+
89101
try {
90102
formattedRepository = await getNpmRepository(repository);
91103
}
92-
catch {
93-
throw new Error("Invalid repository, cannot find it on GitHub or NPM registry");
104+
catch (error) {
105+
throw new Error("Invalid repository, cannot find it on GitHub or NPM registry", {
106+
cause: error
107+
});
94108
}
95109
}
96110

test/result.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ describe("#result() FT", () => {
128128
["date", "repo", "scorecard", "score", "checks"].sort()
129129
);
130130
});
131+
132+
it("should throw when given a package and npm resolve is falsy", async() => {
133+
assert.rejects(async() => scorecard.result("@unknown-package/for-sure", { resolveOnNpmRegistry: false }), {
134+
name: "Error",
135+
message: "Invalid repository, cannot find it on GitHub"
136+
});
137+
});
138+
139+
it("should throw when given an unknown npm package", async() => {
140+
assert.rejects(async() => await scorecard.result("@unknown-package/for-sure", { resolveOnNpmRegistry: true }), {
141+
name: "Error",
142+
message: "Invalid repository, cannot find it on GitHub or NPM registry"
143+
});
144+
});
131145
});
132146

133147
function getPath(repository: string): string {

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"compilerOptions": {
33
"declaration": true,
44
"strictNullChecks": true,
5-
"target": "ES2020",
5+
"target": "ES2022",
66
"outDir": "dist",
7-
"module": "ES2020",
7+
"module": "ES2022",
88
"moduleResolution": "node",
99
"esModuleInterop": true,
1010
"resolveJsonModule": true,

0 commit comments

Comments
 (0)