Skip to content

Commit ee6d61d

Browse files
authored
feat: Add --raw-json flag and fix --json output (#669)
1 parent 543cd60 commit ee6d61d

18 files changed

Lines changed: 219 additions & 179 deletions

File tree

docs/ai.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Sends a request to supported LLMs using Box AI. This is intended for direct use,
1616
USAGE
1717
$ box ai:ask --prompt <value> --items <value>... [-t <value>] [--as-user <value>] [--no-color] [--json |
1818
--csv] [-s | --save-to-file-path <value>] [--fields <value>] [--bulk-file-path <value>] [-h] [-v] [-y] [-q]
19-
[--ai-agent <value>]
19+
[--ai-agent <value>] [--raw-json | ]
2020
2121
FLAGS
2222
-h, --help Show CLI help
@@ -36,6 +36,9 @@ FLAGS
3636
--json Output formatted JSON
3737
--no-color Turn off colors for logging
3838
--prompt=<value> (required) The prompt for the AI request
39+
--raw-json Output the raw API JSON response instead of the tsClient-normalized object fields.
40+
Added as a non-breaking compatibility flag for users who need JSON field names to
41+
match the API schema exactly. Implies --json.
3942
--save-to-file-path=<value> Override default file path to save report
4043
4144
DESCRIPTION
@@ -55,7 +58,7 @@ Sends an AI request to supported Large Language Models (LLMs) and extracts metad
5558
USAGE
5659
$ box ai:extract --prompt <value> --items <value>... [-t <value>] [--as-user <value>] [--no-color] [--json |
5760
--csv] [-s | --save-to-file-path <value>] [--fields <value>] [--bulk-file-path <value>] [-h] [-v] [-y] [-q]
58-
[--ai-agent <value>]
61+
[--raw-json | ] [--ai-agent <value>]
5962
6063
FLAGS
6164
-h, --help Show CLI help
@@ -76,6 +79,9 @@ FLAGS
7679
--json Output formatted JSON
7780
--no-color Turn off colors for logging
7881
--prompt=<value> (required) The prompt provided to a Large Language Model (LLM) in the request.
82+
--raw-json Output the raw API JSON response instead of the tsClient-normalized object fields.
83+
Added as a non-breaking compatibility flag for users who need JSON field names to
84+
match the API schema exactly. Implies --json.
7985
--save-to-file-path=<value> Override default file path to save report
8086
8187
DESCRIPTION

docs/integration-mappings.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ List Teams integration mappings
233233
```
234234
USAGE
235235
$ box integration-mappings:teams [-t <value>] [--as-user <value>] [--no-color] [--json | --csv] [-s | --save-to-file-path
236-
<value>] [--fields <value>] [--bulk-file-path <value>] [-h] [-v] [-y] [-q] [--max-items <value>] [--partner-item-id
237-
<value>] [--partner-item-type <value>] [--box-item-id <value>] [--box-item-type <value>]
236+
<value>] [--fields <value>] [--bulk-file-path <value>] [-h] [-v] [-y] [-q] [--max-items <value>] [--raw-json | ]
237+
[--partner-item-id <value>] [--partner-item-type <value>] [--box-item-id <value>] [--box-item-type <value>]
238238
239239
FLAGS
240240
-h, --help Show CLI help
@@ -258,6 +258,9 @@ FLAGS
258258
--partner-item-id=<value> ID of the mapped item, for which the mapping should be returned
259259
--partner-item-type=<value> Mapped item type, for which the mapping should be returned, value is one of: channel,
260260
team
261+
--raw-json Output the raw API JSON response instead of the tsClient-normalized object fields.
262+
Added as a non-breaking compatibility flag for users who need JSON field names to
263+
match the API schema exactly. Implies --json.
261264
--save-to-file-path=<value> Override default file path to save report
262265
263266
DESCRIPTION
@@ -358,8 +361,8 @@ List Teams integration mappings
358361
```
359362
USAGE
360363
$ box integration-mappings:teams:list [-t <value>] [--as-user <value>] [--no-color] [--json | --csv] [-s | --save-to-file-path
361-
<value>] [--fields <value>] [--bulk-file-path <value>] [-h] [-v] [-y] [-q] [--max-items <value>] [--partner-item-id
362-
<value>] [--partner-item-type <value>] [--box-item-id <value>] [--box-item-type <value>]
364+
<value>] [--fields <value>] [--bulk-file-path <value>] [-h] [-v] [-y] [-q] [--max-items <value>] [--raw-json | ]
365+
[--partner-item-id <value>] [--partner-item-type <value>] [--box-item-id <value>] [--box-item-type <value>]
363366
364367
FLAGS
365368
-h, --help Show CLI help
@@ -383,6 +386,9 @@ FLAGS
383386
--partner-item-id=<value> ID of the mapped item, for which the mapping should be returned
384387
--partner-item-type=<value> Mapped item type, for which the mapping should be returned, value is one of: channel,
385388
team
389+
--raw-json Output the raw API JSON response instead of the tsClient-normalized object fields.
390+
Added as a non-breaking compatibility flag for users who need JSON field names to
391+
match the API schema exactly. Implies --json.
386392
--save-to-file-path=<value> Override default file path to save report
387393
388394
DESCRIPTION
@@ -404,7 +410,7 @@ Update Teams integration mapping
404410
```
405411
USAGE
406412
$ box integration-mappings:teams:update ID [-t <value>] [--as-user <value>] [--no-color] [--json | --csv] [-s | --save-to-file-path
407-
<value>] [--fields <value>] [--bulk-file-path <value>] [-h] [-v] [-y] [-q] [--box-item-id <value>]
413+
<value>] [--fields <value>] [--bulk-file-path <value>] [-h] [-v] [-y] [-q] [--raw-json | ] [--box-item-id <value>]
408414
409415
ARGUMENTS
410416
ID ID of an integration mapping
@@ -423,6 +429,9 @@ FLAGS
423429
--fields=<value> Comma separated list of fields to show
424430
--json Output formatted JSON
425431
--no-color Turn off colors for logging
432+
--raw-json Output the raw API JSON response instead of the tsClient-normalized object fields.
433+
Added as a non-breaking compatibility flag for users who need JSON field names to
434+
match the API schema exactly. Implies --json.
426435
--save-to-file-path=<value> Override default file path to save report
427436
428437
DESCRIPTION

src/box-command.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,44 @@ class BoxCommand extends Command {
11871187
return client;
11881188
}
11891189

1190+
/**
1191+
* Returns true when raw API JSON output was requested.
1192+
*
1193+
* @returns {boolean} True if raw JSON output should be used
1194+
* @private
1195+
*/
1196+
_wantsRawJsonOutput() {
1197+
return Boolean(this.flags && this.flags['raw-json']);
1198+
}
1199+
1200+
/**
1201+
* Preserves default output behavior while adding raw JSON support.
1202+
*
1203+
* The generated TypeScript client can expose response objects with normalized
1204+
* field names that do not match the original API schema. That behavior was
1205+
* introduced as newer commands moved to `tsClient`. To fix the JSON output
1206+
* shape without a breaking change, commands that support `--raw-json` use this
1207+
* wrapper on the top-level response object before calling `output()`.
1208+
*
1209+
* @param {*} content The content to potentially replace with rawData
1210+
* @returns {*} The content to send to output formatting
1211+
*/
1212+
getOutputContentWithRawJsonSupport(content) {
1213+
if (typeof content === 'object' && content !== null) {
1214+
if (this._wantsRawJsonOutput()) {
1215+
return content.rawData ?? content;
1216+
}
1217+
1218+
if (Object.hasOwn(content, 'rawData')) {
1219+
const output = { ...content };
1220+
delete output.rawData;
1221+
return output;
1222+
}
1223+
}
1224+
1225+
return content;
1226+
}
1227+
11901228
/**
11911229
* Format data for output to stdout
11921230
* @param {*} content The content to output
@@ -1323,7 +1361,11 @@ class BoxCommand extends Command {
13231361
}
13241362

13251363
const page = await fetchPage(pageQueryParams);
1326-
const pageEntries = page.entries || [];
1364+
const rawPage =
1365+
typeof page?.rawData === 'object' && page.rawData !== null
1366+
? page.rawData
1367+
: page;
1368+
const pageEntries = rawPage.entries || page.entries || [];
13271369
entries.push(...pageEntries);
13281370
remaining -= pageEntries.length;
13291371
marker = page.nextMarker || page.next_marker;
@@ -1393,6 +1435,12 @@ class BoxCommand extends Command {
13931435
return 'json';
13941436
}
13951437

1438+
// Raw API payloads are only intended for JSON output, so `--raw-json`
1439+
// implicitly promotes the format to JSON without changing default behavior.
1440+
if (this._wantsRawJsonOutput()) {
1441+
return 'json';
1442+
}
1443+
13961444
if (this.flags.csv) {
13971445
return 'csv';
13981446
}
@@ -2214,6 +2262,14 @@ BoxCommand.flags = {
22142262
}),
22152263
};
22162264

2265+
BoxCommand.rawJsonFlags = Object.freeze({
2266+
'raw-json': Flags.boolean({
2267+
description:
2268+
'Output the raw API JSON response instead of the tsClient-normalized object fields. Added as a non-breaking compatibility flag for users who need JSON field names to match the API schema exactly. Implies --json.',
2269+
exclusive: ['csv'],
2270+
}),
2271+
});
2272+
22172273
BoxCommand.minFlags = _.pick(BoxCommand.flags, [
22182274
'no-color',
22192275
'help',

src/commands/ai/ask.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class AiAskCommand extends BoxCommand {
2323
}
2424

2525
let answer = await this.tsClient.ai.createAiAsk(options);
26-
delete answer.rawData;
27-
await this.output(answer);
26+
await this.output(this.getOutputContentWithRawJsonSupport(answer));
2827
}
2928
}
3029

@@ -96,6 +95,7 @@ AiAskCommand.flags = {
9695
}
9796
},
9897
}),
98+
...BoxCommand.rawJsonFlags,
9999
};
100100

101101
module.exports = AiAskCommand;

src/commands/ai/extract.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ class AiExtractCommand extends BoxCommand {
2020
}
2121

2222
let answer = await this.tsClient.ai.createAiExtract(options);
23-
24-
delete answer.rawData;
25-
await this.output(answer);
23+
await this.output(this.getOutputContentWithRawJsonSupport(answer));
2624
}
2725
}
2826

@@ -37,6 +35,7 @@ AiExtractCommand._endpoint = 'post_ai_extract';
3735
// Flags definition
3836
AiExtractCommand.flags = {
3937
...BoxCommand.flags,
38+
...BoxCommand.rawJsonFlags,
4039
prompt: Flags.string({
4140
required: true,
4241
description:

src/commands/hubs/collaborations/create.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ class HubsCreateCollaborationCommand extends BoxCommand {
4646
await this.tsClient.hubCollaborations.createHubCollaborationV2025R0(
4747
requestBody
4848
);
49-
delete collaboration.rawData;
50-
await this.output(collaboration);
49+
await this.output(collaboration.rawData ?? collaboration);
5150
}
5251
}
5352
HubsCreateCollaborationCommand.aliases = ['hubs:collaborations:add'];

src/commands/hubs/collaborations/get.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class HubsGetCollaborationCommand extends BoxCommand {
1010
await this.tsClient.hubCollaborations.getHubCollaborationByIdV2025R0(
1111
args.id
1212
);
13-
delete collaboration.rawData;
14-
await this.output(collaboration);
13+
await this.output(collaboration.rawData ?? collaboration);
1514
}
1615
}
1716

src/commands/hubs/collaborations/update.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class HubsUpdateCollaborationCommand extends BoxCommand {
1515
role: flags.role,
1616
}
1717
);
18-
delete collaboration.rawData;
19-
await this.output(collaboration);
18+
await this.output(collaboration.rawData ?? collaboration);
2019
}
2120
}
2221

src/commands/hubs/copy.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class HubsCopyCommand extends BoxCommand {
1717
}
1818

1919
const hub = await this.tsClient.hubs.copyHubV2025R0(args.id, requestBody);
20-
delete hub.rawData;
21-
await this.output(hub);
20+
await this.output(hub.rawData ?? hub);
2221
}
2322
}
2423

src/commands/hubs/create.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class HubsCreateCommand extends BoxCommand {
1616
}
1717

1818
const hub = await this.tsClient.hubs.createHubV2025R0(requestBody);
19-
delete hub.rawData;
20-
await this.output(hub);
19+
await this.output(hub.rawData ?? hub);
2120
}
2221
}
2322

0 commit comments

Comments
 (0)