@jinshuju/cli 0.1.3 → 0.2.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.
Files changed (59) hide show
  1. package/README.md +58 -1
  2. package/dist/args.d.ts +47 -0
  3. package/dist/args.js +185 -0
  4. package/dist/auth.js +43 -11
  5. package/dist/cli-bin.js +1 -1
  6. package/dist/cli.d.ts +2 -20
  7. package/dist/cli.js +71 -659
  8. package/dist/commands/account.d.ts +2 -0
  9. package/dist/commands/account.js +15 -0
  10. package/dist/commands/comment.d.ts +2 -0
  11. package/dist/commands/comment.js +66 -0
  12. package/dist/commands/entry.d.ts +31 -0
  13. package/dist/commands/entry.js +715 -0
  14. package/dist/commands/field.d.ts +3 -0
  15. package/dist/commands/field.js +259 -0
  16. package/dist/commands/folder.d.ts +2 -0
  17. package/dist/commands/folder.js +31 -0
  18. package/dist/commands/form.d.ts +2 -0
  19. package/dist/commands/form.js +394 -0
  20. package/dist/commands/index.d.ts +16 -0
  21. package/dist/commands/index.js +68 -0
  22. package/dist/commands/local.d.ts +10 -0
  23. package/dist/commands/local.js +76 -0
  24. package/dist/commands/opensearch.d.ts +2 -0
  25. package/dist/commands/opensearch.js +73 -0
  26. package/dist/commands/shared.d.ts +57 -0
  27. package/dist/commands/shared.js +122 -0
  28. package/dist/commands/table.d.ts +2 -0
  29. package/dist/commands/table.js +101 -0
  30. package/dist/{commands.d.ts → commands/types.d.ts} +24 -30
  31. package/dist/commands/types.js +1 -0
  32. package/dist/commands/upload.d.ts +7 -0
  33. package/dist/commands/upload.js +40 -0
  34. package/dist/commands/view.d.ts +2 -0
  35. package/dist/commands/view.js +81 -0
  36. package/dist/config.d.ts +2 -0
  37. package/dist/config.js +45 -5
  38. package/dist/errors.d.ts +48 -0
  39. package/dist/errors.js +99 -0
  40. package/dist/help.d.ts +1 -1
  41. package/dist/help.js +11 -1
  42. package/dist/http.d.ts +37 -4
  43. package/dist/http.js +119 -14
  44. package/dist/local.d.ts +24 -0
  45. package/dist/local.js +208 -0
  46. package/dist/options.d.ts +8 -4
  47. package/dist/options.js +63 -18
  48. package/dist/payload.d.ts +14 -8
  49. package/dist/payload.js +22 -54
  50. package/dist/render.d.ts +21 -0
  51. package/dist/render.js +315 -0
  52. package/dist/result.d.ts +31 -0
  53. package/dist/result.js +32 -0
  54. package/dist/values.d.ts +4 -0
  55. package/dist/values.js +8 -0
  56. package/dist/version.d.ts +6 -0
  57. package/dist/version.js +16 -0
  58. package/package.json +23 -18
  59. package/dist/commands.js +0 -1690
@@ -0,0 +1,3 @@
1
+ import type { Command } from './types.js';
2
+ export declare const FIELD_SCOPES: readonly ["normal", "exam", "customized"];
3
+ export declare const FIELD: readonly Command[];
@@ -0,0 +1,259 @@
1
+ import { CONTAINER_OPTIONS, JSON_OPTION, UsageError } from '../options.js';
2
+ import { API, YES_OPTION, confirmed, containerPath, one, payload, requiredOption } from './shared.js';
3
+ /** One field, or a list of them — the same objects `fields` holds above. */
4
+ const FIELD_PAYLOAD = [
5
+ '{ "type": "TextField", "label": "Notes", "required": false }',
6
+ '',
7
+ 'Field types: jinshuju field types'
8
+ ];
9
+ /**
10
+ * A form's fields arrive as one object per field, keyed by api_code. A list of
11
+ * fields is what a caller asked for, so it is a list here, with the api_code
12
+ * alongside the rest rather than hidden in the key.
13
+ */
14
+ function selectFields(body) {
15
+ const fields = body?.fields ?? [];
16
+ return fields.flatMap((entry) => Object.entries(entry).map(([api_code, attributes]) => ({ api_code, ...attributes })));
17
+ }
18
+ /** `field_7:choice_1` into the target the check endpoint reads. */
19
+ function parseCheckTarget(target) {
20
+ const [field_api_code, choice_value] = target.split(':');
21
+ if (!field_api_code)
22
+ throw new UsageError(`a check target must be '<api-code>[:<choice>]', got ${JSON.stringify(target)}`);
23
+ return choice_value === undefined ? { field_api_code } : { field_api_code, choice_value };
24
+ }
25
+ /**
26
+ * The targets a batch check asks about: the plain ones as arguments, and the
27
+ * shapes the argument grammar cannot reach through --json. Both at once is
28
+ * allowed — one edit's targets belong in one call, whatever shape each is.
29
+ */
30
+ function checks(input, parse) {
31
+ const fromArgs = input.rest.map(parse);
32
+ const fromJson = input.options.json === undefined ? [] : one(input.options.json);
33
+ const all = [...fromArgs, ...fromJson];
34
+ if (all.length === 0)
35
+ throw new UsageError('name at least one target, as an argument or through --json');
36
+ return all;
37
+ }
38
+ export const FIELD_SCOPES = ['normal', 'exam', 'customized'];
39
+ /**
40
+ * A listing has to stay scannable and one type has to be readable, and those
41
+ * want opposite things from `structure`: its descriptions are sentences, which
42
+ * turn a table of fifty rows into a wall. A listing shows which keys a type
43
+ * takes; naming one type spells them out.
44
+ */
45
+ function fieldTypesForReading(body) {
46
+ const rows = body.data ?? [];
47
+ if (rows.length !== 1) {
48
+ return {
49
+ data: rows.map((row) => ({
50
+ ...row,
51
+ structure: Object.keys(row.structure ?? {}).join(', ') || undefined
52
+ }))
53
+ };
54
+ }
55
+ return rows[0];
56
+ }
57
+ /**
58
+ * What a field write touched, out of the whole form it answers with.
59
+ *
60
+ * A patch returns the container, so adding one field to a form of twenty-five
61
+ * answers with all twenty-five and leaves the caller hunting for the api_code it
62
+ * just created. `update` and `update-choices` name their field, so that row is
63
+ * the answer; `add` does not get an api_code back to match on, but it does know
64
+ * the labels it sent, and those name the rows that were not there before.
65
+ */
66
+ function touchedFields(body, wanted) {
67
+ const fields = selectFields(body);
68
+ const touched = fields.filter(wanted);
69
+ return { data: touched.length > 0 ? touched : fields };
70
+ }
71
+ /** The labels a field payload carries, one object or a list of them. */
72
+ function labelsOf(payload) {
73
+ const each = Array.isArray(payload) ? payload : [payload];
74
+ return new Set(each.flatMap((item) => {
75
+ const label = item?.label;
76
+ return typeof label === 'string' ? [label] : [];
77
+ }));
78
+ }
79
+ const KIND_OPTION = {
80
+ name: '--kind',
81
+ type: 'string',
82
+ choices: ['form', 'table'],
83
+ placeholder: '<kind>',
84
+ description: 'Which container the types are for (default form)'
85
+ };
86
+ export const FIELD = [
87
+ {
88
+ path: ['field', 'types'],
89
+ summary: 'List the field types a form or table can hold',
90
+ description: 'What to put in `type` when adding a field, and what each type accepts. `structure` is the ' +
91
+ "part no example can carry: the keys that give a type its shape, like a table's columns or " +
92
+ "a cascade's nesting. `settings` are the flat keys beside them, `flags` the booleans, and " +
93
+ '`read_as` the name the same field answers with when read back — which is not the name you ' +
94
+ 'write. Name one type to see its structure described in full. A table holds far fewer types ' +
95
+ 'than a form, and a scorable question type only belongs to a form that scores answers.',
96
+ args: [{ name: 'type', required: false, description: 'One type name, e.g. RadioButton' }],
97
+ options: [
98
+ KIND_OPTION,
99
+ {
100
+ name: '--scope',
101
+ type: 'string',
102
+ choices: FIELD_SCOPES,
103
+ placeholder: '<scope>',
104
+ description: `Only types of this scope: ${FIELD_SCOPES.join(', ')}`
105
+ }
106
+ ],
107
+ request: (input) => ({
108
+ method: 'GET',
109
+ path: input.args.type ? `${API}/field_types/${input.args.type}` : `${API}/field_types`,
110
+ query: { kind: input.options.kind }
111
+ }),
112
+ select: (body, input) => {
113
+ const rows = Array.isArray(body.data)
114
+ ? body.data
115
+ : [body];
116
+ const scope = input.options.scope;
117
+ return { data: scope ? rows.filter((row) => row.scope === scope) : rows };
118
+ },
119
+ render: fieldTypesForReading,
120
+ examples: [
121
+ 'jinshuju field types',
122
+ 'jinshuju field types --kind table',
123
+ 'jinshuju field types --scope exam',
124
+ 'jinshuju field types CascadeDropDown'
125
+ ]
126
+ },
127
+ {
128
+ path: ['field', 'list'],
129
+ summary: 'List the fields of a form or table',
130
+ description: 'Read out of the object structure, the same fields `form get` and `table get` return.',
131
+ options: [...CONTAINER_OPTIONS],
132
+ request: (input) => ({ method: 'GET', path: containerPath(input) }),
133
+ select: (body) => ({ data: selectFields(body) })
134
+ },
135
+ {
136
+ path: ['field', 'add'],
137
+ summary: 'Add fields to a form or table',
138
+ description: 'One field object, or a list of them. Do not pass api_code: the backend generates it.',
139
+ options: [...CONTAINER_OPTIONS, JSON_OPTION],
140
+ payload: FIELD_PAYLOAD,
141
+ request: (input) => ({
142
+ method: 'PATCH',
143
+ path: containerPath(input),
144
+ body: { fields: { add: one(payload(input)) } }
145
+ }),
146
+ select: (body, input) => {
147
+ const labels = labelsOf(payload(input));
148
+ return touchedFields(body, (field) => labels.has(String(field.label)));
149
+ },
150
+ examples: ['jinshuju field add --form Kp7mQ2 --json \'{"type":"TextField","label":"备注"}\'']
151
+ },
152
+ {
153
+ path: ['field', 'update'],
154
+ summary: 'Update one field',
155
+ description: 'The patch is merged onto the field; the api_code comes from the argument, not the payload.',
156
+ args: [{ name: 'api-code', required: true, description: 'Field api_code, e.g. field_3' }],
157
+ options: [...CONTAINER_OPTIONS, JSON_OPTION],
158
+ request: (input) => ({
159
+ method: 'PATCH',
160
+ path: containerPath(input),
161
+ body: {
162
+ fields: { update: [{ ...payload(input), api_code: input.args['api-code'] }] }
163
+ }
164
+ }),
165
+ select: (body, input) => touchedFields(body, (field) => field.api_code === input.args['api-code']),
166
+ examples: ['jinshuju field update --form Kp7mQ2 field_3 --json \'{"required":true}\'']
167
+ },
168
+ {
169
+ path: ['field', 'update-choices'],
170
+ summary: "Change a field's choices",
171
+ description: 'The payload is the choice operations the field takes, e.g. {add, update, remove}.',
172
+ args: [{ name: 'api-code', required: true, description: 'Field api_code' }],
173
+ options: [...CONTAINER_OPTIONS, JSON_OPTION],
174
+ request: (input) => ({
175
+ method: 'PATCH',
176
+ path: containerPath(input),
177
+ body: {
178
+ fields: {
179
+ update_choices: [{ ...payload(input), field_api_code: input.args['api-code'] }]
180
+ }
181
+ }
182
+ }),
183
+ select: (body, input) => touchedFields(body, (field) => field.api_code === input.args['api-code'])
184
+ },
185
+ {
186
+ path: ['field', 'check'],
187
+ summary: 'Ask whether fields or choices already hold data',
188
+ description: 'The question to ask before removing one: removing a field or choice that still holds ' +
189
+ 'entries deletes those entries with it. Name a choice with <api-code>:<choice>. A shape ' +
190
+ 'this cannot express — a matrix statement, a cascade level — goes through --json.',
191
+ args: [{ name: 'target', required: false, variadic: true, description: 'field api_code, or api_code:choice' }],
192
+ options: [...CONTAINER_OPTIONS, JSON_OPTION],
193
+ request: (input) => ({
194
+ method: 'GET',
195
+ path: `${containerPath(input)}/fields/check`,
196
+ query: { checks: JSON.stringify(checks(input, parseCheckTarget)) }
197
+ }),
198
+ examples: [
199
+ 'jinshuju field check --form Kp7mQ2 field_3 field_9',
200
+ 'jinshuju field check --form Kp7mQ2 field_7:choice_1'
201
+ ]
202
+ },
203
+ {
204
+ path: ['field', 'preview-convert'],
205
+ summary: "Preview what changing a field's type would do to its data",
206
+ description: 'The conversion happens in place, so the only thing at stake is the data: this reports how ' +
207
+ 'many values are kept and how many are cleared. The edit itself is `field update <api-code> ' +
208
+ '--json \'{"type":"TextArea"}\'`, and supported=false means that call would refuse it. ' +
209
+ 'source_type and target_type are write names (NumberField), not the names a read answers with.',
210
+ args: [
211
+ { name: 'api-code', required: false, description: 'Field api_code; omit it only when --json names the field' }
212
+ ],
213
+ options: [
214
+ ...CONTAINER_OPTIONS,
215
+ { name: '--to', type: 'string', placeholder: '<type>', description: 'Target field type, e.g. RadioButton' },
216
+ {
217
+ name: '--precision',
218
+ type: 'string',
219
+ placeholder: '<precision>',
220
+ description: 'For a DateTimeField target, the precision the edit will use'
221
+ },
222
+ JSON_OPTION
223
+ ],
224
+ request: (input) => {
225
+ // Taking --json used to drop the argument beside it without a word, and
226
+ // the server answered about a field named nothing. One or the other.
227
+ if (input.options.json !== undefined && input.args['api-code'] !== undefined) {
228
+ throw new UsageError('name the field as an argument or through --json, not both');
229
+ }
230
+ const inline = input.options.json === undefined
231
+ ? [
232
+ {
233
+ field_api_code: input.args['api-code'],
234
+ target_type: requiredOption(input, 'to'),
235
+ target_precision: input.options.precision
236
+ }
237
+ ]
238
+ : one(input.options.json);
239
+ return {
240
+ method: 'GET',
241
+ path: `${containerPath(input)}/fields/preview_convert`,
242
+ query: { checks: JSON.stringify(inline) }
243
+ };
244
+ },
245
+ examples: ['jinshuju field preview-convert --form Kp7mQ2 field_1 --to RadioButton']
246
+ },
247
+ {
248
+ path: ['field', 'remove'],
249
+ summary: 'Remove a field',
250
+ description: 'Removing a field that still holds answers deletes those answers with it, and cannot be undone.',
251
+ args: [{ name: 'api-code', required: true, description: 'Field api_code' }],
252
+ options: [...CONTAINER_OPTIONS, YES_OPTION],
253
+ request: (input) => {
254
+ confirmed(input, `Removing ${input.args['api-code']} and any answers it holds`);
255
+ return { method: 'PATCH', path: containerPath(input), body: { fields: { remove: [input.args['api-code']] } } };
256
+ },
257
+ examples: ['jinshuju field remove --form Kp7mQ2 field_9 --yes']
258
+ }
259
+ ];
@@ -0,0 +1,2 @@
1
+ import type { Command } from './types.js';
2
+ export declare const FOLDER: readonly Command[];
@@ -0,0 +1,31 @@
1
+ import { LIMIT_OPTION } from '../options.js';
2
+ import { API, paging } from './shared.js';
3
+ export const FOLDER = [
4
+ {
5
+ path: ['folder', 'list'],
6
+ summary: 'List folders',
7
+ options: [LIMIT_OPTION],
8
+ request: (input) => ({ method: 'GET', path: `${API}/folders`, query: paging(input) })
9
+ },
10
+ {
11
+ path: ['folder', 'create'],
12
+ summary: 'Create a folder',
13
+ description: 'A folder holds one kind. A table refuses a form folder, so say which when it is not forms.',
14
+ args: [{ name: 'name', required: true, description: 'Folder name' }],
15
+ options: [
16
+ {
17
+ name: '--kind',
18
+ type: 'string',
19
+ choices: ['form', 'table'],
20
+ placeholder: '<kind>',
21
+ description: 'What the folder holds (default form)'
22
+ }
23
+ ],
24
+ request: (input) => ({
25
+ method: 'POST',
26
+ path: `${API}/folders`,
27
+ body: { name: input.args.name, kind: input.options.kind }
28
+ }),
29
+ examples: ['jinshuju folder create 2026年活动', 'jinshuju folder create 台账 --kind table']
30
+ }
31
+ ];
@@ -0,0 +1,2 @@
1
+ import type { Command } from './types.js';
2
+ export declare const FORM: readonly Command[];