@jinshuju/cli 0.7.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
@@ -269,7 +269,7 @@ rejected when combined with `--mine`.
269
269
 
270
270
  ## Working with files
271
271
 
272
- 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
273
273
  ticket to fetch first.
274
274
 
275
275
  ```bash
@@ -278,6 +278,7 @@ jinshuju entry import --table Vn4xR8 ./rows.csv --map field_1=1 --map field_2=2
278
278
 
279
279
  jinshuju entry create --form Kp7mQ2 --json '{"field_1":"Alice"}' --attach field_5=./id-card.jpg
280
280
  jinshuju form theme set Kp7mQ2 --wallpaper ./bg.png
281
+ jinshuju opensearch edit Qy7nR3 --header ./banner.png
281
282
  ```
282
283
 
283
284
  ```bash
@@ -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.7.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": {