@jinshuju/cli 0.6.0 → 0.8.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
@@ -138,6 +138,9 @@ jinshuju entry list --form Kp7mQ2 --all
138
138
  `--limit` can only ask for less: a listing's default page size is also its
139
139
  maximum (50 in most cases), and anything larger is capped.
140
140
 
141
+ `--all` reads at most 200 pages. Past that it stops with an error naming the
142
+ cursor; pass it to `--next` (with `--all` again) to read on.
143
+
141
144
  `--filter` is repeatable and the conditions are AND-combined. For conditions it
142
145
  cannot express, use `--filters <json|@file>`. Cursors are opaque strings — pass
143
146
  the `next` value from the previous response back verbatim.
@@ -266,7 +269,7 @@ rejected when combined with `--mine`.
266
269
 
267
270
  ## Working with files
268
271
 
269
- Three commands take a file. The CLI uploads with its own credential; there is no
272
+ Four commands take a file. The CLI uploads with its own credential; there is no
270
273
  ticket to fetch first.
271
274
 
272
275
  ```bash
@@ -275,6 +278,7 @@ jinshuju entry import --table Vn4xR8 ./rows.csv --map field_1=1 --map field_2=2
275
278
 
276
279
  jinshuju entry create --form Kp7mQ2 --json '{"field_1":"Alice"}' --attach field_5=./id-card.jpg
277
280
  jinshuju form theme set Kp7mQ2 --wallpaper ./bg.png
281
+ jinshuju opensearch edit Qy7nR3 --header ./banner.png
278
282
  ```
279
283
 
280
284
  ```bash
package/dist/cli.js CHANGED
@@ -84,10 +84,10 @@ async function runRemote(command, words, flags, runtime, stdin) {
84
84
  return ok(format(selected, output, width, hints));
85
85
  }
86
86
  /**
87
- * More pages than any listing has. A cursor that never runs out would
88
- * otherwise read forever; stopping here says so instead, with what was read.
87
+ * The most pages one `--all` reads. Past it the listing goes on with `--next`,
88
+ * and a cursor that never runs out cannot read forever.
89
89
  */
90
- const MAX_PAGES = 10_000;
90
+ const MAX_PAGES = 200;
91
91
  /**
92
92
  * Every page of a listing, handed on a page at a time. A cursor is opaque: it
93
93
  * goes back exactly as it came. Answers how many rows there were.
@@ -113,9 +113,9 @@ async function readAllPages(client, request, paginate, watching, onPage) {
113
113
  if (String(next) === cursor) {
114
114
  throw new HttpError(`the server answered page ${page} with the cursor it was asked for; stopping rather than looping`, 200, body);
115
115
  }
116
- if (page >= MAX_PAGES)
117
- throw new Error(`stopped after ${MAX_PAGES} pages and ${count} rows; the listing has no end`);
118
116
  cursor = String(next);
117
+ if (page >= MAX_PAGES)
118
+ throw new Error(`stopped after ${MAX_PAGES} pages and ${count} rows; carry on with --next ${cursor}`);
119
119
  }
120
120
  watching.done();
121
121
  return count;
@@ -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 });
@@ -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.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Command line interface for Jinshuju Open API v1",
5
5
  "homepage": "https://github.com/jinshuju/cli#readme",
6
6
  "bugs": {