@jinshuju/cli 0.11.0 → 0.13.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,19 +1,18 @@
1
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';
2
+ import { LISTING, YES_OPTION, confirmed, filters, given, onContainer, paging, payload, refuseWithMine, requiredOption, sort } from './shared.js';
3
3
  import { RefusedError } from '../errors.js';
4
4
  import { progress } from '../progress.js';
5
5
  import { TransportError } from '../http.js';
6
+ import { call, choices } from '../spec.js';
6
7
  import { isRecord } from '../values.js';
7
8
  import { basename } from 'node:path';
8
9
  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
10
  function list(value) {
12
11
  const values = value;
13
12
  return values && values.length > 0 ? values.join(',') : undefined;
14
13
  }
15
14
  function labels(input) {
16
- return input.options.labels ? 'true' : undefined;
15
+ return input.options.labels ? true : undefined;
17
16
  }
18
17
  /**
19
18
  * The web host is not the API host on every environment, so the link is the
@@ -160,10 +159,7 @@ export async function awaitImport(client, token, jobId, watching) {
160
159
  for (;;) {
161
160
  let job;
162
161
  try {
163
- job = await client.request({
164
- method: 'GET',
165
- path: `${API}/forms/${token}/entry_imports/${jobId}`
166
- });
162
+ job = await client.request(call('getEntryImport', { path: { form_token: token, id: jobId } }));
167
163
  }
168
164
  catch (error) {
169
165
  throw recoverable(`the import started, but asking how it is going failed: ${error.message}`, error);
@@ -202,7 +198,7 @@ async function withUploads(input, client, attached) {
202
198
  try {
203
199
  for (const { field, row, dimension, file } of attached) {
204
200
  watching.step(`uploading ${basename(file)}…`);
205
- 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 }));
201
+ const uploaded = await client.request(upload('uploadEntryAttachment', { form_token: token }, file, dimension === undefined ? { field_api_code: field } : { field_api_code: field, dimension_api_code: dimension }));
206
202
  // A field holds a list of attachments, so each upload appends rather
207
203
  // than replacing what an earlier --attach for the same field put there.
208
204
  if (dimension === undefined) {
@@ -250,18 +246,38 @@ const ANALYSIS_VIEW_OPTION = {
250
246
  placeholder: '<view>',
251
247
  description: 'Only the entries this view shows; a filter narrows them further'
252
248
  };
249
+ const COUNT = {
250
+ form: 'countEntries',
251
+ table: 'countTableEntries',
252
+ formView: 'countFormViewEntries',
253
+ tableView: 'countTableViewEntries'
254
+ };
255
+ const AGGREGATE = {
256
+ form: 'aggregateEntries',
257
+ table: 'aggregateTableEntries',
258
+ formView: 'aggregateFormViewEntries',
259
+ tableView: 'aggregateTableViewEntries'
260
+ };
261
+ const SUMMARY = {
262
+ form: 'summarizeEntries',
263
+ table: 'summarizeTableEntries',
264
+ formView: 'summarizeFormViewEntries',
265
+ tableView: 'summarizeTableViewEntries'
266
+ };
253
267
  /** Through a view, an analysis covers only the entries that view shows. */
254
- function analysisPath(input, read) {
268
+ function analysis(input, ops, query) {
255
269
  const view = input.options.view;
256
- return `${containerPath(input)}${view ? `/views/${view}` : ''}/entries/${read}`;
270
+ return view
271
+ ? onContainer(input, ops.formView, ops.tableView, { path: { view_token: view }, query })
272
+ : onContainer(input, ops.form, ops.table, { query });
257
273
  }
258
274
  /**
259
275
  * Batch writes are served under the forms path alone, and it takes a table's
260
- * token just as well, so a table batches through the same URL.
276
+ * token just as well, so a table batches through the same operation.
261
277
  */
262
- function batchPath(input) {
278
+ function batch(input, id, entries) {
263
279
  const { token } = resolveContainer(input.options);
264
- return `${API}/forms/${token}/entries/batch`;
280
+ return call(id, { path: { form_token: token }, body: { entries } });
265
281
  }
266
282
  export const ENTRY = [
267
283
  {
@@ -284,9 +300,8 @@ export const ENTRY = [
284
300
  if (input.options.mine) {
285
301
  refuseWithMine(input, ['view', 'sort']);
286
302
  const { token } = resolveContainer(input.options);
287
- return {
288
- method: 'GET',
289
- path: `${API}/my/forms/${token}/entries`,
303
+ return call('listMySubmittedEntries', {
304
+ path: { form_token: token },
290
305
  query: {
291
306
  filters: filters(input),
292
307
  keyword: input.options.keyword,
@@ -294,7 +309,7 @@ export const ENTRY = [
294
309
  include_labels: labels(input),
295
310
  ...paging(input)
296
311
  }
297
- };
312
+ });
298
313
  }
299
314
  const view = input.options.view;
300
315
  if (view) {
@@ -311,15 +326,16 @@ export const ENTRY = [
311
326
  throw new UsageError('--fields cannot be combined with --view: the view decides its own columns. ' +
312
327
  'Change them with `view edit --columns`, or read the form without --view.');
313
328
  }
314
- return {
315
- method: 'GET',
316
- path: `${containerPath(input)}/views/${view}/entries`,
317
- query: { include_labels: labels(input), ...paging(input) }
318
- };
329
+ // A view is read fifty rows a page, and the API takes no page size for it.
330
+ if (input.options.limit !== undefined) {
331
+ throw new UsageError('--limit cannot be combined with --view: a view is read 50 rows a page');
332
+ }
333
+ return onContainer(input, 'listFormViewEntries', 'listTableViewEntries', {
334
+ path: { view_token: view },
335
+ query: { include_labels: labels(input), next: input.options.next }
336
+ });
319
337
  }
320
- return {
321
- method: 'GET',
322
- path: `${containerPath(input)}/entries`,
338
+ return onContainer(input, 'listEntries', 'listTableEntries', {
323
339
  query: {
324
340
  filters: filters(input),
325
341
  keyword: input.options.keyword,
@@ -328,7 +344,7 @@ export const ENTRY = [
328
344
  sort: sort(input, 'api_code'),
329
345
  ...paging(input)
330
346
  }
331
- };
347
+ });
332
348
  },
333
349
  paginate: LISTING,
334
350
  examples: [
@@ -352,14 +368,17 @@ export const ENTRY = [
352
368
  FILTERS_OPTION
353
369
  ],
354
370
  request: (input) => {
355
- const { tokens } = resolveContainers(input.options, MAX_COUNTED_CONTAINERS);
356
- const query = { filters: filters(input), keyword: input.options.keyword };
371
+ const { tokens } = resolveContainers(input.options);
372
+ const keyword = input.options.keyword;
357
373
  if (tokens.length === 1)
358
- return { method: 'GET', path: analysisPath(input, 'count'), query };
374
+ return analysis(input, COUNT, { filters: filters(input), keyword });
359
375
  if (input.options.view !== undefined) {
360
376
  throw new UsageError('--view belongs to one form or table, so it counts one container, not several');
361
377
  }
362
- return { method: 'GET', path: `${API}/entries/count`, query: { ...query, form_tokens: tokens.join(',') } };
378
+ if (keyword !== undefined) {
379
+ throw new UsageError('--keyword searches the fields of one form or table, so it counts one container, not several');
380
+ }
381
+ return call('countEntriesAcrossForms', { query: { filters: filters(input), form_tokens: tokens.join(',') } });
363
382
  },
364
383
  examples: [
365
384
  "jinshuju entry count --form Kp7mQ2 --filter 'field_3 gte 80'",
@@ -393,25 +412,21 @@ export const ENTRY = [
393
412
  const { tokens } = namedContainers(input.options);
394
413
  if (input.options.mine) {
395
414
  refuseWithMine(input, ['table', 'scope_filter']);
396
- return {
397
- method: 'GET',
398
- path: `${API}/my/search`,
415
+ return call('searchMySubmittedEntries', {
399
416
  query: {
400
417
  keyword: input.args.keyword,
401
418
  form_tokens: tokens.length > 0 ? tokens.join(',') : undefined
402
419
  }
403
- };
420
+ });
404
421
  }
405
422
  const scope = input.options.scope_filter ?? [];
406
- return {
407
- method: 'GET',
408
- path: `${API}/entries/search`,
423
+ return call('searchEntriesAcrossForms', {
409
424
  query: {
410
425
  keyword: input.args.keyword,
411
426
  form_tokens: tokens.length > 0 ? tokens.join(',') : undefined,
412
427
  filters: scope.length > 0 ? JSON.stringify(scope.map(parseFilter)) : undefined
413
428
  }
414
- };
429
+ });
415
430
  },
416
431
  examples: [
417
432
  'jinshuju entry search 某某公司',
@@ -441,7 +456,7 @@ export const ENTRY = [
441
456
  {
442
457
  name: '--kind',
443
458
  type: 'string',
444
- choices: ['form', 'table'],
459
+ choices: choices('getEntryStats', 'kind'),
445
460
  placeholder: '<kind>',
446
461
  description: 'Count only forms, or only tables'
447
462
  },
@@ -454,21 +469,19 @@ export const ENTRY = [
454
469
  {
455
470
  name: '--by',
456
471
  type: 'string',
457
- choices: ['day', 'week', 'month'],
472
+ choices: choices('getEntryStats', 'by'),
458
473
  placeholder: '<unit>',
459
474
  description: 'Also split the range into buckets: every one, empty ones with total 0, --limit per bucket. ' +
460
475
  'Weeks start on Monday; the first and last are clipped to the range. ' +
461
476
  'At most 92 days, 53 weeks or 24 months'
462
477
  }
463
478
  ],
464
- request: (input) => ({
465
- method: 'GET',
466
- path: `${API}/entries/stats`,
479
+ request: (input) => call('getEntryStats', {
467
480
  query: {
468
481
  from: requiredOption(input, 'from'),
469
482
  to: input.options.to,
470
483
  kind: input.options.kind,
471
- limit: input.options.limit === undefined ? undefined : String(input.options.limit),
484
+ limit: input.options.limit,
472
485
  by: input.options.by
473
486
  }
474
487
  }),
@@ -515,16 +528,12 @@ export const ENTRY = [
515
528
  if (metrics.length === 0)
516
529
  throw new UsageError('--metric <func>:<field> is required, up to 20');
517
530
  const dimensions = input.options.by ?? [];
518
- return {
519
- method: 'GET',
520
- path: analysisPath(input, 'aggregate'),
521
- query: {
522
- metrics: JSON.stringify(metrics.map(parseMetric)),
523
- dimensions: dimensions.length > 0 ? JSON.stringify(dimensions.map(parseDimension)) : undefined,
524
- limit: input.options.limit === undefined ? undefined : String(input.options.limit),
525
- filters: filters(input)
526
- }
527
- };
531
+ return analysis(input, AGGREGATE, {
532
+ metrics: JSON.stringify(metrics.map(parseMetric)),
533
+ dimensions: dimensions.length > 0 ? JSON.stringify(dimensions.map(parseDimension)) : undefined,
534
+ limit: input.options.limit,
535
+ filters: filters(input)
536
+ });
528
537
  },
529
538
  render: aggregateTable,
530
539
  examples: [
@@ -554,14 +563,10 @@ export const ENTRY = [
554
563
  FILTER_OPTION,
555
564
  FILTERS_OPTION
556
565
  ],
557
- request: (input) => ({
558
- method: 'GET',
559
- path: analysisPath(input, 'summary'),
560
- query: {
561
- fields: list(input.options.fields),
562
- include_overview: input.options.no_overview ? 'false' : undefined,
563
- filters: filters(input)
564
- }
566
+ request: (input) => analysis(input, SUMMARY, {
567
+ fields: list(input.options.fields),
568
+ include_overview: input.options.no_overview ? false : undefined,
569
+ filters: filters(input)
565
570
  }),
566
571
  examples: [
567
572
  'jinshuju entry summary --form Kp7mQ2',
@@ -580,16 +585,16 @@ export const ENTRY = [
580
585
  options: [...CONTAINER_OPTIONS, JSON_OPTION, BATCH_OPTION, ATTACH_OPTION],
581
586
  run: async (input, client) => {
582
587
  const attached = attachments(input);
583
- const batch = batchRows(input);
588
+ const rows = batchRows(input);
584
589
  if (attached.length === 0) {
585
- if (batch)
586
- return client.request({ method: 'POST', path: batchPath(input), body: { entries: batch } });
587
- return client.request({ method: 'POST', path: `${containerPath(input)}/entries`, body: payload(input) });
590
+ if (rows)
591
+ return client.request(batch(input, 'createEntriesBatch', rows));
592
+ return client.request(onContainer(input, 'createEntry', 'createTableEntry', { body: payload(input) }));
588
593
  }
589
- if (batch !== undefined)
594
+ if (rows !== undefined)
590
595
  throw new UsageError('--attach cannot be combined with --batch');
591
596
  const body = await withUploads(input, client, attached);
592
- return await client.request({ method: 'POST', path: `${containerPath(input)}/entries`, body });
597
+ return await client.request(onContainer(input, 'createEntry', 'createTableEntry', { body }));
593
598
  },
594
599
  examples: [
595
600
  'jinshuju entry create --form Kp7mQ2 --json \'{"field_1":"张三"}\'',
@@ -623,16 +628,16 @@ export const ENTRY = [
623
628
  // that arrived without one had no way to gain one. The upload happens first
624
629
  // and its id joins the patch, exactly as it does on create.
625
630
  run: async (input, client) => {
626
- const batch = batchRows(input);
631
+ const rows = batchRows(input);
627
632
  const attached = attachments(input);
628
- if (batch) {
633
+ if (rows) {
629
634
  if (input.args.serial)
630
635
  throw new UsageError('--batch carries its own serial numbers, so <serial> is not taken');
631
636
  if (input.options.replace)
632
637
  throw new UsageError('--replace cannot be combined with --batch');
633
638
  if (attached.length > 0)
634
639
  throw new UsageError('--attach cannot be combined with --batch');
635
- return client.request({ method: 'PATCH', path: batchPath(input), body: { entries: batch } });
640
+ return client.request(batch(input, 'patchEntriesBatch', rows));
636
641
  }
637
642
  if (!input.args.serial) {
638
643
  throw new UsageError(attached.length > 0
@@ -640,11 +645,10 @@ export const ENTRY = [
640
645
  : '<serial> is required, or pass --batch');
641
646
  }
642
647
  const body = attached.length > 0 ? await withUploads(input, client, attached) : payload(input);
643
- return client.request({
644
- method: input.options.replace ? 'PUT' : 'PATCH',
645
- path: `${containerPath(input)}/entries/${input.args.serial}`,
646
- body
647
- });
648
+ const path = { serial_number: input.args.serial };
649
+ return client.request(input.options.replace
650
+ ? onContainer(input, 'replaceEntry', 'replaceTableEntry', { path, body })
651
+ : onContainer(input, 'updateEntry', 'updateTableEntry', { path, body }));
648
652
  },
649
653
  examples: [
650
654
  'jinshuju entry update --form Kp7mQ2 12 --json \'{"field_1":"李四"}\'',
@@ -695,18 +699,17 @@ export const ENTRY = [
695
699
  const watching = progress();
696
700
  try {
697
701
  watching.step(`uploading ${basename(input.args.file)}…`);
698
- const uploaded = await client.request(upload(`${API}/forms/${token}/import_files`, input.args.file));
702
+ const uploaded = await client.request(upload('uploadImportFile', { form_token: token }, input.args.file));
699
703
  watching.step('starting the import…');
700
- const started = await client.request({
701
- method: 'POST',
702
- path: `${API}/forms/${token}/entry_imports`,
704
+ const started = await client.request(call('createEntryImport', {
705
+ path: { form_token: token },
703
706
  body: given({
704
707
  attachment_id: uploaded.id,
705
708
  columns: mappings.map(parseColumnMapping),
706
709
  header_row_index: input.options.header_row,
707
710
  unique_field_code: input.options.unique
708
711
  })
709
- });
712
+ }));
710
713
  if (!input.options.wait)
711
714
  return started;
712
715
  return await awaitImport(client, token, started.job_id, watching);
@@ -730,7 +733,7 @@ export const ENTRY = [
730
733
  options: [...CONTAINER_OPTIONS],
731
734
  request: (input) => {
732
735
  const { token } = resolveContainer(input.options);
733
- return { method: 'GET', path: `${API}/forms/${token}/entry_imports/${input.args.job}` };
736
+ return call('getEntryImport', { path: { form_token: token, id: input.args.job } });
734
737
  },
735
738
  examples: ['jinshuju entry import-status --form Kp7mQ2 6ab12edb3134316548d106a9']
736
739
  },
@@ -741,7 +744,7 @@ export const ENTRY = [
741
744
  options: [...CONTAINER_OPTIONS, YES_OPTION],
742
745
  request: (input) => {
743
746
  confirmed(input, `Deleting entry ${input.args.serial}`);
744
- return { method: 'DELETE', path: `${containerPath(input)}/entries/${input.args.serial}` };
747
+ return onContainer(input, 'deleteEntry', 'deleteTableEntry', { path: { serial_number: input.args.serial } });
745
748
  },
746
749
  examples: ['jinshuju entry delete --form Kp7mQ2 12 --yes']
747
750
  },
@@ -755,9 +758,8 @@ export const ENTRY = [
755
758
  { name: '--fields', type: 'list', placeholder: '<api-code,...>', description: 'Return only these fields' },
756
759
  LABELS_OPTION
757
760
  ],
758
- request: (input) => ({
759
- method: 'GET',
760
- path: `${containerPath(input)}/entries/${input.args.serial}`,
761
+ request: (input) => onContainer(input, 'getEntry', 'getTableEntry', {
762
+ path: { serial_number: input.args.serial },
761
763
  query: { fields: list(input.options.fields), include_labels: labels(input) }
762
764
  })
763
765
  }
@@ -1,5 +1,6 @@
1
1
  import { CONTAINER_OPTIONS, JSON_OPTION, UsageError } from '../options.js';
2
- import { API, YES_OPTION, confirmed, containerPath, one, payload, requiredOption } from './shared.js';
2
+ import { call } from '../spec.js';
3
+ import { YES_OPTION, confirmed, onContainer, one, payload, requiredOption } from './shared.js';
3
4
  /** One field, or a list of them — the same objects `fields` holds above. */
4
5
  const FIELD_PAYLOAD = [
5
6
  '{ "type": "TextField", "label": "Notes", "required": false }',
@@ -106,11 +107,12 @@ export const FIELD = [
106
107
  description: `Only types of this scope: ${FIELD_SCOPES.join(', ')}`
107
108
  }
108
109
  ],
109
- request: (input) => ({
110
- method: 'GET',
111
- path: input.args.type ? `${API}/field_types/${input.args.type}` : `${API}/field_types`,
112
- query: { kind: input.options.kind }
113
- }),
110
+ request: (input) => {
111
+ const query = { kind: input.options.kind };
112
+ return input.args.type
113
+ ? call('getFieldType', { path: { type: input.args.type }, query })
114
+ : call('listFieldTypes', { query });
115
+ },
114
116
  select: (body, input) => {
115
117
  const rows = Array.isArray(body.data)
116
118
  ? body.data
@@ -131,7 +133,7 @@ export const FIELD = [
131
133
  summary: 'List the fields of a form or table',
132
134
  description: 'Read out of the object structure, the same fields `form get` and `table get` return.',
133
135
  options: [...CONTAINER_OPTIONS],
134
- request: (input) => ({ method: 'GET', path: containerPath(input) }),
136
+ request: (input) => onContainer(input, 'getForm', 'getTable'),
135
137
  select: (body) => ({ data: selectFields(body) })
136
138
  },
137
139
  {
@@ -140,11 +142,7 @@ export const FIELD = [
140
142
  description: 'One field object, or a list of them. Do not pass api_code: the backend generates it.',
141
143
  options: [...CONTAINER_OPTIONS, JSON_OPTION],
142
144
  payload: FIELD_PAYLOAD,
143
- request: (input) => ({
144
- method: 'PATCH',
145
- path: containerPath(input),
146
- body: { fields: { add: one(payload(input)) } }
147
- }),
145
+ request: (input) => onContainer(input, 'updateForm', 'updateTable', { body: { fields: { add: one(payload(input)) } } }),
148
146
  select: (body, input) => {
149
147
  const labels = labelsOf(payload(input));
150
148
  return touchedFields(body, (field) => labels.has(String(field.label)));
@@ -157,9 +155,7 @@ export const FIELD = [
157
155
  description: 'The patch is merged onto the field; the api_code comes from the argument, not the payload.',
158
156
  args: [{ name: 'api-code', required: true, description: 'Field api_code, e.g. field_3' }],
159
157
  options: [...CONTAINER_OPTIONS, JSON_OPTION],
160
- request: (input) => ({
161
- method: 'PATCH',
162
- path: containerPath(input),
158
+ request: (input) => onContainer(input, 'updateForm', 'updateTable', {
163
159
  body: {
164
160
  fields: { update: [{ ...payload(input), api_code: input.args['api-code'] }] }
165
161
  }
@@ -173,9 +169,7 @@ export const FIELD = [
173
169
  description: 'The payload is the choice operations the field takes, e.g. {add, update, remove}.',
174
170
  args: [{ name: 'api-code', required: true, description: 'Field api_code' }],
175
171
  options: [...CONTAINER_OPTIONS, JSON_OPTION],
176
- request: (input) => ({
177
- method: 'PATCH',
178
- path: containerPath(input),
172
+ request: (input) => onContainer(input, 'updateForm', 'updateTable', {
179
173
  body: {
180
174
  fields: {
181
175
  update_choices: [{ ...payload(input), field_api_code: input.args['api-code'] }]
@@ -192,9 +186,7 @@ export const FIELD = [
192
186
  'this cannot express — a matrix statement, a cascade level — goes through --json.',
193
187
  args: [{ name: 'target', required: false, variadic: true, description: 'field api_code, or api_code:choice' }],
194
188
  options: [...CONTAINER_OPTIONS, JSON_OPTION],
195
- request: (input) => ({
196
- method: 'GET',
197
- path: `${containerPath(input)}/fields/check`,
189
+ request: (input) => onContainer(input, 'checkFormFieldData', 'checkTableFieldData', {
198
190
  query: { checks: JSON.stringify(checks(input, parseCheckTarget)) }
199
191
  }),
200
192
  examples: [
@@ -238,11 +230,9 @@ export const FIELD = [
238
230
  }
239
231
  ]
240
232
  : one(input.options.json);
241
- return {
242
- method: 'GET',
243
- path: `${containerPath(input)}/fields/preview_convert`,
233
+ return onContainer(input, 'previewFormFieldTypeChange', 'previewTableFieldTypeChange', {
244
234
  query: { checks: JSON.stringify(inline) }
245
- };
235
+ });
246
236
  },
247
237
  examples: ['jinshuju field preview-convert --form Kp7mQ2 field_1 --to RadioButton']
248
238
  },
@@ -254,7 +244,9 @@ export const FIELD = [
254
244
  options: [...CONTAINER_OPTIONS, YES_OPTION],
255
245
  request: (input) => {
256
246
  confirmed(input, `Removing ${input.args['api-code']} and any answers it holds`);
257
- return { method: 'PATCH', path: containerPath(input), body: { fields: { remove: [input.args['api-code']] } } };
247
+ return onContainer(input, 'updateForm', 'updateTable', {
248
+ body: { fields: { remove: [input.args['api-code']] } }
249
+ });
258
250
  },
259
251
  examples: ['jinshuju field remove --form Kp7mQ2 field_9 --yes']
260
252
  }
@@ -1,4 +1,2 @@
1
- import type { Command, CommandInput } from './types.js';
2
- /** `/api/v1/fill/<route>/<token>` for a fill link, or for the `f/…` / `v/…` path of one. */
3
- export declare function fillPagePath(input: CommandInput): string;
1
+ import type { Command } from './types.js';
4
2
  export declare const FILL: readonly Command[];
@@ -1,15 +1,22 @@
1
1
  import { UsageError } from '../errors.js';
2
2
  import { JSON_OPTION } from '../options.js';
3
- // The two kinds of fill page: `/f/<token>` for a form, `/v/<token>` for a
4
- // table's form view. The API path mirrors them, so a link is all it takes.
5
- const ROUTES = ['f', 'v'];
3
+ import { call } from '../spec.js';
4
+ /**
5
+ * The two kinds of fill page: `/f/<token>` for a form, `/v/<token>` for a
6
+ * table's form view. The API mirrors them as two operations each, so a link is
7
+ * all it takes to know which.
8
+ */
9
+ const ROUTES = {
10
+ f: { token: 'form_token', page: 'getFillFormPage', submit: 'createFillFormEntry' },
11
+ v: { token: 'view_token', page: 'getFillViewPage', submit: 'createFillViewEntry' }
12
+ };
6
13
  const PAGE_ARG = {
7
14
  name: 'page',
8
15
  required: true,
9
16
  description: 'The fill link as shared, e.g. https://example.jinshuju.net/v/aB3dE9, or just its path: v/aB3dE9'
10
17
  };
11
- /** `/api/v1/fill/<route>/<token>` for a fill link, or for the `f/…` / `v/…` path of one. */
12
- export function fillPagePath(input) {
18
+ /** The page's or the submit's request for a fill link, or for the `f/…` / `v/…` path of one. */
19
+ function fillPage(input, what, parts = {}) {
13
20
  const page = input.args.page.trim();
14
21
  let path = page;
15
22
  if (/^https?:\/\//i.test(page)) {
@@ -21,10 +28,11 @@ export function fillPagePath(input) {
21
28
  }
22
29
  }
23
30
  const [route, token] = path.split('/').filter(Boolean);
24
- if (route === undefined || !ROUTES.includes(route) || !token) {
31
+ const kind = route !== undefined && Object.hasOwn(ROUTES, route) ? ROUTES[route] : undefined;
32
+ if (!kind || !token) {
25
33
  throw new UsageError(`Not a fill link: ${page}. A fill link ends in /f/<token> or /v/<token>.`);
26
34
  }
27
- return `/api/v1/fill/${route}/${encodeURIComponent(token)}`;
35
+ return call(kind[what], { ...parts, path: { [kind.token]: token } });
28
36
  }
29
37
  export const FILL = [
30
38
  {
@@ -36,7 +44,7 @@ export const FILL = [
36
44
  'not empty the page has to be filled in a browser. `manageable` appears only when you could already ' +
37
45
  'read the form or table behind the page.',
38
46
  args: [PAGE_ARG],
39
- request: (input) => ({ method: 'GET', path: fillPagePath(input) }),
47
+ request: (input) => fillPage(input, 'page'),
40
48
  examples: ['jinshuju fill get https://example.jinshuju.net/v/aB3dE9', 'jinshuju fill get f/Kp7mQ2']
41
49
  },
42
50
  {
@@ -63,7 +71,7 @@ export const FILL = [
63
71
  const body = { entry };
64
72
  if (input.options.password !== undefined)
65
73
  body.password = input.options.password;
66
- return { method: 'POST', path: `${fillPagePath(input)}/entries`, body };
74
+ return fillPage(input, 'submit', { body });
67
75
  },
68
76
  examples: ['jinshuju fill submit https://example.jinshuju.net/v/aB3dE9 --json \'{"field_1":"张三"}\'']
69
77
  }
@@ -1,11 +1,12 @@
1
1
  import { LIMIT_OPTION } from '../options.js';
2
- import { API, paging } from './shared.js';
2
+ import { bodyChoices, call } from '../spec.js';
3
+ import { given, paging } from './shared.js';
3
4
  export const FOLDER = [
4
5
  {
5
6
  path: ['folder', 'list'],
6
7
  summary: 'List folders',
7
8
  options: [LIMIT_OPTION],
8
- request: (input) => ({ method: 'GET', path: `${API}/folders`, query: paging(input) })
9
+ request: (input) => call('listFolders', { query: paging(input) })
9
10
  },
10
11
  {
11
12
  path: ['folder', 'create'],
@@ -16,16 +17,12 @@ export const FOLDER = [
16
17
  {
17
18
  name: '--kind',
18
19
  type: 'string',
19
- choices: ['form', 'table'],
20
+ choices: bodyChoices('createFolder', 'kind'),
20
21
  placeholder: '<kind>',
21
22
  description: 'What the folder holds (default form)'
22
23
  }
23
24
  ],
24
- request: (input) => ({
25
- method: 'POST',
26
- path: `${API}/folders`,
27
- body: { name: input.args.name, kind: input.options.kind }
28
- }),
25
+ request: (input) => call('createFolder', { body: given({ name: input.args.name, kind: input.options.kind }) }),
29
26
  examples: ['jinshuju folder create 2026年活动', 'jinshuju folder create 台账 --kind table']
30
27
  }
31
28
  ];