73 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-08-15 12:08:51 +02:00
import { Api } from "../api/config";
2024-09-27 17:35:50 +02:00
import { CodexError, CodexValibotIssuesMap } from "../errors/errors";
2025-03-28 09:13:01 +01:00
import {
Fetch,
FetchAuthBuilder,
type FetchAuth,
} from "../fetch-safe/fetch-safe";
2024-08-30 12:24:00 +02:00
import type { SafeValue } from "../values/values";
2025-03-28 16:52:21 +01:00
import {
CodexLogLevelInput,
type CodexDebugInfo,
type CodexInfoResponse,
type CodexLogLevel,
} from "./types";
2024-08-15 12:08:51 +02:00
import * as v from "valibot";
2025-03-28 09:13:01 +01:00
type CodexDebugOptions = {
auth?: FetchAuth;
};
export class CodexDebug {
2024-08-15 12:08:51 +02:00
readonly url: string;
2025-03-28 09:13:01 +01:00
readonly auth: FetchAuth = {};
2024-08-15 12:08:51 +02:00
2025-03-28 09:13:01 +01:00
constructor(url: string, options?: CodexDebugOptions) {
2024-08-15 12:08:51 +02:00
this.url = url;
2025-03-28 09:13:01 +01:00
if (options?.auth) {
this.auth = options.auth;
}
2024-08-15 12:08:51 +02:00
}
/**
* Set log level at run time
*/
2025-03-28 16:52:21 +01:00
async setLogLevel(level: CodexLogLevel): Promise<SafeValue<string>> {
const result = v.safeParse(CodexLogLevelInput, level);
2024-08-15 12:08:51 +02:00
if (!result.success) {
2024-08-30 12:24:00 +02:00
return Promise.resolve({
2024-08-15 12:08:51 +02:00
error: true,
2024-09-27 17:35:50 +02:00
data: new CodexError("Cannot validate the input", {
2024-08-15 12:08:51 +02:00
errors: CodexValibotIssuesMap(result.issues),
2024-09-27 17:35:50 +02:00
}),
2024-08-30 12:24:00 +02:00
});
2024-08-15 12:08:51 +02:00
}
const url =
this.url +
Api.config.prefix +
"/debug/chronicles/loglevel?level=" +
level;
2025-03-28 16:52:21 +01:00
return Fetch.safeText(url, {
2024-08-15 12:08:51 +02:00
method: "POST",
2025-03-28 09:13:01 +01:00
headers: FetchAuthBuilder.build(this.auth),
2024-08-15 12:08:51 +02:00
body: "",
});
}
/**
* Gets node information
*/
2025-03-28 16:52:21 +01:00
info(): Promise<SafeValue<CodexDebugInfo>> {
2024-08-15 12:08:51 +02:00
const url = this.url + Api.config.prefix + `/debug/info`;
2025-03-28 16:52:21 +01:00
return Fetch.safeJson<CodexInfoResponse>(url, {
2024-08-15 12:08:51 +02:00
method: "GET",
2025-03-28 09:13:01 +01:00
headers: FetchAuthBuilder.build(this.auth),
2024-08-15 12:08:51 +02:00
});
}
}