Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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