2024-11-18 20:09:26 +00:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { NEXT_PUBLIC_DOMAIN_SUB_PATH } from "@/lib/environment.client";
|
2024-09-26 03:12:20 +00:00
|
|
|
import { fileSourceResponseSchema, listRepositoriesResponseSchema, searchResponseSchema } from "@/lib/schemas";
|
2024-11-07 02:28:10 +00:00
|
|
|
import { FileSourceRequest, FileSourceResponse, ListRepositoriesResponse, SearchRequest, SearchResponse } from "@/lib/types";
|
2024-09-10 06:16:41 +00:00
|
|
|
|
|
|
|
|
export const search = async (body: SearchRequest): Promise<SearchResponse> => {
|
2024-11-18 20:09:26 +00:00
|
|
|
const path = resolveServerPath("/api/search");
|
|
|
|
|
const result = await fetch(path, {
|
2024-09-10 06:16:41 +00:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
|
}).then(response => response.json());
|
|
|
|
|
|
|
|
|
|
return searchResponseSchema.parse(result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 02:28:10 +00:00
|
|
|
export const fetchFileSource = async (body: FileSourceRequest): Promise<FileSourceResponse> => {
|
2024-11-18 20:09:26 +00:00
|
|
|
const path = resolveServerPath("/api/source");
|
|
|
|
|
const result = await fetch(path, {
|
2024-09-10 06:16:41 +00:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
2024-11-07 02:28:10 +00:00
|
|
|
body: JSON.stringify(body),
|
2024-09-10 06:16:41 +00:00
|
|
|
}).then(response => response.json());
|
|
|
|
|
|
|
|
|
|
return fileSourceResponseSchema.parse(result);
|
2024-09-11 05:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getRepos = async (): Promise<ListRepositoriesResponse> => {
|
2024-11-18 20:09:26 +00:00
|
|
|
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",
|
|
|
|
|
},
|
|
|
|
|
}).then(response => response.json());
|
|
|
|
|
|
|
|
|
|
return listRepositoriesResponseSchema.parse(result);
|
|
|
|
|
}
|
2024-11-18 20:09:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) => {
|
|
|
|
|
return `${NEXT_PUBLIC_DOMAIN_SUB_PATH}${path}`;
|
|
|
|
|
}
|