insightfulpipe 0.1.4 → 0.3.0
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 +71 -0
- package/README.md +65 -2
- package/SKILL.md +8 -5
- package/bin/cli.js +4 -1
- package/package.json +7 -3
- package/skills/insightfulpipe/SKILL.md +8 -5
- package/src/api.js +88 -75
- package/src/cli/commands/discovery.js +80 -19
- package/src/cli/commands/execution.js +58 -17
- package/src/cli/commands/skills.js +454 -0
- package/src/cli/errors.js +167 -13
- package/src/cli/input.js +6 -0
- package/src/cli/program.js +18 -3
- package/src/cli/query-contexts.js +3 -2
- package/src/config.js +54 -2
- package/src/http.js +39 -0
- package/src/output.js +5 -1
- package/src/skills/agents.js +94 -0
- package/src/skills/catalog.js +177 -0
- package/src/skills/installer.js +331 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,76 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
**`insightfulpipe skills`: install InsightfulPipe skills from the terminal.** Modelled on
|
|
6
|
+
`openclaw skills install`, for our catalogue on insightfulpipe.com.
|
|
7
|
+
|
|
8
|
+
- `skills search [words] [--category]`, `skills info <skill>`, and `skills install <skill...>` (or
|
|
9
|
+
`--category <c>`). Install writes into Claude Code's global folder by default; `--agent openclaw |
|
|
10
|
+
codex | cursor | gemini-cli`, `--project` and `--dir` pick another.
|
|
11
|
+
- Before writing, every file is checked against its sha256 and the skill's content version, paths
|
|
12
|
+
that would escape the folder are refused, and SKILL.md must be named after the skill. Files land in
|
|
13
|
+
a staging folder and are renamed into place, so a failure leaves nothing half-written.
|
|
14
|
+
- Each install keeps a `.insightfulpipe-skill.json` marker. `skills list` shows installed skills and
|
|
15
|
+
available updates. `skills update <skill> | --all` keeps local edits unless `--force`. `skills
|
|
16
|
+
remove` only deletes folders this CLI installed and asks first (`--yes` when non-interactive).
|
|
17
|
+
- `skills download <skill>` saves the ZIP for uploading in Claude.ai or Claude Desktop.
|
|
18
|
+
- No login needed. The catalogue never receives the API token. `INSIGHTFULPIPE_SKILLS_URL` accepts only
|
|
19
|
+
insightfulpipe.com, its subdomains or localhost, so installs can't be pointed at a look-alike catalogue.
|
|
20
|
+
- Added CI: unit tests on Node 18, 20 and 22, plus a live install of a real skill into a throwaway
|
|
21
|
+
home folder.
|
|
22
|
+
|
|
23
|
+
**Clearer errors from connected platforms.**
|
|
24
|
+
|
|
25
|
+
- A rejection from a connected platform now reads `Pinterest Pages error 50 (HTTP 404): Pin not
|
|
26
|
+
found.`: the platform (the one named in the response, otherwise the one the command was run for),
|
|
27
|
+
its own error code, the status it answered with and its message. Before, the error read
|
|
28
|
+
`"message": true`. JSON output also carries `api_status_code`, the raw `api_error`, `details`,
|
|
29
|
+
`error_type` and `retry_after`, plus the app's write-outcome fields (`write_state`,
|
|
30
|
+
`ambiguous_write`, `outcome_unknown`, `status_code` and the like) and the response's `http_status`,
|
|
31
|
+
so a script can tell a write may have landed (any of those flags, or a status of 500 or more)
|
|
32
|
+
before it retries. Like the app, an error body with no `status_code` gets the HTTP status as one.
|
|
33
|
+
- `details` that arrive as an object are shown as JSON instead of `[object Object]`; string `details`
|
|
34
|
+
still read as before. A message or any of those fields longer than 2,000 characters is cut with a marker that
|
|
35
|
+
says how much was omitted, and the error is written out whole before the CLI exits, so a piped
|
|
36
|
+
stderr never receives half a JSON document.
|
|
37
|
+
- A 429 keeps `retry_after` in the JSON and adds `Tip: Retry after N seconds.` under `--table`.
|
|
38
|
+
- An error body with no message reports the HTTP status (`Request failed with status 500`).
|
|
39
|
+
- A request that fails after it may have reached the server (connection dropped, response cut off,
|
|
40
|
+
timeout, a 2xx that is not JSON) reports `outcome_unknown: true`, the app's own sign that a write may
|
|
41
|
+
have landed. A request that never left (no connection, a failed certificate check, an invalid
|
|
42
|
+
header) does not, and an invalid header never echoes the token.
|
|
43
|
+
- A response that stops arriving ends after the timeout passes with no new data; a slow but steady
|
|
44
|
+
one is never cut, and query results have no size cap.
|
|
45
|
+
- Requests identify themselves as `insightfulpipe-cli/<version>` (User-Agent).
|
|
46
|
+
- Needs Node.js 18.20 or later: older 18.x releases print a fetch warning on stderr ahead of the JSON.
|
|
47
|
+
|
|
48
|
+
## 0.2.0
|
|
49
|
+
|
|
50
|
+
**JSON is now the default output.** It is read by scripts and agents far more often than by eyes.
|
|
51
|
+
`--table` prints the previous human-readable columns. `--json` is still accepted and does nothing, so
|
|
52
|
+
existing scripts, docs and skills keep working unchanged.
|
|
53
|
+
|
|
54
|
+
- Errors follow the same contract: `{"status":"error","message":"…"}` on stderr by default, a plain
|
|
55
|
+
sentence under `--table`. Both still exit 1 and both stay off stdout, so `… | jq` is never polluted.
|
|
56
|
+
Auth and config failures were printing their own text and now go through the same formatter.
|
|
57
|
+
- `accounts` and `sources` show `Workspace ID` and `Brand ID`. Their absence is why customers
|
|
58
|
+
concluded the ids were unavailable — `sources` even advertised "with IDs" while showing only the
|
|
59
|
+
source's own id.
|
|
60
|
+
- New `insightfulpipe use` saves a default workspace/brand, and every query fills them in:
|
|
61
|
+
`use` alone adopts your only workspace, `use -w <id> -b <id>` picks explicitly, `use --clear`
|
|
62
|
+
forgets. `--workspace` / `--brand` override per call, and a body that states the ids is untouched.
|
|
63
|
+
- `brands` no longer requires `-w`: it resolves the saved scope, or your only workspace. It always
|
|
64
|
+
lists brand ids and names.
|
|
65
|
+
- `--version` reports the package version instead of a hardcoded string (it said 0.1.0 while 0.1.4
|
|
66
|
+
shipped).
|
|
67
|
+
- `--help` now teaches ONE form. The 0.1.x shapes (`query_contexts`, `sources`, `query_data`,
|
|
68
|
+
`execute_action`) still run exactly as before — a stored script does not care that they stopped
|
|
69
|
+
being advertised — they are just no longer listed, so a newcomer sees `accounts`, `brands`, `use`,
|
|
70
|
+
`query` and `action` and nothing redundant.
|
|
71
|
+
- Added `test/output-contract.test.js`, which asserts the new defaults, that the legacy shapes are
|
|
72
|
+
hidden from help yet still resolve, and that every 0.1.x command form runs.
|
|
73
|
+
|
|
3
74
|
## Unreleased
|
|
4
75
|
|
|
5
76
|
- Refactored the CLI into modular command registration under `src/cli/`
|
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Recommended workflow:
|
|
|
12
12
|
|
|
13
13
|
## Requirements
|
|
14
14
|
|
|
15
|
-
- Node.js 18+
|
|
15
|
+
- Node.js 18.20+
|
|
16
16
|
- An InsightfulPipe API token starting with `ip_sk_`
|
|
17
17
|
|
|
18
18
|
## Installation
|
|
@@ -46,6 +46,45 @@ The CLI also reads these environment variables:
|
|
|
46
46
|
- `INSIGHTFULPIPE_TOKEN`
|
|
47
47
|
- `INSIGHTFULPIPE_API_URL`
|
|
48
48
|
- `INSIGHTFULPIPE_TIMEOUT_MS`
|
|
49
|
+
- `INSIGHTFULPIPE_SKILLS_URL` (skills catalogue: `https://insightfulpipe.com` by default; only its subdomains or localhost are accepted)
|
|
50
|
+
|
|
51
|
+
## Skills
|
|
52
|
+
|
|
53
|
+
Install InsightfulPipe's marketing skills (Google Ads audits, GA4 reports, SEO and more) into your
|
|
54
|
+
AI agent's skills folder. No login is needed to install: skills are free to download. They read live
|
|
55
|
+
data through the InsightfulPipe MCP once it is connected.
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npx insightfulpipe skills search # browse every skill
|
|
59
|
+
npx insightfulpipe skills search google ads audit # search by words
|
|
60
|
+
npx insightfulpipe skills info google-ads-account-audit
|
|
61
|
+
npx insightfulpipe skills install google-ads-account-audit
|
|
62
|
+
npx insightfulpipe skills install --category seo # every skill in a category
|
|
63
|
+
npx insightfulpipe skills list # installed skills, and which have updates
|
|
64
|
+
npx insightfulpipe skills update --all # keeps your local edits unless --force
|
|
65
|
+
npx insightfulpipe skills remove google-ads-account-audit --yes
|
|
66
|
+
npx insightfulpipe skills download google-ads-account-audit # ZIP for Claude.ai or Claude Desktop
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Names can be `slug`, `category/slug` or `@insightfulpipe/slug`. A name that exists in two categories
|
|
70
|
+
must be written as `category/slug`.
|
|
71
|
+
|
|
72
|
+
Skills go to Claude Code's global folder by default. Choose another agent with `--agent`, the current
|
|
73
|
+
project with `--project`, or any folder with `--dir`:
|
|
74
|
+
|
|
75
|
+
| `--agent` | Global folder | `--project` folder |
|
|
76
|
+
|---|---|---|
|
|
77
|
+
| `claude-code` (default) | `~/.claude/skills` (or `$CLAUDE_CONFIG_DIR/skills`) | `.claude/skills` |
|
|
78
|
+
| `openclaw` | `~/.openclaw/skills` (or `$OPENCLAW_SKILLS_DIR`) | `skills` |
|
|
79
|
+
| `codex` | `~/.codex/skills` (or `$CODEX_HOME/skills`) | `.agents/skills` |
|
|
80
|
+
| `cursor` | `~/.cursor/skills` | `.agents/skills` |
|
|
81
|
+
| `gemini-cli` | `~/.gemini/skills` | `.agents/skills` |
|
|
82
|
+
|
|
83
|
+
Every file is checked against its sha256 before anything is written, and each installed skill keeps
|
|
84
|
+
a `.insightfulpipe-skill.json` marker. `list`, `update` and `remove` only touch folders that carry it,
|
|
85
|
+
and `update` will not overwrite files you edited unless you add `--force`. The catalogue never
|
|
86
|
+
receives your API token. `INSIGHTFULPIPE_SKILLS_URL` can point the commands at a staging copy on an
|
|
87
|
+
insightfulpipe.com subdomain or at localhost (for testing); any other host is refused.
|
|
49
88
|
|
|
50
89
|
## Core Commands
|
|
51
90
|
|
|
@@ -121,7 +160,31 @@ Set a request timeout:
|
|
|
121
160
|
node bin/cli.js config --set-timeout 15000
|
|
122
161
|
```
|
|
123
162
|
|
|
124
|
-
|
|
163
|
+
Output is **JSON by default** — it is read by scripts and agents far more often than by eyes. Add
|
|
164
|
+
`--table` when you want the human-readable view:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
insightfulpipe accounts # JSON
|
|
168
|
+
insightfulpipe accounts --table # aligned columns
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
`--json` is still accepted and does nothing, so existing scripts and docs keep working. Errors follow
|
|
172
|
+
the same rule: `{"status":"error","message":"…"}` on stderr, or a plain sentence under `--table`.
|
|
173
|
+
Either way they go to stderr and exit 1, so `insightfulpipe … | jq` is never polluted.
|
|
174
|
+
|
|
175
|
+
### Set your scope once
|
|
176
|
+
|
|
177
|
+
`workspace_id` and `brand_id` are required by most queries. Save them instead of repeating them:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
insightfulpipe use # adopt your only workspace
|
|
181
|
+
insightfulpipe use -w 299 -b 592 # or pick explicitly
|
|
182
|
+
insightfulpipe brands # brand ids and names
|
|
183
|
+
insightfulpipe query google-ads -b '{"action":"campaigns","account_id":"…"}'
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Per-call `--workspace` / `--brand` override the saved scope, and a body that states the ids itself is
|
|
187
|
+
never modified. `insightfulpipe use --clear` forgets them.
|
|
125
188
|
|
|
126
189
|
## Testing
|
|
127
190
|
|
package/SKILL.md
CHANGED
|
@@ -51,8 +51,11 @@ The `INSIGHTFULPIPE_TOKEN` environment variable should already be configured. Do
|
|
|
51
51
|
Every query follows: discover → schema → execute. Run commands directly — do not ask the user for permission to run each one.
|
|
52
52
|
|
|
53
53
|
```bash
|
|
54
|
-
#
|
|
55
|
-
insightfulpipe
|
|
54
|
+
# 0. (once) Save the workspace/brand so queries do not need the ids
|
|
55
|
+
insightfulpipe use
|
|
56
|
+
|
|
57
|
+
# 1. Get account IDs (workspace_id, brand_id, source IDs) — output is JSON by default
|
|
58
|
+
insightfulpipe accounts --platform <platform>
|
|
56
59
|
|
|
57
60
|
# 2. See available actions
|
|
58
61
|
insightfulpipe helper <platform>
|
|
@@ -74,7 +77,7 @@ insightfulpipe action <platform> -b '{"action":"<action>",...}' --yes
|
|
|
74
77
|
### Google Analytics — Top Pages
|
|
75
78
|
|
|
76
79
|
```bash
|
|
77
|
-
insightfulpipe accounts --platform google-analytics
|
|
80
|
+
insightfulpipe accounts --platform google-analytics
|
|
78
81
|
insightfulpipe helper google-analytics --actions pages
|
|
79
82
|
insightfulpipe query google-analytics -b '{
|
|
80
83
|
"action": "pages",
|
|
@@ -284,7 +287,7 @@ insightfulpipe prompts google-analytics --id 12 # Get specific prompt
|
|
|
284
287
|
## Gotchas
|
|
285
288
|
|
|
286
289
|
1. **Never guess the request body** — always run `helper <platform> --actions <action>` first.
|
|
287
|
-
2. **Replace ALL "xxx" placeholders** — get real values from `accounts --platform <platform
|
|
290
|
+
2. **Replace ALL "xxx" placeholders** — get real values from `accounts --platform <platform>`.
|
|
288
291
|
3. **GA property_id needs "properties/" prefix** — use `"properties/510157516"` not `"510157516"`.
|
|
289
292
|
4. **Date format is YYYY-MM-DD** — use `"2026-03-20"` not `"2026-03-20T00:00:00Z"`.
|
|
290
293
|
5. **Use `query` for reads, `action --yes` for writes** — using the wrong one will fail.
|
|
@@ -300,7 +303,7 @@ insightfulpipe prompts google-analytics --id 12 # Get specific prompt
|
|
|
300
303
|
```bash
|
|
301
304
|
# Discovery
|
|
302
305
|
insightfulpipe platforms # All supported platforms
|
|
303
|
-
insightfulpipe accounts --platform <platform>
|
|
306
|
+
insightfulpipe accounts --platform <platform> # Account IDs
|
|
304
307
|
insightfulpipe sources --platform <platform> # Sources with IDs
|
|
305
308
|
insightfulpipe brands --workspace <id> # Brand metadata
|
|
306
309
|
insightfulpipe whoami # Current user
|
package/bin/cli.js
CHANGED
|
@@ -8,6 +8,9 @@ try {
|
|
|
8
8
|
await program.parseAsync();
|
|
9
9
|
} catch (error) {
|
|
10
10
|
const message = error instanceof Error ? error.message : String(error);
|
|
11
|
-
|
|
11
|
+
// Output is JSON by default, and a caller parsing stdout must be able to parse failures too.
|
|
12
|
+
// `--table` keeps the plain sentence. Errors go to stderr either way, so `cmd | jq` stays clean.
|
|
13
|
+
const humanOutput = process.argv.includes('--table');
|
|
14
|
+
console.error(humanOutput ? `Error: ${message}` : JSON.stringify({ status: 'error', message }, null, 2));
|
|
12
15
|
process.exit(1);
|
|
13
16
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "insightfulpipe",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "InsightfulPipe CLI
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "InsightfulPipe CLI \u2014 query your marketing data from the terminal",
|
|
5
5
|
"bin": {
|
|
6
6
|
"insightfulpipe": "bin/cli.js"
|
|
7
7
|
},
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"test:live": "node --test test/live-smoke.test.js"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
|
-
"node": ">=18"
|
|
28
|
+
"node": ">=18.20"
|
|
29
29
|
},
|
|
30
30
|
"keywords": [
|
|
31
31
|
"insightfulpipe",
|
|
@@ -38,5 +38,9 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@napi-rs/keyring": "^1.2.0",
|
|
40
40
|
"commander": "^13.0.0"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/Vqwa/insightful-pipe-cli.git"
|
|
41
45
|
}
|
|
42
46
|
}
|
|
@@ -51,8 +51,11 @@ The `INSIGHTFULPIPE_TOKEN` environment variable should already be configured. Do
|
|
|
51
51
|
Every query follows: discover → schema → execute. Run commands directly — do not ask the user for permission to run each one.
|
|
52
52
|
|
|
53
53
|
```bash
|
|
54
|
-
#
|
|
55
|
-
insightfulpipe
|
|
54
|
+
# 0. (once) Save the workspace/brand so queries do not need the ids
|
|
55
|
+
insightfulpipe use
|
|
56
|
+
|
|
57
|
+
# 1. Get account IDs (workspace_id, brand_id, source IDs) — output is JSON by default
|
|
58
|
+
insightfulpipe accounts --platform <platform>
|
|
56
59
|
|
|
57
60
|
# 2. See available actions
|
|
58
61
|
insightfulpipe helper <platform>
|
|
@@ -74,7 +77,7 @@ insightfulpipe action <platform> -b '{"action":"<action>",...}' --yes
|
|
|
74
77
|
### Google Analytics — Top Pages
|
|
75
78
|
|
|
76
79
|
```bash
|
|
77
|
-
insightfulpipe accounts --platform google-analytics
|
|
80
|
+
insightfulpipe accounts --platform google-analytics
|
|
78
81
|
insightfulpipe helper google-analytics --actions pages
|
|
79
82
|
insightfulpipe query google-analytics -b '{
|
|
80
83
|
"action": "pages",
|
|
@@ -284,7 +287,7 @@ insightfulpipe prompts google-analytics --id 12 # Get specific prompt
|
|
|
284
287
|
## Gotchas
|
|
285
288
|
|
|
286
289
|
1. **Never guess the request body** — always run `helper <platform> --actions <action>` first.
|
|
287
|
-
2. **Replace ALL "xxx" placeholders** — get real values from `accounts --platform <platform
|
|
290
|
+
2. **Replace ALL "xxx" placeholders** — get real values from `accounts --platform <platform>`.
|
|
288
291
|
3. **GA property_id needs "properties/" prefix** — use `"properties/510157516"` not `"510157516"`.
|
|
289
292
|
4. **Date format is YYYY-MM-DD** — use `"2026-03-20"` not `"2026-03-20T00:00:00Z"`.
|
|
290
293
|
5. **Use `query` for reads, `action --yes` for writes** — using the wrong one will fail.
|
|
@@ -300,7 +303,7 @@ insightfulpipe prompts google-analytics --id 12 # Get specific prompt
|
|
|
300
303
|
```bash
|
|
301
304
|
# Discovery
|
|
302
305
|
insightfulpipe platforms # All supported platforms
|
|
303
|
-
insightfulpipe accounts --platform <platform>
|
|
306
|
+
insightfulpipe accounts --platform <platform> # Account IDs
|
|
304
307
|
insightfulpipe sources --platform <platform> # Sources with IDs
|
|
305
308
|
insightfulpipe brands --workspace <id> # Brand metadata
|
|
306
309
|
insightfulpipe whoami # Current user
|
package/src/api.js
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
|
+
import { bounded, fail } from './cli/errors.js';
|
|
1
2
|
import { getApiUrl, getRequestTimeoutMs, getToken } from './config.js';
|
|
3
|
+
import { readBody, USER_AGENT } from './http.js';
|
|
2
4
|
|
|
3
5
|
let _insecureWarned = false;
|
|
6
|
+
// Far above any real error body; past it the body is dropped unread instead of held in memory.
|
|
7
|
+
const MAX_ERROR_BYTES = 10 * 1024 * 1024;
|
|
8
|
+
// Failures before a request could reach the server: nothing can have been written.
|
|
9
|
+
const NOT_SENT_CODES = new Set([
|
|
10
|
+
'ECONNREFUSED',
|
|
11
|
+
'ENOTFOUND',
|
|
12
|
+
'EAI_AGAIN',
|
|
13
|
+
'ENETUNREACH',
|
|
14
|
+
'EHOSTUNREACH',
|
|
15
|
+
'UND_ERR_CONNECT_TIMEOUT',
|
|
16
|
+
]);
|
|
17
|
+
// Certificate checks happen in the handshake, before any request byte is sent.
|
|
18
|
+
const TLS_FAILURE = /^(CERT_|UNABLE_TO_|ERR_TLS_CERT_)|SELF_SIGNED/;
|
|
4
19
|
|
|
5
20
|
function warnIfInsecure() {
|
|
6
21
|
if (_insecureWarned) return;
|
|
@@ -15,16 +30,14 @@ const PLATFORM_SLUG_RE = /^[a-z0-9][a-z0-9-]*[a-z0-9]$/;
|
|
|
15
30
|
|
|
16
31
|
export function validatePlatformSlug(slug) {
|
|
17
32
|
if (!PLATFORM_SLUG_RE.test(slug) || slug.includes('--')) {
|
|
18
|
-
|
|
19
|
-
process.exit(1);
|
|
33
|
+
fail(`Invalid platform slug "${slug}". Use lowercase letters, numbers, and hyphens only.`);
|
|
20
34
|
}
|
|
21
35
|
}
|
|
22
36
|
|
|
23
37
|
function requireAuth() {
|
|
24
38
|
const token = getToken();
|
|
25
39
|
if (!token) {
|
|
26
|
-
|
|
27
|
-
process.exit(1);
|
|
40
|
+
fail('Not authenticated.', 'Run: insightfulpipe auth');
|
|
28
41
|
}
|
|
29
42
|
return token;
|
|
30
43
|
}
|
|
@@ -64,13 +77,11 @@ function summarizeTextError(text, res) {
|
|
|
64
77
|
return {
|
|
65
78
|
error: true,
|
|
66
79
|
status: res.status,
|
|
67
|
-
message: text,
|
|
80
|
+
message: bounded(text),
|
|
68
81
|
};
|
|
69
82
|
}
|
|
70
83
|
|
|
71
|
-
|
|
72
|
-
const text = await res.text();
|
|
73
|
-
|
|
84
|
+
function parseJsonResponse(text, res) {
|
|
74
85
|
if (!text.trim()) {
|
|
75
86
|
return {};
|
|
76
87
|
}
|
|
@@ -82,8 +93,9 @@ async function parseJsonResponse(res) {
|
|
|
82
93
|
return {
|
|
83
94
|
error: true,
|
|
84
95
|
status: res.status,
|
|
96
|
+
outcome_unknown: true,
|
|
85
97
|
message: 'Received invalid JSON from server.',
|
|
86
|
-
details: text,
|
|
98
|
+
details: bounded(text),
|
|
87
99
|
};
|
|
88
100
|
}
|
|
89
101
|
|
|
@@ -91,6 +103,64 @@ async function parseJsonResponse(res) {
|
|
|
91
103
|
}
|
|
92
104
|
}
|
|
93
105
|
|
|
106
|
+
/** A request that failed once it may have reached the server is an unknown outcome, never a refusal:
|
|
107
|
+
* a write can land even though no complete answer came back, so a script must not simply resend it. */
|
|
108
|
+
function unanswered(error, res, timeoutMs, method) {
|
|
109
|
+
const cause = error?.cause?.code;
|
|
110
|
+
const notBuilt = !res && error?.name === 'TypeError' && !error.cause;
|
|
111
|
+
const refused = NOT_SENT_CODES.has(cause) || TLS_FAILURE.test(cause ?? '') || error?.cause?.message === 'bad port';
|
|
112
|
+
const neverSent = notBuilt || (!res && refused);
|
|
113
|
+
let message;
|
|
114
|
+
if (error?.name === 'AbortError') {
|
|
115
|
+
message = `Network request timed out after ${timeoutMs} ms`;
|
|
116
|
+
} else if (notBuilt) {
|
|
117
|
+
message = 'The request could not be sent: a header is invalid. Check the API token for line breaks or other control characters.';
|
|
118
|
+
} else if (neverSent) {
|
|
119
|
+
message = `Network request failed: ${error.message} (${cause ?? error.cause?.message})`;
|
|
120
|
+
} else {
|
|
121
|
+
message = `Network request failed before a complete response arrived: ${error?.message}${cause ? ` (${cause})` : ''}`;
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
error: true,
|
|
125
|
+
message,
|
|
126
|
+
...(res ? { status: res.status } : {}),
|
|
127
|
+
...(neverSent || method === 'GET' ? {} : { outcome_unknown: true }),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** One request under the shared timeout. An error body is read under that timeout and capped; a success
|
|
132
|
+
* body has no size or total-time cap (query results are never cut), only an idle timeout, so a stalled
|
|
133
|
+
* server still cannot hold the command open. Every non-2xx result carries its HTTP status. */
|
|
134
|
+
async function send(url, options, timeoutMs) {
|
|
135
|
+
const controller = new AbortController();
|
|
136
|
+
const abort = () => controller.abort();
|
|
137
|
+
const timeout = setTimeout(abort, timeoutMs);
|
|
138
|
+
let res;
|
|
139
|
+
let raw;
|
|
140
|
+
try {
|
|
141
|
+
res = await fetch(url, { ...options, signal: controller.signal });
|
|
142
|
+
if (res.ok) {
|
|
143
|
+
clearTimeout(timeout);
|
|
144
|
+
}
|
|
145
|
+
raw = await readBody(res, res.ok ? { idleMs: timeoutMs, onIdle: abort } : { maxBytes: MAX_ERROR_BYTES });
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return unanswered(error, res, timeoutMs, options.method);
|
|
148
|
+
} finally {
|
|
149
|
+
clearTimeout(timeout);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (res.ok) {
|
|
153
|
+
return parseJsonResponse(new TextDecoder().decode(raw), res);
|
|
154
|
+
}
|
|
155
|
+
const data =
|
|
156
|
+
raw === null
|
|
157
|
+
? { message: `The server sent an error response larger than ${MAX_ERROR_BYTES} bytes.` }
|
|
158
|
+
: parseJsonResponse(new TextDecoder().decode(raw), res);
|
|
159
|
+
const body = data && typeof data === 'object' ? data : { message: String(data) };
|
|
160
|
+
const statusCode = body.status_code || body.api_status_code ? {} : { status_code: res.status };
|
|
161
|
+
return { ...body, ...statusCode, error: body.error || true, status: res.status };
|
|
162
|
+
}
|
|
163
|
+
|
|
94
164
|
async function apiRequest(method, path, body) {
|
|
95
165
|
warnIfInsecure();
|
|
96
166
|
const token = requireAuth();
|
|
@@ -111,6 +181,7 @@ async function apiRequest(method, path, body) {
|
|
|
111
181
|
headers: {
|
|
112
182
|
'Authorization': `Bearer ${token}`,
|
|
113
183
|
'Accept': 'application/json',
|
|
184
|
+
'User-Agent': USER_AGENT,
|
|
114
185
|
},
|
|
115
186
|
};
|
|
116
187
|
|
|
@@ -119,38 +190,7 @@ async function apiRequest(method, path, body) {
|
|
|
119
190
|
options.body = JSON.stringify(body);
|
|
120
191
|
}
|
|
121
192
|
|
|
122
|
-
|
|
123
|
-
const controller = new AbortController();
|
|
124
|
-
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
125
|
-
try {
|
|
126
|
-
res = await fetch(url, { ...options, signal: controller.signal });
|
|
127
|
-
} catch (error) {
|
|
128
|
-
clearTimeout(timeout);
|
|
129
|
-
return {
|
|
130
|
-
error: true,
|
|
131
|
-
message:
|
|
132
|
-
error?.name === 'AbortError'
|
|
133
|
-
? `Network request timed out after ${timeoutMs} ms`
|
|
134
|
-
: `Network request failed: ${error.message}`,
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
clearTimeout(timeout);
|
|
138
|
-
|
|
139
|
-
const data = await parseJsonResponse(res);
|
|
140
|
-
|
|
141
|
-
if (data && data.error) {
|
|
142
|
-
return data;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!res.ok) {
|
|
146
|
-
return {
|
|
147
|
-
error: true,
|
|
148
|
-
status: res.status,
|
|
149
|
-
...(data && typeof data === 'object' ? data : { message: String(data) }),
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return data;
|
|
193
|
+
return send(url, options, timeoutMs);
|
|
154
194
|
}
|
|
155
195
|
|
|
156
196
|
export async function apiGet(path) {
|
|
@@ -175,43 +215,16 @@ export async function accountSummary() {
|
|
|
175
215
|
};
|
|
176
216
|
}
|
|
177
217
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
try {
|
|
182
|
-
res = await fetch(url, {
|
|
218
|
+
return send(
|
|
219
|
+
url,
|
|
220
|
+
{
|
|
183
221
|
method: 'GET',
|
|
184
222
|
headers: {
|
|
185
223
|
'Authorization': `Bearer ${token}`,
|
|
186
224
|
'Accept': 'application/json',
|
|
225
|
+
'User-Agent': USER_AGENT,
|
|
187
226
|
},
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
clearTimeout(timeout);
|
|
192
|
-
return {
|
|
193
|
-
error: true,
|
|
194
|
-
message:
|
|
195
|
-
error?.name === 'AbortError'
|
|
196
|
-
? `Network request timed out after ${timeoutMs} ms`
|
|
197
|
-
: `Network request failed: ${error.message}`,
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
clearTimeout(timeout);
|
|
201
|
-
|
|
202
|
-
const data = await parseJsonResponse(res);
|
|
203
|
-
|
|
204
|
-
if (data && data.error) {
|
|
205
|
-
return data;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (!res.ok) {
|
|
209
|
-
return {
|
|
210
|
-
error: true,
|
|
211
|
-
status: res.status,
|
|
212
|
-
...(data && typeof data === 'object' ? data : { message: String(data) }),
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return data;
|
|
227
|
+
},
|
|
228
|
+
timeoutMs,
|
|
229
|
+
);
|
|
217
230
|
}
|