Files
radicle-explorer/src/lib/search.ts
T
Sebastian Martinez 4525771264 Use /nodes/ in URL instead of /seeds/
We still allow already published URLs with `/seeds/` in them to be
resolved.

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
Signed-off-by: Rūdolfs Ošiņš <rudolfs@osins.org>
2023-07-31 16:58:45 +02:00

67 lines
1.6 KiB
TypeScript

import type { BaseUrl, Project } from "@httpd-client";
import * as utils from "@app/lib/utils";
import { HttpdClient } from "@httpd-client";
import { config } from "@app/lib/config";
import { isFulfilled } from "@app/lib/utils";
export interface ProjectBaseUrl {
project: Project;
baseUrl: BaseUrl;
}
type SearchResult =
| { type: "nothing" }
| { type: "error"; message: string }
| { type: "projects"; results: ProjectBaseUrl[] };
export async function searchProjectsAndProfiles(
query: string,
): Promise<SearchResult> {
try {
const pinned = config.nodes.pinned.map(node => ({
id: query,
baseUrl: node.baseUrl,
}));
if (utils.isRepositoryId(query)) {
const results = await getProjectsFromNodes(pinned);
if (results.length === 0) {
return { type: "nothing" };
} else {
return {
type: "projects",
results,
};
}
}
return { type: "nothing" };
} catch (error) {
let message = "An unknown error occoured while searching.";
if (error instanceof Error) {
message = error.message;
}
return { type: "error", message };
}
}
export async function getProjectsFromNodes(
params: { id: string; baseUrl: BaseUrl }[],
): Promise<ProjectBaseUrl[]> {
const projectPromises = params.map(async param => {
const api = new HttpdClient(param.baseUrl);
const project = await api.project.getById(param.id);
return {
project,
baseUrl: param.baseUrl,
};
});
const results = await Promise.allSettled(projectPromises);
return results.filter(isFulfilled).map(r => r.value);
}