sourcebot/packages/web/src/app/api/(client)/client.ts

58 lines
2 KiB
TypeScript
Raw Normal View History

'use client';
import { NEXT_PUBLIC_DOMAIN_SUB_PATH } from "@/lib/environment.client";
import { fileSourceResponseSchema, listRepositoriesResponseSchema, searchResponseSchema } from "@/lib/schemas";
2024-11-07 02:28:10 +00:00
import { FileSourceRequest, FileSourceResponse, ListRepositoriesResponse, SearchRequest, SearchResponse } from "@/lib/types";
2025-02-23 01:40:42 +00:00
import { measure } from "@/lib/utils";
import assert from "assert";
2025-02-12 21:05:44 +00:00
export const search = async (body: SearchRequest, domain: string): Promise<SearchResponse> => {
const path = resolveServerPath("/api/search");
2025-02-23 01:40:42 +00:00
const { data: result } = await measure(() => fetch(path, {
method: "POST",
headers: {
"Content-Type": "application/json",
2025-02-12 21:05:44 +00:00
"X-Org-Domain": domain,
},
body: JSON.stringify(body),
2025-02-23 01:40:42 +00:00
}).then(response => response.json()), "client.search");
return searchResponseSchema.parse(result);
}
2025-02-12 21:05:44 +00:00
export const fetchFileSource = async (body: FileSourceRequest, domain: string): Promise<FileSourceResponse> => {
const path = resolveServerPath("/api/source");
const result = await fetch(path, {
method: "POST",
headers: {
"Content-Type": "application/json",
2025-02-12 21:05:44 +00:00
"X-Org-Domain": domain,
},
2024-11-07 02:28:10 +00:00
body: JSON.stringify(body),
}).then(response => response.json());
return fileSourceResponseSchema.parse(result);
2024-09-11 05:59:17 +00:00
}
2025-02-12 21:05:44 +00:00
export const getRepos = async (domain: string): Promise<ListRepositoriesResponse> => {
const path = resolveServerPath("/api/repos");
const result = await fetch(path, {
2024-09-11 05:59:17 +00:00
method: "GET",
headers: {
"Content-Type": "application/json",
2025-02-12 21:05:44 +00:00
"X-Org-Domain": domain,
2024-09-11 05:59:17 +00:00
},
}).then(response => response.json());
return listRepositoriesResponseSchema.parse(result);
}
/**
* Given a subpath to a api route on the server (e.g., /api/search),
* returns the full path to that route on the server, taking into account
* the base path (if any).
*/
export const resolveServerPath = (path: string) => {
assert(path.startsWith("/"));
return `${NEXT_PUBLIC_DOMAIN_SUB_PATH}${path}`;
}