@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.
- package/README.md +58 -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 +15 -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 +715 -0
- package/dist/commands/field.d.ts +3 -0
- package/dist/commands/field.js +259 -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 +394 -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,394 @@
|
|
|
1
|
+
import { FILTER_OPTION, FILTERS_OPTION, JSON_OPTION, MINE_OPTION, PAGINATION_OPTIONS, SORT_OPTION, UsageError } from '../options.js';
|
|
2
|
+
import { API, FOLDER_OPTION, LISTING, filters, given, keywords, overriding, paging, payload, refuseWithMine, sort } from './shared.js';
|
|
3
|
+
import { validateContainerPayload } from '../payload.js';
|
|
4
|
+
import { progress } from '../progress.js';
|
|
5
|
+
import { basename } from 'node:path';
|
|
6
|
+
import { upload } from './upload.js';
|
|
7
|
+
/**
|
|
8
|
+
* The shape behind `--json` on a form, shown rather than described: `fields` is
|
|
9
|
+
* the part nothing else hints at, and one choice field carries more of the
|
|
10
|
+
* grammar than a paragraph would. What `type` may be, and what each type takes
|
|
11
|
+
* beyond this, is `field types`.
|
|
12
|
+
*/
|
|
13
|
+
const FORM_PAYLOAD = [
|
|
14
|
+
'{',
|
|
15
|
+
' "name": "Event signup",',
|
|
16
|
+
' "description": "optional",',
|
|
17
|
+
' "fields": [',
|
|
18
|
+
' { "type": "TextField", "label": "Name", "required": true },',
|
|
19
|
+
' { "type": "RadioButton", "label": "Ticket", "choices": [',
|
|
20
|
+
' { "name": "Standard" }, { "name": "VIP" } ] }',
|
|
21
|
+
' ]',
|
|
22
|
+
'}',
|
|
23
|
+
'',
|
|
24
|
+
'A choice carries "name". The "value" it reads back with is the code the',
|
|
25
|
+
'backend assigns, not what you sent.',
|
|
26
|
+
'',
|
|
27
|
+
'Reading a form back does not give you something you can send again: a field',
|
|
28
|
+
'written as "TextField" reads as "single_line_text". `field types` lists the',
|
|
29
|
+
'names to write, and `field types <Type>` describes one.',
|
|
30
|
+
'',
|
|
31
|
+
'Field types: jinshuju field types'
|
|
32
|
+
];
|
|
33
|
+
/**
|
|
34
|
+
* What `form get --include` accepts, and the parameter each one asks for. The
|
|
35
|
+
* names are the CLI's — short, and about the thing rather than about the flag
|
|
36
|
+
* that fetches it.
|
|
37
|
+
*/
|
|
38
|
+
const FORM_INCLUDES = {
|
|
39
|
+
// `setting` is in the design and already part of the payload, so asking for
|
|
40
|
+
// it is honoured by there being nothing to fetch. It is listed so the help
|
|
41
|
+
// matches what the flag accepts.
|
|
42
|
+
setting: '',
|
|
43
|
+
theme: 'include_theme',
|
|
44
|
+
rules: 'include_field_rules',
|
|
45
|
+
extended: 'include_extended_attributes',
|
|
46
|
+
transactions: 'include_transactions',
|
|
47
|
+
analytics: 'include_analytics'
|
|
48
|
+
};
|
|
49
|
+
const INCLUDE_OPTION = {
|
|
50
|
+
name: '--include',
|
|
51
|
+
type: 'list',
|
|
52
|
+
placeholder: Object.keys(FORM_INCLUDES).join(','),
|
|
53
|
+
description: `Extra blocks to carry: ${Object.keys(FORM_INCLUDES).join(', ')}. The setting is always there`
|
|
54
|
+
};
|
|
55
|
+
function includes(input) {
|
|
56
|
+
const asked = input.options.include ?? [];
|
|
57
|
+
const query = {};
|
|
58
|
+
for (const name of asked) {
|
|
59
|
+
const parameter = FORM_INCLUDES[name];
|
|
60
|
+
if (parameter === '')
|
|
61
|
+
continue;
|
|
62
|
+
if (!parameter) {
|
|
63
|
+
throw new UsageError(`--include takes ${Object.keys(FORM_INCLUDES).join(', ')}, got ${JSON.stringify(name)}`);
|
|
64
|
+
}
|
|
65
|
+
query[parameter] = 'true';
|
|
66
|
+
}
|
|
67
|
+
return query;
|
|
68
|
+
}
|
|
69
|
+
function themeBody(input) {
|
|
70
|
+
const rest = input.options.json ?? {};
|
|
71
|
+
return overriding(rest, {
|
|
72
|
+
primary_color: input.options.primary_color,
|
|
73
|
+
secondary_color: input.options.secondary_color
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
async function uploadImage(client, file, imageType) {
|
|
77
|
+
const watching = progress();
|
|
78
|
+
watching.step(`uploading ${basename(file)}…`);
|
|
79
|
+
try {
|
|
80
|
+
const uploaded = await client.request(upload(`${API}/form_image_attachments`, file, { image_type: imageType }));
|
|
81
|
+
return uploaded.attachment_id;
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
watching.done();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** The scenes a form can be created for, as the API names them. */
|
|
88
|
+
const FORM_SCENES = [
|
|
89
|
+
'survey',
|
|
90
|
+
'registry',
|
|
91
|
+
'vote',
|
|
92
|
+
'exam',
|
|
93
|
+
'reservation',
|
|
94
|
+
'customer_acquisition',
|
|
95
|
+
'evaluation',
|
|
96
|
+
'online_payment'
|
|
97
|
+
];
|
|
98
|
+
/**
|
|
99
|
+
* A form's type picks both the scene it is created in and the settings block
|
|
100
|
+
* that belongs to it. Those settings live behind their own endpoint, so a
|
|
101
|
+
* payload carrying one is two requests, not one — and a generic edit would
|
|
102
|
+
* drop the block on the floor, since the form update only reads name,
|
|
103
|
+
* description, setting, fields and field_rules.
|
|
104
|
+
*/
|
|
105
|
+
const FORM_TYPES = {
|
|
106
|
+
normal: {},
|
|
107
|
+
exam: { scene: 'exam', settingKey: 'exam_setting', path: 'exam_setting' },
|
|
108
|
+
evaluation: { scene: 'evaluation', settingKey: 'evaluation_setting', path: 'evaluation_setting' }
|
|
109
|
+
};
|
|
110
|
+
/** Which settings block a payload carries, and where it has to be sent. */
|
|
111
|
+
function settingsBlock(body) {
|
|
112
|
+
for (const { settingKey, path } of Object.values(FORM_TYPES)) {
|
|
113
|
+
if (settingKey && path && body[settingKey] !== undefined) {
|
|
114
|
+
return { key: settingKey, path, value: body[settingKey] };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
/** The scene a --type implies, refusing a --scene that contradicts it. */
|
|
120
|
+
function sceneFor(input) {
|
|
121
|
+
const type = input.options.type ?? 'normal';
|
|
122
|
+
const scene = input.options.scene;
|
|
123
|
+
const implied = FORM_TYPES[type]?.scene;
|
|
124
|
+
if (implied && scene && scene !== implied) {
|
|
125
|
+
throw new UsageError(`--type ${type} is the ${implied} scene, so --scene ${scene} contradicts it`);
|
|
126
|
+
}
|
|
127
|
+
return implied ?? scene;
|
|
128
|
+
}
|
|
129
|
+
function createFormBody(input) {
|
|
130
|
+
const payloadBody = { ...validateContainerPayload(payload(input), 'form') };
|
|
131
|
+
const settings = settingsBlock(payloadBody);
|
|
132
|
+
if (settings)
|
|
133
|
+
delete payloadBody[settings.key];
|
|
134
|
+
return {
|
|
135
|
+
body: overriding(payloadBody, {
|
|
136
|
+
scene: sceneFor(input),
|
|
137
|
+
layout: input.options.layout,
|
|
138
|
+
folder_token: input.options.folder
|
|
139
|
+
}),
|
|
140
|
+
settings
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
export const FORM = [
|
|
144
|
+
{
|
|
145
|
+
path: ['form', 'list'],
|
|
146
|
+
summary: 'List forms',
|
|
147
|
+
description: 'Filters act on the form itself: form_name, created_at, last_entry_created_at, entries_count.',
|
|
148
|
+
options: [
|
|
149
|
+
{
|
|
150
|
+
name: '--name',
|
|
151
|
+
type: 'string',
|
|
152
|
+
repeatable: true,
|
|
153
|
+
placeholder: '<kw>',
|
|
154
|
+
description: 'Match forms whose name contains the keyword, repeatable'
|
|
155
|
+
},
|
|
156
|
+
{ name: '--with-transactions', type: 'boolean', description: "Carry each payment form's collected totals" },
|
|
157
|
+
MINE_OPTION,
|
|
158
|
+
FILTER_OPTION,
|
|
159
|
+
FILTERS_OPTION,
|
|
160
|
+
SORT_OPTION,
|
|
161
|
+
...PAGINATION_OPTIONS
|
|
162
|
+
],
|
|
163
|
+
request: (input) => {
|
|
164
|
+
if (input.options.mine) {
|
|
165
|
+
refuseWithMine(input, ['name', 'with_transactions', 'filter', 'filters', 'sort']);
|
|
166
|
+
return { method: 'GET', path: `${API}/my/forms`, query: paging(input) };
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
method: 'GET',
|
|
170
|
+
path: `${API}/forms`,
|
|
171
|
+
query: {
|
|
172
|
+
q: keywords(input.options.name),
|
|
173
|
+
include_transactions: input.options.with_transactions ? 'true' : undefined,
|
|
174
|
+
filters: filters(input),
|
|
175
|
+
sort: sort(input, 'field'),
|
|
176
|
+
...paging(input)
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
},
|
|
180
|
+
paginate: LISTING,
|
|
181
|
+
examples: [
|
|
182
|
+
'jinshuju form list --name 报名',
|
|
183
|
+
'jinshuju form list --sort entries_count:desc --limit 10',
|
|
184
|
+
'jinshuju form list --mine'
|
|
185
|
+
]
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
path: ['form', 'get'],
|
|
189
|
+
summary: 'Show a form: fields, types, choices',
|
|
190
|
+
description: 'The form carries its setting already. --include adds the blocks that are separate reads ' +
|
|
191
|
+
'otherwise, so asking for a form and its rules is one round trip. analytics says which ' +
|
|
192
|
+
'statistics each field takes, which is what the analysis reads validate against. `fields` ' +
|
|
193
|
+
'comes back as a list of one-key objects keyed by api_code, not a flat list — `field list` ' +
|
|
194
|
+
'answers the same fields flattened, with api_code on each.',
|
|
195
|
+
args: [{ name: 'form', required: true, description: 'Form token, six letters and digits, e.g. Kp7mQ2' }],
|
|
196
|
+
options: [INCLUDE_OPTION],
|
|
197
|
+
request: (input) => ({
|
|
198
|
+
method: 'GET',
|
|
199
|
+
path: `${API}/forms/${input.args.form}`,
|
|
200
|
+
query: includes(input)
|
|
201
|
+
}),
|
|
202
|
+
examples: ['jinshuju form get Kp7mQ2', 'jinshuju form get Kp7mQ2 --include theme,rules,analytics']
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
path: ['form', 'create'],
|
|
206
|
+
summary: 'Create a form',
|
|
207
|
+
description: 'Field types use the API v1 names. Run `jinshuju field types` for the full list and what each ' +
|
|
208
|
+
'one accepts. Do not pass api_code: the backend generates it. The scene decides what kind of ' +
|
|
209
|
+
'form it is — an exam scores its answers, a reservation holds slots — and the card layout ' +
|
|
210
|
+
'refuses the field types it cannot show.',
|
|
211
|
+
payload: FORM_PAYLOAD,
|
|
212
|
+
options: [
|
|
213
|
+
JSON_OPTION,
|
|
214
|
+
{
|
|
215
|
+
name: '--type',
|
|
216
|
+
type: 'string',
|
|
217
|
+
choices: Object.keys(FORM_TYPES),
|
|
218
|
+
placeholder: '<type>',
|
|
219
|
+
description: 'normal, exam or evaluation. An exam or evaluation also takes its own settings block in the payload'
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
name: '--scene',
|
|
223
|
+
type: 'string',
|
|
224
|
+
choices: FORM_SCENES,
|
|
225
|
+
placeholder: '<scene>',
|
|
226
|
+
description: `What the form is for: ${FORM_SCENES.join(', ')}`
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
name: '--layout',
|
|
230
|
+
type: 'string',
|
|
231
|
+
choices: ['classic', 'card'],
|
|
232
|
+
placeholder: '<layout>',
|
|
233
|
+
description: 'classic shows every field at once, card one page at a time'
|
|
234
|
+
},
|
|
235
|
+
FOLDER_OPTION
|
|
236
|
+
],
|
|
237
|
+
run: async (input, client) => {
|
|
238
|
+
const { body, settings } = createFormBody(input);
|
|
239
|
+
if (!settings)
|
|
240
|
+
return client.request({ method: 'POST', path: `${API}/forms`, body });
|
|
241
|
+
const form = await client.request({ method: 'POST', path: `${API}/forms`, body });
|
|
242
|
+
try {
|
|
243
|
+
await client.request({
|
|
244
|
+
method: 'PATCH',
|
|
245
|
+
path: `${API}/forms/${form.token}/${settings.path}`,
|
|
246
|
+
body: settings.value
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
catch (error) {
|
|
250
|
+
// The form exists; saying so beats an error that reads as though
|
|
251
|
+
// nothing happened and inviting a second one to be created.
|
|
252
|
+
throw new Error(`form ${form.token} was created, but its ${settings.key} was refused: ` +
|
|
253
|
+
`${error.message}. Fix it and apply with \`form edit\`.`, { cause: error });
|
|
254
|
+
}
|
|
255
|
+
return client.request({ method: 'GET', path: `${API}/forms/${form.token}` });
|
|
256
|
+
},
|
|
257
|
+
examples: [
|
|
258
|
+
'jinshuju form create --json @form.json',
|
|
259
|
+
'jinshuju form create --json @exam.json --type exam',
|
|
260
|
+
'cat form.json | jinshuju form create --json -'
|
|
261
|
+
]
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
path: ['form', 'edit'],
|
|
265
|
+
summary: 'Edit a form',
|
|
266
|
+
description: 'The payload carries the operations to apply: name, description, setting, and fields as ' +
|
|
267
|
+
'{add, update, update_choices, remove}. Only what is named changes.',
|
|
268
|
+
args: [{ name: 'form', required: true, description: 'Form token' }],
|
|
269
|
+
options: [JSON_OPTION],
|
|
270
|
+
run: async (input, client) => {
|
|
271
|
+
const body = { ...payload(input) };
|
|
272
|
+
const settings = settingsBlock(body);
|
|
273
|
+
if (!settings)
|
|
274
|
+
return client.request({ method: 'PATCH', path: `${API}/forms/${input.args.form}`, body });
|
|
275
|
+
delete body[settings.key];
|
|
276
|
+
// The settings go first: they are the half that can be refused for what
|
|
277
|
+
// the form is, so leading with them is what keeps a refusal from landing
|
|
278
|
+
// after the rest was already written.
|
|
279
|
+
await client.request({
|
|
280
|
+
method: 'PATCH',
|
|
281
|
+
path: `${API}/forms/${input.args.form}/${settings.path}`,
|
|
282
|
+
body: settings.value
|
|
283
|
+
});
|
|
284
|
+
if (Object.keys(body).length === 0)
|
|
285
|
+
return client.request({ method: 'GET', path: `${API}/forms/${input.args.form}` });
|
|
286
|
+
try {
|
|
287
|
+
return await client.request({ method: 'PATCH', path: `${API}/forms/${input.args.form}`, body });
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
// Ordering cannot make two requests atomic; it only chooses which half
|
|
291
|
+
// fails first. When the second one fails the first has landed, and an
|
|
292
|
+
// error that reads as though nothing happened would invite the whole
|
|
293
|
+
// edit to be sent again.
|
|
294
|
+
throw new Error(`${settings.key} was saved, but the rest of the edit (${Object.keys(body).join(', ')}) ` +
|
|
295
|
+
`was refused: ${error.message}. Re-send only what failed.`, { cause: error });
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
examples: [
|
|
299
|
+
'jinshuju form edit Kp7mQ2 --json \'{"name":"2026 活动报名"}\'',
|
|
300
|
+
'jinshuju form edit Kp7mQ2 --json \'{"exam_setting":{"total_score":100}}\''
|
|
301
|
+
]
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
path: ['form', 'copy'],
|
|
305
|
+
summary: 'Copy a form',
|
|
306
|
+
args: [{ name: 'form', required: true, description: 'Form token to copy' }],
|
|
307
|
+
options: [
|
|
308
|
+
{ name: '--name', type: 'string', placeholder: '<name>', description: 'Name for the copy' },
|
|
309
|
+
FOLDER_OPTION
|
|
310
|
+
],
|
|
311
|
+
request: (input) => ({
|
|
312
|
+
method: 'POST',
|
|
313
|
+
path: `${API}/forms/${input.args.form}/copy`,
|
|
314
|
+
body: given({ name: input.options.name, folder_token: input.options.folder })
|
|
315
|
+
})
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
path: ['form', 'move'],
|
|
319
|
+
summary: 'Move a form into a folder, or out of one',
|
|
320
|
+
args: [{ name: 'form', required: true, description: 'Form token' }],
|
|
321
|
+
options: [FOLDER_OPTION],
|
|
322
|
+
request: (input) => ({
|
|
323
|
+
method: 'PATCH',
|
|
324
|
+
path: `${API}/forms/${input.args.form}/folder`,
|
|
325
|
+
body: { folder_token: input.options.folder ?? '' }
|
|
326
|
+
}),
|
|
327
|
+
examples: ['jinshuju form move Kp7mQ2 --folder Fd2xK8', 'jinshuju form move Kp7mQ2']
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
path: ['form', 'theme', 'set'],
|
|
331
|
+
summary: "Set a form's theme",
|
|
332
|
+
description: 'The colours have flags of their own, and --wallpaper and --header each upload an image and ' +
|
|
333
|
+
'bind it to the theme in one command. Everything else the theme takes — typography, ' +
|
|
334
|
+
'form_container, submit_button — goes through --json.',
|
|
335
|
+
args: [{ name: 'form', required: true, description: 'Form token' }],
|
|
336
|
+
options: [
|
|
337
|
+
{ name: '--primary-color', type: 'string', placeholder: '<hex>', description: 'Primary colour, e.g. #1F6FEB' },
|
|
338
|
+
{ name: '--secondary-color', type: 'string', placeholder: '<hex>', description: 'Secondary colour' },
|
|
339
|
+
{
|
|
340
|
+
name: '--wallpaper',
|
|
341
|
+
type: 'string',
|
|
342
|
+
placeholder: '<file>',
|
|
343
|
+
description: 'Image file to use as the background'
|
|
344
|
+
},
|
|
345
|
+
{ name: '--header', type: 'string', placeholder: '<file>', description: 'Image file to use as the header' },
|
|
346
|
+
JSON_OPTION
|
|
347
|
+
],
|
|
348
|
+
run: async (input, client) => {
|
|
349
|
+
const wallpaper = input.options.wallpaper;
|
|
350
|
+
const header = input.options.header;
|
|
351
|
+
const path = `${API}/forms/${input.args.form}/theme`;
|
|
352
|
+
if (!wallpaper && !header)
|
|
353
|
+
return client.request({ method: 'PATCH', path, body: themeBody(input) });
|
|
354
|
+
const body = themeBody(input);
|
|
355
|
+
if (wallpaper) {
|
|
356
|
+
const image = await uploadImage(client, wallpaper, 'wallpaper');
|
|
357
|
+
body.wallpaper = { ...body.wallpaper, background_image_attachment_id: image };
|
|
358
|
+
}
|
|
359
|
+
if (header) {
|
|
360
|
+
const image = await uploadImage(client, header, 'header');
|
|
361
|
+
body.header = { ...body.header, header_image_attachment_id: image };
|
|
362
|
+
}
|
|
363
|
+
return client.request({ method: 'PATCH', path, body });
|
|
364
|
+
},
|
|
365
|
+
examples: [
|
|
366
|
+
'jinshuju form theme set Kp7mQ2 --primary-color "#1F6FEB"',
|
|
367
|
+
'jinshuju form theme set Kp7mQ2 --wallpaper ./bg.png'
|
|
368
|
+
]
|
|
369
|
+
},
|
|
370
|
+
{
|
|
371
|
+
path: ['form', 'rule', 'get'],
|
|
372
|
+
summary: 'Show the field display rules of a form',
|
|
373
|
+
args: [{ name: 'form', required: true, description: 'Form token' }],
|
|
374
|
+
request: (input) => ({ method: 'GET', path: `${API}/forms/${input.args.form}/field_rules` })
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
path: ['form', 'rule', 'edit'],
|
|
378
|
+
summary: 'Edit the field display rules of a form',
|
|
379
|
+
description: 'The payload is {add, update, remove}; a rule is targeted by the index `form rule get` shows.',
|
|
380
|
+
args: [{ name: 'form', required: true, description: 'Form token' }],
|
|
381
|
+
options: [JSON_OPTION],
|
|
382
|
+
request: (input) => ({
|
|
383
|
+
method: 'PATCH',
|
|
384
|
+
path: `${API}/forms/${input.args.form}`,
|
|
385
|
+
body: { field_rules: payload(input) }
|
|
386
|
+
})
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
path: ['form', 'cooperator', 'list'],
|
|
390
|
+
summary: 'List the cooperators of a form',
|
|
391
|
+
args: [{ name: 'form', required: true, description: 'Form token' }],
|
|
392
|
+
request: (input) => ({ method: 'GET', path: `${API}/forms/${input.args.form}/cooperators` })
|
|
393
|
+
}
|
|
394
|
+
];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every command the CLI has, as data.
|
|
3
|
+
*
|
|
4
|
+
* The shape is `jinshuju <resource> <verb> [args] [flags]`: resources kept at
|
|
5
|
+
* one level, the parent given by a flag —
|
|
6
|
+
* `entry list --form <token>`, not `form entry list <token>`. Help, argument
|
|
7
|
+
* checking and dispatch all read this table, so a command cannot be reachable
|
|
8
|
+
* without its help, nor accept a flag it never described.
|
|
9
|
+
*/
|
|
10
|
+
import type { Command, Resource } from './types.js';
|
|
11
|
+
export type { ArgSpec, Command, CommandInput, Pagination, Resource } from './types.js';
|
|
12
|
+
/** Resource order in the root help, and the one-liner each gets. */
|
|
13
|
+
export declare const RESOURCES: readonly Resource[];
|
|
14
|
+
export declare const COMMANDS: readonly Command[];
|
|
15
|
+
/** The command whose path the words begin with, longest match first. */
|
|
16
|
+
export declare function findCommand(words: readonly string[], commands?: readonly Command[]): Command | undefined;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every command the CLI has, as data.
|
|
3
|
+
*
|
|
4
|
+
* The shape is `jinshuju <resource> <verb> [args] [flags]`: resources kept at
|
|
5
|
+
* one level, the parent given by a flag —
|
|
6
|
+
* `entry list --form <token>`, not `form entry list <token>`. Help, argument
|
|
7
|
+
* checking and dispatch all read this table, so a command cannot be reachable
|
|
8
|
+
* without its help, nor accept a flag it never described.
|
|
9
|
+
*/
|
|
10
|
+
import { ACCOUNT } from './account.js';
|
|
11
|
+
import { COMMENT } from './comment.js';
|
|
12
|
+
import { ENTRY } from './entry.js';
|
|
13
|
+
import { FIELD } from './field.js';
|
|
14
|
+
import { FOLDER } from './folder.js';
|
|
15
|
+
import { FORM } from './form.js';
|
|
16
|
+
import { LOCAL } from './local.js';
|
|
17
|
+
import { OPENSEARCH } from './opensearch.js';
|
|
18
|
+
import { TABLE } from './table.js';
|
|
19
|
+
import { VIEW } from './view.js';
|
|
20
|
+
/** Resource order in the root help, and the one-liner each gets. */
|
|
21
|
+
export const RESOURCES = [
|
|
22
|
+
{
|
|
23
|
+
name: 'auth',
|
|
24
|
+
summary: 'Manage authentication',
|
|
25
|
+
note: 'login is the browser flow. To use a token instead: `jinshuju config set access_token <token>`, ' +
|
|
26
|
+
'or set JINSHUJU_ACCESS_TOKEN. `auth status --verify` says which account the credential belongs to.'
|
|
27
|
+
},
|
|
28
|
+
{ name: 'account', summary: 'Account and members' },
|
|
29
|
+
{ name: 'folder', summary: 'Manage folders' },
|
|
30
|
+
{
|
|
31
|
+
name: 'form',
|
|
32
|
+
summary: 'Manage forms',
|
|
33
|
+
note: 'There is no delete, on purpose: removing a form takes its entries with it, so it is ' +
|
|
34
|
+
'left to the web app, where the person doing it can see what they are about to lose.'
|
|
35
|
+
},
|
|
36
|
+
{ name: 'table', summary: 'Manage tables' },
|
|
37
|
+
{ name: 'field', summary: 'Manage fields' },
|
|
38
|
+
{ name: 'view', summary: 'Manage views' },
|
|
39
|
+
{ name: 'entry', summary: 'Manage entries' },
|
|
40
|
+
{ name: 'comment', summary: 'Manage entry comments' },
|
|
41
|
+
{ name: 'opensearch', summary: 'Manage public queries' },
|
|
42
|
+
{ name: 'config', summary: 'Manage CLI configuration' }
|
|
43
|
+
];
|
|
44
|
+
export const COMMANDS = [
|
|
45
|
+
...LOCAL,
|
|
46
|
+
...ACCOUNT,
|
|
47
|
+
...FOLDER,
|
|
48
|
+
...FORM,
|
|
49
|
+
...TABLE,
|
|
50
|
+
...FIELD,
|
|
51
|
+
...VIEW,
|
|
52
|
+
...ENTRY,
|
|
53
|
+
...COMMENT,
|
|
54
|
+
...OPENSEARCH
|
|
55
|
+
];
|
|
56
|
+
/** The command whose path the words begin with, longest match first. */
|
|
57
|
+
export function findCommand(words, commands = COMMANDS) {
|
|
58
|
+
let best;
|
|
59
|
+
for (const command of commands) {
|
|
60
|
+
if (command.path.length > words.length)
|
|
61
|
+
continue;
|
|
62
|
+
if (command.path.some((part, index) => words[index] !== part))
|
|
63
|
+
continue;
|
|
64
|
+
if (!best || command.path.length > best.path.length)
|
|
65
|
+
best = command;
|
|
66
|
+
}
|
|
67
|
+
return best;
|
|
68
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Command } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The commands that never reach the API: they read and write the config file,
|
|
4
|
+
* or run a browser login. `cli.ts` dispatches them itself, so they carry no
|
|
5
|
+
* request — but they belong in this table all the same, because help is
|
|
6
|
+
* rendered from it. Left out, `auth login --help` answered with the root
|
|
7
|
+
* listing and `--no-open`, `--verify` and `--show-secret` were documented
|
|
8
|
+
* nowhere a caller could reach.
|
|
9
|
+
*/
|
|
10
|
+
export declare const LOCAL: readonly Command[];
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { LOCAL_OPTIONS } from '../options.js';
|
|
2
|
+
import { CONFIG_KEYS } from '../config.js';
|
|
3
|
+
/** The local options a command takes, by flag name. */
|
|
4
|
+
function local(...names) {
|
|
5
|
+
return names.map((name) => {
|
|
6
|
+
const spec = LOCAL_OPTIONS.find((candidate) => candidate.name === name);
|
|
7
|
+
if (!spec)
|
|
8
|
+
throw new Error(`no local option ${name}`);
|
|
9
|
+
return spec;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The commands that never reach the API: they read and write the config file,
|
|
14
|
+
* or run a browser login. `cli.ts` dispatches them itself, so they carry no
|
|
15
|
+
* request — but they belong in this table all the same, because help is
|
|
16
|
+
* rendered from it. Left out, `auth login --help` answered with the root
|
|
17
|
+
* listing and `--no-open`, `--verify` and `--show-secret` were documented
|
|
18
|
+
* nowhere a caller could reach.
|
|
19
|
+
*/
|
|
20
|
+
export const LOCAL = [
|
|
21
|
+
{
|
|
22
|
+
path: ['auth', 'login'],
|
|
23
|
+
summary: 'Log in through the browser and store the session',
|
|
24
|
+
description: 'Opens the authorization page, waits for the redirect on a loopback port, and writes the ' +
|
|
25
|
+
'session to the config file. An access token or an API key pair, if configured, still ' +
|
|
26
|
+
'outranks what this stores.',
|
|
27
|
+
options: local('--auth-host', '--client-id', '--scopes', '--no-open', '--host'),
|
|
28
|
+
examples: ['jinshuju auth login', 'jinshuju auth login --no-open']
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
path: ['auth', 'status'],
|
|
32
|
+
summary: 'Show which credential is in use, and where it came from',
|
|
33
|
+
description: 'The precedence is access token, then API key and secret, then a stored browser login. ' +
|
|
34
|
+
'--verify spends one call to confirm the credential still works, and reports the account it ' +
|
|
35
|
+
'belongs to — with more than one configured, nothing else here says which is in play.',
|
|
36
|
+
options: local('--verify', '--api-key', '--api-secret', '--host', '--auth-host', '--client-id'),
|
|
37
|
+
examples: ['jinshuju auth status', 'jinshuju auth status --verify']
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
path: ['auth', 'refresh'],
|
|
41
|
+
summary: 'Renew the stored browser session',
|
|
42
|
+
description: 'Only an OAuth session can be refreshed; a token that stopped working has to be replaced by whoever issued it.',
|
|
43
|
+
options: local('--auth-host', '--client-id')
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
path: ['auth', 'logout'],
|
|
47
|
+
summary: 'Revoke the stored browser session and forget it',
|
|
48
|
+
description: "Leaves an access token or API key pair in the config alone: those are not this command's to drop.",
|
|
49
|
+
options: local('--auth-host', '--client-id')
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
path: ['config', 'get'],
|
|
53
|
+
summary: 'Read one configuration value',
|
|
54
|
+
description: `Keys: ${CONFIG_KEYS.join(', ')}. Secrets are masked unless --show-secret says otherwise.`,
|
|
55
|
+
args: [{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` }],
|
|
56
|
+
options: local('--show-secret'),
|
|
57
|
+
examples: ['jinshuju config get api_key', 'jinshuju config get access_token --show-secret']
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
path: ['config', 'set'],
|
|
61
|
+
summary: 'Write one configuration value',
|
|
62
|
+
description: `Keys: ${CONFIG_KEYS.join(', ')}. The file is written with mode 600. Environment variables ` +
|
|
63
|
+
'of the same name (JINSHUJU_ACCESS_TOKEN, JINSHUJU_API_KEY, …) outrank whatever is stored here.',
|
|
64
|
+
args: [
|
|
65
|
+
{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` },
|
|
66
|
+
{ name: 'value', required: true, description: 'The value to store' }
|
|
67
|
+
],
|
|
68
|
+
examples: ['jinshuju config set access_token xxx', 'jinshuju config set host https://jinshuju.net']
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
path: ['config', 'unset'],
|
|
72
|
+
summary: 'Remove one configuration value',
|
|
73
|
+
args: [{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` }],
|
|
74
|
+
examples: ['jinshuju config unset api_secret']
|
|
75
|
+
}
|
|
76
|
+
];
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { JSON_OPTION, UsageError } from '../options.js';
|
|
2
|
+
import { API, overriding, payload } from './shared.js';
|
|
3
|
+
export const OPENSEARCH = [
|
|
4
|
+
{
|
|
5
|
+
path: ['opensearch', 'list'],
|
|
6
|
+
summary: 'List public queries',
|
|
7
|
+
options: [{ name: '--form', type: 'string', placeholder: '<token>', description: 'Only the queries of this form' }],
|
|
8
|
+
request: (input) => ({
|
|
9
|
+
method: 'GET',
|
|
10
|
+
path: `${API}/opensearch/queries`,
|
|
11
|
+
query: { form_token: input.options.form }
|
|
12
|
+
})
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
path: ['opensearch', 'get'],
|
|
16
|
+
summary: 'Show a public query',
|
|
17
|
+
args: [{ name: 'query', required: true, description: 'Public query token' }],
|
|
18
|
+
request: (input) => ({ method: 'GET', path: `${API}/opensearch/queries/${input.args.query}` })
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
path: ['opensearch', 'fields'],
|
|
22
|
+
summary: 'Suggest the fields a public query can use',
|
|
23
|
+
options: [{ name: '--form', type: 'string', placeholder: '<token>', description: 'Form token' }],
|
|
24
|
+
request: (input) => {
|
|
25
|
+
const form = input.options.form;
|
|
26
|
+
if (!form)
|
|
27
|
+
throw new UsageError('--form <token> is required');
|
|
28
|
+
return { method: 'GET', path: `${API}/opensearch/query_suggestions`, query: { form_token: form } };
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
path: ['opensearch', 'create'],
|
|
33
|
+
summary: 'Create a public query',
|
|
34
|
+
description: 'Ask `opensearch fields --form <token>` which fields a query may search on and return.',
|
|
35
|
+
options: [
|
|
36
|
+
{ name: '--form', type: 'string', placeholder: '<token>', description: 'Form the query reads' },
|
|
37
|
+
JSON_OPTION
|
|
38
|
+
],
|
|
39
|
+
request: (input) => {
|
|
40
|
+
const form = input.options.form;
|
|
41
|
+
if (!form)
|
|
42
|
+
throw new UsageError('--form <token> is required');
|
|
43
|
+
return {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
path: `${API}/opensearch/queries`,
|
|
46
|
+
body: overriding(payload(input), { form_token: form })
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
path: ['opensearch', 'edit'],
|
|
52
|
+
summary: 'Edit a public query, or turn it on and off',
|
|
53
|
+
args: [{ name: 'query', required: true, description: 'Public query token' }],
|
|
54
|
+
options: [
|
|
55
|
+
JSON_OPTION,
|
|
56
|
+
{ name: '--enable', type: 'boolean', description: 'Turn the query on' },
|
|
57
|
+
{ name: '--disable', type: 'boolean', description: 'Turn the query off' }
|
|
58
|
+
],
|
|
59
|
+
request: (input) => {
|
|
60
|
+
if (input.options.enable && input.options.disable) {
|
|
61
|
+
throw new UsageError('--enable and --disable are opposites, pass one');
|
|
62
|
+
}
|
|
63
|
+
const rest = input.options.json ?? {};
|
|
64
|
+
const enabled = input.options.enable ? true : input.options.disable ? false : undefined;
|
|
65
|
+
return {
|
|
66
|
+
method: 'PATCH',
|
|
67
|
+
path: `${API}/opensearch/queries/${input.args.query}`,
|
|
68
|
+
body: overriding(rest, { enabled })
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
examples: ['jinshuju opensearch edit Qy7nR3 --disable']
|
|
72
|
+
}
|
|
73
|
+
];
|