Skip to content

refactor: extract search service #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
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
2 changes: 1 addition & 1 deletion src/cli/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { logValidDependency } from "./dependency-logging";
import { unityRegistryUrl } from "../domain/registry-url";
import { tryGetTargetEditorVersionFor } from "../domain/package-manifest";
import { VersionNotFoundError } from "../domain/packument";
import {FetchPackumentError} from "../io/packument-io";
import { FetchPackumentError } from "../io/packument-io";

export class InvalidPackumentDataError extends CustomError {
private readonly _class = "InvalidPackumentDataError";
Expand Down
59 changes: 12 additions & 47 deletions src/cli/cmd-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import * as os from "os";
import { EnvParseError, ParseEnvService } from "../services/parse-env";
import { CmdOptions } from "./options";
import { formatAsTable } from "./output-formatting";
import { AsyncResult, Ok, Result } from "ts-results-es";
import { Ok, Result } from "ts-results-es";
import { HttpErrorBase } from "npm-registry-fetch/lib/errors";
import { SearchedPackument, SearchRegistry } from "../io/npm-search";
import { Registry } from "../domain/registry";
import { FetchAllPackuments } from "../io/all-packuments-io";
import { Logger } from "npmlog";
import { SearchPackages } from "../services/search-packages";

export type SearchError = EnvParseError | HttpErrorBase;

Expand All @@ -28,66 +26,33 @@ export type SearchCmd = (
*/
export function makeSearchCmd(
parseEnv: ParseEnvService,
searchRegistry: SearchRegistry,
fetchAllPackuments: FetchAllPackuments,
searchPackages: SearchPackages,
log: Logger
): SearchCmd {
function searchEndpoint(
registry: Registry,
keyword: string
): AsyncResult<ReadonlyArray<SearchedPackument>, HttpErrorBase> {
return searchRegistry(registry, keyword).map((results) => {
log.verbose("npmsearch", results.join(os.EOL));
return results;
});
}

function searchOld(
registry: Registry,
keyword: string
): AsyncResult<SearchedPackument[], HttpErrorBase> {
return fetchAllPackuments(registry).map((allPackuments) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { _updated, ...packumentEntries } = allPackuments;
const packuments = Object.values(packumentEntries);

log.verbose("endpoint.all", packuments.join(os.EOL));

// filter keyword
const klc = keyword.toLowerCase();

return packuments.filter((packument) =>
packument.name.toLowerCase().includes(klc)
);
});
}

return async (keyword, options) => {
// parse env
const envResult = await parseEnv(options);
if (envResult.isErr()) return envResult;
const env = envResult.value;

// search endpoint
let result = await searchEndpoint(env.registry, keyword).promise;

// search old search
if (result.isErr()) {
let usedEndpoint = "npmsearch";
const searchResult = await searchPackages(env.registry, keyword, () => {
usedEndpoint = "endpoint.all";
log.warn("", "fast search endpoint is not available, using old search.");
result = await searchOld(env.registry, keyword).promise;
}
if (result.isErr()) {
}).promise;

if (searchResult.isErr()) {
log.warn("", "/-/all endpoint is not available");
return result;
return searchResult;
}

const results = result.value;

const results = searchResult.value;
if (results.length === 0) {
log.notice("", `No matches found for "${keyword}"`);
return Ok(undefined);
}

log.verbose(usedEndpoint, results.map((it) => it.name).join(os.EOL));
console.log(formatAsTable(results));
return Ok(undefined);
};
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cmd-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "../common-errors";
import { Logger } from "npmlog";

import {FetchPackument} from "../io/packument-io";
import { FetchPackument } from "../io/packument-io";

export type ViewOptions = CmdOptions;

Expand Down
9 changes: 3 additions & 6 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { makeCwdGetter, makeHomePathGetter } from "../io/special-paths";
import { makeProjectVersionLoader } from "../io/project-version-io";
import { makeSaveAuthToUpmConfigService } from "../services/upm-auth";
import { makePackumentFetcher } from "../io/packument-io";
import { makePackagesSearcher } from "../services/search-packages";

// Composition root

Expand Down Expand Up @@ -84,6 +85,7 @@ const saveAuthToUpmConfig = makeSaveAuthToUpmConfigService(
loadUpmConfig,
writeFile
);
const searchPackages = makePackagesSearcher(searchRegistry, fetchAllPackuments);

const addCmd = makeAddCmd(
parseEnv,
Expand All @@ -101,12 +103,7 @@ const loginCmd = makeLoginCmd(
saveAuthToUpmConfig,
log
);
const searchCmd = makeSearchCmd(
parseEnv,
searchRegistry,
fetchAllPackuments,
log
);
const searchCmd = makeSearchCmd(parseEnv, searchPackages, log);
const depsCmd = makeDepsCmd(parseEnv, resolveDependencies, log);
const removeCmd = makeRemoveCmd(
parseEnv,
Expand Down
2 changes: 1 addition & 1 deletion src/services/dependency-resolving.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { Err, Ok, Result } from "ts-results-es";
import { PackumentNotFoundError } from "../common-errors";
import { HttpErrorBase } from "npm-registry-fetch/lib/errors";
import {FetchPackumentError} from "../io/packument-io";
import { FetchPackumentError } from "../io/packument-io";

export type DependencyBase = {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/services/resolve-latest-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SemanticVersion } from "../domain/semantic-version";
import { PackumentNotFoundError } from "../common-errors";
import { NoVersionsError, tryGetLatestVersion } from "../domain/packument";
import { recordKeys } from "../utils/record-utils";
import {FetchPackumentError, FetchPackument} from "../io/packument-io";
import { FetchPackumentError, FetchPackument } from "../io/packument-io";

/**
* Error which may occur when resolving the latest version for a package.
Expand Down
2 changes: 1 addition & 1 deletion src/services/resolve-remote-packument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../packument-resolving";
import { PackumentNotFoundError } from "../common-errors";
import { tryResolvePackumentVersion } from "../domain/packument";
import {FetchPackumentError, FetchPackument} from "../io/packument-io";
import { FetchPackumentError, FetchPackument } from "../io/packument-io";

/**
* Service function for resolving remove packuments.
Expand Down
45 changes: 45 additions & 0 deletions src/services/search-packages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Registry } from "../domain/registry";
import { AsyncResult } from "ts-results-es";
import { SearchedPackument, SearchRegistry } from "../io/npm-search";
import { FetchAllPackuments } from "../io/all-packuments-io";
import { HttpErrorBase } from "npm-registry-fetch/lib/errors";

export type SearchPackagesError = HttpErrorBase;

export type SearchPackages = (
registry: Registry,
keyword: string,
onUseAllFallback?: () => void
) => AsyncResult<ReadonlyArray<SearchedPackument>, SearchPackagesError>;

export function makePackagesSearcher(
searchRegistry: SearchRegistry,
fetchAllPackuments: FetchAllPackuments
): SearchPackages {
function searchInAll(
registry: Registry,
keyword: string
): AsyncResult<SearchedPackument[], HttpErrorBase> {
return fetchAllPackuments(registry).map((allPackuments) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { _updated, ...packumentEntries } = allPackuments;
const packuments = Object.values(packumentEntries);

// filter keyword
const klc = keyword.toLowerCase();

return packuments.filter((packument) =>
packument.name.toLowerCase().includes(klc)
);
});
}

return (registry, keyword, onUseOldSearch) => {
// search endpoint
return searchRegistry(registry, keyword).orElse(() => {
// search old search
onUseOldSearch && onUseOldSearch();
return searchInAll(registry, keyword);
});
};
}
Loading
Loading