59 lines
1.3 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";
2024-08-15 12:08:51 +02:00
import { Fetch } from "../fetch-safe/fetch-safe";
2024-08-30 12:24:00 +02:00
import type { SafeValue } from "../values/values";
2024-08-15 12:08:51 +02:00
import { CodexLogLevel, type CodexDebugInfo } from "./types";
import * as v from "valibot";
export class CodexDebug {
2024-08-15 12:08:51 +02:00
readonly url: string;
constructor(url: string) {
this.url = url;
}
/**
* Set log level at run time
*/
async setLogLevel(level: CodexLogLevel): Promise<SafeValue<"">> {
2024-08-15 12:08:51 +02:00
const result = v.safeParse(CodexLogLevel, level);
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;
const res = await Fetch.safe(url, {
2024-08-15 12:08:51 +02:00
method: "POST",
body: "",
});
if (res.error) {
return res;
}
return { error: false, data: "" };
2024-08-15 12:08:51 +02:00
}
/**
* Gets node information
*/
info() {
const url = this.url + Api.config.prefix + `/debug/info`;
return Fetch.safeJson<CodexDebugInfo>(url, {
method: "GET",
});
}
}