|
| 1 | +import { defineCommand } from "../core/define-command.ts"; |
| 2 | +import { resolveConfig } from "../config/index.ts"; |
| 3 | +import { logger } from "../core/logger.ts"; |
| 4 | +import { UserError } from "../core/errors.ts"; |
| 5 | +import { VERSION } from "../core/version.ts"; |
| 6 | + |
| 7 | +const BASE_URL = "https://api.bunny.net"; |
| 8 | + |
| 9 | +const COMMAND = "api <method> [path]"; |
| 10 | +const DESCRIPTION = "Make a raw API request to bunny.net."; |
| 11 | + |
| 12 | +const VALID_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const; |
| 13 | + |
| 14 | +interface ApiArgs { |
| 15 | + method: string; |
| 16 | + path?: string; |
| 17 | + body?: string; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Make a raw authenticated HTTP request to the bunny.net API. |
| 22 | + * |
| 23 | + * A convenient low-level way to call any bunny.net API endpoint. |
| 24 | + * Auth is handled automatically via your configured API key. |
| 25 | + * |
| 26 | + * @example |
| 27 | + * ```bash |
| 28 | + * # List pull zones |
| 29 | + * bunny api GET /pullzone |
| 30 | + * |
| 31 | + * # List databases |
| 32 | + * bunny api GET /database/v2/databases |
| 33 | + * |
| 34 | + * # Create a database |
| 35 | + * bunny api POST /database/v2/databases --body '{"name":"test","storage_region":"DE","primary_regions":["DE"]}' |
| 36 | + * |
| 37 | + * # Delete a resource |
| 38 | + * bunny api DELETE /dnszone/12345 |
| 39 | + * |
| 40 | + * # Pipe body from stdin |
| 41 | + * echo '{"name":"test"}' | bunny api POST /database/v2/databases |
| 42 | + * ``` |
| 43 | + */ |
| 44 | +export const apiCommand = defineCommand<ApiArgs>({ |
| 45 | + command: COMMAND, |
| 46 | + describe: DESCRIPTION, |
| 47 | + examples: [ |
| 48 | + ["$0 api GET /pullzone", "List pull zones"], |
| 49 | + ["$0 api GET /database/v2/databases", "List databases"], |
| 50 | + ["$0 api POST /database/v2/databases --body '{\"name\":\"test\"}'", "Create with JSON body"], |
| 51 | + ], |
| 52 | + |
| 53 | + builder: (yargs) => |
| 54 | + yargs |
| 55 | + .positional("method", { |
| 56 | + type: "string", |
| 57 | + describe: "HTTP method (GET, POST, PUT, PATCH, DELETE)", |
| 58 | + demandOption: true, |
| 59 | + }) |
| 60 | + .positional("path", { |
| 61 | + type: "string", |
| 62 | + describe: "API endpoint path (e.g. /pullzone)", |
| 63 | + }) |
| 64 | + .option("body", { |
| 65 | + alias: "b", |
| 66 | + type: "string", |
| 67 | + describe: "JSON request body", |
| 68 | + }), |
| 69 | + |
| 70 | + handler: async ({ method: rawMethod, path, body: bodyFlag, profile, output, verbose, apiKey }) => { |
| 71 | + const method = rawMethod.toUpperCase(); |
| 72 | + if (!VALID_METHODS.includes(method as any)) { |
| 73 | + throw new UserError( |
| 74 | + `Invalid method: ${rawMethod}`, |
| 75 | + `Use one of: ${VALID_METHODS.join(", ")}`, |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + if (!path) { |
| 80 | + throw new UserError( |
| 81 | + "Path is required.", |
| 82 | + "Example: bunny api GET /pullzone", |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + const config = resolveConfig(profile, apiKey); |
| 87 | + if (!config.apiKey) { |
| 88 | + throw new UserError("Not logged in.", 'Run "bunny login" to authenticate.'); |
| 89 | + } |
| 90 | + |
| 91 | + const baseUrl = config.apiUrl ?? BASE_URL; |
| 92 | + const url = `${baseUrl}${path.startsWith("/") ? path : `/${path}`}`; |
| 93 | + |
| 94 | + // Body: --body flag, or read from stdin if not a TTY |
| 95 | + let requestBody: string | undefined = bodyFlag; |
| 96 | + if (!requestBody && !process.stdin.isTTY && method !== "GET") { |
| 97 | + const chunks: Buffer[] = []; |
| 98 | + for await (const chunk of process.stdin) { |
| 99 | + chunks.push(chunk); |
| 100 | + } |
| 101 | + const stdin = Buffer.concat(chunks).toString("utf-8").trim(); |
| 102 | + if (stdin) requestBody = stdin; |
| 103 | + } |
| 104 | + |
| 105 | + if (requestBody) { |
| 106 | + // Validate JSON |
| 107 | + try { |
| 108 | + JSON.parse(requestBody); |
| 109 | + } catch { |
| 110 | + throw new UserError("Invalid JSON body.", "Ensure --body contains valid JSON."); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + const headers: Record<string, string> = { |
| 115 | + AccessKey: config.apiKey, |
| 116 | + "User-Agent": `bunny-cli/${VERSION}`, |
| 117 | + Accept: "application/json", |
| 118 | + }; |
| 119 | + |
| 120 | + if (requestBody) { |
| 121 | + headers["Content-Type"] = "application/json"; |
| 122 | + } |
| 123 | + |
| 124 | + if (verbose) { |
| 125 | + logger.debug(`→ ${method} ${url}`, true); |
| 126 | + if (requestBody) logger.debug(`→ Body: ${requestBody}`, true); |
| 127 | + } |
| 128 | + |
| 129 | + const res = await fetch(url, { |
| 130 | + method, |
| 131 | + headers, |
| 132 | + body: requestBody, |
| 133 | + }); |
| 134 | + |
| 135 | + if (verbose) { |
| 136 | + logger.debug(`← ${res.status} ${res.statusText}`, true); |
| 137 | + } |
| 138 | + |
| 139 | + const text = await res.text(); |
| 140 | + |
| 141 | + // Try to parse and pretty-print JSON |
| 142 | + let parsed: unknown; |
| 143 | + try { |
| 144 | + parsed = JSON.parse(text); |
| 145 | + } catch { |
| 146 | + // Not JSON — output raw |
| 147 | + if (!res.ok) { |
| 148 | + throw new UserError(`${res.status} ${res.statusText}`, text || undefined); |
| 149 | + } |
| 150 | + if (text) logger.log(text); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + if (!res.ok) { |
| 155 | + if (output === "json") { |
| 156 | + logger.log(JSON.stringify({ error: parsed, status: res.status }, null, 2)); |
| 157 | + } else { |
| 158 | + const msg = typeof parsed === "object" && parsed !== null |
| 159 | + ? (parsed as any).detail ?? (parsed as any).Message ?? (parsed as any).title ?? `${res.status} ${res.statusText}` |
| 160 | + : `${res.status} ${res.statusText}`; |
| 161 | + throw new UserError(msg); |
| 162 | + } |
| 163 | + process.exit(1); |
| 164 | + } |
| 165 | + |
| 166 | + logger.log(JSON.stringify(parsed, null, 2)); |
| 167 | + }, |
| 168 | +}); |
0 commit comments