@jinshuju/cli 0.7.0 → 0.9.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 CHANGED
@@ -267,9 +267,25 @@ you need no permission on the form itself. Flags that only make sense from an
267
267
  owner's point of view (`--sort`, `--view`, `--scope-filter` and the like) are
268
268
  rejected when combined with `--mine`.
269
269
 
270
+ ## Filling in someone else's form
271
+
272
+ A fill link — `…/f/<token>` for a form, `…/v/<token>` for a table's form view —
273
+ can be filled from the CLI the way anyone with the link fills it in a browser:
274
+
275
+ ```bash
276
+ jinshuju fill get https://acme.jinshuju.net/v/aB3dE9 # what it asks for, and whether you can submit
277
+ jinshuju fill submit https://acme.jinshuju.net/v/aB3dE9 --json '{"field_1":"Ada"}'
278
+ ```
279
+
280
+ No role on the form is needed, and only what the page shows its fillers comes
281
+ back. The entry is recorded as yours. A page that asks for something only the
282
+ person at it can do — a captcha, an SMS code, WeChat, a payment — lists it under
283
+ `blockers`, and is refused until it is filled in a browser. An OAuth session
284
+ needs the `submit_entries` scope for these.
285
+
270
286
  ## Working with files
271
287
 
272
- Three commands take a file. The CLI uploads with its own credential; there is no
288
+ Four commands take a file. The CLI uploads with its own credential; there is no
273
289
  ticket to fetch first.
274
290
 
275
291
  ```bash
@@ -278,6 +294,7 @@ jinshuju entry import --table Vn4xR8 ./rows.csv --map field_1=1 --map field_2=2
278
294
 
279
295
  jinshuju entry create --form Kp7mQ2 --json '{"field_1":"Alice"}' --attach field_5=./id-card.jpg
280
296
  jinshuju form theme set Kp7mQ2 --wallpaper ./bg.png
297
+ jinshuju opensearch edit Qy7nR3 --header ./banner.png
281
298
  ```
282
299
 
283
300
  ```bash
@@ -0,0 +1,4 @@
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;
4
+ export declare const FILL: readonly Command[];
@@ -0,0 +1,70 @@
1
+ import { UsageError } from '../errors.js';
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'];
6
+ const PAGE_ARG = {
7
+ name: 'page',
8
+ required: true,
9
+ description: 'The fill link as shared, e.g. https://example.jinshuju.net/v/aB3dE9, or just its path: v/aB3dE9'
10
+ };
11
+ /** `/api/v1/fill/<route>/<token>` for a fill link, or for the `f/…` / `v/…` path of one. */
12
+ export function fillPagePath(input) {
13
+ const page = input.args.page.trim();
14
+ let path = page;
15
+ if (/^https?:\/\//i.test(page)) {
16
+ try {
17
+ path = new URL(page).pathname;
18
+ }
19
+ catch {
20
+ throw new UsageError(`Not a link: ${page}`);
21
+ }
22
+ }
23
+ const [route, token] = path.split('/').filter(Boolean);
24
+ if (route === undefined || !ROUTES.includes(route) || !token) {
25
+ throw new UsageError(`Not a fill link: ${page}. A fill link ends in /f/<token> or /v/<token>.`);
26
+ }
27
+ return `/api/v1/fill/${route}/${encodeURIComponent(token)}`;
28
+ }
29
+ export const FILL = [
30
+ {
31
+ path: ['fill', 'get'],
32
+ summary: 'Show what a fill page asks for, and whether you can submit to it',
33
+ description: 'Reads the page as anyone with its link would: no role on the form or table is needed, and only what ' +
34
+ 'the page shows its fillers comes back. `status.key` is `open` when you can submit. `blockers` lists ' +
35
+ 'what only the person at the page can do — a captcha, an SMS code, WeChat, a payment — and while it is ' +
36
+ 'not empty the page has to be filled in a browser. `manageable` appears only when you could already ' +
37
+ 'read the form or table behind the page.',
38
+ args: [PAGE_ARG],
39
+ request: (input) => ({ method: 'GET', path: fillPagePath(input) }),
40
+ examples: ['jinshuju fill get https://example.jinshuju.net/v/aB3dE9', 'jinshuju fill get f/Kp7mQ2']
41
+ },
42
+ {
43
+ path: ['fill', 'submit'],
44
+ summary: 'Submit an entry through a fill page',
45
+ description: 'Submits as you, the way the page’s submit button does: only the fields the page shows are taken, the ' +
46
+ 'page’s required set applies, and the entry is recorded as yours. The payload is keyed by field ' +
47
+ 'api_code, as `fill get` lists them.',
48
+ args: [PAGE_ARG],
49
+ options: [
50
+ JSON_OPTION,
51
+ { name: '--password', type: 'string', placeholder: '<password>', description: 'The page’s access password' }
52
+ ],
53
+ payload: [
54
+ '{ "field_1": "张三", "field_2": "Abc1" }',
55
+ '',
56
+ 'Each type takes a value of its own shape — a choice by its code, as `fill get` lists them —',
57
+ 'and `jinshuju field types <Type>` shows it under `value`.'
58
+ ],
59
+ request: (input) => {
60
+ const entry = input.options.json;
61
+ if (entry === undefined)
62
+ throw new UsageError('--json is required: the values to submit, keyed by api_code');
63
+ const body = { entry };
64
+ if (input.options.password !== undefined)
65
+ body.password = input.options.password;
66
+ return { method: 'POST', path: `${fillPagePath(input)}/entries`, body };
67
+ },
68
+ examples: ['jinshuju fill submit https://example.jinshuju.net/v/aB3dE9 --json \'{"field_1":"张三"}\'']
69
+ }
70
+ ];
@@ -1,9 +1,7 @@
1
1
  import { FILTER_OPTION, FILTERS_OPTION, JSON_OPTION, MINE_OPTION, PAGINATION_OPTIONS, SORT_OPTION, UsageError } from '../options.js';
2
2
  import { API, FOLDER_OPTION, LISTING, filters, given, keywords, overriding, paging, payload, refuseWithMine, sort } from './shared.js';
3
3
  import { validateContainerPayload } from '../payload.js';
4
- import { progress } from '../progress.js';
5
- import { basename } from 'node:path';
6
- import { upload } from './upload.js';
4
+ import { uploadFormImage } from './upload.js';
7
5
  /**
8
6
  * The shape behind `--json` on a form, shown rather than described: `fields` is
9
7
  * the part nothing else hints at, and one choice field carries more of the
@@ -89,17 +87,6 @@ function themeBody(input) {
89
87
  secondary_color: input.options.secondary_color
90
88
  });
91
89
  }
92
- async function uploadImage(client, file, imageType) {
93
- const watching = progress();
94
- watching.step(`uploading ${basename(file)}…`);
95
- try {
96
- const uploaded = await client.request(upload(`${API}/form_image_attachments`, file, { image_type: imageType }));
97
- return uploaded.attachment_id;
98
- }
99
- finally {
100
- watching.done();
101
- }
102
- }
103
90
  /** The scenes a form can be created for, as the API names them. */
104
91
  const FORM_SCENES = [
105
92
  'survey',
@@ -371,11 +358,11 @@ export const FORM = [
371
358
  return client.request({ method: 'PATCH', path, body: themeBody(input) });
372
359
  const body = themeBody(input);
373
360
  if (wallpaper) {
374
- const image = await uploadImage(client, wallpaper, 'wallpaper');
361
+ const image = await uploadFormImage(client, wallpaper, 'wallpaper');
375
362
  body.wallpaper = { ...body.wallpaper, background_image_attachment_id: image };
376
363
  }
377
364
  if (header) {
378
- const image = await uploadImage(client, header, 'header');
365
+ const image = await uploadFormImage(client, header, 'header');
379
366
  body.header = { ...body.header, header_image_attachment_id: image };
380
367
  }
381
368
  return client.request({ method: 'PATCH', path, body });
@@ -11,6 +11,7 @@ import { ACCOUNT } from './account.js';
11
11
  import { COMMENT } from './comment.js';
12
12
  import { ENTRY } from './entry.js';
13
13
  import { FIELD } from './field.js';
14
+ import { FILL } from './fill.js';
14
15
  import { FOLDER } from './folder.js';
15
16
  import { FORM } from './form.js';
16
17
  import { LOCAL } from './local.js';
@@ -41,6 +42,12 @@ export const RESOURCES = [
41
42
  { name: 'entry', summary: 'Manage entries' },
42
43
  { name: 'comment', summary: 'Manage entry comments' },
43
44
  { name: 'opensearch', summary: 'Manage public queries' },
45
+ {
46
+ name: 'fill',
47
+ summary: 'Fill in a form through its fill link',
48
+ note: 'For a form or a table’s form view you were sent a link to, rather than one you manage. ' +
49
+ 'OAuth sessions need the submit_entries scope: `jinshuju auth login --scopes "… submit_entries"`.'
50
+ },
44
51
  { name: 'merchant', summary: '小金商户: onboarding, balance and settlement' },
45
52
  { name: 'transaction', summary: 'Payment orders placed on payment forms' },
46
53
  { name: 'refund', summary: 'Refunds of payment orders' },
@@ -59,6 +66,7 @@ export const COMMANDS = [
59
66
  ...ENTRY,
60
67
  ...COMMENT,
61
68
  ...OPENSEARCH,
69
+ ...FILL,
62
70
  ...TRADE,
63
71
  ...SMS
64
72
  ];
@@ -1,5 +1,6 @@
1
1
  import { JSON_OPTION, UsageError } from '../options.js';
2
2
  import { API, overriding, payload } from './shared.js';
3
+ import { uploadFormImage } from './upload.js';
3
4
  export const OPENSEARCH = [
4
5
  {
5
6
  path: ['opensearch', 'list'],
@@ -50,24 +51,29 @@ export const OPENSEARCH = [
50
51
  {
51
52
  path: ['opensearch', 'edit'],
52
53
  summary: 'Edit a public query, or turn it on and off',
54
+ description: '--header uploads an image and sets it as the header of the query page in one command.',
53
55
  args: [{ name: 'query', required: true, description: 'Public query token' }],
54
56
  options: [
55
57
  JSON_OPTION,
56
58
  { name: '--enable', type: 'boolean', description: 'Turn the query on' },
57
- { name: '--disable', type: 'boolean', description: 'Turn the query off' }
59
+ { name: '--disable', type: 'boolean', description: 'Turn the query off' },
60
+ { name: '--header', type: 'string', placeholder: '<file>', description: 'Image file to use as the header' }
58
61
  ],
59
- request: (input) => {
62
+ run: async (input, client) => {
60
63
  if (input.options.enable && input.options.disable) {
61
64
  throw new UsageError('--enable and --disable are opposites, pass one');
62
65
  }
63
66
  const rest = input.options.json ?? {};
64
67
  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
- };
68
+ const body = overriding(rest, { enabled });
69
+ const header = input.options.header;
70
+ if (header) {
71
+ // A query's header goes up as one of its form's images, the way a theme's does.
72
+ const image = await uploadFormImage(client, header, 'opensearch_header');
73
+ body.header = { background_image: { attachment_id: image } };
74
+ }
75
+ return client.request({ method: 'PATCH', path: `${API}/opensearch/queries/${input.args.query}`, body });
70
76
  },
71
- examples: ['jinshuju opensearch edit Qy7nR3 --disable']
77
+ examples: ['jinshuju opensearch edit Qy7nR3 --disable', 'jinshuju opensearch edit Qy7nR3 --header ./banner.png']
72
78
  }
73
79
  ];
@@ -1,7 +1,12 @@
1
- import type { HttpRequest } from '../http.js';
1
+ import type { HttpClient, HttpRequest } from '../http.js';
2
2
  /**
3
3
  * A file on disk, as multipart. The three endpoints that take one authenticate
4
4
  * like every other request, so there is no ticket to fetch first: the file goes
5
5
  * up in one call and comes back with an id to refer to it by.
6
6
  */
7
7
  export declare function upload(path: string, file: string, extra?: Record<string, string>): HttpRequest;
8
+ /**
9
+ * An image that a form's theme or one of its public queries refers to by id, uploaded
10
+ * with `imageType` saying which (header, wallpaper, opensearch_header).
11
+ */
12
+ export declare function uploadFormImage(client: HttpClient, file: string, imageType: string): Promise<string>;
@@ -1,4 +1,6 @@
1
1
  import { UsageError } from '../options.js';
2
+ import { progress } from '../progress.js';
3
+ import { API } from './shared.js';
2
4
  import { readFileSync } from 'node:fs';
3
5
  import { basename, extname } from 'node:path';
4
6
  /**
@@ -38,3 +40,18 @@ export function upload(path, file, extra = {}) {
38
40
  form.append(name, value);
39
41
  return { method: 'POST', path, form };
40
42
  }
43
+ /**
44
+ * An image that a form's theme or one of its public queries refers to by id, uploaded
45
+ * with `imageType` saying which (header, wallpaper, opensearch_header).
46
+ */
47
+ export async function uploadFormImage(client, file, imageType) {
48
+ const watching = progress();
49
+ watching.step(`uploading ${basename(file)}…`);
50
+ try {
51
+ const uploaded = await client.request(upload(`${API}/form_image_attachments`, file, { image_type: imageType }));
52
+ return uploaded.attachment_id;
53
+ }
54
+ finally {
55
+ watching.done();
56
+ }
57
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jinshuju/cli",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "Command line interface for Jinshuju Open API v1",
5
5
  "homepage": "https://github.com/jinshuju/cli#readme",
6
6
  "bugs": {