Skip to content

Commit 70e7f2d

Browse files
committed
refactor: api command uses JSON-only output for both normal and dry-run
The api command is a JSON proxy — its output should always be JSON, not conditionally switch to human-readable markdown in dry-run mode. - Revert output config to 'json' (flag-only, no human formatter) - Dry-run writes JSON directly via writeJson (imperative, since flag-only mode doesn't intercept returns) - Remove formatDryRunRequest and its markdown imports entirely - Remove formatDryRunRequest test suite (6 tests)
1 parent 1617ab3 commit 70e7f2d

File tree

2 files changed

+8
-127
lines changed

2 files changed

+8
-127
lines changed

src/commands/api.ts

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import { buildSearchParams, rawApiRequest } from "../lib/api-client.js";
1010
import { buildCommand } from "../lib/command.js";
1111
import { ValidationError } from "../lib/errors.js";
1212
import { writeJson } from "../lib/formatters/json.js";
13-
import {
14-
escapeMarkdownInline,
15-
mdKvTable,
16-
renderMarkdown,
17-
safeCodeSpan,
18-
} from "../lib/formatters/markdown.js";
1913
import { validateEndpoint } from "../lib/input-validation.js";
2014
import { getDefaultSdkConfig } from "../lib/sentry-client.js";
2115
import type { Writer } from "../types/index.js";
@@ -991,45 +985,6 @@ export function buildDryRunRequest(
991985
};
992986
}
993987

994-
/**
995-
* Format a dry-run request preview as rendered markdown.
996-
*
997-
* Uses the standard mdKvTable layout for consistency with other commands.
998-
*
999-
* @internal Exported for testing
1000-
*/
1001-
export function formatDryRunRequest(request: DryRunRequest): string {
1002-
const lines: string[] = [];
1003-
lines.push(
1004-
`## <muted>Dry run</muted> — ${escapeMarkdownInline(request.method)} ${escapeMarkdownInline(request.url)}`
1005-
);
1006-
lines.push("");
1007-
1008-
const kvRows: [string, string][] = [
1009-
["Method", request.method],
1010-
["URL", request.url],
1011-
];
1012-
1013-
const headerEntries = Object.entries(request.headers);
1014-
if (headerEntries.length > 0) {
1015-
kvRows.push([
1016-
"Headers",
1017-
headerEntries.map(([k, v]) => `${k}: ${v}`).join(", "),
1018-
]);
1019-
}
1020-
1021-
if (request.body !== null) {
1022-
const bodyStr =
1023-
typeof request.body === "string"
1024-
? request.body
1025-
: JSON.stringify(request.body, null, 2);
1026-
kvRows.push(["Body", safeCodeSpan(bodyStr)]);
1027-
}
1028-
1029-
lines.push(mdKvTable(kvRows));
1030-
return renderMarkdown(lines.join("\n"));
1031-
}
1032-
1033988
/**
1034989
* Handle response output based on flags
1035990
* @internal Exported for testing
@@ -1197,7 +1152,7 @@ export async function resolveBody(
11971152
// Command Definition
11981153

11991154
export const apiCommand = buildCommand({
1200-
output: { json: true, human: formatDryRunRequest },
1155+
output: "json",
12011156
docs: {
12021157
brief: "Make an authenticated API request",
12031158
fullDescription:
@@ -1322,13 +1277,13 @@ export const apiCommand = buildCommand({
13221277

13231278
// Dry-run mode: show the resolved request without sending it
13241279
if (flags["dry-run"]) {
1325-
return {
1326-
data: buildDryRunRequest(flags.method, normalizedEndpoint, {
1327-
params,
1328-
headers,
1329-
body,
1330-
}),
1331-
};
1280+
const request = buildDryRunRequest(flags.method, normalizedEndpoint, {
1281+
params,
1282+
headers,
1283+
body,
1284+
});
1285+
writeJson(stdout, request, flags.fields);
1286+
return;
13321287
}
13331288

13341289
// Verbose mode: show request details (unless silent)

test/commands/api.test.ts

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ import {
1515
buildQueryParams,
1616
buildQueryParamsFromFields,
1717
buildRawQueryParams,
18-
type DryRunRequest,
1918
dataToQueryParams,
2019
extractJsonBody,
21-
formatDryRunRequest,
2220
handleResponse,
2321
normalizeEndpoint,
2422
normalizeFields,
@@ -1829,75 +1827,3 @@ describe("buildDryRunRequest", () => {
18291827
expect(request.body).toBe('{"raw":"string"}');
18301828
});
18311829
});
1832-
1833-
describe("formatDryRunRequest", () => {
1834-
const req = (overrides: Partial<DryRunRequest> = {}): DryRunRequest => ({
1835-
method: "GET",
1836-
url: "https://sentry.io/api/0/organizations/",
1837-
headers: {},
1838-
body: null,
1839-
...overrides,
1840-
});
1841-
1842-
test("includes method and URL", () => {
1843-
const output = formatDryRunRequest(req());
1844-
1845-
expect(output).toContain("GET");
1846-
expect(output).toContain("https://sentry.io/api/0/organizations/");
1847-
expect(output).toContain("Dry run");
1848-
});
1849-
1850-
test("includes headers", () => {
1851-
const output = formatDryRunRequest(
1852-
req({
1853-
method: "POST",
1854-
url: "https://sentry.io/api/0/issues/",
1855-
headers: {
1856-
"Content-Type": "application/json",
1857-
Authorization: "Bearer token",
1858-
},
1859-
})
1860-
);
1861-
1862-
expect(output).toContain("Content-Type: application/json");
1863-
expect(output).toContain("Authorization: Bearer token");
1864-
});
1865-
1866-
test("includes JSON body", () => {
1867-
const output = formatDryRunRequest(
1868-
req({
1869-
method: "PUT",
1870-
url: "https://sentry.io/api/0/issues/123/",
1871-
body: { status: "resolved" },
1872-
})
1873-
);
1874-
1875-
expect(output).toContain("Body");
1876-
expect(output).toContain('"status": "resolved"');
1877-
});
1878-
1879-
test("includes string body as-is", () => {
1880-
const output = formatDryRunRequest(
1881-
req({
1882-
method: "POST",
1883-
url: "https://sentry.io/api/0/events/",
1884-
body: "raw-body-content",
1885-
})
1886-
);
1887-
1888-
expect(output).toContain("Body");
1889-
expect(output).toContain("raw-body-content");
1890-
});
1891-
1892-
test("omits body when null", () => {
1893-
const output = formatDryRunRequest(req());
1894-
1895-
expect(output).not.toContain("Body");
1896-
});
1897-
1898-
test("omits headers row when empty", () => {
1899-
const output = formatDryRunRequest(req());
1900-
1901-
expect(output).not.toContain("Headers");
1902-
});
1903-
});

0 commit comments

Comments
 (0)