Skip to content

Commit 0570e52

Browse files
committed
[DX-793] Add JSON envelope documentation to README
1 parent 197120f commit 0570e52

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

AGENTS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ All output helpers use the `format` prefix and are exported from `src/utils/outp
197197
- **Count labels**: `formatCountLabel(n, "message")` — cyan count + pluralized label.
198198
- **Limit warnings**: `formatLimitWarning(count, limit, "items")` — yellow warning if results truncated.
199199
- **JSON guard**: All human-readable output (progress, success, listening messages) must be wrapped in `if (!this.shouldOutputJson(flags))` so it doesn't pollute `--json` output. Only JSON payloads should be emitted when `--json` is active.
200-
- **JSON errors**: In catch blocks, use `this.handleCommandError(error, flags, component, context?)` for consistent error handling. It logs the event, emits JSON error when `--json` is active, and calls `this.error()` for human-readable output. For non-standard error flows, use `this.jsonError()` directly.
200+
- **JSON envelope**: Use `this.formatJsonRecord(type, data, flags)` for all JSON output. It wraps data in `{type, command, success?, ...data}`. Types: `"result"` (one-shot), `"event"` (streaming), `"error"`, `"log"` (verbose). Do NOT add ad-hoc `success: true/false` — the envelope handles it. `--json` produces compact single-line output (NDJSON for streaming). `--pretty-json` is unchanged.
201+
- **JSON errors**: In catch blocks, use `this.handleCommandError(error, flags, component, context?)` for consistent error handling. It logs the event, emits JSON error when `--json` is active, and calls `this.error()` for human-readable output. For non-standard error flows, use `this.jsonError()` directly (it uses `formatJsonRecord("error", ...)` internally).
201202
- **History output**: Use `[index] timestamp` ordering: `` `${formatIndex(index + 1)} ${formatTimestamp(timestamp)}` ``. Consistent across all history commands (channels, logs, connection-lifecycle, push).
202203
203204
### Additional output patterns (direct chalk, not helpers)

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4103,6 +4103,19 @@ EXAMPLES
41034103
_See code: [src/commands/support/contact.ts](https://github.com/ably/ably-cli/blob/v0.17.0/src/commands/support/contact.ts)_
41044104
<!-- commandsstop -->
41054105

4106+
## JSON Output
4107+
4108+
All commands support `--json` for machine-readable output and `--pretty-json` for human-readable formatted JSON.
4109+
4110+
When using `--json`, every record is wrapped in a standard envelope:
4111+
4112+
- **`type`**`"result"`, `"event"`, `"error"`, or `"log"`
4113+
- **`command`** — the command that produced the record (e.g. `"channels:publish"`)
4114+
- **`success`**`true` or `false` (only on `"result"` and `"error"` types)
4115+
- Additional fields are command-specific
4116+
4117+
Streaming commands (subscribe, logs) emit one JSON object per line (NDJSON).
4118+
41064119
## Environment Variables
41074120

41084121
The CLI supports the following environment variables for authentication and configuration:

docs/Project-Structure.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ This document outlines the directory structure of the Ably CLI project.
105105
│ │ ├── mock-ably-spaces.ts # Mock Ably Spaces SDK
106106
│ │ ├── mock-config-manager.ts # MockConfigManager (provides test auth)
107107
│ │ ├── mock-control-api-keys.ts # Mock Control API key responses
108-
│ │ └── ably-event-emitter.ts # Event emitter helper for mock SDKs
108+
│ │ ├── ably-event-emitter.ts # Event emitter helper for mock SDKs
109+
│ │ └── ndjson.ts # NDJSON parsing helpers (parseNdjsonLines, filterByType)
109110
│ ├── unit/ # Fast, mocked tests
110111
│ │ ├── setup.ts # Unit test setup
111112
│ │ ├── base/ # Base command class tests

0 commit comments

Comments
 (0)