@jinshuju/cli 0.22.0 → 0.24.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/dist/http.js CHANGED
@@ -79,12 +79,28 @@ function describeFailure(response, text, parsed) {
79
79
  const stated = body.message ?? body.error_description;
80
80
  if (typeof stated === 'string' && stated.trim()) {
81
81
  const reasons = everyReason(body, stated);
82
- if (!reasons)
83
- return withCode(stated, body.error);
84
- return [withCode(reasons.head, body.error), ...reasons.rest.map((line) => ` ${line}`)].join('\n');
82
+ const said = reasons
83
+ ? [withCode(reasons.head, body.error), ...reasons.rest.map((line) => ` ${line}`)]
84
+ : [withCode(stated, body.error)];
85
+ return [...said, ...restOf(body)].join('\n');
85
86
  }
86
87
  return rowErrors(body) ?? `${status}: ${snippet(JSON.stringify(parsed.value))}`;
87
88
  }
89
+ /** What a refusal's lines above already say: the reason, the code and every reason in errors[]. */
90
+ const SAID = new Set(['message', 'error_description', 'error', 'errors']);
91
+ /**
92
+ * What a refusal carries besides its reason, a line a field under it: the paid
93
+ * features and the plan a plan refusal names, the balance a short VAS leaves. A
94
+ * caller reading text output, as Jiri does, passes these on as much as it acts
95
+ * on the code; `--output json` has them already, in `body`. Anything but a
96
+ * string is JSON, not a table: a table keeps the scalar columns, and each paid
97
+ * feature's lowest_plan would not reach the caller.
98
+ */
99
+ function restOf(body) {
100
+ return Object.entries(body)
101
+ .filter(([key]) => !SAID.has(key))
102
+ .map(([key, value]) => ` ${key}: ${typeof value === 'string' ? value : JSON.stringify(value)}`);
103
+ }
88
104
  function withCode(reason, code) {
89
105
  if (typeof code !== 'string' || !code.trim() || reason.includes(code))
90
106
  return reason;
@@ -101,43 +117,39 @@ const MAX_REASONS = 20;
101
117
  * three of what may be a hundred and counts the rest. A caller shown the
102
118
  * description alone corrects what it was told, sends again, and learns the
103
119
  * next. So every reason is a line of its own, under whatever the description
104
- * said ahead of the first ("Request body invalid:"). One line per pointer,
105
- * carrying each distinct reason said at it: a value may break two rules at
106
- * once (too short, and not of its pattern), and a field that may be one of
107
- * several shapes is told about the same pointer once per shape. A list too
108
- * long to read is cut, and says how many it kept back.
120
+ * said ahead of the first ("Request body invalid:"). One line per entry of
121
+ * errors[]: API v1 already says a mistake made in several places once, its
122
+ * `pointers` naming them all and its message the first five, so splitting an
123
+ * entry by place would print it again for each, and two reasons at one place
124
+ * (a field that may be one of several shapes) are two things to fix. The
125
+ * places go in front of a line only when its message names none of them; an
126
+ * older server's single `pointer` counts as one place. A list too long to read
127
+ * is cut, and says how many it kept back.
109
128
  */
110
129
  function everyReason(body, stated) {
111
130
  const errors = body.errors;
112
131
  if (!Array.isArray(errors) || errors.length === 0)
113
132
  return undefined;
114
- // Pointer to the reasons said at it, in the order the pointers were first named.
115
- const said = new Map();
133
+ const lines = [];
116
134
  let first;
117
- let count = 0;
118
135
  for (const item of errors) {
119
136
  if (item === null || typeof item !== 'object')
120
137
  continue;
121
- const { pointer, message } = item;
138
+ const { message } = item;
122
139
  if (typeof message !== 'string' || !message.trim())
123
140
  continue;
124
- const at = typeof pointer === 'string' && pointer ? pointer : undefined;
125
- const entry = said.get(at ?? message) ?? { at, messages: [] };
126
- if (entry.messages.includes(message))
141
+ const places = placesOf(item);
142
+ const unplaced = places.length > 0 && !places.some((place) => message.includes(place));
143
+ const line = unplaced ? `${placeList(places)}: ${message}` : message;
144
+ if (lines.includes(line))
127
145
  continue;
128
- entry.messages.push(message);
129
- said.set(at ?? message, entry);
146
+ lines.push(line);
130
147
  first ??= message;
131
- count += 1;
132
148
  }
133
149
  if (first === undefined)
134
150
  return undefined;
135
- if (count === 1 && stated.includes(first))
151
+ if (lines.length === 1 && stated.includes(first))
136
152
  return undefined;
137
- const lines = [...said.values()].map(({ at, messages }) => {
138
- const text = messages.join('; ');
139
- return at && !messages.some((message) => message.includes(at)) ? `${at}: ${text}` : text;
140
- });
141
153
  const kept = lines.slice(0, MAX_REASONS);
142
154
  if (lines.length > kept.length)
143
155
  kept.push(`… and ${lines.length - kept.length} more; --output json carries them all`);
@@ -149,6 +161,18 @@ function everyReason(body, stated) {
149
161
  return { head: heading, rest: kept };
150
162
  return { head: kept[0], rest: kept.slice(1) };
151
163
  }
164
+ /** The JSON Pointers an entry of errors[] names: API v1's `pointers`, or an older server's `pointer`. */
165
+ function placesOf(item) {
166
+ const { pointers, pointer } = item;
167
+ const named = Array.isArray(pointers) ? pointers : [pointer];
168
+ return named.filter((place) => typeof place === 'string' && place !== '');
169
+ }
170
+ /** The most places put in front of one line, as many as API v1 names in a message. */
171
+ const MAX_PLACES = 5;
172
+ function placeList(places) {
173
+ const named = places.slice(0, MAX_PLACES).join(', ');
174
+ return places.length > MAX_PLACES ? `${named} and ${places.length - MAX_PLACES} more` : named;
175
+ }
152
176
  /**
153
177
  * A batch write answers per row: `{errors: [{index, reason}]}`. The index is the
154
178
  * caller's own array position, and is left as the server gave it so it lines up
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jinshuju/cli",
3
- "version": "0.22.0",
3
+ "version": "0.24.0",
4
4
  "description": "Command line interface for Jinshuju Open API v1",
5
5
  "homepage": "https://github.com/jinshuju/cli#readme",
6
6
  "bugs": {