Skip to content

Commit

Permalink
feat: resolveOnNpmRegistry option (#54)
Browse files Browse the repository at this point in the history
* feat: resolveOnNpmRegistry option

* test: add functional tests

* refactor(error): use cause
  • Loading branch information
PierreDemailly authored Jun 24, 2023
1 parent a4bc947 commit 0eb3cd6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const data = await scorecard.result("NodeSecure/scanner");
console.log(data);
```

You can also provide a custom `platform` with the **options** payload:
You can provide a custom `platform` with the **options** payload:

```ts
const data = await scorecard.result("NodeSecure/scanner", {
Expand All @@ -56,6 +56,14 @@ const data = await scorecard.result("NodeSecure/scanner", {
console.log(data);
```

You can disable `resolveOnNpmRegistry` option which is `true` by default.

```ts
const data = await scorecard.result("NodeSecure/scanner", {
resolveOnNpmRegistry: false, // default to true
});
console.log(data);
```
Options are described with the following TypeScript interface:

```ts
Expand All @@ -65,6 +73,11 @@ export interface IResultOptions {
* @default github.com
*/
platform?: string;
/**
* @description Try to resolve the given repository on the NPM registry if its not found on the given platform.
* @default true
*/
resolveOnNpmRegistry?: boolean;
}
```

Expand Down
26 changes: 20 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export interface IResultOptions {
* @default github.com
*/
platform?: string;
/**
* @description Try to resolve the given repository on the NPM registry if its not found on the given platform.
* @default true
*/
resolveOnNpmRegistry?: boolean;
}

async function getNpmRepository(repository: string): Promise<string> {
Expand All @@ -67,7 +72,10 @@ export async function result(
options: IResultOptions = {}
): Promise<ScorecardResult> {
let formattedRepository = repository;
const { platform = kDefaultPlatform } = options;
const {
platform = kDefaultPlatform,
resolveOnNpmRegistry = true
} = options;
const [owner, repo] = repository.replace("@", "").split("/");

try {
Expand All @@ -83,14 +91,20 @@ export async function result(
const data = await response.json() as any;
formattedRepository = data.full_name;
}
catch {
// If the repository is not found, we try to retrieve it from the NPM registry
// i.e: if given repository is "@nodesecure/cli"
catch (error) {
if (!resolveOnNpmRegistry) {
throw new Error("Invalid repository, cannot find it on GitHub", {
cause: error
});
}

try {
formattedRepository = await getNpmRepository(repository);
}
catch {
throw new Error("Invalid repository, cannot find it on GitHub or NPM registry");
catch (error) {
throw new Error("Invalid repository, cannot find it on GitHub or NPM registry", {
cause: error
});
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ describe("#result() FT", () => {
["date", "repo", "scorecard", "score", "checks"].sort()
);
});

it("should throw when given a package and npm resolve is falsy", async() => {
assert.rejects(async() => scorecard.result("@unknown-package/for-sure", { resolveOnNpmRegistry: false }), {
name: "Error",
message: "Invalid repository, cannot find it on GitHub"
});
});

it("should throw when given an unknown npm package", async() => {
assert.rejects(async() => await scorecard.result("@unknown-package/for-sure", { resolveOnNpmRegistry: true }), {
name: "Error",
message: "Invalid repository, cannot find it on GitHub or NPM registry"
});
});
});

function getPath(repository: string): string {
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"compilerOptions": {
"declaration": true,
"strictNullChecks": true,
"target": "ES2020",
"target": "ES2022",
"outDir": "dist",
"module": "ES2020",
"module": "ES2022",
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true,
Expand Down

0 comments on commit 0eb3cd6

Please sign in to comment.