square-sdk-cli 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,99 @@ All notable changes to this project are documented here. The format follows
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project uses
5
5
  [Semantic Versioning](https://semver.org/) (pre-1.0: minor versions may break).
6
6
 
7
+ ## [Unreleased]
8
+
9
+ ### Fixed
10
+
11
+ - Errors no longer suggest a request id is always available: Square v2
12
+ responses usually omit one. `--verbose` now prints the Cloudflare `cf-ray`
13
+ trace id, which those responses do include.
14
+ - `config set <profile> --token <new>` no longer resets the profile's
15
+ environment to `sandbox` when `--env` is omitted. Existing environments are
16
+ preserved; new profiles still default to sandbox.
17
+ - `--env` now overrides the environment of the selected `--profile` (it used to
18
+ be silently ignored). `SQUARE_ENVIRONMENT` still applies only when the token
19
+ comes from `--token`/`SQUARE_ACCESS_TOKEN`, so an ambient variable cannot
20
+ redirect a profile.
21
+ - `--dry-run` no longer shows an `idempotencyKey` that was never used, and an
22
+ aborted production confirmation no longer mints one.
23
+ - A closed pipe (`square payments list --stream | head -5`) exits quietly with
24
+ status 0 instead of printing an EPIPE stack trace.
25
+ - Config saves are atomic (temp file + rename), so an interrupted write can no
26
+ longer truncate every saved profile.
27
+ - The config file is created `0600` inside a `0700` directory, and an existing
28
+ file with looser permissions is tightened on read.
29
+ - Buffered stdout/stderr is flushed by letting the process exit naturally
30
+ instead of calling `process.exit()`, which truncated piped output; an
31
+ unref'd timer forces the exit if SDK handles keep the loop alive.
32
+ - A `--field` path that conflicts with the existing body reports which parent
33
+ conflicts (`"a.b" is already a number`) instead of throwing a bare
34
+ `TypeError`, and no longer silently loses writes into an array.
35
+ - Method and namespace help no longer print the internal arity number.
36
+
37
+ ### Added
38
+
39
+ - `--` ends flag parsing, and `--field=value` is documented for values that
40
+ start with `--` (e.g. `--note=--hello`).
41
+ - `--timeout <seconds>` and `--retry <n>` are forwarded to the SDK client
42
+ (`timeoutInSeconds`, `maxRetries`); `--retry 0` disables retries.
43
+ - `square search <term>` lists namespaces and methods whose name matches, with
44
+ no credentials required; multi-word terms match a path
45
+ (`square search "terminal actions"`).
46
+ - `--select a,b.c` keeps only the listed fields (per item when the result is a
47
+ list), `--table` renders a list as an aligned table, and `--output <file>`
48
+ writes the result to a file instead of stdout.
49
+ - `--json-output` now renders errors as a single JSON object on stderr.
50
+ - `square completion bash|zsh|fish` prints a completion script generated from
51
+ the installed SDK.
52
+ - `square doctor [--offline]` checks runtime, config permissions, credentials,
53
+ keychain availability and API reachability; exits 1 when a check fails.
54
+ - `square config set <profile> --keychain` stores the token in the OS keychain
55
+ (macOS `security`, Linux `secret-tool`); `config get/list/remove` are
56
+ keychain-aware, and re-setting without `--keychain` moves it back to the file.
57
+
58
+ ### Changed
59
+
60
+ - Unknown namespace and method errors now suggest the closest name
61
+ (`Did you mean "payments"?`) instead of always dumping the full valid list.
62
+
63
+ ### Internal
64
+
65
+ - Biome provides linting and formatting (`bun run lint`, `lint:fix`, `format`),
66
+ enforced in CI and `prepublishOnly`.
67
+ - `bun test --coverage` enforces line/function thresholds (`bunfig.toml`).
68
+ - CI runs on macOS as well as Ubuntu; the node smoke test also covers
69
+ `--version` and `search`.
70
+ - The weekly newest-SDK workflow now builds the bundle and smoke-tests it on
71
+ node, so bundle-only breakage is caught too.
72
+ - Pushing a `v*` tag publishes to npm with provenance via npm Trusted
73
+ Publishing (OIDC), so no `NPM_TOKEN` secret is needed; the tag must match
74
+ `package.json`.
75
+ - `run()` takes one options object (`{ deps, stdout, stderr }`) instead of four
76
+ positional parameters.
77
+ - API calls execute by walking straight to the namespace on the authenticated
78
+ client, instead of rebuilding the whole SDK tree a second time.
79
+ - Config file IO uses `fs/promises`, matching the async API it already exposed.
80
+ - `prompt.ts` builds its prompter from injectable streams (`createPrompter`),
81
+ so confirmations, hidden token entry and stdin reading are covered by tests.
82
+ - Added subprocess e2e tests for broken pipes (real EPIPE) and piped output
83
+ larger than the pipe buffer, plus integration coverage for `--` and nested
84
+ help output.
85
+ - Documented exit codes and CI usage, and added `CONTRIBUTING.md` with the
86
+ module layout and conventions.
87
+
88
+ ### Added
89
+
90
+ - `square --version` prints the CLI and Square SDK versions (with
91
+ `--json-output`, as JSON).
92
+ - `--raw-page` returns a single page's raw API response envelope, cursor
93
+ included, instead of flattening every page. Pair it with `--cursor <c>` to
94
+ resume where the previous page stopped; it is rejected together with
95
+ `--stream`.
96
+ - `--verbose` prints the call, environment, auth source and duration to stderr,
97
+ and on failure the raw error body, cause and stack trace.
98
+ - Square API errors now print the API's `request id` when it is available.
99
+
7
100
  ## [0.2.0] - 2026-10-12
8
101
 
9
102
  ### ⚠️ Behavior changes (check scripts that use the CLI)
package/README.md CHANGED
@@ -31,7 +31,9 @@ npm install -g . # or: bun link
31
31
 
32
32
  Credentials are stored on your machine at
33
33
  `~/.config/square-cli/config.json` (XDG-aware; override with `SQUARE_CLI_CONFIG`).
34
- Tokens are written with `0600` permissions and masked when read back.
34
+ Writes are atomic (temp file + rename), so a crash can't truncate your saved
35
+ profiles. Tokens are written with `0600` permissions — an existing file with
36
+ looser permissions is tightened on read — and masked when read back.
35
37
 
36
38
  ```bash
37
39
  square config set sandbox --env sandbox # prompts for the token (hidden)
@@ -47,9 +49,28 @@ square whoami # check the token: merchant, environment, tok
47
49
  Omitting `--token` (or passing `--token -`) keeps the token out of your shell
48
50
  history. `--token <ACCESS_TOKEN>` inline still works.
49
51
 
52
+ ### OS keychain
53
+
54
+ Pass `--keychain` to keep the token out of the config file entirely (macOS
55
+ `security`, Linux `secret-tool`):
56
+
57
+ ```bash
58
+ square config set prod --env production --keychain # token goes to the keychain
59
+ square config get prod # token: (stored in keychain)
60
+ square config set prod --token - # moves it back to the file
61
+ ```
62
+
63
+ Keychain profiles are marked `(keychain)` in `config list`, and removing the
64
+ profile removes the keychain entry. Windows has no supported backend here, so
65
+ `--keychain` reports an error and file storage should be used.
66
+
50
67
  Auth resolution order: `--profile <name>` → `--token` / `SQUARE_ACCESS_TOKEN`
51
68
  env → default profile.
52
69
 
70
+ An explicit `--env` overrides the environment stored in the selected profile.
71
+ `SQUARE_ENVIRONMENT` is only consulted when the token comes from `--token` or
72
+ `SQUARE_ACCESS_TOKEN` — it never redirects a profile.
73
+
53
74
  ## Usage
54
75
 
55
76
  ```
@@ -61,6 +82,8 @@ square <namespace> [<sub-namespace>...] <method> [--json '<body>'] [--field.path
61
82
  square --help
62
83
  square payments --help
63
84
  square terminal actions create --help
85
+ square search payment # find namespaces/methods by name
86
+ square search "terminal actions" # multi-word terms match a path
64
87
 
65
88
  # Call methods
66
89
  square locations list
@@ -85,12 +108,87 @@ square customers list --sortField DEFAULT --sortOrder ASC --stream
85
108
  numeric path segments create arrays; values are JSON-coerced, falling back to string)
86
109
  - `--field.path:str value` — same, but the value is always kept as a string
87
110
  (e.g. `--address.postalCode:str 94103`, which would otherwise become a number)
111
+ - `--field.path=value` — same as the space form, and the way to pass a value
112
+ that starts with `--` (e.g. `--note=--hello`)
113
+ - `--` — ends flag parsing; every later token is treated as a namespace or
114
+ method name
115
+
116
+ A field whose name matches a global flag (`token`, `env`, `json`, `profile`,
117
+ `stream`, `raw-page`, `dry-run`, `max-items`, `max-pages`, `timeout`, `retry`,
118
+ `select`, `output`, `table`, `yes`, `verbose`, `keychain`, `offline`,
119
+ `json-output`, `help`, `version`) can't be set with `--name`; pass it through
120
+ `--json` instead.
88
121
 
89
122
  ```bash
90
123
  square payments create --json @payment.json --dry-run # preview, nothing sent
91
124
  cat payment.json | square payments create --json -
92
125
  ```
93
126
 
127
+ ### Pagination and cursors
128
+
129
+ By default `list` calls fetch every page and print one flattened array (cap it
130
+ with `--max-items`/`--max-pages`), and `--stream` emits one item per line as
131
+ NDJSON. To work page by page, `--raw-page` returns a single page's API response
132
+ envelope — cursor included — without fetching any further pages:
133
+
134
+ ```bash
135
+ square payments list --raw-page --limit 100
136
+ square payments list --raw-page --limit 100 --cursor <cursor-from-previous-page>
137
+ ```
138
+
139
+ `--raw-page` cannot be combined with `--stream`, and `--max-items`/`--max-pages`
140
+ don't apply to it.
141
+
142
+ ### Shaping output
143
+
144
+ ```bash
145
+ square payments list --select id,amountMoney.amount # keep only these fields
146
+ square payments list --select id,status --table # aligned text table
147
+ square payments list --json-output --output payments.json
148
+ square payments list --stream --output payments.ndjson
149
+ ```
150
+
151
+ - `--select a,b.c` keeps the listed dotted fields (arrays are mapped per item,
152
+ and it works with `--stream`)
153
+ - `--table` renders a list result as an aligned table; it needs a list and
154
+ can't be combined with `--json-output`, `--stream`, or `--raw-page`
155
+ - `--output <file>` writes the result to a file instead of stdout (NDJSON when
156
+ streaming)
157
+
158
+ ### Shell completions
159
+
160
+ ```bash
161
+ square completion bash > /etc/bash_completion.d/square
162
+ square completion zsh > ~/.zsh/completions/_square
163
+ square completion fish > ~/.config/fish/completions/square.fish
164
+ ```
165
+
166
+ Completions are generated from the installed SDK, so they cover every namespace
167
+ and method after an SDK update.
168
+
169
+ ### Checking your setup
170
+
171
+ ```bash
172
+ square doctor # runtime, config file, credentials, keychain, API
173
+ square doctor --offline # skip the API round-trip
174
+ square doctor --json-output
175
+ ```
176
+
177
+ `doctor` exits 1 when a check fails and prints the reason (for example a config
178
+ file with loose permissions or a missing keychain entry).
179
+
180
+ ### Diagnosing calls
181
+
182
+ `--verbose` prints the call, environment, auth source and duration to stderr,
183
+ and on failure includes the raw error body, underlying cause, and stack trace.
184
+ Square's v2 responses usually carry no request id, so errors don't print one;
185
+ with `--verbose` the Cloudflare `cf-ray` trace id is shown instead — that's the
186
+ value to give Square support. If an API does return a request id (header or
187
+ `request_id` in the body), it is printed too.
188
+
189
+ Unknown namespace and method names suggest the closest match
190
+ (`Did you mean "payments"?`) instead of dumping hundreds of valid names.
191
+
94
192
  ### Flags
95
193
 
96
194
  | Flag | Description |
@@ -101,12 +199,36 @@ cat payment.json | square payments create --json -
101
199
  | `--json '<j>'` | JSON request body (`@file` or `-` for stdin) |
102
200
  | `--json-output` | compact JSON output (no pretty print) |
103
201
  | `--stream` | stream pageable results as NDJSON |
104
- | `--dry-run` | print the request without sending it |
202
+ | `--raw-page` | return one raw page (with its cursor) instead of auto-paging |
203
+ | `--dry-run` | print the request without sending it (no idempotency key is generated) |
105
204
  | `--max-items <n>` | stop paging after n items |
106
205
  | `--max-pages <n>` | stop paging after n pages |
206
+ | `--timeout <seconds>` | HTTP timeout per request (SDK default if omitted) |
207
+ | `--retry <n>` | retry failed requests n times (SDK default 2; 0 disables) |
208
+ | `--select <a,b.c>` | keep only these fields in the output |
209
+ | `--table` | render a list result as an aligned table |
210
+ | `--output <file>` | write the result to a file instead of stdout |
107
211
  | `--yes` | skip the production write confirmation |
212
+ | `--verbose` | request diagnostics and full error details on stderr |
213
+ | `--keychain` | store/look up the token in the OS keychain (`config set`) |
214
+ | `--offline` | skip the API check (`doctor`) |
215
+ | `--version` | print the CLI and Square SDK versions |
108
216
  | `--help` | show help |
109
217
 
218
+ With `--json-output`, errors are emitted as a single JSON object on stderr
219
+ (`{"error":"square_api"|"cli","message":…,"requestId":…}`), so scripts can
220
+ parse failures instead of scraping prose.
221
+
222
+ ### Exit codes
223
+
224
+ | Code | Meaning |
225
+ | ---- | ------- |
226
+ | 0 | Success, help, or `--version` |
227
+ | 1 | Any failure: bad flags, unknown command, auth error, API error, failed `doctor` check, aborted confirmation |
228
+
229
+ `--stream` writes items as it goes; if the call fails mid-stream, already
230
+ emitted lines remain on stdout and the error goes to stderr.
231
+
110
232
  ### Safety defaults
111
233
 
112
234
  - **Production writes ask first.** Any call against `production` whose method
@@ -116,6 +238,7 @@ cat payment.json | square payments create --json -
116
238
  - **Automatic idempotency keys.** Write calls without an `idempotencyKey` get a
117
239
  random one, printed to stderr. To retry safely, pass it back with
118
240
  `--idempotencyKey <key>`. Endpoints that don't use a key ignore it.
241
+ `--dry-run` and aborted confirmations never generate one.
119
242
  - **Paging limits.** `list` calls fetch every page by default. Cap them with
120
243
  `--max-items` or `--max-pages`. (`--limit` is still the API's own page-size
121
244
  field.)
@@ -123,18 +246,66 @@ cat payment.json | square payments create --json -
123
246
  > Per-field help is structural only — the SDK's field schemas live in its
124
247
  > `.d.ts` types and aren't available at runtime. Use `--json` for full bodies.
125
248
 
249
+ ## Using in CI
250
+
251
+ ```yaml
252
+ - name: Fetch recent payments
253
+ env:
254
+ SQUARE_ACCESS_TOKEN: ${{ secrets.SQUARE_ACCESS_TOKEN }}
255
+ SQUARE_ENVIRONMENT: production
256
+ run: |
257
+ square doctor --offline
258
+ square payments list --sortField CREATED_AT --json-output --output payments.json
259
+ ```
260
+
261
+ - Use `SQUARE_ACCESS_TOKEN` + `SQUARE_ENVIRONMENT` instead of profiles in CI.
262
+ - `--json-output` plus `--output` keeps machine-readable results out of the log.
263
+ - Writes in production need `--yes`; `--retry 0` disables SDK retries when a
264
+ step must be single-shot.
265
+ - Exit code 1 on any failure, so the step fails naturally.
266
+
126
267
  ## Develop
127
268
 
128
269
  ```bash
129
- bun test # full suite (incl. SDK contract guard test)
130
- bun run typecheck # tsc --noEmit
270
+ bun install
271
+ bun test # full suite (incl. SDK contract guard test)
272
+ bun run test:coverage # same, with enforced coverage thresholds
273
+ bun run typecheck # tsc --noEmit
274
+ bun run lint # Biome lint + format check (bun run lint:fix to apply)
131
275
  bun bin/square.ts <namespace> <method>
132
276
  ```
133
277
 
278
+ CI runs lint, typecheck, tests with coverage thresholds, the build, and node
279
+ 18/20/22 smoke tests on both Ubuntu and macOS. A weekly job repeats the suite
280
+ against the newest `square` release and smoke-tests the rebuild bundle.
281
+
282
+ ## Releasing
283
+
284
+ Pushing a version tag publishes to npm with
285
+ [provenance](https://docs.npmjs.com/generating-provenance-statements):
286
+
287
+ ```bash
288
+ npm version patch -m "v%s" # bumps package.json, commits, tags v0.3.1
289
+ git push origin main
290
+ git push origin v0.3.1 # tag push triggers the Release workflow
291
+ ```
292
+
293
+ The tag must match `package.json` (`v0.3.1` ↔ `0.3.1`) or the workflow fails.
294
+
295
+ Authentication uses npm **Trusted Publishing** (OIDC) — no `NPM_TOKEN` secret
296
+ and no 2FA token to rotate. One-time setup on npmjs.com: package
297
+ `square-sdk-cli` → **Settings → Trusted publishing → Add publisher** →
298
+ GitHub Actions, owner `karthikdilliraj`, repository `square-sdk-cli`, workflow
299
+ `release.yml` (no environment). The workflow requires npm ≥ 11.5.1 and upgrades
300
+ npm itself.
301
+
302
+ `npm version` creates a lightweight tag, which `git push --follow-tags` skips —
303
+ push the tag explicitly as shown.
304
+
134
305
  ## Supported Square SDK versions
135
306
 
136
- Currently built and tested against **`square@44.1.0`** (pinned `^44.1.0` in
137
- `package.json`).
307
+ Currently built and tested against **`square@44.2.1`** (`^44.2.1` in
308
+ `package.json`, so any 44.x release is accepted).
138
309
 
139
310
  The CLI is **version-agnostic by design**: it reads the SDK's namespace/method
140
311
  tree at runtime, so new Square APIs, methods, and fields are picked up