2024-08-15 12:08:51 +02:00
|
|
|
import { Api } from "../api/config";
|
|
|
|
|
import { CodexValibotIssuesMap } from "../errors/errors";
|
|
|
|
|
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";
|
|
|
|
|
|
2024-09-13 19:19:56 +02:00
|
|
|
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
|
|
|
|
|
*/
|
2024-09-13 19:19:56 +02:00
|
|
|
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,
|
|
|
|
|
data: {
|
|
|
|
|
message: "Cannot validate the input",
|
|
|
|
|
errors: CodexValibotIssuesMap(result.issues),
|
|
|
|
|
},
|
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;
|
|
|
|
|
|
2024-09-13 19:19:56 +02:00
|
|
|
const res = await Fetch.safe(url, {
|
2024-08-15 12:08:51 +02:00
|
|
|
method: "POST",
|
|
|
|
|
body: "",
|
|
|
|
|
});
|
2024-09-13 19:19:56 +02:00
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|