@jinshuju/cli 0.1.3 → 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/README.md +80 -1
- package/dist/args.d.ts +47 -0
- package/dist/args.js +185 -0
- package/dist/auth.js +43 -11
- package/dist/cli-bin.js +1 -1
- package/dist/cli.d.ts +2 -20
- package/dist/cli.js +71 -659
- package/dist/commands/account.d.ts +2 -0
- package/dist/commands/account.js +20 -0
- package/dist/commands/comment.d.ts +2 -0
- package/dist/commands/comment.js +66 -0
- package/dist/commands/entry.d.ts +31 -0
- package/dist/commands/entry.js +723 -0
- package/dist/commands/field.d.ts +3 -0
- package/dist/commands/field.js +261 -0
- package/dist/commands/folder.d.ts +2 -0
- package/dist/commands/folder.js +31 -0
- package/dist/commands/form.d.ts +2 -0
- package/dist/commands/form.js +412 -0
- package/dist/commands/index.d.ts +16 -0
- package/dist/commands/index.js +68 -0
- package/dist/commands/local.d.ts +10 -0
- package/dist/commands/local.js +76 -0
- package/dist/commands/opensearch.d.ts +2 -0
- package/dist/commands/opensearch.js +73 -0
- package/dist/commands/shared.d.ts +57 -0
- package/dist/commands/shared.js +122 -0
- package/dist/commands/table.d.ts +2 -0
- package/dist/commands/table.js +101 -0
- package/dist/{commands.d.ts → commands/types.d.ts} +24 -30
- package/dist/commands/types.js +1 -0
- package/dist/commands/upload.d.ts +7 -0
- package/dist/commands/upload.js +40 -0
- package/dist/commands/view.d.ts +2 -0
- package/dist/commands/view.js +81 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +45 -5
- package/dist/errors.d.ts +48 -0
- package/dist/errors.js +99 -0
- package/dist/help.d.ts +1 -1
- package/dist/help.js +11 -1
- package/dist/http.d.ts +37 -4
- package/dist/http.js +119 -14
- package/dist/local.d.ts +24 -0
- package/dist/local.js +208 -0
- package/dist/options.d.ts +8 -4
- package/dist/options.js +63 -18
- package/dist/payload.d.ts +14 -8
- package/dist/payload.js +22 -54
- package/dist/render.d.ts +21 -0
- package/dist/render.js +315 -0
- package/dist/result.d.ts +31 -0
- package/dist/result.js +32 -0
- package/dist/values.d.ts +4 -0
- package/dist/values.js +8 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.js +16 -0
- package/package.json +23 -18
- package/dist/commands.js +0 -1690
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
import { CONTAINER_LIST_OPTIONS, CONTAINER_OPTIONS, FILTER_OPTION, FILTERS_OPTION, JSON_OPTION, MINE_OPTION, PAGINATION_OPTIONS, SORT_OPTION, TIME_BUCKETS, UsageError, parseDimension, parseFilter, parseMetric, namedContainers, resolveContainer, resolveContainers } from '../options.js';
|
|
2
|
+
import { API, LISTING, YES_OPTION, confirmed, containerPath, filters, given, paging, payload, refuseWithMine, requiredOption, sort } from './shared.js';
|
|
3
|
+
import { RefusedError } from '../errors.js';
|
|
4
|
+
import { progress } from '../progress.js';
|
|
5
|
+
import { TransportError } from '../http.js';
|
|
6
|
+
import { isRecord } from '../values.js';
|
|
7
|
+
import { basename } from 'node:path';
|
|
8
|
+
import { upload } from './upload.js';
|
|
9
|
+
/** What the batch count endpoint accepts, and what the design document states. */
|
|
10
|
+
const MAX_COUNTED_CONTAINERS = 10;
|
|
11
|
+
function list(value) {
|
|
12
|
+
const values = value;
|
|
13
|
+
return values && values.length > 0 ? values.join(',') : undefined;
|
|
14
|
+
}
|
|
15
|
+
function labels(input) {
|
|
16
|
+
return input.options.labels ? 'true' : undefined;
|
|
17
|
+
}
|
|
18
|
+
const LABELS_OPTION = {
|
|
19
|
+
name: '--labels',
|
|
20
|
+
type: 'boolean',
|
|
21
|
+
description: "Pair each value with its field's label, saving a second read of the form"
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* `entry aggregate` answers a table taken apart: `columns` says what each
|
|
25
|
+
* column is, `rows` holds one positional array per row and no names at all.
|
|
26
|
+
* Nothing generic can put the two back together — a renderer given the rows
|
|
27
|
+
* alone sees arrays of mixed things and prints them as JSON — so they are
|
|
28
|
+
* zipped here, into the rows-of-objects every other listing already is.
|
|
29
|
+
*
|
|
30
|
+
* Only for reading. `--output json` keeps the positional shape: it is the one a
|
|
31
|
+
* script can index by position without knowing what the labels say, and the
|
|
32
|
+
* headings below are Chinese as often as not.
|
|
33
|
+
*/
|
|
34
|
+
function aggregateTable(body) {
|
|
35
|
+
const { columns, rows, ...rest } = (body ?? {});
|
|
36
|
+
if (!Array.isArray(columns) || !Array.isArray(rows))
|
|
37
|
+
return body;
|
|
38
|
+
const headings = uniqueHeadings(columns.map(headingOf));
|
|
39
|
+
return {
|
|
40
|
+
...rest,
|
|
41
|
+
rows: rows.map((row) => {
|
|
42
|
+
const cells = Array.isArray(row) ? row : [row];
|
|
43
|
+
return Object.fromEntries(headings.map((heading, index) => [heading, cellOf(cells[index])]));
|
|
44
|
+
})
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** `count(姓名)`, because `姓名` alone does not say what was done to it. */
|
|
48
|
+
function headingOf(column, index) {
|
|
49
|
+
const { label, field, func } = (column ?? {});
|
|
50
|
+
const name = label || field || `column_${index + 1}`;
|
|
51
|
+
return func ? `${func}(${name})` : name;
|
|
52
|
+
}
|
|
53
|
+
/** A dimension cell is the choice it grouped by; a metric cell is the number. */
|
|
54
|
+
function cellOf(cell) {
|
|
55
|
+
if (cell === null || typeof cell !== 'object' || Array.isArray(cell))
|
|
56
|
+
return cell;
|
|
57
|
+
const { label } = cell;
|
|
58
|
+
return label ?? JSON.stringify(cell);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Two columns heading the same object would leave one of them silently
|
|
62
|
+
* overwriting the other, so a repeat is numbered rather than lost.
|
|
63
|
+
*/
|
|
64
|
+
function uniqueHeadings(headings) {
|
|
65
|
+
const seen = new Map();
|
|
66
|
+
return headings.map((heading) => {
|
|
67
|
+
const count = (seen.get(heading) ?? 0) + 1;
|
|
68
|
+
seen.set(heading, count);
|
|
69
|
+
return count === 1 ? heading : `${heading} (${count})`;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** `field_3=名称` or `field_3=2`: a label if it is not a column number. */
|
|
73
|
+
function parseColumnMapping(input) {
|
|
74
|
+
const at = input.indexOf('=');
|
|
75
|
+
if (at <= 0)
|
|
76
|
+
throw new UsageError(`--map must be '<api-code>=<column>', got ${JSON.stringify(input)}`);
|
|
77
|
+
const field_api_code = input.slice(0, at);
|
|
78
|
+
const column = input.slice(at + 1);
|
|
79
|
+
if (!column)
|
|
80
|
+
throw new UsageError(`--map needs a column after '=', got ${JSON.stringify(input)}`);
|
|
81
|
+
return /^\d+$/.test(column)
|
|
82
|
+
? { field_api_code, sheet_column_index: Number.parseInt(column, 10) }
|
|
83
|
+
: { field_api_code, column_label: column };
|
|
84
|
+
}
|
|
85
|
+
const ATTACH_OPTION = {
|
|
86
|
+
name: '--attach',
|
|
87
|
+
type: 'string',
|
|
88
|
+
repeatable: true,
|
|
89
|
+
placeholder: '<api-code>[.<row>.<sub>]=<file>',
|
|
90
|
+
description: 'Upload a file into this attachment field, repeatable. A subtable column names the row it fills: field_5.0.field_2=<file>'
|
|
91
|
+
};
|
|
92
|
+
const ATTACH_SHAPE = "--attach must be '<api-code>=<file>', or '<api-code>.<row>.<sub>=<file>' for a subtable column";
|
|
93
|
+
// The write answers success for a value its field could not read and keeps
|
|
94
|
+
// nothing of it, so the only warning a caller gets is the one written here.
|
|
95
|
+
const VALUE_SHAPES = 'Each type takes a value of its own shape — some choices only by code, a time as ' +
|
|
96
|
+
'{hour, minute} — and `field types <Type>` shows it under `value`. A value in a shape its ' +
|
|
97
|
+
'field cannot read is dropped without an error, as is a key naming no field, so read the ' +
|
|
98
|
+
'entry back when it matters.';
|
|
99
|
+
/**
|
|
100
|
+
* `field_5=/path/a.png`, or `field_5.0.field_2=/path/a.png` for one row of a
|
|
101
|
+
* subtable column.
|
|
102
|
+
*
|
|
103
|
+
* A subtable answer is a list of rows, so the file belongs in one of them and
|
|
104
|
+
* the row has to be named. Without a row it is the first, which is what a
|
|
105
|
+
* subtable filled in one go almost always has.
|
|
106
|
+
*
|
|
107
|
+
* The row is a dot and not a bracket because zsh reads `field_5[0]` as a
|
|
108
|
+
* pattern and refuses the command before the CLI sees it — a syntax that needs
|
|
109
|
+
* quoting to be typed at all is the wrong one to hand somebody. `[0]` is still
|
|
110
|
+
* accepted, for anyone who quotes it or arrives from another tool.
|
|
111
|
+
*/
|
|
112
|
+
function parseAttachment(input) {
|
|
113
|
+
const at = input.indexOf('=');
|
|
114
|
+
if (at <= 0)
|
|
115
|
+
throw new UsageError(`${ATTACH_SHAPE}, got ${JSON.stringify(input)}`);
|
|
116
|
+
const file = input.slice(at + 1);
|
|
117
|
+
const target = /^([A-Za-z0-9_]+)(?:\[(\d+)\]|\.(\d+))?(?:\.([A-Za-z0-9_]+))?$/.exec(input.slice(0, at));
|
|
118
|
+
if (!target || !file)
|
|
119
|
+
throw new UsageError(`${ATTACH_SHAPE}, got ${JSON.stringify(input)}`);
|
|
120
|
+
const [, field, bracketed, dotted, dimension] = target;
|
|
121
|
+
const row = bracketed ?? dotted;
|
|
122
|
+
if (row !== undefined && dimension === undefined) {
|
|
123
|
+
throw new UsageError(`a row needs the subtable column it is a row of: ${field}.${row}.<sub>=<file>`);
|
|
124
|
+
}
|
|
125
|
+
return { field, row: row === undefined ? 0 : Number(row), dimension, file };
|
|
126
|
+
}
|
|
127
|
+
/** The states an import stops in; the rest mean it is still going. */
|
|
128
|
+
const IMPORT_SETTLED = new Set(['success', 'failed', 'cancelled']);
|
|
129
|
+
const IMPORT_POLL_MS = 1000;
|
|
130
|
+
/**
|
|
131
|
+
* How long `--wait` waits before giving up. An import of any size the plan
|
|
132
|
+
* allows finishes well inside this; one that has not is stuck, and a command
|
|
133
|
+
* that never returns is worse than one that says so and names the job.
|
|
134
|
+
*/
|
|
135
|
+
export const IMPORT_WAIT_MS = 30 * 60 * 1000;
|
|
136
|
+
/**
|
|
137
|
+
* Waits for the rows to be written. A failed import exits non-zero, because
|
|
138
|
+
* the alternative — answering 0 for an import that wrote nothing — is how a
|
|
139
|
+
* caller comes to believe data is there when it is not.
|
|
140
|
+
*/
|
|
141
|
+
export async function awaitImport(client, token, jobId, watching) {
|
|
142
|
+
const deadlineMs = watching.deadlineMs ?? IMPORT_WAIT_MS;
|
|
143
|
+
const sleep = watching.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
144
|
+
const now = watching.now ?? Date.now;
|
|
145
|
+
const started = now();
|
|
146
|
+
// Every way out of this loop but the good one carries the job id. The rows are
|
|
147
|
+
// already being written by the time the first poll happens, so an error that
|
|
148
|
+
// drops the id leaves the caller unable to ask how it went and tempted to
|
|
149
|
+
// import the file a second time.
|
|
150
|
+
const recoverable = (reason, cause) => new (cause === undefined ? RefusedError : Error)(`${reason}. The import is job ${jobId} and may still be running: ` +
|
|
151
|
+
`jinshuju entry import-status --form ${token} ${jobId}`, { cause });
|
|
152
|
+
const stillRunning = () => new TransportError(`gave up waiting after ${Math.round(deadlineMs / 60_000)} minutes. The import is job ${jobId} and is still running: ` +
|
|
153
|
+
`jinshuju entry import-status --form ${token} ${jobId}`, true);
|
|
154
|
+
for (;;) {
|
|
155
|
+
let job;
|
|
156
|
+
try {
|
|
157
|
+
job = await client.request({
|
|
158
|
+
method: 'GET',
|
|
159
|
+
path: `${API}/forms/${token}/entry_imports/${jobId}`
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
throw recoverable(`the import started, but asking how it is going failed: ${error.message}`, error);
|
|
164
|
+
}
|
|
165
|
+
if (IMPORT_SETTLED.has(job.status)) {
|
|
166
|
+
if (job.status !== 'success') {
|
|
167
|
+
throw recoverable(`import ${job.status}${job.error_message ? `: ${job.error_message}` : ''}`);
|
|
168
|
+
}
|
|
169
|
+
return job;
|
|
170
|
+
}
|
|
171
|
+
const seen = job.processed_rows ?? 0;
|
|
172
|
+
const total = job.total_rows;
|
|
173
|
+
watching.step(`importing ${seen}${total ? `/${total}` : ''} rows…`);
|
|
174
|
+
if (now() - started >= deadlineMs)
|
|
175
|
+
throw stillRunning();
|
|
176
|
+
await sleep(IMPORT_POLL_MS);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const BATCH_OPTION = {
|
|
180
|
+
name: '--batch',
|
|
181
|
+
type: 'json',
|
|
182
|
+
placeholder: '<json|@file|->',
|
|
183
|
+
description: 'Several rows in one request'
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Uploads each file and puts its id where the payload wants it.
|
|
187
|
+
*
|
|
188
|
+
* Shared by creating an entry and updating one: the files go up the same way
|
|
189
|
+
* either way, and an attachment field that could only be filled at creation
|
|
190
|
+
* left an entry with no way to gain one afterwards.
|
|
191
|
+
*/
|
|
192
|
+
async function withUploads(input, client, attached) {
|
|
193
|
+
const { token } = resolveContainer(input.options);
|
|
194
|
+
const body = { ...input.options.json };
|
|
195
|
+
const watching = progress();
|
|
196
|
+
try {
|
|
197
|
+
for (const { field, row, dimension, file } of attached) {
|
|
198
|
+
watching.step(`uploading ${basename(file)}…`);
|
|
199
|
+
const uploaded = await client.request(upload(`${API}/forms/${token}/entry_attachments`, file, dimension === undefined ? { field_api_code: field } : { field_api_code: field, dimension_api_code: dimension }));
|
|
200
|
+
// A field holds a list of attachments, so each upload appends rather
|
|
201
|
+
// than replacing what an earlier --attach for the same field put there.
|
|
202
|
+
if (dimension === undefined) {
|
|
203
|
+
body[field] = append(body[field], uploaded.id);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
// A subtable column is a list of rows and the file lives inside one of
|
|
207
|
+
// them: {"field_5": [{"field_2": ["<id>"]}]}. Writing "field_5.field_2"
|
|
208
|
+
// at the top level named no field the form has, so the server dropped it
|
|
209
|
+
// and answered with an entry created — without the file just uploaded.
|
|
210
|
+
const rows = Array.isArray(body[field]) ? [...body[field]] : [];
|
|
211
|
+
while (rows.length <= row)
|
|
212
|
+
rows.push({});
|
|
213
|
+
const cells = { ...(isRecord(rows[row]) ? rows[row] : {}) };
|
|
214
|
+
cells[dimension] = append(cells[dimension], uploaded.id);
|
|
215
|
+
rows[row] = cells;
|
|
216
|
+
body[field] = rows;
|
|
217
|
+
}
|
|
218
|
+
return body;
|
|
219
|
+
}
|
|
220
|
+
finally {
|
|
221
|
+
watching.done();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function attachments(input) {
|
|
225
|
+
return (input.options.attach ?? []).map(parseAttachment);
|
|
226
|
+
}
|
|
227
|
+
/** An attachment slot holds a list, so a second file joins the first. */
|
|
228
|
+
function append(existing, id) {
|
|
229
|
+
return Array.isArray(existing) ? [...existing, id] : [id];
|
|
230
|
+
}
|
|
231
|
+
function batchRows(input) {
|
|
232
|
+
const rows = input.options.batch;
|
|
233
|
+
if (rows === undefined)
|
|
234
|
+
return undefined;
|
|
235
|
+
if (input.options.json !== undefined)
|
|
236
|
+
throw new UsageError('--json and --batch are alternatives, not both');
|
|
237
|
+
if (!Array.isArray(rows))
|
|
238
|
+
throw new UsageError('--batch must be a list');
|
|
239
|
+
return rows;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Batch writes are served under the forms path alone, and it takes a table's
|
|
243
|
+
* token just as well, so a table batches through the same URL.
|
|
244
|
+
*/
|
|
245
|
+
function batchPath(input) {
|
|
246
|
+
const { token } = resolveContainer(input.options);
|
|
247
|
+
return `${API}/forms/${token}/entries/batch`;
|
|
248
|
+
}
|
|
249
|
+
export const ENTRY = [
|
|
250
|
+
{
|
|
251
|
+
path: ['entry', 'list'],
|
|
252
|
+
summary: 'List entries',
|
|
253
|
+
description: 'With --view the view carries its own filter and sort, so --filter, --keyword and --sort cannot be added on top.',
|
|
254
|
+
options: [
|
|
255
|
+
...CONTAINER_OPTIONS,
|
|
256
|
+
{ name: '--view', type: 'string', placeholder: '<view>', description: 'Read the entries of this view' },
|
|
257
|
+
{ name: '--keyword', type: 'string', placeholder: '<kw>', description: 'Search every searchable field at once' },
|
|
258
|
+
{ name: '--fields', type: 'list', placeholder: '<api-code,...>', description: 'Return only these fields' },
|
|
259
|
+
LABELS_OPTION,
|
|
260
|
+
MINE_OPTION,
|
|
261
|
+
FILTER_OPTION,
|
|
262
|
+
FILTERS_OPTION,
|
|
263
|
+
SORT_OPTION,
|
|
264
|
+
...PAGINATION_OPTIONS
|
|
265
|
+
],
|
|
266
|
+
request: (input) => {
|
|
267
|
+
if (input.options.mine) {
|
|
268
|
+
refuseWithMine(input, ['view', 'sort']);
|
|
269
|
+
const { token } = resolveContainer(input.options);
|
|
270
|
+
return {
|
|
271
|
+
method: 'GET',
|
|
272
|
+
path: `${API}/my/forms/${token}/entries`,
|
|
273
|
+
query: {
|
|
274
|
+
filters: filters(input),
|
|
275
|
+
keyword: input.options.keyword,
|
|
276
|
+
fields: list(input.options.fields),
|
|
277
|
+
include_labels: labels(input),
|
|
278
|
+
...paging(input)
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
const view = input.options.view;
|
|
283
|
+
if (view) {
|
|
284
|
+
for (const flag of ['filter', 'filters', 'keyword', 'sort']) {
|
|
285
|
+
const value = input.options[flag];
|
|
286
|
+
if (Array.isArray(value) ? value.length > 0 : value !== undefined) {
|
|
287
|
+
throw new UsageError(`--${flag} cannot be combined with --view: the view carries its own filter and sort`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
// The view decides its own columns and the endpoint takes no field
|
|
291
|
+
// list, so --fields here asked for something that was never going to
|
|
292
|
+
// happen. Refusing beats accepting it and answering every field.
|
|
293
|
+
if (input.options.fields !== undefined) {
|
|
294
|
+
throw new UsageError('--fields cannot be combined with --view: the view decides its own columns. ' +
|
|
295
|
+
'Change them with `view edit --columns`, or read the form without --view.');
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
method: 'GET',
|
|
299
|
+
path: `${containerPath(input)}/views/${view}/entries`,
|
|
300
|
+
query: { include_labels: labels(input), ...paging(input) }
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
method: 'GET',
|
|
305
|
+
path: `${containerPath(input)}/entries`,
|
|
306
|
+
query: {
|
|
307
|
+
filters: filters(input),
|
|
308
|
+
keyword: input.options.keyword,
|
|
309
|
+
fields: list(input.options.fields),
|
|
310
|
+
include_labels: labels(input),
|
|
311
|
+
sort: sort(input, 'api_code'),
|
|
312
|
+
...paging(input)
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
},
|
|
316
|
+
paginate: LISTING,
|
|
317
|
+
examples: [
|
|
318
|
+
'jinshuju entry list --form Kp7mQ2',
|
|
319
|
+
"jinshuju entry list --form Kp7mQ2 --filter 'field_3 gte 80' --sort created_at:desc",
|
|
320
|
+
'jinshuju entry list --form Kp7mQ2 --all',
|
|
321
|
+
'jinshuju entry list --form Kp7mQ2 --mine'
|
|
322
|
+
]
|
|
323
|
+
},
|
|
324
|
+
{
|
|
325
|
+
path: ['entry', 'count'],
|
|
326
|
+
summary: 'Count the entries matching a filter',
|
|
327
|
+
description: 'The container is repeatable, up to 10. Counting several at once answers one row each plus the ' +
|
|
328
|
+
'sum, and takes no --keyword; a filter then has to name created_at, updated_at or creator_id, ' +
|
|
329
|
+
'because an api_code is a different field on every form.',
|
|
330
|
+
options: [
|
|
331
|
+
...CONTAINER_LIST_OPTIONS,
|
|
332
|
+
{ name: '--keyword', type: 'string', placeholder: '<kw>', description: 'Search every searchable field at once' },
|
|
333
|
+
FILTER_OPTION,
|
|
334
|
+
FILTERS_OPTION
|
|
335
|
+
],
|
|
336
|
+
request: (input) => {
|
|
337
|
+
const { tokens, kind } = resolveContainers(input.options, MAX_COUNTED_CONTAINERS);
|
|
338
|
+
const query = { filters: filters(input), keyword: input.options.keyword };
|
|
339
|
+
if (tokens.length === 1) {
|
|
340
|
+
return {
|
|
341
|
+
method: 'GET',
|
|
342
|
+
path: `${API}/${kind === 'table' ? 'tables' : 'forms'}/${tokens[0]}/entries/count`,
|
|
343
|
+
query
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return { method: 'GET', path: `${API}/entries/count`, query: { ...query, form_tokens: tokens.join(',') } };
|
|
347
|
+
},
|
|
348
|
+
examples: [
|
|
349
|
+
"jinshuju entry count --form Kp7mQ2 --filter 'field_3 gte 80'",
|
|
350
|
+
'jinshuju entry count --form Kp7mQ2 --form Vn4xR8 --form aB3dE9'
|
|
351
|
+
]
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
path: ['entry', 'search'],
|
|
355
|
+
summary: 'Search several forms for one keyword at once',
|
|
356
|
+
description: 'Answers, per form, how many entries matched and some of their serial numbers; values are ' +
|
|
357
|
+
'never returned. Read what you need afterwards with `entry get` or `entry list --keyword`. ' +
|
|
358
|
+
'A form that matched nothing is left out, so a token you named and cannot find was searched ' +
|
|
359
|
+
'and matched nothing. A form that could not be searched is listed with a reason instead — ' +
|
|
360
|
+
'"not searched" is not "no match". Name up to 10 containers, or name none and let ' +
|
|
361
|
+
'--scope-filter describe them; the call is refused rather than truncated when more than 10 match.',
|
|
362
|
+
args: [{ name: 'keyword', required: true, description: 'The text to search for' }],
|
|
363
|
+
options: [
|
|
364
|
+
...CONTAINER_LIST_OPTIONS,
|
|
365
|
+
{
|
|
366
|
+
name: '--scope-filter',
|
|
367
|
+
type: 'string',
|
|
368
|
+
repeatable: true,
|
|
369
|
+
placeholder: "'<field> <op> [value]'",
|
|
370
|
+
description: 'Which forms to search, not which entries match. Takes form_name, created_at, ' +
|
|
371
|
+
'last_entry_created_at, entries_count. Empty forms are skipped unless you say otherwise'
|
|
372
|
+
},
|
|
373
|
+
MINE_OPTION
|
|
374
|
+
],
|
|
375
|
+
request: (input) => {
|
|
376
|
+
const { tokens } = namedContainers(input.options);
|
|
377
|
+
if (input.options.mine) {
|
|
378
|
+
refuseWithMine(input, ['table', 'scope_filter']);
|
|
379
|
+
return {
|
|
380
|
+
method: 'GET',
|
|
381
|
+
path: `${API}/my/search`,
|
|
382
|
+
query: {
|
|
383
|
+
keyword: input.args.keyword,
|
|
384
|
+
form_tokens: tokens.length > 0 ? tokens.join(',') : undefined
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
const scope = input.options.scope_filter ?? [];
|
|
389
|
+
return {
|
|
390
|
+
method: 'GET',
|
|
391
|
+
path: `${API}/entries/search`,
|
|
392
|
+
query: {
|
|
393
|
+
keyword: input.args.keyword,
|
|
394
|
+
form_tokens: tokens.length > 0 ? tokens.join(',') : undefined,
|
|
395
|
+
filters: scope.length > 0 ? JSON.stringify(scope.map(parseFilter)) : undefined
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
},
|
|
399
|
+
examples: [
|
|
400
|
+
'jinshuju entry search 某某公司',
|
|
401
|
+
'jinshuju entry search 13800138000 --form Kp7mQ2 --form Vn4xR8',
|
|
402
|
+
"jinshuju entry search 报修 --scope-filter 'entries_count gt 100'",
|
|
403
|
+
'jinshuju entry search 某某公司 --mine'
|
|
404
|
+
]
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
path: ['entry', 'stats'],
|
|
408
|
+
summary: 'Count submissions per form over a date range',
|
|
409
|
+
description: 'Not the question `entry count` answers. These are submissions as they happened: an import ' +
|
|
410
|
+
'lands on the day it ran whatever dates its rows carry, and deletions are never subtracted, ' +
|
|
411
|
+
'so this is how much arrived rather than how much is still there. A whole day is the ' +
|
|
412
|
+
'smallest window; both ends are inclusive and days are cut in the reported time zone.',
|
|
413
|
+
options: [
|
|
414
|
+
{ name: '--from', type: 'string', placeholder: '<YYYY-MM-DD>', description: 'First day to count, inclusive' },
|
|
415
|
+
{
|
|
416
|
+
name: '--to',
|
|
417
|
+
type: 'string',
|
|
418
|
+
placeholder: '<YYYY-MM-DD>',
|
|
419
|
+
description: 'Last day to count, inclusive. Defaults to today'
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
name: '--kind',
|
|
423
|
+
type: 'string',
|
|
424
|
+
choices: ['form', 'table'],
|
|
425
|
+
placeholder: '<kind>',
|
|
426
|
+
description: 'Count only forms, or only tables'
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
name: '--limit',
|
|
430
|
+
type: 'integer',
|
|
431
|
+
placeholder: '<n>',
|
|
432
|
+
description: 'How many forms to list, most submissions first (default 100, max 100)'
|
|
433
|
+
}
|
|
434
|
+
],
|
|
435
|
+
request: (input) => ({
|
|
436
|
+
method: 'GET',
|
|
437
|
+
path: `${API}/entries/stats`,
|
|
438
|
+
query: {
|
|
439
|
+
from: requiredOption(input, 'from'),
|
|
440
|
+
to: input.options.to,
|
|
441
|
+
kind: input.options.kind,
|
|
442
|
+
limit: input.options.limit === undefined ? undefined : String(input.options.limit)
|
|
443
|
+
}
|
|
444
|
+
}),
|
|
445
|
+
examples: [
|
|
446
|
+
'jinshuju entry stats --from 2026-09-01',
|
|
447
|
+
'jinshuju entry stats --from 2026-09-01 --to 2026-09-07 --kind form --limit 10'
|
|
448
|
+
]
|
|
449
|
+
},
|
|
450
|
+
{
|
|
451
|
+
path: ['entry', 'aggregate'],
|
|
452
|
+
summary: 'Compute statistics over the entries matching a filter',
|
|
453
|
+
description: 'The response is as big as the metrics and groups asked for, never as big as the data. Which ' +
|
|
454
|
+
"functions a field takes is the field's own answer: read analytics.agg_funcs from `form get`.",
|
|
455
|
+
options: [
|
|
456
|
+
...CONTAINER_OPTIONS,
|
|
457
|
+
{
|
|
458
|
+
name: '--metric',
|
|
459
|
+
type: 'string',
|
|
460
|
+
repeatable: true,
|
|
461
|
+
placeholder: '<func>:<field>',
|
|
462
|
+
description: 'Statistic to compute, repeatable, 1 to 20. e.g. avg:field_3'
|
|
463
|
+
},
|
|
464
|
+
{
|
|
465
|
+
name: '--by',
|
|
466
|
+
type: 'string',
|
|
467
|
+
repeatable: true,
|
|
468
|
+
placeholder: `<field>[:${TIME_BUCKETS.join('|')}]`,
|
|
469
|
+
description: 'Group by this field, repeatable, at most 2. A date field needs a bucket'
|
|
470
|
+
},
|
|
471
|
+
{
|
|
472
|
+
name: '--limit',
|
|
473
|
+
type: 'integer',
|
|
474
|
+
placeholder: '<n>',
|
|
475
|
+
description: 'How many groups, ranked by the first metric (default 20, max 200)'
|
|
476
|
+
},
|
|
477
|
+
FILTER_OPTION,
|
|
478
|
+
FILTERS_OPTION
|
|
479
|
+
],
|
|
480
|
+
request: (input) => {
|
|
481
|
+
const metrics = input.options.metric ?? [];
|
|
482
|
+
if (metrics.length === 0)
|
|
483
|
+
throw new UsageError('--metric <func>:<field> is required, up to 20');
|
|
484
|
+
const dimensions = input.options.by ?? [];
|
|
485
|
+
return {
|
|
486
|
+
method: 'GET',
|
|
487
|
+
path: `${containerPath(input)}/entries/aggregate`,
|
|
488
|
+
query: {
|
|
489
|
+
metrics: JSON.stringify(metrics.map(parseMetric)),
|
|
490
|
+
dimensions: dimensions.length > 0 ? JSON.stringify(dimensions.map(parseDimension)) : undefined,
|
|
491
|
+
limit: input.options.limit === undefined ? undefined : String(input.options.limit),
|
|
492
|
+
filters: filters(input)
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
},
|
|
496
|
+
render: aggregateTable,
|
|
497
|
+
examples: [
|
|
498
|
+
'jinshuju entry aggregate --form Kp7mQ2 --metric avg:field_3',
|
|
499
|
+
'jinshuju entry aggregate --form Kp7mQ2 --metric count:field_1 --by created_at:month',
|
|
500
|
+
"jinshuju entry aggregate --form Kp7mQ2 --metric sum:field_5 --by field_7 --limit 5 --filter 'created_at within_last 30d'"
|
|
501
|
+
]
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
path: ['entry', 'summary'],
|
|
505
|
+
summary: 'Profile every analysable field at once',
|
|
506
|
+
// The buckets are what a profile is; a table that drops them says nothing.
|
|
507
|
+
text: { essentialLists: ['buckets'] },
|
|
508
|
+
description: 'One pass over the data describing each field in its own terms: choices by share, numbers by ' +
|
|
509
|
+
'spread, dates by range. Submission metadata is left out — it describes the submitting, not the answer.',
|
|
510
|
+
options: [
|
|
511
|
+
...CONTAINER_OPTIONS,
|
|
512
|
+
{
|
|
513
|
+
name: '--fields',
|
|
514
|
+
type: 'list',
|
|
515
|
+
placeholder: '<api-code,...>',
|
|
516
|
+
description: 'Profile only these fields, at most 60'
|
|
517
|
+
},
|
|
518
|
+
{ name: '--no-overview', type: 'boolean', description: 'Leave out the form-level totals' },
|
|
519
|
+
FILTER_OPTION,
|
|
520
|
+
FILTERS_OPTION
|
|
521
|
+
],
|
|
522
|
+
request: (input) => ({
|
|
523
|
+
method: 'GET',
|
|
524
|
+
path: `${containerPath(input)}/entries/summary`,
|
|
525
|
+
query: {
|
|
526
|
+
fields: list(input.options.fields),
|
|
527
|
+
include_overview: input.options.no_overview ? 'false' : undefined,
|
|
528
|
+
filters: filters(input)
|
|
529
|
+
}
|
|
530
|
+
}),
|
|
531
|
+
examples: [
|
|
532
|
+
'jinshuju entry summary --form Kp7mQ2',
|
|
533
|
+
'jinshuju entry summary --form Kp7mQ2 --fields field_3,field_7 --no-overview'
|
|
534
|
+
]
|
|
535
|
+
},
|
|
536
|
+
{
|
|
537
|
+
path: ['entry', 'create'],
|
|
538
|
+
summary: 'Create entries',
|
|
539
|
+
description: 'The payload is keyed by field api_code, not by field label. --batch takes a list of them and ' +
|
|
540
|
+
'writes them in one request. ' +
|
|
541
|
+
VALUE_SHAPES,
|
|
542
|
+
options: [...CONTAINER_OPTIONS, JSON_OPTION, BATCH_OPTION, ATTACH_OPTION],
|
|
543
|
+
run: async (input, client) => {
|
|
544
|
+
const attached = attachments(input);
|
|
545
|
+
const batch = batchRows(input);
|
|
546
|
+
if (attached.length === 0) {
|
|
547
|
+
if (batch)
|
|
548
|
+
return client.request({ method: 'POST', path: batchPath(input), body: { entries: batch } });
|
|
549
|
+
return client.request({ method: 'POST', path: `${containerPath(input)}/entries`, body: payload(input) });
|
|
550
|
+
}
|
|
551
|
+
if (batch !== undefined)
|
|
552
|
+
throw new UsageError('--attach cannot be combined with --batch');
|
|
553
|
+
const body = await withUploads(input, client, attached);
|
|
554
|
+
return await client.request({ method: 'POST', path: `${containerPath(input)}/entries`, body });
|
|
555
|
+
},
|
|
556
|
+
examples: [
|
|
557
|
+
'jinshuju entry create --form Kp7mQ2 --json \'{"field_1":"张三"}\'',
|
|
558
|
+
'jinshuju entry create --form Kp7mQ2 --json @entry.json --attach field_5=./id-card.jpg',
|
|
559
|
+
'jinshuju entry create --form Kp7mQ2 --json \'{"field_2":[{"field_1":"高铁票"}]}\' --attach field_2.0.field_2=./invoice.pdf',
|
|
560
|
+
'jinshuju entry create --form Kp7mQ2 --batch @entries.json'
|
|
561
|
+
]
|
|
562
|
+
},
|
|
563
|
+
{
|
|
564
|
+
path: ['entry', 'update'],
|
|
565
|
+
summary: 'Update entries',
|
|
566
|
+
description: 'The payload merges onto the entry, leaving the fields it does not name alone; --replace ' +
|
|
567
|
+
'writes the entry as given, clearing the rest. --batch takes [{serial_number, entry}] and ' +
|
|
568
|
+
'always merges. ' +
|
|
569
|
+
VALUE_SHAPES,
|
|
570
|
+
args: [{ name: 'serial', required: false, description: 'Entry serial number; leave out with --batch' }],
|
|
571
|
+
options: [
|
|
572
|
+
...CONTAINER_OPTIONS,
|
|
573
|
+
JSON_OPTION,
|
|
574
|
+
BATCH_OPTION,
|
|
575
|
+
ATTACH_OPTION,
|
|
576
|
+
{
|
|
577
|
+
name: '--replace',
|
|
578
|
+
type: 'boolean',
|
|
579
|
+
description: 'Write the entry as given, clearing fields the payload leaves out'
|
|
580
|
+
}
|
|
581
|
+
],
|
|
582
|
+
// An attachment could only be added while creating the entry, so an entry
|
|
583
|
+
// that arrived without one had no way to gain one. The upload happens first
|
|
584
|
+
// and its id joins the patch, exactly as it does on create.
|
|
585
|
+
run: async (input, client) => {
|
|
586
|
+
const batch = batchRows(input);
|
|
587
|
+
const attached = attachments(input);
|
|
588
|
+
if (batch) {
|
|
589
|
+
if (input.args.serial)
|
|
590
|
+
throw new UsageError('--batch carries its own serial numbers, so <serial> is not taken');
|
|
591
|
+
if (input.options.replace)
|
|
592
|
+
throw new UsageError('--replace cannot be combined with --batch');
|
|
593
|
+
if (attached.length > 0)
|
|
594
|
+
throw new UsageError('--attach cannot be combined with --batch');
|
|
595
|
+
return client.request({ method: 'PATCH', path: batchPath(input), body: { entries: batch } });
|
|
596
|
+
}
|
|
597
|
+
if (!input.args.serial) {
|
|
598
|
+
throw new UsageError(attached.length > 0
|
|
599
|
+
? '<serial> is required to attach a file to an entry'
|
|
600
|
+
: '<serial> is required, or pass --batch');
|
|
601
|
+
}
|
|
602
|
+
const body = attached.length > 0 ? await withUploads(input, client, attached) : payload(input);
|
|
603
|
+
return client.request({
|
|
604
|
+
method: input.options.replace ? 'PUT' : 'PATCH',
|
|
605
|
+
path: `${containerPath(input)}/entries/${input.args.serial}`,
|
|
606
|
+
body
|
|
607
|
+
});
|
|
608
|
+
},
|
|
609
|
+
examples: [
|
|
610
|
+
'jinshuju entry update --form Kp7mQ2 12 --json \'{"field_1":"李四"}\'',
|
|
611
|
+
'jinshuju entry update --form Kp7mQ2 --batch @rows.json'
|
|
612
|
+
]
|
|
613
|
+
},
|
|
614
|
+
{
|
|
615
|
+
path: ['entry', 'import'],
|
|
616
|
+
summary: 'Import a spreadsheet into a form or table',
|
|
617
|
+
description: 'Two requests underneath: the file goes up, then the mapping says which column feeds which ' +
|
|
618
|
+
'field. Everything knowable up front — the file, the size your plan allows, the header row, ' +
|
|
619
|
+
'the mapping — is checked before any row is written, so a refused import has changed nothing ' +
|
|
620
|
+
"and the message names the sheet's real layout. Once accepted the rows are written in the " +
|
|
621
|
+
'background: the answer means started, not finished.',
|
|
622
|
+
args: [{ name: 'file', required: true, description: 'Path to an .xlsx, .xls or .csv file' }],
|
|
623
|
+
options: [
|
|
624
|
+
...CONTAINER_OPTIONS,
|
|
625
|
+
{
|
|
626
|
+
name: '--map',
|
|
627
|
+
type: 'string',
|
|
628
|
+
repeatable: true,
|
|
629
|
+
placeholder: '<api-code>=<column>',
|
|
630
|
+
description: 'Which column feeds which field. A number is a column index, anything else a header label'
|
|
631
|
+
},
|
|
632
|
+
{
|
|
633
|
+
name: '--header-row',
|
|
634
|
+
type: 'integer',
|
|
635
|
+
placeholder: '<n>',
|
|
636
|
+
description: 'Which row holds the headers, when it is not the first'
|
|
637
|
+
},
|
|
638
|
+
{
|
|
639
|
+
name: '--unique',
|
|
640
|
+
type: 'string',
|
|
641
|
+
placeholder: '<api-code>',
|
|
642
|
+
description: 'Treat this field as the key: a row matching an existing one updates it'
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
name: '--wait',
|
|
646
|
+
type: 'boolean',
|
|
647
|
+
description: 'Wait for the rows to be written and report what the import did, failing if it failed'
|
|
648
|
+
}
|
|
649
|
+
],
|
|
650
|
+
run: async (input, client) => {
|
|
651
|
+
const { token } = resolveContainer(input.options);
|
|
652
|
+
const mappings = input.options.map ?? [];
|
|
653
|
+
if (mappings.length === 0)
|
|
654
|
+
throw new UsageError('--map <api-code>=<column> is required, at least once');
|
|
655
|
+
const watching = progress();
|
|
656
|
+
try {
|
|
657
|
+
watching.step(`uploading ${basename(input.args.file)}…`);
|
|
658
|
+
const uploaded = await client.request(upload(`${API}/forms/${token}/import_files`, input.args.file));
|
|
659
|
+
watching.step('starting the import…');
|
|
660
|
+
const started = await client.request({
|
|
661
|
+
method: 'POST',
|
|
662
|
+
path: `${API}/forms/${token}/entry_imports`,
|
|
663
|
+
body: given({
|
|
664
|
+
attachment_id: uploaded.id,
|
|
665
|
+
columns: mappings.map(parseColumnMapping),
|
|
666
|
+
header_row_index: input.options.header_row,
|
|
667
|
+
unique_field_code: input.options.unique
|
|
668
|
+
})
|
|
669
|
+
});
|
|
670
|
+
if (!input.options.wait)
|
|
671
|
+
return started;
|
|
672
|
+
return await awaitImport(client, token, started.job_id, watching);
|
|
673
|
+
}
|
|
674
|
+
finally {
|
|
675
|
+
watching.done();
|
|
676
|
+
}
|
|
677
|
+
},
|
|
678
|
+
examples: [
|
|
679
|
+
'jinshuju entry import --form Kp7mQ2 ./报名.xlsx --map field_1=姓名 --map field_2=手机号',
|
|
680
|
+
'jinshuju entry import --form Kp7mQ2 ./报名.xlsx --map field_1=姓名 --wait',
|
|
681
|
+
'jinshuju entry import --table Vn4xR8 ./rows.csv --map field_1=1 --map field_2=2 --header-row 2'
|
|
682
|
+
]
|
|
683
|
+
},
|
|
684
|
+
{
|
|
685
|
+
path: ['entry', 'import-status'],
|
|
686
|
+
summary: 'Show what an import did, or how far it has got',
|
|
687
|
+
description: 'The id `entry import` answered with. A finished import reports how many rows it wrote, ' +
|
|
688
|
+
'skipped and rejected; one still running reports how far it has got.',
|
|
689
|
+
args: [{ name: 'job', required: true, description: 'Job id from `entry import`' }],
|
|
690
|
+
options: [...CONTAINER_OPTIONS],
|
|
691
|
+
request: (input) => {
|
|
692
|
+
const { token } = resolveContainer(input.options);
|
|
693
|
+
return { method: 'GET', path: `${API}/forms/${token}/entry_imports/${input.args.job}` };
|
|
694
|
+
},
|
|
695
|
+
examples: ['jinshuju entry import-status --form Kp7mQ2 6ab12edb3134316548d106a9']
|
|
696
|
+
},
|
|
697
|
+
{
|
|
698
|
+
path: ['entry', 'delete'],
|
|
699
|
+
summary: 'Delete one entry',
|
|
700
|
+
args: [{ name: 'serial', required: true, description: 'Entry serial number' }],
|
|
701
|
+
options: [...CONTAINER_OPTIONS, YES_OPTION],
|
|
702
|
+
request: (input) => {
|
|
703
|
+
confirmed(input, `Deleting entry ${input.args.serial}`);
|
|
704
|
+
return { method: 'DELETE', path: `${containerPath(input)}/entries/${input.args.serial}` };
|
|
705
|
+
},
|
|
706
|
+
examples: ['jinshuju entry delete --form Kp7mQ2 12 --yes']
|
|
707
|
+
},
|
|
708
|
+
{
|
|
709
|
+
path: ['entry', 'get'],
|
|
710
|
+
summary: 'Show one entry',
|
|
711
|
+
args: [{ name: 'serial', required: true, description: 'Entry serial number' }],
|
|
712
|
+
options: [
|
|
713
|
+
...CONTAINER_OPTIONS,
|
|
714
|
+
{ name: '--fields', type: 'list', placeholder: '<api-code,...>', description: 'Return only these fields' },
|
|
715
|
+
LABELS_OPTION
|
|
716
|
+
],
|
|
717
|
+
request: (input) => ({
|
|
718
|
+
method: 'GET',
|
|
719
|
+
path: `${containerPath(input)}/entries/${input.args.serial}`,
|
|
720
|
+
query: { fields: list(input.options.fields), include_labels: labels(input) }
|
|
721
|
+
})
|
|
722
|
+
}
|
|
723
|
+
];
|