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.
@@ -1,4 +1,4 @@
1
- import { getConfigSnapshot, getToken } from '../../config.js';
1
+ import { getConfigSnapshot, getContext, getToken, saveContext } from '../../config.js';
2
2
  import { apiGet } from '../../api.js';
3
3
  import { printJson, printTable, wantsJson } from '../../output.js';
4
4
  import {
@@ -7,7 +7,7 @@ import {
7
7
  parseIntegerOption,
8
8
  toPromptDataSource,
9
9
  } from '../options.js';
10
- import { fail, handleApiError } from '../errors.js';
10
+ import { errorMessage, fail, handleApiError } from '../errors.js';
11
11
  import {
12
12
  fetchBrandMetadata,
13
13
  fetchConnectedSources,
@@ -61,7 +61,7 @@ export function registerDiscoveryCommands(program) {
61
61
  });
62
62
 
63
63
  program
64
- .command('query_contexts')
64
+ .command('query_contexts', { hidden: true })
65
65
  .argument('[request]', 'One of: accounts, brands, available_actions, actions_details')
66
66
  .description('Retrieve accounts, brands, and action schemas')
67
67
  .option('-r, --request <request>', 'Request type if you prefer flags over the positional argument')
@@ -103,11 +103,13 @@ export function registerDiscoveryCommands(program) {
103
103
 
104
104
  program
105
105
  .command('brands')
106
- .description('List brand metadata for a workspace')
107
- .requiredOption('-w, --workspace <id>', 'Workspace ID')
106
+ .description('List your brands with their IDs')
107
+ .option('-w, --workspace <id>', 'Workspace ID (defaults to the saved scope, or your only workspace)')
108
108
  .option('--brand-ids <ids>', 'Comma-separated brand IDs')
109
109
  .action(async (opts, command) => {
110
- const workspaceId = parseIntegerOption('workspace', opts.workspace);
110
+ const workspaceId = opts.workspace
111
+ ? parseIntegerOption('workspace', opts.workspace)
112
+ : await resolveWorkspaceId();
111
113
  const brandIds = opts.brandIds ? parseIdList(opts.brandIds, 'brand ID') : undefined;
112
114
  const data = await fetchBrandMetadata(workspaceId, brandIds);
113
115
  const brands = Array.isArray(data.brands) ? data.brands : [];
@@ -117,21 +119,16 @@ export function registerDiscoveryCommands(program) {
117
119
  return;
118
120
  }
119
121
 
120
- if (brands.length === 1 || brandIds) {
121
- printJson(brands.length === 1 ? brands[0] : brands);
122
- return;
123
- }
124
-
125
122
  printTable(brands, [
126
- { key: 'id', label: 'ID' },
127
- { key: 'name', label: 'Name' },
123
+ { key: 'id', label: 'Brand ID' },
124
+ { key: 'name', label: 'Brand' },
128
125
  { key: 'website_url', label: 'Website' },
129
126
  ]);
130
127
  });
131
128
 
132
129
  program
133
130
  .command('accounts')
134
- .description('List connected accounts')
131
+ .description('List connected accounts with their workspace and brand IDs')
135
132
  .option('-p, --platform <platform>', 'Filter by platform')
136
133
  .option('--prod', 'Only include production-ready sources')
137
134
  .action(async (opts, command) => {
@@ -152,19 +149,27 @@ export function registerDiscoveryCommands(program) {
152
149
  platform: source.platform || '-',
153
150
  account: sourceDisplayName(source),
154
151
  workspace: source.workspace_name || '-',
152
+ workspaceId: source.workspace_id ?? '-',
153
+ brand: source.brand_name || '-',
154
+ brandId: source.brand_id ?? '-',
155
155
  access: source.access_level || '-',
156
156
  }));
157
157
 
158
+ // The ids are what a query body needs, so they belong in the table a human reads — their
159
+ // absence here is why customers concluded they were not available at all.
158
160
  printTable(rows, [
159
161
  { key: 'platform', label: 'Platform' },
160
162
  { key: 'account', label: 'Account' },
161
163
  { key: 'workspace', label: 'Workspace' },
164
+ { key: 'workspaceId', label: 'Workspace ID' },
165
+ { key: 'brand', label: 'Brand' },
166
+ { key: 'brandId', label: 'Brand ID' },
162
167
  { key: 'access', label: 'Access' },
163
168
  ]);
164
169
  });
165
170
 
166
171
  program
167
- .command('sources')
172
+ .command('sources', { hidden: true })
168
173
  .description('List connected data sources with IDs')
169
174
  .option('-p, --platform <platform>', 'Filter by platform')
170
175
  .option('--prod', 'Only include production-ready sources')
@@ -181,17 +186,21 @@ export function registerDiscoveryCommands(program) {
181
186
  platform: source.platform || '-',
182
187
  name: sourceDisplayName(source),
183
188
  id: sourceIdentifier(source),
184
- workspace: source.workspace_name || '-',
189
+ workspaceId: source.workspace_id ?? '-',
190
+ brandId: source.brand_id ?? '-',
185
191
  brand: source.brand_name || '-',
186
192
  access: source.access_level || '-',
187
193
  }));
188
194
 
195
+ // "ID" is the SOURCE's own id (a customer id, a site url...). The workspace and brand ids are
196
+ // separate, and are the ones a query body asks for.
189
197
  printTable(rows, [
190
198
  { key: 'platform', label: 'Platform' },
191
199
  { key: 'name', label: 'Name' },
192
- { key: 'id', label: 'ID' },
193
- { key: 'workspace', label: 'Workspace' },
200
+ { key: 'id', label: 'Source ID' },
201
+ { key: 'workspaceId', label: 'Workspace ID' },
194
202
  { key: 'brand', label: 'Brand' },
203
+ { key: 'brandId', label: 'Brand ID' },
195
204
  { key: 'access', label: 'Access' },
196
205
  ]);
197
206
  });
@@ -318,7 +327,7 @@ export function registerDiscoveryCommands(program) {
318
327
  if (data?.error) {
319
328
  connectivity = {
320
329
  status: 'error',
321
- details: data.message || data.details || 'Request failed',
330
+ details: errorMessage(data),
322
331
  };
323
332
  } else {
324
333
  connectivity = {
@@ -351,3 +360,55 @@ export function registerDiscoveryCommands(program) {
351
360
  }
352
361
  });
353
362
  }
363
+
364
+ /** The workspace to act in: the saved scope first, otherwise the only one this token can see.
365
+ * Asking a user to look up an id they cannot find in the product is what started all of this. */
366
+ async function resolveWorkspaceId() {
367
+ const saved = getContext().workspace_id;
368
+ if (saved) return saved;
369
+
370
+ const data = handleApiError(await apiGet('/user-info/'));
371
+ const workspaces = Array.isArray(data.workspaces) ? data.workspaces : [];
372
+ if (workspaces.length === 1) return workspaces[0].id;
373
+ if (workspaces.length === 0) {
374
+ fail('No workspaces found for this token.');
375
+ }
376
+ fail(
377
+ 'You have access to more than one workspace.',
378
+ 'Pick one with "insightfulpipe use --workspace <id>", or pass -w <id>. Run "insightfulpipe workspaces" to see the ids.',
379
+ );
380
+ }
381
+
382
+ export function registerContextCommand(program) {
383
+ program
384
+ .command('use')
385
+ .description('Show or set the default workspace/brand used by every query')
386
+ .option('-w, --workspace <id>', 'Workspace ID to use by default')
387
+ .option('-b, --brand <id>', 'Brand ID to use by default')
388
+ .option('--clear', 'Forget the saved workspace and brand')
389
+ .action(async (opts, command) => {
390
+ let context;
391
+ if (opts.clear) {
392
+ context = saveContext({ workspaceId: null, brandId: null });
393
+ } else if (opts.workspace || opts.brand) {
394
+ context = saveContext({
395
+ workspaceId: opts.workspace ? parseIntegerOption('workspace', opts.workspace) : undefined,
396
+ brandId: opts.brand ? parseIntegerOption('brand', opts.brand) : undefined,
397
+ });
398
+ } else if (!getContext().workspace_id) {
399
+ // No scope and nothing asked for: adopt the only workspace, so `use` alone is useful.
400
+ context = saveContext({ workspaceId: await resolveWorkspaceId() });
401
+ } else {
402
+ context = getContext();
403
+ }
404
+
405
+ if (wantsJson(command)) {
406
+ printJson({ status: 'success', ...context });
407
+ return;
408
+ }
409
+ printTable([{ workspace: context.workspace_id ?? '-', brand: context.brand_id ?? '-' }], [
410
+ { key: 'workspace', label: 'Workspace ID' },
411
+ { key: 'brand', label: 'Brand ID' },
412
+ ]);
413
+ });
414
+ }
@@ -1,4 +1,5 @@
1
1
  import { apiPost } from '../../api.js';
2
+ import { applyContextToBody } from '../../config.js';
2
3
  import { printJson } from '../../output.js';
3
4
  import {
4
5
  confirmWriteAction,
@@ -6,36 +7,76 @@ import {
6
7
  readJsonInput,
7
8
  } from '../input.js';
8
9
  import { handleApiError } from '../errors.js';
9
- import { withPlatform } from '../options.js';
10
+ import { parseIntegerOption, withPlatform } from '../options.js';
11
+
12
+ /** The saved scope (`insightfulpipe use`) fills in workspace_id/brand_id, and `--workspace`/`--brand`
13
+ * override it for one call. A body that states them itself is never touched. */
14
+ function scopedBody(body, opts) {
15
+ return applyContextToBody(body, {
16
+ workspace_id: opts.workspace ? parseIntegerOption('workspace', opts.workspace) : undefined,
17
+ brand_id: opts.brand ? parseIntegerOption('brand', opts.brand) : undefined,
18
+ });
19
+ }
20
+
21
+ const SCOPE_OPTIONS = [
22
+ ['-w, --workspace <id>', 'Workspace ID for this call (defaults to the saved scope)'],
23
+ ['--brand <id>', 'Brand ID for this call (defaults to the saved scope)'],
24
+ ];
10
25
 
11
26
  export function registerExecutionCommands(program) {
27
+ const readAction = async (platform, opts) => {
28
+ const body = scopedBody(ensureJsonObject(await readJsonInput(opts, true), 'Request body'), opts);
29
+ const data = handleApiError(await apiPost('/', withPlatform(platform, body)), { platform });
30
+ printJson(data);
31
+ };
32
+
33
+ const writeAction = async (platform, opts) => {
34
+ const body = scopedBody(ensureJsonObject(await readJsonInput(opts, true), 'Request body'), opts);
35
+ await confirmWriteAction(platform, body, opts.yes);
36
+ const data = handleApiError(await apiPost('/', withPlatform(platform, body)), { platform });
37
+ printJson(data);
38
+ };
39
+
12
40
  program
13
- .command('query_data')
14
- .alias('query')
41
+ .command('query')
15
42
  .argument('<platform>', 'Platform slug (e.g., google-analytics, shopify)')
16
43
  .option('-b, --body <json>', 'Request body as inline JSON')
17
44
  .option('-f, --file <path>', 'Request body from a JSON file')
45
+ .option(...SCOPE_OPTIONS[0])
46
+ .option(...SCOPE_OPTIONS[1])
18
47
  .description('Execute a read-only data query for a platform')
19
- .action(async (platform, opts) => {
20
- const body = ensureJsonObject(await readJsonInput(opts, true), 'Request body');
21
- const data = handleApiError(await apiPost('/', withPlatform(platform, body)));
22
- printJson(data);
23
- });
48
+ .action(readAction);
24
49
 
25
50
  program
26
- .command('execute_action')
27
- .alias('action')
51
+ .command('action')
28
52
  .argument('<platform>', 'Platform slug (e.g., facebook-pages, google-ads)')
29
53
  .option('-b, --body <json>', 'Request body as inline JSON')
30
54
  .option('-f, --file <path>', 'Request body from a JSON file')
31
55
  .option('-y, --yes', 'Skip confirmation prompt')
56
+ .option(...SCOPE_OPTIONS[0])
57
+ .option(...SCOPE_OPTIONS[1])
32
58
  .description('Execute a write or mutating action for a platform')
33
- .action(async (platform, opts) => {
34
- const body = ensureJsonObject(await readJsonInput(opts, true), 'Request body');
35
- await confirmWriteAction(platform, body, opts.yes);
36
- const data = handleApiError(await apiPost('/', withPlatform(platform, body)));
37
- printJson(data);
38
- });
59
+ .action(writeAction);
60
+
61
+ // 0.1.x names: still accepted so stored scripts keep running, hidden so help teaches one form.
62
+ program
63
+ .command('query_data', { hidden: true })
64
+ .argument('<platform>')
65
+ .option('-b, --body <json>', 'Request body as inline JSON')
66
+ .option('-f, --file <path>', 'Request body from a JSON file')
67
+ .option(...SCOPE_OPTIONS[0])
68
+ .option(...SCOPE_OPTIONS[1])
69
+ .action(readAction);
70
+
71
+ program
72
+ .command('execute_action', { hidden: true })
73
+ .argument('<platform>')
74
+ .option('-b, --body <json>', 'Request body as inline JSON')
75
+ .option('-f, --file <path>', 'Request body from a JSON file')
76
+ .option('-y, --yes', 'Skip confirmation prompt')
77
+ .option(...SCOPE_OPTIONS[0])
78
+ .option(...SCOPE_OPTIONS[1])
79
+ .action(writeAction);
39
80
 
40
81
  program
41
82
  .command('raw')
@@ -44,7 +85,7 @@ export function registerExecutionCommands(program) {
44
85
  .description('Send a raw request to the universal dispatcher')
45
86
  .action(async (opts) => {
46
87
  const body = ensureJsonObject(await readJsonInput(opts, true), 'Request body');
47
- const data = handleApiError(await apiPost('/', body));
88
+ const data = handleApiError(await apiPost('/', body), { platform: body.platform });
48
89
  printJson(data);
49
90
  });
50
91
  }