Skip to content

Commit

Permalink
check url
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Aug 11, 2024
1 parent ac6b2ac commit eea3d5f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import pkginfo from "pkginfo";
import updateNotifier from "update-notifier";
import pkg from "../../package.json";
import { getAllRegistryPackuments } from "../io/all-packuments-io";
import { makeCheckUrlExists } from "../io/check-url";
import { checkUrlExists } from "../io/check-url";
import { searchRegistry } from "../io/npm-search";
import { findNpmrcPath, loadNpmrc, saveNpmrc } from "../io/npmrc-io";
import { getRegistryPackument } from "../io/packument-io";
Expand Down Expand Up @@ -54,8 +54,6 @@ import {

const log = npmlog;

const checkUrlExists = makeCheckUrlExists();

const loadRegistryAuth = makeLoadRegistryAuth(loadUpmConfig);
const parseEnv = makeParseEnv(
log,
Expand Down
10 changes: 8 additions & 2 deletions src/io/check-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import fetch from "node-fetch";
export type CheckUrlExists = (url: string) => Promise<boolean>;

/**
* Makes a {@link CheckUrlExists} function.
* Makes a {@link CheckUrlExists} function that determines whether a url
* exists by checking whether it responds to a HEAD request with 200.
*/
export function makeCheckUrlExists(): CheckUrlExists {
export function CheckUrlIsOk(): CheckUrlExists {
return async (url) => {
const response = await fetch(url, { method: "HEAD" });
return response.status === 200;
};
}

/**
* Default {@link CheckUrlExists} function. Uses {@link CheckUrlIsOk}.
*/
export const checkUrlExists: CheckUrlExists = CheckUrlIsOk();
16 changes: 8 additions & 8 deletions test/io/check-url.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { makeCheckUrlExists } from "../../src/io/check-url";
import fetch, { Response } from "node-fetch";
import { CheckUrlIsOk } from "../../src/io/check-url";

jest.mock("node-fetch");

describe("check url exists", () => {
describe("check url is ok", () => {
function makeDependencies() {
const checkUrlExists = makeCheckUrlExists();
return { checkUrlExists } as const;
const checkUrlIsOk = CheckUrlIsOk();
return { checkUrlIsOk } as const;
}

it("should be true if url responds with 200", async () => {
jest.mocked(fetch).mockResolvedValue({
status: 200,
} as Response);
const { checkUrlExists } = makeDependencies();
const { checkUrlIsOk } = makeDependencies();

const actual = await checkUrlExists("https://some.url.com");
const actual = await checkUrlIsOk("https://some.url.com");

expect(actual).toBeTruthy();
});
Expand All @@ -26,9 +26,9 @@ describe("check url exists", () => {
jest.mocked(fetch).mockResolvedValue({
status: statusCode,
} as Response);
const { checkUrlExists } = makeDependencies();
const { checkUrlIsOk } = makeDependencies();

const actual = await checkUrlExists("https://some.url.com");
const actual = await checkUrlIsOk("https://some.url.com");

expect(actual).toBeFalsy();
}
Expand Down

0 comments on commit eea3d5f

Please sign in to comment.