mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-06 04:10:31 +08:00
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>
67 lines
1.6 KiB
TypeScript
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);
|
|
}
|