feat: add code queue submit cli
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { runCommand } from "./command";
|
||||
import { type UniDeskConfig, repoRoot } from "./config";
|
||||
import { jsonByteLength, previewJson } from "./preview";
|
||||
@@ -58,9 +59,38 @@ function stringOption(args: string[], name: string): string | undefined {
|
||||
return raw;
|
||||
}
|
||||
|
||||
function methodOption(args: string[]): string {
|
||||
const method = (stringOption(args, "--method") ?? "GET").toUpperCase();
|
||||
function hasFlag(args: string[], name: string): boolean {
|
||||
return args.includes(name);
|
||||
}
|
||||
|
||||
function parseJsonOption(raw: string, name: string): unknown {
|
||||
try {
|
||||
return JSON.parse(raw) as unknown;
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
throw new Error(`${name} must be valid JSON: ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function requestBodyOption(args: string[]): unknown | undefined {
|
||||
const bodyJson = stringOption(args, "--body-json");
|
||||
const bodyFile = stringOption(args, "--body-file");
|
||||
const bodyStdin = hasFlag(args, "--body-stdin");
|
||||
const sources = [bodyJson !== undefined, bodyFile !== undefined, bodyStdin].filter(Boolean).length;
|
||||
if (sources > 1) throw new Error("microservice proxy accepts only one request body source: --body-json, --body-file, or --body-stdin");
|
||||
if (bodyJson !== undefined) return parseJsonOption(bodyJson, "--body-json");
|
||||
if (bodyFile !== undefined) {
|
||||
const text = bodyFile === "-" ? readFileSync(0, "utf8") : readFileSync(bodyFile, "utf8");
|
||||
return parseJsonOption(text, "--body-file");
|
||||
}
|
||||
if (bodyStdin) return parseJsonOption(readFileSync(0, "utf8"), "--body-stdin");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function methodOption(args: string[], hasBody = false): string {
|
||||
const method = (stringOption(args, "--method") ?? (hasBody ? "POST" : "GET")).toUpperCase();
|
||||
if (!["GET", "HEAD", "POST", "DELETE", "PUT", "PATCH"].includes(method)) throw new Error(`unsupported --method ${method}`);
|
||||
if (hasBody && (method === "GET" || method === "HEAD")) throw new Error(`microservice proxy cannot send a request body with ${method}`);
|
||||
return method;
|
||||
}
|
||||
|
||||
@@ -98,7 +128,8 @@ export async function runMicroserviceCommand(_config: UniDeskConfig, args: strin
|
||||
if (action === "proxy") {
|
||||
const id = requireId(idArg, "microservice proxy");
|
||||
const path = requireProxyPath(pathArg);
|
||||
return summarizeMicroserviceProxyResponse(coreInternalFetch(`/api/microservices/${encodeId(id)}/proxy${path}`, { method: methodOption(args) }), args);
|
||||
const body = requestBodyOption(args);
|
||||
return summarizeMicroserviceProxyResponse(coreInternalFetch(`/api/microservices/${encodeId(id)}/proxy${path}`, { method: methodOption(args, body !== undefined), body }), args);
|
||||
}
|
||||
throw new Error("microservice command must be one of: list, status, health, proxy");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user