mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-01 09:40:28 +08:00
Instead of having one record per service, we configure the host and expect services to run on certain ports.
55 lines
1.2 KiB
TypeScript
55 lines
1.2 KiB
TypeScript
import type { Config } from '@app/config';
|
|
|
|
export async function get(
|
|
path: string,
|
|
params: Record<string, any>,
|
|
config: Config
|
|
): Promise<any> {
|
|
if (! config.seed.host) {
|
|
throw new Error("Seed host unavailable");
|
|
}
|
|
|
|
const query: Record<string, string> = {};
|
|
for (const [key, val] of Object.entries(params)) {
|
|
query[key] = val.toString();
|
|
}
|
|
|
|
const base = config.seed.host;
|
|
const port = config.seed.port;
|
|
const search = new URLSearchParams(query).toString();
|
|
const baseUrl = `https://${base}:${port}/v1/${path}`;
|
|
const url = search ? `${baseUrl}?${search}` : baseUrl;
|
|
|
|
let response = null;
|
|
try {
|
|
response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
}
|
|
});
|
|
} catch (err) {
|
|
throw new ApiError(url, "API request failed");
|
|
}
|
|
|
|
if (! response.ok) {
|
|
throw new ApiError(url, "Not found");
|
|
}
|
|
return response.json();
|
|
}
|
|
|
|
class ApiError extends Error {
|
|
url: string;
|
|
|
|
constructor(url: string, message: string) {
|
|
super(message);
|
|
|
|
if (Error.captureStackTrace) {
|
|
Error.captureStackTrace(this, ApiError);
|
|
}
|
|
|
|
this.name = "ApiError";
|
|
this.url = url;
|
|
}
|
|
}
|