emailengine-app 2.78.0 → 2.79.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/.ncurc.js +23 -13
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +68 -0
- package/config/e2e.toml +2 -1
- package/config/test.toml +2 -1
- package/data/google-crawlers.json +1 -1
- package/lib/account.js +112 -36
- package/lib/api-reference/format.js +91 -4
- package/lib/api-reference/model.js +144 -19
- package/lib/api-reference/schema-tree.js +282 -28
- package/lib/api-routes/account-routes.js +80 -34
- package/lib/api-routes/blocklist-routes.js +21 -47
- package/lib/api-routes/chat-routes.js +10 -12
- package/lib/api-routes/delivery-test-routes.js +6 -26
- package/lib/api-routes/export-routes.js +2 -0
- package/lib/api-routes/gateway-routes.js +52 -11
- package/lib/api-routes/license-routes.js +2 -0
- package/lib/api-routes/mailbox-routes.js +29 -26
- package/lib/api-routes/message-routes.js +27 -21
- package/lib/api-routes/oauth2-app-routes.js +11 -29
- package/lib/api-routes/operation-impact.js +80 -0
- package/lib/api-routes/outbox-routes.js +15 -5
- package/lib/api-routes/permission-map.js +401 -0
- package/lib/api-routes/route-helpers.js +294 -9
- package/lib/api-routes/route-metadata.js +28 -0
- package/lib/api-routes/settings-routes.js +66 -38
- package/lib/api-routes/stats-routes.js +2 -2
- package/lib/api-routes/submit-routes.js +53 -31
- package/lib/api-routes/template-routes.js +3 -0
- package/lib/api-routes/token-routes.js +526 -72
- package/lib/api-routes/webhook-route-routes.js +14 -4
- package/lib/arf-detect.js +17 -4
- package/lib/auth-token.js +80 -8
- package/lib/bounce-detect.js +28 -17
- package/lib/consts.js +16 -0
- package/lib/db.js +26 -2
- package/lib/delivery-error.js +30 -1
- package/lib/email-client/base-client.js +27 -41
- package/lib/email-client/gmail-client.js +23 -17
- package/lib/email-client/imap/mailbox.js +58 -59
- package/lib/email-client/imap/subconnection.js +10 -7
- package/lib/email-client/imap/sync-operations.js +46 -46
- package/lib/email-client/imap-client.js +85 -28
- package/lib/email-client/message-builder.js +3 -3
- package/lib/email-client/notification-handler.js +1 -1
- package/lib/email-client/outlook/graph-api.js +10 -4
- package/lib/email-client/outlook-client.js +56 -64
- package/lib/email-client/smtp-pool-manager.js +1 -1
- package/lib/enum-descriptions.js +92 -2
- package/lib/export.js +1 -1
- package/lib/field-usage.js +133 -0
- package/lib/gateway.js +1 -1
- package/lib/imapproxy/imap-core/lib/commands/authenticate-plain.js +3 -3
- package/lib/imapproxy/imap-core/lib/commands/id.js +2 -13
- package/lib/imapproxy/imap-core/lib/commands/starttls.js +1 -1
- package/lib/imapproxy/imap-core/lib/imap-command.js +21 -5
- package/lib/imapproxy/imap-core/lib/imap-connection.js +15 -5
- package/lib/imapproxy/imap-server.js +27 -7
- package/lib/license-beacon.js +4 -9
- package/lib/lists.js +49 -1
- package/lib/llm-pre-process.js +5 -5
- package/lib/logger.js +5 -1
- package/lib/lua/h-set-new-mark.lua +36 -0
- package/lib/message-port-stream.js +84 -1
- package/lib/metrics-collector.js +2 -2
- package/lib/oauth/gmail.js +9 -12
- package/lib/oauth/mail-ru.js +5 -7
- package/lib/oauth/outlook.js +9 -9
- package/lib/oauth/pubsub/google.js +25 -10
- package/lib/oauth/scope-checker.js +1 -1
- package/lib/oauth2-apps.js +290 -292
- package/lib/openapi/build-document.js +20 -8
- package/lib/openapi/joi-schema.js +20 -0
- package/lib/outbox.js +85 -17
- package/lib/pre-process.js +8 -8
- package/lib/queue-stats.js +49 -0
- package/lib/read-env-value.js +1 -1
- package/lib/reconnection-manager.js +2 -2
- package/lib/rewrite-text-nodes.js +4 -3
- package/lib/routes-ui.js +4 -0
- package/lib/schemas.js +218 -162
- package/lib/settings.js +98 -5
- package/lib/sub-script.js +2 -1
- package/lib/swagger-options.js +2 -1
- package/lib/templates.js +4 -4
- package/lib/threads.js +2 -2
- package/lib/token-audit-log.js +231 -0
- package/lib/token-permission-view.js +219 -0
- package/lib/token-permissions.js +172 -0
- package/lib/tokens.js +247 -66
- package/lib/tools.js +142 -37
- package/lib/ui-routes/account-routes.js +42 -15
- package/lib/ui-routes/admin-config-routes.js +18 -10
- package/lib/ui-routes/admin-entities-routes.js +117 -38
- package/lib/ui-routes/auth-routes.js +30 -24
- package/lib/ui-routes/export-routes.js +10 -4
- package/lib/ui-routes/oauth-config-routes.js +12 -12
- package/lib/ui-routes/reference-routes.js +3 -3
- package/lib/ui-routes/route-helpers.js +78 -2
- package/lib/ui-routes/settings-page.js +1 -1
- package/lib/ui-routes/smtp-test-routes.js +1 -1
- package/lib/ui-routes/suppression-list-routes.js +299 -0
- package/lib/ui-routes/unsubscribe-routes.js +19 -29
- package/lib/utils/decode-headers.js +42 -0
- package/lib/utils/header-map.js +48 -0
- package/lib/utils/network.js +2 -2
- package/lib/utils/unsafe-key.js +59 -0
- package/lib/web-safe-html.js +1 -1
- package/lib/webhook-routing.js +84 -0
- package/lib/webhooks.js +48 -14
- package/package.json +11 -10
- package/sbom.json +1 -1
- package/server.js +116 -55
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +136 -9
- package/static/js/reference.js +456 -10
- package/static/js/ui.js +43 -0
- package/static/licenses.html +51 -41
- package/translations/messages.pot +59 -59
- package/views/accounts/account.hbs +75 -13
- package/views/accounts/browse.hbs +19 -36
- package/views/accounts/edit.hbs +2 -2
- package/views/config/logging.hbs +6 -0
- package/views/config/oauth/app.hbs +0 -2
- package/views/config/security.hbs +13 -0
- package/views/gateways/edit.hbs +1 -1
- package/views/gateways/gateway.hbs +2 -2
- package/views/gateways/index.hbs +7 -7
- package/views/partials/accounts_table.hbs +7 -7
- package/views/partials/oauth_form.hbs +0 -15
- package/views/partials/reference/disclosure.hbs +8 -2
- package/views/partials/reference/operation.hbs +47 -13
- package/views/partials/reference/params.hbs +17 -1
- package/views/partials/reference/schema-node.hbs +110 -52
- package/views/partials/reference/schema-tools.hbs +25 -0
- package/views/partials/reference/try-it.hbs +84 -11
- package/views/partials/side_menu.hbs +1 -0
- package/views/partials/ui/checkbox.hbs +2 -2
- package/views/partials/ui/tabs.hbs +7 -1
- package/views/reference/token.hbs +4 -0
- package/views/suppression-lists/index.hbs +85 -0
- package/views/suppression-lists/list.hbs +87 -0
- package/views/templates/index.hbs +10 -4
- package/views/templates/template.hbs +1 -1
- package/views/tokens/index.hbs +53 -6
- package/views/tokens/new.hbs +334 -5
- package/workers/api.js +185 -102
- package/workers/documents.js +46 -26
- package/workers/export.js +20 -8
- package/workers/imap-proxy.js +2 -2
- package/workers/imap.js +77 -78
- package/workers/smtp.js +13 -7
- package/workers/submit.js +40 -30
- package/workers/webhooks.js +80 -26
- package/lib/feature-flags.js +0 -48
|
@@ -9,17 +9,31 @@
|
|
|
9
9
|
// that breaks those assumptions degrades into a visible marker instead of hanging the
|
|
10
10
|
// worker.
|
|
11
11
|
|
|
12
|
-
const { formatDescription, constraintList } = require('./format');
|
|
12
|
+
const { formatDescription, constraintList, formatExample } = require('./format');
|
|
13
13
|
const { SCHEMA_REF_PREFIX } = require('../openapi');
|
|
14
14
|
|
|
15
15
|
// Depth cap for both the property tree and the example. The deepest schema in the
|
|
16
16
|
// current document nests 9 levels, so this only trips on something new.
|
|
17
17
|
const MAX_DEPTH = 14;
|
|
18
18
|
|
|
19
|
-
// Nested property groups render inside a <details
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
|
|
19
|
+
// Nested property groups render inside a <details>, and which of them start open is
|
|
20
|
+
// decided from the size of the tree rather than from a fixed depth - see
|
|
21
|
+
// assignDisclosure(). A depth rule cannot tell a six-property object from
|
|
22
|
+
// AccountResponse's 128, and applied the same two open levels to both: right for the
|
|
23
|
+
// first, a 6,700 pixel operation for the second.
|
|
24
|
+
//
|
|
25
|
+
// The budget is how many rows may be on screen before a level is left collapsed. 87 of the
|
|
26
|
+
// document's 111 trees fit under it and so arrive fully open; the rest arrive as a
|
|
27
|
+
// scannable top level. It can be this tight because reference/operation now renders the
|
|
28
|
+
// example payload above the tree and open, so the shape of a wrapper response is already
|
|
29
|
+
// on screen and its four top-level keys do not have to carry that job alone.
|
|
30
|
+
const OPEN_ROW_BUDGET = 25;
|
|
31
|
+
|
|
32
|
+
// How many child names a collapsed group lists in its summary before it says "+N more".
|
|
33
|
+
const CHILD_PREVIEW_COUNT = 4;
|
|
34
|
+
|
|
35
|
+
// Rows a schema has to render before it is worth a filter box and expand/collapse controls.
|
|
36
|
+
const SCHEMA_TOOLS_MIN_NODES = 12;
|
|
23
37
|
|
|
24
38
|
// Placeholder values for string formats that have no example of their own, so a
|
|
25
39
|
// synthesized example still shows the expected shape rather than a wall of "string".
|
|
@@ -107,10 +121,38 @@ function typeLabel(spec, schema, refStack = []) {
|
|
|
107
121
|
return target.type || 'any';
|
|
108
122
|
}
|
|
109
123
|
|
|
124
|
+
// Whether a value could be an instance of the schema's own declared type. A schema with no
|
|
125
|
+
// declared type is all enum, so everything in it belongs.
|
|
126
|
+
function matchesType(type, value) {
|
|
127
|
+
switch (type) {
|
|
128
|
+
case 'string':
|
|
129
|
+
return typeof value === 'string';
|
|
130
|
+
case 'number':
|
|
131
|
+
case 'integer':
|
|
132
|
+
return typeof value === 'number';
|
|
133
|
+
case 'boolean':
|
|
134
|
+
return typeof value === 'boolean';
|
|
135
|
+
case 'array':
|
|
136
|
+
return Array.isArray(value);
|
|
137
|
+
case 'object':
|
|
138
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
139
|
+
default:
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
110
144
|
// Enum values with their per-value documentation, when the joi schema carries any.
|
|
111
145
|
// `.meta({ enumDescriptions })` in lib/schemas.js surfaces as `x-meta.enumDescriptions`
|
|
112
146
|
// beside the `enum` array - OpenAPI itself has no field for documenting individual values.
|
|
113
147
|
// Returns null when the schema is not an enum, so callers can assign both fields in one go.
|
|
148
|
+
//
|
|
149
|
+
// The enum is split by whether a value can be an instance of the schema's own type.
|
|
150
|
+
// lib/openapi/joi-schema.js emits `.valid()` and `.allow()` into one `enum`, so a joi
|
|
151
|
+
// `.allow(false)` lands there too - 35 schemas in the document carry `enum: [false]` beside
|
|
152
|
+
// a `string`, `integer` or `object` type. Those are sentinels standing next to the type, not
|
|
153
|
+
// its value domain: `false` clears the block on an update payload and means "not available"
|
|
154
|
+
// on a response. Rendered through the normal "Allowed" row they claimed the only value an
|
|
155
|
+
// IMAP configuration object accepts is `false`. `alsoAccepts` gets its own line instead.
|
|
114
156
|
function enumInfo(schema) {
|
|
115
157
|
if (!Array.isArray(schema.enum) || !schema.enum.length) {
|
|
116
158
|
return null;
|
|
@@ -120,12 +162,135 @@ function enumInfo(schema) {
|
|
|
120
162
|
|
|
121
163
|
// Object.hasOwn, not a bare lookup: an enum value named `constructor` or `toString`
|
|
122
164
|
// would otherwise pick up an inherited function and render as garbage.
|
|
123
|
-
const values = schema.enum
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
165
|
+
const values = schema.enum
|
|
166
|
+
.filter(value => matchesType(schema.type, value))
|
|
167
|
+
.map(value => {
|
|
168
|
+
const key = typeof value === 'string' ? value : JSON.stringify(value);
|
|
169
|
+
return { value: key, description: Object.hasOwn(documented, key) ? documented[key] : null };
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const alsoAccepts = schema.enum.filter(value => !matchesType(schema.type, value)).map(value => JSON.stringify(value));
|
|
127
173
|
|
|
128
|
-
return { values, documented: values.some(entry => entry.description) };
|
|
174
|
+
return { values, documented: values.some(entry => entry.description), alsoAccepts };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// The example a schema uses: its own if it has one, otherwise the slice it inherited from the
|
|
178
|
+
// nearest ancestor that did. Three places need exactly this rule - the property row, the
|
|
179
|
+
// tree's descent into children, and the payload builder - and they have to agree, or the row
|
|
180
|
+
// and the payload above it end up stating different values for the same key.
|
|
181
|
+
function ownExample(schema, inherited) {
|
|
182
|
+
return typeof schema.example !== 'undefined' ? schema.example : inherited;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// The example value shown on a property row, or null when the row is better off without one.
|
|
186
|
+
//
|
|
187
|
+
// The document carries an `example` on 1,315 of its 1,644 leaf properties and the page used
|
|
188
|
+
// to render none of them: the only place a value appeared was the curated payload above the
|
|
189
|
+
// tree, which - because an object-level example stops the descent in exampleFor() - shows
|
|
190
|
+
// only the keys its author chose. On POST /v1/account/{account}/submit that was 10 of 71
|
|
191
|
+
// fields, so 61 documented properties had no value anywhere on the page.
|
|
192
|
+
//
|
|
193
|
+
// `inherited` is the matching slice of the nearest ancestor's example, threaded down by
|
|
194
|
+
// buildNode(). It fills in a property whose own schema carries nothing but whose parent
|
|
195
|
+
// object was given a curated payload, which is most of the search query, and it also
|
|
196
|
+
// guarantees the row and the payload above it can never disagree.
|
|
197
|
+
//
|
|
198
|
+
// What is deliberately left off:
|
|
199
|
+
// enum the Allowed row already lists the whole value domain, an example only picks
|
|
200
|
+
// a favourite out of it
|
|
201
|
+
// boolean `true` and `false` add nothing to a row that already says `boolean`, and the
|
|
202
|
+
// Default chip already says which one you get
|
|
203
|
+
// object the value lives on the child rows, and an object printed onto one line is
|
|
204
|
+
// the payload block's job
|
|
205
|
+
// default the Default chip is on the row already
|
|
206
|
+
//
|
|
207
|
+
// `enums` is enumInfo(schema), which every caller has already computed for the row's own
|
|
208
|
+
// Allowed list. Only a real enum suppresses the example - 35 schemas in the document carry
|
|
209
|
+
// `enum: [false]` from a joi `.allow(false)` beside a string or integer type, a sentinel
|
|
210
|
+
// standing next to the type rather than its value domain. Those render no Allowed row, so
|
|
211
|
+
// suppressing their example too would leave the row with no value on it at all, which is
|
|
212
|
+
// exactly what happened to every `accessToken` until enumInfo() got the final say here.
|
|
213
|
+
function rowExample(schema, options = {}) {
|
|
214
|
+
const { inherited, itemExample, enums } = options;
|
|
215
|
+
|
|
216
|
+
if (!schema || typeof schema !== 'object') {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (schema.type === 'boolean') {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (enums && enums.values.length) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let value = ownExample(schema, inherited);
|
|
229
|
+
|
|
230
|
+
// An array of scalars usually documents itself one level down: `folders` carries no
|
|
231
|
+
// example of its own while every entry in it is an `INBOX`. The list has no child rows to
|
|
232
|
+
// put that on - the tree only descends into arrays of objects - so it belongs here, as
|
|
233
|
+
// the one-entry list the item describes.
|
|
234
|
+
if (typeof value === 'undefined' && typeof itemExample !== 'undefined' && itemExample !== null && typeof itemExample !== 'object') {
|
|
235
|
+
value = [itemExample];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (typeof value === 'undefined' || value === null) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (Array.isArray(value)) {
|
|
243
|
+
// An empty array says only what `array of x` said, and an array of objects is a
|
|
244
|
+
// group of child rows rather than a value
|
|
245
|
+
if (!value.length || value.some(entry => entry && typeof entry === 'object')) {
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
} else if (typeof value === 'object') {
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (typeof schema.default !== 'undefined' && JSON.stringify(value) === JSON.stringify(schema.default)) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return formatExample(value);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// The example an array's items carry, or undefined for anything that is not an array of
|
|
260
|
+
// scalars. Needs the spec because 46 array properties in the document reach their items
|
|
261
|
+
// through a $ref.
|
|
262
|
+
function itemExampleFor(spec, schema, refStack) {
|
|
263
|
+
if (!schema || schema.type !== 'array' || !schema.items) {
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const resolved = resolve(spec, schema.items, refStack);
|
|
268
|
+
|
|
269
|
+
return resolved.schema ? resolved.schema.example : undefined;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// The slice of an example that belongs to a named property of it, or undefined. Arrays are
|
|
273
|
+
// entered through their first entry, matching how the tree renders an array of objects as
|
|
274
|
+
// the item's properties.
|
|
275
|
+
function inheritedFor(example, propertyName) {
|
|
276
|
+
const source = Array.isArray(example) ? example[0] : example;
|
|
277
|
+
|
|
278
|
+
if (!source || typeof source !== 'object' || Array.isArray(source)) {
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return source[propertyName];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Names listed in a collapsed group's summary. "6 properties" is the only evidence a
|
|
286
|
+
// reader has for whether to open a group, and it says nothing about what is inside; the
|
|
287
|
+
// Account page alone renders 135 closed groups. Required names come first because
|
|
288
|
+
// requiredFirst() has already sorted the children.
|
|
289
|
+
function previewNames(children) {
|
|
290
|
+
const shown = children.slice(0, CHILD_PREVIEW_COUNT).map(child => child.name);
|
|
291
|
+
const rest = children.length - shown.length;
|
|
292
|
+
|
|
293
|
+
return rest ? `${shown.join(', ')}, +${rest} more` : shown.join(', ');
|
|
129
294
|
}
|
|
130
295
|
|
|
131
296
|
// Required entries first, declared order preserved within each group (Array#sort is
|
|
@@ -164,7 +329,7 @@ function childSource(spec, schema, refStack) {
|
|
|
164
329
|
}
|
|
165
330
|
|
|
166
331
|
function buildNode(spec, rawSchema, options) {
|
|
167
|
-
const { name, required, depth, refStack, path } = options;
|
|
332
|
+
const { name, required, depth, refStack, path, inherited } = options;
|
|
168
333
|
|
|
169
334
|
const resolved = resolve(spec, rawSchema, refStack);
|
|
170
335
|
|
|
@@ -173,7 +338,9 @@ function buildNode(spec, rawSchema, options) {
|
|
|
173
338
|
// Anchor id for this property, dotted from the tree root - see buildSchemaTree()
|
|
174
339
|
anchor: path || null,
|
|
175
340
|
required: !!required,
|
|
176
|
-
|
|
341
|
+
// Set by assignDisclosure() once the whole tree is built, since the decision needs
|
|
342
|
+
// sibling and subtree sizes that are not known while descending
|
|
343
|
+
openByDefault: false,
|
|
177
344
|
circular: !!resolved.circular,
|
|
178
345
|
unresolved: resolved.unresolved || null,
|
|
179
346
|
truncated: false,
|
|
@@ -192,15 +359,26 @@ function buildNode(spec, rawSchema, options) {
|
|
|
192
359
|
node.typeLabel = typeLabel(spec, schema, resolved.refStack);
|
|
193
360
|
|
|
194
361
|
node.descriptionHtml = formatDescription(schema.description);
|
|
362
|
+
// Long-form guidance the one-line description cannot carry: when you would reach for
|
|
363
|
+
// this field and what it does to the others. Authored in lib/field-usage.js, attached
|
|
364
|
+
// with `.meta({ usage })` and published by the generator as `x-meta.usage`, exactly like
|
|
365
|
+
// `.meta({ enumDescriptions })`. Rendered as a closed disclosure, so a field without one
|
|
366
|
+
// is unchanged and a field with one costs a single line until it is opened.
|
|
367
|
+
node.usageHtml = formatDescription(schema['x-meta'] && schema['x-meta'].usage);
|
|
195
368
|
node.constraints = constraintList(schema);
|
|
196
369
|
node.deprecated = !!schema.deprecated;
|
|
197
370
|
node.nullable = !!schema.nullable;
|
|
198
371
|
|
|
199
372
|
const enums = enumInfo(schema);
|
|
200
|
-
|
|
373
|
+
node.example = rowExample(schema, { inherited, itemExample: itemExampleFor(spec, schema, resolved.refStack), enums });
|
|
374
|
+
|
|
375
|
+
if (enums && enums.values.length) {
|
|
201
376
|
node.enumValues = enums.values;
|
|
202
377
|
node.enumDocumented = enums.documented;
|
|
203
378
|
}
|
|
379
|
+
if (enums && enums.alsoAccepts.length) {
|
|
380
|
+
node.alsoAccepts = enums.alsoAccepts;
|
|
381
|
+
}
|
|
204
382
|
|
|
205
383
|
if (typeof schema.default !== 'undefined') {
|
|
206
384
|
node.defaultValue = typeof schema.default === 'string' ? schema.default : JSON.stringify(schema.default);
|
|
@@ -211,6 +389,11 @@ function buildNode(spec, rawSchema, options) {
|
|
|
211
389
|
return node;
|
|
212
390
|
}
|
|
213
391
|
|
|
392
|
+
// What the rows below this one inherit from: this node's own example if it has one,
|
|
393
|
+
// otherwise whatever it inherited itself, so a curated payload keeps reaching down
|
|
394
|
+
// through levels that carry nothing of their own
|
|
395
|
+
const descend = ownExample(schema, inherited);
|
|
396
|
+
|
|
214
397
|
if (Array.isArray(schema.anyOf) && schema.anyOf.length) {
|
|
215
398
|
node.variants = schema.anyOf.map((variant, index) =>
|
|
216
399
|
buildNode(spec, variant, {
|
|
@@ -220,7 +403,8 @@ function buildNode(spec, rawSchema, options) {
|
|
|
220
403
|
refStack: resolved.refStack,
|
|
221
404
|
// The index, not the display name: an anchor cannot carry the space in
|
|
222
405
|
// "option 1", and two variants would otherwise share the parent's path
|
|
223
|
-
path: path ? `${path}.option${index + 1}` : null
|
|
406
|
+
path: path ? `${path}.option${index + 1}` : null,
|
|
407
|
+
inherited: descend
|
|
224
408
|
})
|
|
225
409
|
);
|
|
226
410
|
return node;
|
|
@@ -236,7 +420,8 @@ function buildNode(spec, rawSchema, options) {
|
|
|
236
420
|
required: requiredNames.includes(propertyName),
|
|
237
421
|
depth: depth + 1,
|
|
238
422
|
refStack: source.refStack,
|
|
239
|
-
path: path ? `${path}.${propertyName}` : null
|
|
423
|
+
path: path ? `${path}.${propertyName}` : null,
|
|
424
|
+
inherited: inheritedFor(descend, propertyName)
|
|
240
425
|
})
|
|
241
426
|
)
|
|
242
427
|
);
|
|
@@ -245,11 +430,52 @@ function buildNode(spec, rawSchema, options) {
|
|
|
245
430
|
// conditional that can pluralize "property" without a block helper, and the
|
|
246
431
|
// disclosure partial takes its summary as a plain string
|
|
247
432
|
node.childLabel = `${node.children.length} ${node.children.length === 1 ? 'property' : 'properties'}`;
|
|
433
|
+
node.childPreview = previewNames(node.children);
|
|
248
434
|
}
|
|
249
435
|
|
|
250
436
|
return node;
|
|
251
437
|
}
|
|
252
438
|
|
|
439
|
+
// Decides which nested groups arrive open, once the whole tree is known.
|
|
440
|
+
//
|
|
441
|
+
// Level at a time rather than node by node: opening `imap` but not `smtp` because one was
|
|
442
|
+
// reached first reads as arbitrary, and a reader cannot tell a collapsed group from a
|
|
443
|
+
// group with nothing in it. Whole levels keep the page predictable - everything at a given
|
|
444
|
+
// depth is either open or closed.
|
|
445
|
+
//
|
|
446
|
+
// anyOf variants render inline (reference/schema-node puts them in a bare <ul>, not a
|
|
447
|
+
// disclosure), so they are already on screen and descend for free. buildNode() returns
|
|
448
|
+
// early on anyOf, so a node carries either children or variants, never both.
|
|
449
|
+
function assignDisclosure(root) {
|
|
450
|
+
let level = [...root.children, ...root.variants];
|
|
451
|
+
let visible = level.length;
|
|
452
|
+
|
|
453
|
+
while (level.length) {
|
|
454
|
+
const groups = level.filter(node => node.children.length);
|
|
455
|
+
if (!groups.length) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const cost = groups.reduce((total, node) => total + node.children.length, 0);
|
|
460
|
+
if (visible + cost > OPEN_ROW_BUDGET) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
for (const node of groups) {
|
|
465
|
+
node.openByDefault = true;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
visible += cost;
|
|
469
|
+
// `groups` is filtered on children.length, and a node never carries both, so there
|
|
470
|
+
// are no variants left to descend into here
|
|
471
|
+
level = groups.flatMap(node => node.children);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function countNodes(node) {
|
|
476
|
+
return [...node.children, ...node.variants].reduce((total, child) => total + 1 + countNodes(child), 0);
|
|
477
|
+
}
|
|
478
|
+
|
|
253
479
|
// Public entry point: property tree for a schema, or null when there is no schema.
|
|
254
480
|
//
|
|
255
481
|
// `path` is the anchor prefix for the tree, e.g. "accountCreate.body". Every node below
|
|
@@ -262,7 +488,15 @@ function buildSchemaTree(spec, schema, path) {
|
|
|
262
488
|
return null;
|
|
263
489
|
}
|
|
264
490
|
|
|
265
|
-
|
|
491
|
+
const root = buildNode(spec, schema, { name: null, required: false, depth: 0, refStack: [], path: path || null, inherited: undefined });
|
|
492
|
+
assignDisclosure(root);
|
|
493
|
+
|
|
494
|
+
// Whether this schema is big enough to be worth a filter and expand/collapse controls
|
|
495
|
+
// beside its heading (reference/schema-tools). On a three-property body those are noise,
|
|
496
|
+
// and a tag page would otherwise carry two dozen of them.
|
|
497
|
+
root.showTools = countNodes(root) >= SCHEMA_TOOLS_MIN_NODES;
|
|
498
|
+
|
|
499
|
+
return root;
|
|
266
500
|
}
|
|
267
501
|
|
|
268
502
|
function scalarExample(schema) {
|
|
@@ -302,7 +536,8 @@ function scalarExample(schema) {
|
|
|
302
536
|
}
|
|
303
537
|
}
|
|
304
538
|
|
|
305
|
-
function exampleFor(spec, rawSchema, depth, refStack) {
|
|
539
|
+
function exampleFor(spec, rawSchema, depth, refStack, options) {
|
|
540
|
+
const { complete, inherited } = options;
|
|
306
541
|
const resolved = resolve(spec, rawSchema, refStack);
|
|
307
542
|
const schema = resolved.schema;
|
|
308
543
|
|
|
@@ -310,10 +545,20 @@ function exampleFor(spec, rawSchema, depth, refStack) {
|
|
|
310
545
|
return null;
|
|
311
546
|
}
|
|
312
547
|
|
|
313
|
-
// An explicit example
|
|
314
|
-
//
|
|
315
|
-
|
|
316
|
-
|
|
548
|
+
// An explicit example wins, including on nested objects - those are the curated payloads
|
|
549
|
+
// the joi schemas carry. The one exception is the complete view, where an object-level
|
|
550
|
+
// example is not allowed to end the descent: hiding every key its author left out is
|
|
551
|
+
// precisely what that view exists to undo. It still wins on anything with no named keys
|
|
552
|
+
// underneath it, so a curated array of strings or a formatted scalar is never rebuilt
|
|
553
|
+
// into a worse version of itself.
|
|
554
|
+
//
|
|
555
|
+
// childSource() decides what "has named keys underneath it" means, rather than a second
|
|
556
|
+
// copy of the rule: this tab exists to name the keys the property tree shows as rows, so
|
|
557
|
+
// the two have to expand the same things or the tab contradicts the tree beneath it.
|
|
558
|
+
const own = ownExample(schema, inherited);
|
|
559
|
+
|
|
560
|
+
if (typeof own !== 'undefined' && !(complete && childSource(spec, schema, resolved.refStack))) {
|
|
561
|
+
return own;
|
|
317
562
|
}
|
|
318
563
|
|
|
319
564
|
if (depth >= MAX_DEPTH) {
|
|
@@ -321,20 +566,25 @@ function exampleFor(spec, rawSchema, depth, refStack) {
|
|
|
321
566
|
}
|
|
322
567
|
|
|
323
568
|
if (Array.isArray(schema.anyOf) && schema.anyOf.length) {
|
|
324
|
-
return exampleFor(spec, schema.anyOf[0], depth + 1, resolved.refStack);
|
|
569
|
+
return exampleFor(spec, schema.anyOf[0], depth + 1, resolved.refStack, { complete, inherited: own });
|
|
325
570
|
}
|
|
326
571
|
|
|
327
572
|
if (schema.type === 'array') {
|
|
328
573
|
if (!schema.items) {
|
|
329
|
-
return [];
|
|
574
|
+
return Array.isArray(own) ? own : [];
|
|
330
575
|
}
|
|
331
|
-
return [exampleFor(spec, schema.items, depth + 1, resolved.refStack)];
|
|
576
|
+
return [exampleFor(spec, schema.items, depth + 1, resolved.refStack, { complete, inherited: Array.isArray(own) ? own[0] : undefined })];
|
|
332
577
|
}
|
|
333
578
|
|
|
334
579
|
if (schema.properties) {
|
|
335
580
|
const result = {};
|
|
336
581
|
for (const propertyName of Object.keys(schema.properties)) {
|
|
337
|
-
result[propertyName] = exampleFor(spec, schema.properties[propertyName], depth + 1, resolved.refStack
|
|
582
|
+
result[propertyName] = exampleFor(spec, schema.properties[propertyName], depth + 1, resolved.refStack, {
|
|
583
|
+
complete,
|
|
584
|
+
// The curated value for this key still wins over a synthesized one, at every
|
|
585
|
+
// level, so the complete payload is the curated one plus the keys it omitted
|
|
586
|
+
inherited: inheritedFor(own, propertyName)
|
|
587
|
+
});
|
|
338
588
|
}
|
|
339
589
|
return result;
|
|
340
590
|
}
|
|
@@ -348,12 +598,14 @@ function exampleFor(spec, rawSchema, depth, refStack) {
|
|
|
348
598
|
|
|
349
599
|
// Public entry point: a JSON value illustrating the schema, or undefined when the
|
|
350
600
|
// schema carries nothing to build one from.
|
|
351
|
-
|
|
601
|
+
//
|
|
602
|
+
// `complete` builds the every-field view instead of the typical one - see exampleFor().
|
|
603
|
+
function buildExample(spec, schema, options = {}) {
|
|
352
604
|
if (!schema) {
|
|
353
605
|
return undefined;
|
|
354
606
|
}
|
|
355
607
|
|
|
356
|
-
const value = exampleFor(spec, schema, 0, []);
|
|
608
|
+
const value = exampleFor(spec, schema, 0, [], { complete: !!options.complete, inherited: undefined });
|
|
357
609
|
return value === null && !schema.nullable ? undefined : value;
|
|
358
610
|
}
|
|
359
611
|
|
|
@@ -362,5 +614,7 @@ module.exports = {
|
|
|
362
614
|
buildExample,
|
|
363
615
|
typeLabel,
|
|
364
616
|
enumInfo,
|
|
365
|
-
requiredFirst
|
|
617
|
+
requiredFirst,
|
|
618
|
+
rowExample,
|
|
619
|
+
itemExampleFor
|
|
366
620
|
};
|