@jinshuju/cli 0.24.0 → 0.25.1

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
@@ -246,6 +246,12 @@ jinshuju field update-choices --form Kp7mQ2 field_7 --json '{"add":[{"label":"Th
246
246
  jinshuju field remove --form Kp7mQ2 field_9 --yes
247
247
  ```
248
248
 
249
+ `add`, `update` and `update-choices` answer with the fields they touched rather
250
+ than the whole container the PATCH returns. A form whose fields need a plan its
251
+ account lacks also answers with `unusable_features`, and these commands and
252
+ `field list` keep it beside the fields: the form is saved, but respondents
253
+ cannot submit it until the account upgrades.
254
+
249
255
  A payload is looked at once before it is sent, against the openapi.yaml the
250
256
  CLI shipped with, and refused only for what a newer API could not have made
251
257
  right: a required key missing, a value of the wrong type, `null` for a key
@@ -588,7 +594,10 @@ webhook notification pushes it. A webhook can also push JSON of your own
588
594
  instead: `--body` is the whole request body, the text of a JSON object or
589
595
  array, posted as it is. A sent message cannot be recalled, so it takes
590
596
  `--yes`. After a timeout, send again with the same `--idempotency-key`: the
591
- API answers the first records instead of sending twice.
597
+ API answers the first records instead of sending twice. `--estimate` in place
598
+ of `--yes` checks the message as the send would and sends nothing: it answers
599
+ how many records it would create and how many SMS or emails it would bill, and
600
+ is refused as the send would be, a balance that does not cover it included.
592
601
 
593
602
  `message channel list` gives the ids `--channel` takes, `--type` narrowing
594
603
  them, and `message channel get <id>` shows one. Email needs no channel: without
@@ -599,6 +608,7 @@ SMTP account.
599
608
  jinshuju message channel list --type wxwork
600
609
  jinshuju message send --type wxwork --channel 6729f1c2a1b2c3d4e5f60718 --content '今天是星期三,该吃午饭啦!' --mention 13800000000 --yes
601
610
  jinshuju message send --type email --to someone@example.com --title 上课提醒 --content '明天上午 9 点开课' --yes
611
+ jinshuju message send --type sms --channel 6729f1c2a1b2c3d4e5f60719 --to 13800000000 --variable var_1=张三 --variable var_2=9:00 --estimate
602
612
  jinshuju message send --type sms --channel 6729f1c2a1b2c3d4e5f60719 --to 13800000000 --variable var_1=张三 --variable var_2=9:00 --yes
603
613
  jinshuju message send --type webhook --channel 6729f1c2a1b2c3d4e5f6071a --body '{"report":"orders","order_count":1}' --yes
604
614
  ```
@@ -65,6 +65,18 @@ function fieldTypesForReading(body) {
65
65
  }
66
66
  return rows[0];
67
67
  }
68
+ /**
69
+ * Fields as a listing, cut out of the form that answered, with the one thing
70
+ * beside them that the cut must not lose: `unusable_features`. A form answers
71
+ * with it when its fields need a plan its account lacks. The form is saved, but
72
+ * a respondent's submission is refused until the account upgrades, and the
73
+ * fields are where that news arrives: dropping it left the caller thinking all
74
+ * was well. A table never has it.
75
+ */
76
+ function fieldListing(body, rows) {
77
+ const unusable = body?.unusable_features;
78
+ return { data: rows, ...(unusable === undefined ? {} : { unusable_features: unusable }) };
79
+ }
68
80
  /**
69
81
  * What a field write touched, out of the whole form it answers with.
70
82
  *
@@ -77,7 +89,7 @@ function fieldTypesForReading(body) {
77
89
  function touchedFields(body, wanted) {
78
90
  const fields = selectFields(body);
79
91
  const touched = fields.filter(wanted);
80
- return { data: touched.length > 0 ? touched : fields };
92
+ return fieldListing(body, touched.length > 0 ? touched : fields);
81
93
  }
82
94
  /** The labels a field payload carries, one object or a list of them. */
83
95
  function labelsOf(payload) {
@@ -119,6 +131,10 @@ function conditionFieldsForReading(body) {
119
131
  return [heading, { ...rest, operators }];
120
132
  }));
121
133
  }
134
+ /** What `unusable_features` beside the fields means, which help says for each command that keeps it. */
135
+ const UNUSABLE_FEATURES = "unusable_features beside the fields means the form holds fields its account's plan cannot use: " +
136
+ 'it is saved, but respondents cannot submit it until the account upgrades.';
137
+ const WRITE_ANSWER = `Answers with the fields it touched. ${UNUSABLE_FEATURES}`;
122
138
  const KIND_OPTION = {
123
139
  name: '--kind',
124
140
  type: 'string',
@@ -173,10 +189,10 @@ export const FIELD = [
173
189
  {
174
190
  path: ['field', 'list'],
175
191
  summary: 'List the fields of a form or table',
176
- description: 'Read out of the object structure, the same fields `form get` and `table get` return.',
192
+ description: `Read out of the object structure, the same fields \`form get\` and \`table get\` return. ${UNUSABLE_FEATURES}`,
177
193
  options: [...CONTAINER_OPTIONS],
178
194
  request: (input) => onContainer(input, 'getForm', 'getTable'),
179
- select: (body) => ({ data: selectFields(body) })
195
+ select: (body) => fieldListing(body, selectFields(body))
180
196
  },
181
197
  {
182
198
  path: ['field', 'filterable'],
@@ -224,7 +240,7 @@ export const FIELD = [
224
240
  {
225
241
  path: ['field', 'add'],
226
242
  summary: 'Add fields to a form or table',
227
- description: 'One field object, or a list of them. Do not pass api_code: the backend generates it.',
243
+ description: `One field object, or a list of them. Do not pass api_code: the backend generates it. ${WRITE_ANSWER}`,
228
244
  options: [...CONTAINER_OPTIONS, JSON_OPTION],
229
245
  payload: FIELD_PAYLOAD,
230
246
  request: (input) => onContainer(input, 'updateForm', 'updateTable', { body: { fields: { add: one(payload(input)) } } }),
@@ -238,7 +254,8 @@ export const FIELD = [
238
254
  path: ['field', 'update'],
239
255
  summary: 'Update one field',
240
256
  description: 'The patch is merged onto the field; the api_code comes from the argument, not the payload. ' +
241
- 'Write only the keys to change, flat on the field, as the payload below says.',
257
+ 'Write only the keys to change, flat on the field, as the payload below says. ' +
258
+ WRITE_ANSWER,
242
259
  args: [{ name: 'api-code', required: true, description: 'Field api_code, e.g. field_3' }],
243
260
  options: [...CONTAINER_OPTIONS, JSON_OPTION],
244
261
  payload: FIELD_PAYLOAD,
@@ -253,7 +270,7 @@ export const FIELD = [
253
270
  {
254
271
  path: ['field', 'update-choices'],
255
272
  summary: "Change a field's choices",
256
- description: 'The payload is the choice operations the field takes, e.g. {add, update, remove}.',
273
+ description: `The payload is the choice operations the field takes, e.g. {add, update, remove}. ${WRITE_ANSWER}`,
257
274
  args: [{ name: 'api-code', required: true, description: 'Field api_code' }],
258
275
  options: [...CONTAINER_OPTIONS, JSON_OPTION],
259
276
  request: (input) => onContainer(input, 'updateForm', 'updateTable', {
@@ -1,7 +1,7 @@
1
1
  import { UsageError } from '../errors.js';
2
2
  import { PAGINATION_OPTIONS } from '../options.js';
3
3
  import { call, choices } from '../spec.js';
4
- import { given, LISTING, paging, requiredOption, SEND_OPTION } from './shared.js';
4
+ import { ESTIMATE_OPTION, given, LISTING, paging, requiredOption, SEND_OPTION } from './shared.js';
5
5
  /** `var_1=张三`: one variable of an SMS channel's template. */
6
6
  function parseVariable(input) {
7
7
  const at = input.indexOf('=');
@@ -21,7 +21,9 @@ export const MESSAGE = [
21
21
  'request of its own, shaped as a form webhook notification pushes it. A webhook can also push JSON of your own ' +
22
22
  'instead: --body is the whole request body, posted as it is. Sent messages cannot be recalled: after a timeout ' +
23
23
  'or a dropped connection, send again with the same --idempotency-key, which answers the first records instead ' +
24
- 'of sending twice. --yes sends.',
24
+ 'of sending twice. --estimate checks the message as the send would and sends nothing: it answers how many ' +
25
+ 'records it would create and how many SMS or emails it would bill, and is refused as the send would be, a ' +
26
+ 'balance that does not cover it included. --yes sends.',
25
27
  options: [
26
28
  {
27
29
  name: '--type',
@@ -99,6 +101,7 @@ export const MESSAGE = [
99
101
  placeholder: '<key>',
100
102
  description: 'Sends once for the same key within 24 hours; retry with the same key'
101
103
  },
104
+ ESTIMATE_OPTION,
102
105
  SEND_OPTION
103
106
  ],
104
107
  request: (input) => {
@@ -114,10 +117,14 @@ export const MESSAGE = [
114
117
  }
115
118
  }
116
119
  }
117
- if (!input.options.yes)
118
- throw new UsageError('A sent message cannot be recalled; pass --yes to send');
120
+ const estimate = input.options.estimate === true;
121
+ if (estimate && input.options.yes)
122
+ throw new UsageError('--estimate and --yes are alternatives, not both');
123
+ if (!estimate && !input.options.yes) {
124
+ throw new UsageError('A sent message cannot be recalled; pass --estimate to see what it takes, or --yes to send');
125
+ }
119
126
  const variables = input.options.variable;
120
- return call('sendMessage', {
127
+ return call(estimate ? 'estimateMessage' : 'sendMessage', {
121
128
  body: given({
122
129
  type,
123
130
  channel_id: input.options.channel,
@@ -139,6 +146,8 @@ export const MESSAGE = [
139
146
  "jinshuju message send --type email --to someone@example.com --title 上课提醒 --content '明天上午 9 点开课' --yes",
140
147
  'jinshuju message send --type sms --channel 6729f1c2a1b2c3d4e5f60719 --to 13800000000 --variable var_1=张三 ' +
141
148
  '--variable var_2=9:00 --yes',
149
+ 'jinshuju message send --type sms --channel 6729f1c2a1b2c3d4e5f60719 --to 13800000000 --variable var_1=张三 ' +
150
+ '--variable var_2=9:00 --estimate',
142
151
  'jinshuju message send --type webhook --channel 6729f1c2a1b2c3d4e5f6071a --form Kp7mQ2 --entry 1282 --entry 1283 --yes',
143
152
  'jinshuju message send --type webhook --channel 6729f1c2a1b2c3d4e5f6071a ' +
144
153
  '--body \'{"report":"orders","order_count":1}\' --yes'
@@ -39,6 +39,7 @@ const ROUTINE_PAYLOAD = [
39
39
  ' weekly "weekdays": [1-7], "time": "HH:MM"; 1 is Monday, 7 is Sunday',
40
40
  ' monthly "month_days": [1-31, or -1 for the last day], "time": "HH:MM";',
41
41
  ' a month without the day is skipped, not moved',
42
+ 'It may also be a string as --schedule takes it: "daily 09:00".',
42
43
  '',
43
44
  'event, with "trigger_kind": "event" in place of a schedule:',
44
45
  ' { "source_kind": "form" | "table", "source_token": "Kp7mQ2",',
@@ -133,6 +134,20 @@ export function parseSchedule(input) {
133
134
  return { freq, time: first };
134
135
  }
135
136
  }
137
+ /**
138
+ * A schedule written in --json as --schedule takes it, `"daily 09:00"`, read
139
+ * the same way: the words are what an agent writes in a payload as often as
140
+ * on the command line, and sent as they are the API refuses them.
141
+ */
142
+ function payloadSchedule(raw) {
143
+ try {
144
+ return parseSchedule(raw);
145
+ }
146
+ catch {
147
+ throw new UsageError('schedule in --json must be an object such as {"freq":"daily","time":"09:00"} (see --help), ' +
148
+ `or a string as --schedule reads it: ${SCHEDULE_GRAMMAR}; got ${JSON.stringify(raw)}`);
149
+ }
150
+ }
136
151
  /** `form:Kp7mQ2` into `{ kind, token }`. The kinds are the server's to check. */
137
152
  export function parseObject(input) {
138
153
  const separator = input.indexOf(':');
@@ -192,10 +207,11 @@ function routineBody(input) {
192
207
  throw new UsageError('delete_authorized cannot be sent: letting a routine delete data is turned on on the 自动任务 page only. ' +
193
208
  'Take it out of --json');
194
209
  }
210
+ const payload = typeof raw?.schedule === 'string' ? { ...raw, schedule: payloadSchedule(raw.schedule) } : raw;
195
211
  const objects = input.options.object?.map(parseObject);
196
212
  const spec = input.options.schedule;
197
213
  const schedule = spec === undefined ? undefined : parseSchedule(spec);
198
- return overriding(raw ?? {}, {
214
+ return overriding(payload ?? {}, {
199
215
  name: input.options.name,
200
216
  description: input.options.description,
201
217
  instruction: input.options.instruction,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jinshuju/cli",
3
- "version": "0.24.0",
3
+ "version": "0.25.1",
4
4
  "description": "Command line interface for Jinshuju Open API v1",
5
5
  "homepage": "https://github.com/jinshuju/cli#readme",
6
6
  "bugs": {