|
|
| import { apiFetch } from "../api.mjs";
|
| import { emit } from "../output.mjs";
|
| import { readFileSync } from "node:fs";
|
|
|
| export function register_usage(parent) {
|
| const tag = parent.command("usage").description("Usage endpoints");
|
| tag
|
| .command("get-api-usage-analytics")
|
| .description("Get usage analytics")
|
| .option("--period <period>", "")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/analytics";
|
| const qs = new URLSearchParams();
|
| if (opts.period != null) qs.set("period", String(opts.period));
|
| if (qs.toString()) url += "?" + qs.toString();
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("get-api-usage-call-logs")
|
| .description("Get call logs")
|
| .option("--limit <limit>", "")
|
| .option("--offset <offset>", "")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/call-logs";
|
| const qs = new URLSearchParams();
|
| if (opts.limit != null) qs.set("limit", String(opts.limit));
|
| if (opts.offset != null) qs.set("offset", String(opts.offset));
|
| if (qs.toString()) url += "?" + qs.toString();
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("get-api-usage-call-logs-id-")
|
| .description("Get a specific call log")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/call-logs/{id}";
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("get-api-usage-connection-id-")
|
| .description("Get usage for a specific connection")
|
| .requiredOption("--connection-id <connectionId>", "")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/{connectionId}";
|
| url = url.replace("{connectionId}", encodeURIComponent(opts.connectionId ?? ""));
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("get-api-usage-history")
|
| .description("Get usage history")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/history";
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("get-api-usage-logs")
|
| .description("Get usage logs")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/logs";
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("get-api-usage-proxy-logs")
|
| .description("Get proxy logs")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/proxy-logs";
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("get-api-usage-request-logs")
|
| .description("Get request logs")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/request-logs";
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("get-api-usage-budget")
|
| .description("Get usage budget status")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/budget";
|
| const res = await apiFetch(url, {
|
| method: "GET",
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| tag
|
| .command("post-api-usage-budget")
|
| .description("Configure usage budget")
|
| .option("--body <jsonOrPath>", "JSON body or @path/to/file.json")
|
| .action(async (opts, cmd) => {
|
| const gOpts = cmd.optsWithGlobals();
|
| let url = "/api/usage/budget";
|
| let body;
|
| if (opts.body) {
|
| body = opts.body.startsWith("@")
|
| ? JSON.parse(readFileSync(opts.body.slice(1), "utf8"))
|
| : JSON.parse(opts.body);
|
| }
|
| const res = await apiFetch(url, {
|
| method: "POST",
|
| body,
|
| baseUrl: gOpts.baseUrl,
|
| apiKey: gOpts.apiKey,
|
| });
|
| const data = res.ok ? await res.json() : await res.text();
|
| emit(data, gOpts);
|
| });
|
| }
|
|
|