emailengine-app 2.75.0 → 2.77.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/.github/workflows/release.yaml +24 -19
- package/.github/workflows/test.yml +0 -10
- package/.ncurc.js +14 -2
- package/.release-please-manifest.json +3 -0
- package/CHANGELOG.md +38 -0
- package/README.md +1 -1
- package/config/e2e.toml +1 -1
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +71 -4
- package/lib/address-strategies.js +18 -0
- package/lib/api-reference/code-samples.js +194 -0
- package/lib/api-reference/format.js +179 -0
- package/lib/api-reference/index.js +205 -0
- package/lib/api-reference/model.js +336 -0
- package/lib/api-reference/schema-tree.js +366 -0
- package/lib/api-routes/account-routes.js +117 -37
- package/lib/api-routes/behavior-notes.js +54 -0
- package/lib/api-routes/blocklist-routes.js +9 -9
- package/lib/api-routes/changes-routes.js +6 -4
- package/lib/api-routes/chat-routes.js +3 -3
- package/lib/api-routes/delivery-test-routes.js +24 -6
- package/lib/api-routes/export-routes.js +14 -12
- package/lib/api-routes/gateway-routes.js +11 -11
- package/lib/api-routes/license-routes.js +7 -7
- package/lib/api-routes/mailbox-routes.js +18 -9
- package/lib/api-routes/message-routes.js +55 -35
- package/lib/api-routes/oauth2-app-routes.js +22 -16
- package/lib/api-routes/outbox-routes.js +7 -7
- package/lib/api-routes/pubsub-routes.js +3 -3
- package/lib/api-routes/route-helpers.js +3 -0
- package/lib/api-routes/settings-routes.js +10 -9
- package/lib/api-routes/stats-routes.js +3 -3
- package/lib/api-routes/submit-routes.js +213 -56
- package/lib/api-routes/template-routes.js +13 -13
- package/lib/api-routes/token-routes.js +15 -10
- package/lib/api-routes/webhook-route-routes.js +5 -5
- package/lib/consts.js +7 -0
- package/lib/email-client/base-client.js +230 -31
- package/lib/email-client/gmail/send-request.js +16 -1
- package/lib/email-client/gmail-client.js +89 -5
- package/lib/email-client/imap-client.js +103 -1
- package/lib/email-client/message-builder.js +24 -7
- package/lib/email-client/outlook/send-request.js +18 -1
- package/lib/email-client/outlook-client.js +79 -4
- package/lib/enum-descriptions.js +130 -0
- package/lib/imapproxy/imap-server.js +30 -26
- package/lib/imapproxy/upstream-client.js +38 -0
- package/lib/oauth2-apps.js +9 -3
- package/lib/openapi/build-document.js +318 -0
- package/lib/openapi/index.js +95 -0
- package/lib/openapi/joi-schema.js +523 -0
- package/lib/routes-ui.js +5 -1
- package/lib/schemas.js +76 -17
- package/lib/swagger-options.js +173 -0
- package/lib/tokens.js +69 -5
- package/lib/ui-routes/admin-entities-routes.js +3 -0
- package/lib/ui-routes/dashboard-routes.js +2 -26
- package/lib/ui-routes/reference-routes.js +195 -0
- package/lib/ui-routes/route-helpers.js +11 -0
- package/package.json +19 -26
- package/playwright.config.js +1 -1
- package/prepare-bundle-deps.sh +28 -0
- package/release-please-config.json +11 -0
- package/sbom.json +0 -1
- package/server.js +22 -2
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +90 -0
- package/static/js/ee-client.js +238 -8
- package/static/js/reference.js +736 -0
- package/static/js/ui.js +61 -33
- package/static/licenses.html +16 -16
- package/translations/messages.pot +13 -13
- package/views/config/ai.hbs +1 -1
- package/views/config/document-store/chat.hbs +5 -5
- package/views/config/document-store/index.hbs +2 -2
- package/views/config/document-store/pre-processing/index.hbs +1 -1
- package/views/config/email-processing.hbs +1 -1
- package/views/config/imap-proxy.hbs +1 -1
- package/views/config/license.hbs +1 -1
- package/views/config/oauth/index.hbs +3 -3
- package/views/config/security.hbs +1 -1
- package/views/config/smtp.hbs +2 -2
- package/views/dashboard.hbs +1 -1
- package/views/layout/app.hbs +18 -7
- package/views/partials/oauth_form.hbs +4 -4
- package/views/partials/reference/anchor-link.hbs +20 -0
- package/views/partials/reference/disclosure.hbs +37 -0
- package/views/partials/reference/nav.hbs +93 -0
- package/views/partials/reference/operation.hbs +127 -0
- package/views/partials/reference/params.hbs +20 -0
- package/views/partials/reference/schema-node.hbs +99 -0
- package/views/partials/reference/schema.hbs +19 -0
- package/views/partials/reference/section-heading.hbs +16 -0
- package/views/partials/reference/token-alert.hbs +24 -0
- package/views/partials/reference/try-field.hbs +31 -0
- package/views/partials/reference/try-it.hbs +78 -0
- package/views/partials/side_menu.hbs +1 -1
- package/views/partials/ui/badge.hbs +4 -2
- package/views/partials/ui/page-header.hbs +1 -1
- package/views/partials/ui/search-input.hbs +31 -0
- package/views/partials/ui/setting-ref.hbs +1 -1
- package/views/partials/ui/stat-card.hbs +4 -2
- package/views/partials/ui/tab.hbs +6 -1
- package/views/partials/ui/tabs.hbs +2 -2
- package/views/reference/index.hbs +76 -0
- package/views/reference/redirect.hbs +49 -0
- package/views/reference/tag.hbs +28 -0
- package/views/reference/token.hbs +49 -0
- package/views/tokens/index.hbs +9 -1
- package/views/webhooks/index.hbs +3 -3
- package/workers/api.js +37 -215
- package/Gruntfile.js +0 -58
- package/views/swagger/index.hbs +0 -82
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Builds the render model for the API reference from the OpenAPI document.
|
|
4
|
+
//
|
|
5
|
+
// Everything derived here depends only on the spec, so the result is built once per
|
|
6
|
+
// API worker and shared by every request. Anything that depends on the request (the
|
|
7
|
+
// base URL used in the code samples) is applied later, in lib/api-reference/index.js.
|
|
8
|
+
|
|
9
|
+
const { formatDescription, slugify, methodVariant, constraintList, stringifyExample } = require('./format');
|
|
10
|
+
const { buildSchemaTree, buildExample, typeLabel, enumInfo, requiredFirst } = require('./schema-tree');
|
|
11
|
+
const { readCodeSamples } = require('./code-samples');
|
|
12
|
+
const { NOTES_SEPARATOR } = require('../openapi');
|
|
13
|
+
|
|
14
|
+
const METHOD_ORDER = ['get', 'post', 'put', 'patch', 'delete', 'options', 'head'];
|
|
15
|
+
|
|
16
|
+
const JSON_CONTENT_TYPE = 'application/json';
|
|
17
|
+
|
|
18
|
+
function statusVariant(code) {
|
|
19
|
+
const numeric = Number(code);
|
|
20
|
+
if (numeric >= 200 && numeric < 300) {
|
|
21
|
+
return 'success';
|
|
22
|
+
}
|
|
23
|
+
if (numeric >= 400 && numeric < 500) {
|
|
24
|
+
return 'warning';
|
|
25
|
+
}
|
|
26
|
+
if (numeric >= 500) {
|
|
27
|
+
return 'error';
|
|
28
|
+
}
|
|
29
|
+
return 'neutral';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Picks the media type to document. Every operation in the current document either
|
|
33
|
+
// speaks JSON or streams (text/event-stream, application/octet-stream); JSON is
|
|
34
|
+
// preferred so the schema tree and examples have something to render.
|
|
35
|
+
function pickContent(content) {
|
|
36
|
+
if (!content || typeof content !== 'object') {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const types = Object.keys(content);
|
|
41
|
+
if (!types.length) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const contentType = types.includes(JSON_CONTENT_TYPE) ? JSON_CONTENT_TYPE : types[0];
|
|
46
|
+
return { contentType, media: content[contentType] };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildParameter(spec, parameter, operationId) {
|
|
50
|
+
const schema = parameter.schema || {};
|
|
51
|
+
|
|
52
|
+
const view = {
|
|
53
|
+
name: parameter.name,
|
|
54
|
+
anchor: `${operationId}.param.${parameter.name}`,
|
|
55
|
+
in: parameter.in,
|
|
56
|
+
required: !!parameter.required,
|
|
57
|
+
typeLabel: typeLabel(spec, schema),
|
|
58
|
+
descriptionHtml: formatDescription(parameter.description || schema.description),
|
|
59
|
+
constraints: constraintList(schema),
|
|
60
|
+
deprecated: !!parameter.deprecated,
|
|
61
|
+
// Booleans and enums render as a select in the try-it form, not a text field
|
|
62
|
+
isBoolean: schema.type === 'boolean'
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const enums = enumInfo(schema);
|
|
66
|
+
if (enums) {
|
|
67
|
+
view.enumValues = enums.values;
|
|
68
|
+
view.enumDocumented = enums.documented;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (typeof schema.default !== 'undefined') {
|
|
72
|
+
view.defaultValue = typeof schema.default === 'string' ? schema.default : JSON.stringify(schema.default);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (typeof schema.example !== 'undefined') {
|
|
76
|
+
view.exampleValue = typeof schema.example === 'string' ? schema.example : JSON.stringify(schema.example);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Prefilled value for the try-it form, so an operation can be executed after filling
|
|
80
|
+
// in only the values that are genuinely instance specific. Left undefined when the
|
|
81
|
+
// schema offers nothing, which renders as an empty field.
|
|
82
|
+
view.tryValue = view.exampleValue ?? view.defaultValue;
|
|
83
|
+
|
|
84
|
+
// Value substituted into the generated code samples. Unlike tryValue this always
|
|
85
|
+
// resolves, so a snippet shows `<account>` rather than an empty path segment.
|
|
86
|
+
view.sampleValue = view.tryValue ?? (view.enumValues && view.enumValues[0].value) ?? `<${parameter.name}>`;
|
|
87
|
+
|
|
88
|
+
return view;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function buildBody(spec, requestBody, operationId) {
|
|
92
|
+
if (!requestBody) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const picked = pickContent(requestBody.content);
|
|
97
|
+
if (!picked) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const schema = picked.media.schema;
|
|
102
|
+
const exampleValue = typeof picked.media.example !== 'undefined' ? picked.media.example : buildExample(spec, schema);
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
contentType: picked.contentType,
|
|
106
|
+
descriptionHtml: formatDescription(requestBody.description),
|
|
107
|
+
tree: buildSchemaTree(spec, schema, `${operationId}.body`),
|
|
108
|
+
exampleValue,
|
|
109
|
+
exampleJson: stringifyExample(exampleValue)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Tab ids are built here rather than in the template because ui/tabs neutralizes its own
|
|
114
|
+
// hash params inside the partial block, which puts the operation id out of reach for
|
|
115
|
+
// composing them there.
|
|
116
|
+
function buildResponses(spec, responses, operationId) {
|
|
117
|
+
if (!responses) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const entries = Object.keys(responses).map(code => {
|
|
122
|
+
const response = responses[code] || {};
|
|
123
|
+
const variant = statusVariant(code);
|
|
124
|
+
const picked = pickContent(response.content);
|
|
125
|
+
const schema = picked && picked.media.schema;
|
|
126
|
+
const exampleValue = picked && typeof picked.media.example !== 'undefined' ? picked.media.example : buildExample(spec, schema);
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
code,
|
|
130
|
+
variant,
|
|
131
|
+
descriptionHtml: formatDescription(response.description),
|
|
132
|
+
contentType: picked ? picked.contentType : null,
|
|
133
|
+
// Property trees are built for success responses only. The document has 474
|
|
134
|
+
// non-2xx responses sharing 8 distinct shapes - all variations on the
|
|
135
|
+
// {statusCode, error, message} envelope - so expanding them per operation
|
|
136
|
+
// added 2,400 of a tag page's 4,400 tree nodes and a quarter of its bytes to
|
|
137
|
+
// restate the same thing. The error envelope is documented once on the
|
|
138
|
+
// landing page, and each response still carries its own example.
|
|
139
|
+
tree: schema && variant === 'success' ? buildSchemaTree(spec, schema, `${operationId}.resp.${code}`) : null,
|
|
140
|
+
exampleJson: schema ? stringifyExample(exampleValue) : ''
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return entries.map((entry, index) =>
|
|
145
|
+
Object.assign(entry, {
|
|
146
|
+
tabId: `${operationId}-resp-${entry.code}`,
|
|
147
|
+
active: index === 0
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function buildOperation(spec, path, method, operation, tagName) {
|
|
153
|
+
// Resolved first because it is the anchor prefix every parameter, property and
|
|
154
|
+
// response node below hangs its own id off
|
|
155
|
+
const id = operation.operationId || slugify(`${method}-${path}`);
|
|
156
|
+
|
|
157
|
+
const parameters = (operation.parameters || []).map(parameter => buildParameter(spec, parameter, id));
|
|
158
|
+
|
|
159
|
+
const body = buildBody(spec, operation.requestBody, id);
|
|
160
|
+
|
|
161
|
+
// Behavior notes (lib/api-routes/behavior-notes.js): things the schemas cannot express,
|
|
162
|
+
// like "a 2xx means queued, not sent", or a provider that handles the call differently.
|
|
163
|
+
//
|
|
164
|
+
// Routes declare them twice on purpose. They are appended to the route's `notes` array,
|
|
165
|
+
// which the generator joins into the standard `description` - so every consumer of
|
|
166
|
+
// /swagger.json gets them, not just this page. They are ALSO listed under the
|
|
167
|
+
// x-ee-behavior extension so this renderer can tell them apart from the operation's own
|
|
168
|
+
// prose and pull them into their own callout. A test asserts the two stay in step.
|
|
169
|
+
const behaviorNotes = Array.isArray(operation['x-ee-behavior']) ? operation['x-ee-behavior'] : [];
|
|
170
|
+
|
|
171
|
+
// Split back on the separator the generator joined the route's `notes` array with, so the two
|
|
172
|
+
// halves of that contract cannot drift apart
|
|
173
|
+
const paragraphs = (operation.description || '').split(NOTES_SEPARATOR);
|
|
174
|
+
const prose = paragraphs.filter(paragraph => !behaviorNotes.includes(paragraph));
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
id,
|
|
178
|
+
behavior: behaviorNotes.map(note => formatDescription(note)),
|
|
179
|
+
// Hand-written snippets the route declared; buildCodeSamples() puts them ahead of
|
|
180
|
+
// the generated ones
|
|
181
|
+
codeSamples: readCodeSamples(operation),
|
|
182
|
+
method: method.toLowerCase(),
|
|
183
|
+
methodLabel: method.toUpperCase(),
|
|
184
|
+
methodVariant: methodVariant(method),
|
|
185
|
+
path,
|
|
186
|
+
summary: operation.summary || '',
|
|
187
|
+
descriptionHtml: formatDescription(prose.join('\n\n')),
|
|
188
|
+
deprecated: !!operation.deprecated,
|
|
189
|
+
tag: tagName,
|
|
190
|
+
tagSlug: slugify(tagName),
|
|
191
|
+
// Path parameters keep their order in the path, which is the only order that makes
|
|
192
|
+
// sense for them. The other two are grouped required-first: GET /v1/settings carries
|
|
193
|
+
// 106 query parameters, and the handful that are mandatory should not be buried.
|
|
194
|
+
pathParams: parameters.filter(parameter => parameter.in === 'path'),
|
|
195
|
+
queryParams: requiredFirst(parameters.filter(parameter => parameter.in === 'query')),
|
|
196
|
+
headerParams: requiredFirst(parameters.filter(parameter => parameter.in === 'header')),
|
|
197
|
+
body,
|
|
198
|
+
responses: buildResponses(spec, operation.responses, id)
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Collects the property names in a schema tree, at every depth.
|
|
203
|
+
function collectNames(node, into) {
|
|
204
|
+
if (!node) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (node.name) {
|
|
209
|
+
into.add(node.name);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
for (const child of node.children) {
|
|
213
|
+
collectNames(child, into);
|
|
214
|
+
}
|
|
215
|
+
for (const variant of node.variants) {
|
|
216
|
+
collectNames(variant, into);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// The whole corpus the sidebar filter matches an operation against. Built here rather than
|
|
221
|
+
// interpolated in reference/nav.hbs so that what is searchable is decided in one place -
|
|
222
|
+
// the template just emits it.
|
|
223
|
+
//
|
|
224
|
+
// It is more than the hit row displays. The two questions the filter could not answer
|
|
225
|
+
// before are "which endpoint takes a `mailbox`" and "which one accepts `mailMerge`", so it
|
|
226
|
+
// also carries the operation id and every name a caller can SEND: query and header
|
|
227
|
+
// parameters, and request body properties at any depth. Path parameters are already covered
|
|
228
|
+
// by the path itself, which spells them out as `{account}`.
|
|
229
|
+
//
|
|
230
|
+
// Response property names are deliberately left out. The filter ANDs plain substrings, so
|
|
231
|
+
// every added term is also a chance to match something unrelated - and response shapes
|
|
232
|
+
// repeat the same envelope fields (`account`, `id`, `messages`) across most operations,
|
|
233
|
+
// which would blunt exactly the queries this is for. Descriptions are left out for the same
|
|
234
|
+
// reason, and because they are the bulk of the bytes.
|
|
235
|
+
function searchCorpus(operation) {
|
|
236
|
+
const terms = new Set([operation.methodLabel, operation.path, operation.summary, operation.tag, operation.id]);
|
|
237
|
+
|
|
238
|
+
for (const parameter of [...operation.queryParams, ...operation.headerParams]) {
|
|
239
|
+
terms.add(parameter.name);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (operation.body) {
|
|
243
|
+
collectNames(operation.body.tree, terms);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
terms.delete('');
|
|
247
|
+
return [...terms].join(' ');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Sorts operations inside a tag the way the old swagger-ui page did (`sortEndpoints:
|
|
251
|
+
// 'method'`), so the ordering people are used to carries over.
|
|
252
|
+
function sortOperations(operations) {
|
|
253
|
+
return operations.sort((a, b) => {
|
|
254
|
+
const methodDiff = METHOD_ORDER.indexOf(a.method) - METHOD_ORDER.indexOf(b.method);
|
|
255
|
+
if (methodDiff) {
|
|
256
|
+
return methodDiff;
|
|
257
|
+
}
|
|
258
|
+
return a.path.localeCompare(b.path);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function buildModel(spec) {
|
|
263
|
+
const tagsByName = new Map();
|
|
264
|
+
|
|
265
|
+
// Seed from the spec's tag list first so the declared order (the `tags` array in
|
|
266
|
+
// lib/swagger-options.js) drives the navigation, not path iteration order
|
|
267
|
+
for (const tag of spec.tags || []) {
|
|
268
|
+
tagsByName.set(tag.name, {
|
|
269
|
+
name: tag.name,
|
|
270
|
+
slug: slugify(tag.name),
|
|
271
|
+
descriptionHtml: formatDescription(tag.description),
|
|
272
|
+
externalDocs: tag.externalDocs && tag.externalDocs.url ? tag.externalDocs : null,
|
|
273
|
+
operations: []
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const allOperations = [];
|
|
278
|
+
|
|
279
|
+
for (const path of Object.keys(spec.paths || {})) {
|
|
280
|
+
const pathItem = spec.paths[path] || {};
|
|
281
|
+
for (const method of Object.keys(pathItem)) {
|
|
282
|
+
if (!METHOD_ORDER.includes(method.toLowerCase())) {
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const operation = pathItem[method];
|
|
287
|
+
const tagName = (operation.tags && operation.tags[0]) || 'Other';
|
|
288
|
+
|
|
289
|
+
if (!tagsByName.has(tagName)) {
|
|
290
|
+
tagsByName.set(tagName, {
|
|
291
|
+
name: tagName,
|
|
292
|
+
slug: slugify(tagName),
|
|
293
|
+
descriptionHtml: '',
|
|
294
|
+
externalDocs: null,
|
|
295
|
+
operations: []
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const view = buildOperation(spec, path, method, operation, tagName);
|
|
300
|
+
tagsByName.get(tagName).operations.push(view);
|
|
301
|
+
allOperations.push(view);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const tags = [...tagsByName.values()].filter(tag => tag.operations.length);
|
|
306
|
+
for (const tag of tags) {
|
|
307
|
+
sortOperations(tag.operations);
|
|
308
|
+
tag.operationCount = tag.operations.length;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
version: (spec.info && spec.info.version) || '',
|
|
313
|
+
// The document's own introduction, rendered on the landing page rather than
|
|
314
|
+
// restated there. It is written as plain paragraphs for exactly this reason -
|
|
315
|
+
// see the `info` block in workers/api.js.
|
|
316
|
+
descriptionHtml: formatDescription(spec.info && spec.info.description),
|
|
317
|
+
tags,
|
|
318
|
+
tagsBySlug: new Map(tags.map(tag => [tag.slug, tag])),
|
|
319
|
+
operationCount: allOperations.length,
|
|
320
|
+
// Flat list backing the client-side filter on every page
|
|
321
|
+
searchIndex: allOperations.map(operation => ({
|
|
322
|
+
id: operation.id,
|
|
323
|
+
method: operation.methodLabel,
|
|
324
|
+
methodVariant: operation.methodVariant,
|
|
325
|
+
path: operation.path,
|
|
326
|
+
summary: operation.summary,
|
|
327
|
+
tag: operation.tag,
|
|
328
|
+
search: searchCorpus(operation),
|
|
329
|
+
url: `/admin/reference/${operation.tagSlug}#${operation.id}`
|
|
330
|
+
}))
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
module.exports = {
|
|
335
|
+
buildModel
|
|
336
|
+
};
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Turns an OpenAPI schema into the two things the reference pages render: a nested
|
|
4
|
+
// property tree and a synthesized JSON example.
|
|
5
|
+
//
|
|
6
|
+
// The generated document (lib/openapi/) uses a small corner of OpenAPI: no allOf, no
|
|
7
|
+
// oneOf, no discriminator, no additionalProperties and no recursive schemas. This module
|
|
8
|
+
// deliberately handles exactly that subset plus the guards needed so a future schema
|
|
9
|
+
// that breaks those assumptions degrades into a visible marker instead of hanging the
|
|
10
|
+
// worker.
|
|
11
|
+
|
|
12
|
+
const { formatDescription, constraintList } = require('./format');
|
|
13
|
+
const { SCHEMA_REF_PREFIX } = require('../openapi');
|
|
14
|
+
|
|
15
|
+
// Depth cap for both the property tree and the example. The deepest schema in the
|
|
16
|
+
// current document nests 9 levels, so this only trips on something new.
|
|
17
|
+
const MAX_DEPTH = 14;
|
|
18
|
+
|
|
19
|
+
// Nested property groups render inside a <details>. The first levels start open so the
|
|
20
|
+
// shape of a payload is visible without clicking; deeper ones start collapsed so a
|
|
21
|
+
// large schema (GET /v1/settings carries 104 properties) does not bury the page.
|
|
22
|
+
const OPEN_BY_DEFAULT_DEPTH = 2;
|
|
23
|
+
|
|
24
|
+
// Placeholder values for string formats that have no example of their own, so a
|
|
25
|
+
// synthesized example still shows the expected shape rather than a wall of "string".
|
|
26
|
+
const FORMAT_PLACEHOLDERS = {
|
|
27
|
+
'date-time': '2026-01-15T09:30:00.000Z',
|
|
28
|
+
date: '2026-01-15',
|
|
29
|
+
email: 'user@example.com',
|
|
30
|
+
uri: 'https://example.com',
|
|
31
|
+
hostname: 'example.com',
|
|
32
|
+
ipv4: '127.0.0.1',
|
|
33
|
+
ipv6: '::1',
|
|
34
|
+
uuid: '00000000-0000-0000-0000-000000000000',
|
|
35
|
+
guid: '00000000-0000-0000-0000-000000000000',
|
|
36
|
+
byte: 'aGVsbG8=',
|
|
37
|
+
binary: '<binary>',
|
|
38
|
+
password: 'secret'
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Resolves a local $ref against the document. Every named schema lives in one place, so an
|
|
42
|
+
// unresolvable ref means the document is malformed rather than that another bucket needs
|
|
43
|
+
// looking in - buildNode() surfaces that as a visible marker.
|
|
44
|
+
function lookupRef(spec, ref) {
|
|
45
|
+
if (typeof ref !== 'string' || !ref.startsWith(SCHEMA_REF_PREFIX)) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const name = ref.slice(SCHEMA_REF_PREFIX.length);
|
|
50
|
+
|
|
51
|
+
return (spec.components && spec.components.schemas && spec.components.schemas[name]) || null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Follows a $ref chain to the underlying schema. `refStack` carries the refs already
|
|
55
|
+
// visited on the current branch; revisiting one means the document is recursive, which
|
|
56
|
+
// is reported rather than followed.
|
|
57
|
+
function resolve(spec, schema, refStack) {
|
|
58
|
+
let current = schema;
|
|
59
|
+
let stack = refStack;
|
|
60
|
+
|
|
61
|
+
while (current && typeof current === 'object' && current.$ref) {
|
|
62
|
+
const ref = current.$ref;
|
|
63
|
+
if (stack.includes(ref)) {
|
|
64
|
+
return { schema: null, refStack: stack, circular: true };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const target = lookupRef(spec, ref);
|
|
68
|
+
if (!target) {
|
|
69
|
+
return { schema: null, refStack: stack, unresolved: ref };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
stack = stack.concat(ref);
|
|
73
|
+
current = target;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return { schema: current, refStack: stack };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Human-readable type for a schema node. Arrays name their item type so a property row
|
|
80
|
+
// reads "array of object" instead of a bare "array".
|
|
81
|
+
//
|
|
82
|
+
// This lives here rather than in format.js because it has to follow $refs to be correct:
|
|
83
|
+
// 46 array properties in the document have `items: {$ref}`, and an anyOf's branches are
|
|
84
|
+
// refs too. format.js is deliberately spec-free, so a copy there could only ever answer
|
|
85
|
+
// "any" for those - which it did, until buildNode() patched the result afterwards. One
|
|
86
|
+
// ref-aware implementation replaces that and the two overrides it needed.
|
|
87
|
+
function typeLabel(spec, schema, refStack = []) {
|
|
88
|
+
const resolved = resolve(spec, schema, refStack);
|
|
89
|
+
const target = resolved.schema;
|
|
90
|
+
|
|
91
|
+
if (!target || typeof target !== 'object') {
|
|
92
|
+
return 'any';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (Array.isArray(target.anyOf) && target.anyOf.length) {
|
|
96
|
+
return target.anyOf.map(entry => typeLabel(spec, entry, resolved.refStack)).join(' or ');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (target.type === 'array') {
|
|
100
|
+
return `array of ${target.items ? typeLabel(spec, target.items, resolved.refStack) : 'any'}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (target.enum && target.enum.length && !target.type) {
|
|
104
|
+
return 'enum';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return target.type || 'any';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Enum values with their per-value documentation, when the joi schema carries any.
|
|
111
|
+
// `.meta({ enumDescriptions })` in lib/schemas.js surfaces as `x-meta.enumDescriptions`
|
|
112
|
+
// beside the `enum` array - OpenAPI itself has no field for documenting individual values.
|
|
113
|
+
// Returns null when the schema is not an enum, so callers can assign both fields in one go.
|
|
114
|
+
function enumInfo(schema) {
|
|
115
|
+
if (!Array.isArray(schema.enum) || !schema.enum.length) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const documented = (schema['x-meta'] && schema['x-meta'].enumDescriptions) || {};
|
|
120
|
+
|
|
121
|
+
// Object.hasOwn, not a bare lookup: an enum value named `constructor` or `toString`
|
|
122
|
+
// would otherwise pick up an inherited function and render as garbage.
|
|
123
|
+
const values = schema.enum.map(value => {
|
|
124
|
+
const key = typeof value === 'string' ? value : JSON.stringify(value);
|
|
125
|
+
return { value: key, description: Object.hasOwn(documented, key) ? documented[key] : null };
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return { values, documented: values.some(entry => entry.description) };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Required entries first, declared order preserved within each group (Array#sort is
|
|
132
|
+
// stable). "What do I have to send" is the first question a schema answers, and a
|
|
133
|
+
// payload like SubmitMessage has 33 properties of which 0 to 3 are required - finding
|
|
134
|
+
// them meant reading every row.
|
|
135
|
+
//
|
|
136
|
+
// Exported because operation parameters get the same treatment in model.js: they are
|
|
137
|
+
// deliberately built with the same field names as a tree node so they can render through
|
|
138
|
+
// the same partial, which makes this the right place for the ordering rule too.
|
|
139
|
+
function requiredFirst(nodes) {
|
|
140
|
+
return nodes.sort((a, b) => Number(b.required) - Number(a.required));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// The properties to show underneath a node: an object's own properties, or - for an
|
|
144
|
+
// array of objects - the item properties, so an array row expands straight into the
|
|
145
|
+
// item shape instead of an intermediate "items" level.
|
|
146
|
+
function childSource(spec, schema, refStack) {
|
|
147
|
+
if (!schema || typeof schema !== 'object') {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (schema.type === 'array' && schema.items) {
|
|
152
|
+
const resolved = resolve(spec, schema.items, refStack);
|
|
153
|
+
if (resolved.schema && resolved.schema.properties) {
|
|
154
|
+
return { schema: resolved.schema, refStack: resolved.refStack };
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (schema.properties) {
|
|
160
|
+
return { schema, refStack };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function buildNode(spec, rawSchema, options) {
|
|
167
|
+
const { name, required, depth, refStack, path } = options;
|
|
168
|
+
|
|
169
|
+
const resolved = resolve(spec, rawSchema, refStack);
|
|
170
|
+
|
|
171
|
+
const node = {
|
|
172
|
+
name: name || null,
|
|
173
|
+
// Anchor id for this property, dotted from the tree root - see buildSchemaTree()
|
|
174
|
+
anchor: path || null,
|
|
175
|
+
required: !!required,
|
|
176
|
+
openByDefault: depth < OPEN_BY_DEFAULT_DEPTH,
|
|
177
|
+
circular: !!resolved.circular,
|
|
178
|
+
unresolved: resolved.unresolved || null,
|
|
179
|
+
truncated: false,
|
|
180
|
+
children: [],
|
|
181
|
+
variants: []
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const schema = resolved.schema;
|
|
185
|
+
if (!schema || typeof schema !== 'object') {
|
|
186
|
+
node.typeLabel = node.circular ? 'circular reference' : 'any';
|
|
187
|
+
node.descriptionHtml = '';
|
|
188
|
+
node.constraints = [];
|
|
189
|
+
return node;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
node.typeLabel = typeLabel(spec, schema, resolved.refStack);
|
|
193
|
+
|
|
194
|
+
node.descriptionHtml = formatDescription(schema.description);
|
|
195
|
+
node.constraints = constraintList(schema);
|
|
196
|
+
node.deprecated = !!schema.deprecated;
|
|
197
|
+
node.nullable = !!schema.nullable;
|
|
198
|
+
|
|
199
|
+
const enums = enumInfo(schema);
|
|
200
|
+
if (enums) {
|
|
201
|
+
node.enumValues = enums.values;
|
|
202
|
+
node.enumDocumented = enums.documented;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (typeof schema.default !== 'undefined') {
|
|
206
|
+
node.defaultValue = typeof schema.default === 'string' ? schema.default : JSON.stringify(schema.default);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (depth >= MAX_DEPTH) {
|
|
210
|
+
node.truncated = true;
|
|
211
|
+
return node;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (Array.isArray(schema.anyOf) && schema.anyOf.length) {
|
|
215
|
+
node.variants = schema.anyOf.map((variant, index) =>
|
|
216
|
+
buildNode(spec, variant, {
|
|
217
|
+
name: `option ${index + 1}`,
|
|
218
|
+
required: false,
|
|
219
|
+
depth: depth + 1,
|
|
220
|
+
refStack: resolved.refStack,
|
|
221
|
+
// The index, not the display name: an anchor cannot carry the space in
|
|
222
|
+
// "option 1", and two variants would otherwise share the parent's path
|
|
223
|
+
path: path ? `${path}.option${index + 1}` : null
|
|
224
|
+
})
|
|
225
|
+
);
|
|
226
|
+
return node;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const source = childSource(spec, schema, resolved.refStack);
|
|
230
|
+
if (source) {
|
|
231
|
+
const requiredNames = Array.isArray(source.schema.required) ? source.schema.required : [];
|
|
232
|
+
node.children = requiredFirst(
|
|
233
|
+
Object.keys(source.schema.properties).map(propertyName =>
|
|
234
|
+
buildNode(spec, source.schema.properties[propertyName], {
|
|
235
|
+
name: propertyName,
|
|
236
|
+
required: requiredNames.includes(propertyName),
|
|
237
|
+
depth: depth + 1,
|
|
238
|
+
refStack: source.refStack,
|
|
239
|
+
path: path ? `${path}.${propertyName}` : null
|
|
240
|
+
})
|
|
241
|
+
)
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
// Composed here rather than in the template: Handlebars has no inline
|
|
245
|
+
// conditional that can pluralize "property" without a block helper, and the
|
|
246
|
+
// disclosure partial takes its summary as a plain string
|
|
247
|
+
node.childLabel = `${node.children.length} ${node.children.length === 1 ? 'property' : 'properties'}`;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return node;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Public entry point: property tree for a schema, or null when there is no schema.
|
|
254
|
+
//
|
|
255
|
+
// `path` is the anchor prefix for the tree, e.g. "accountCreate.body". Every node below
|
|
256
|
+
// it gets a dotted id built from the property names on the way down, which is what makes
|
|
257
|
+
// an individual field linkable ("#accountCreate.body.imap.tls.rejectUnauthorized"). The
|
|
258
|
+
// ids are derived from the schema alone, so they stay stable as long as the field does.
|
|
259
|
+
// Passing no prefix leaves every anchor null and the rows render without a link.
|
|
260
|
+
function buildSchemaTree(spec, schema, path) {
|
|
261
|
+
if (!schema) {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return buildNode(spec, schema, { name: null, required: false, depth: 0, refStack: [], path: path || null });
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function scalarExample(schema) {
|
|
269
|
+
if (typeof schema.default !== 'undefined') {
|
|
270
|
+
return schema.default;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (Array.isArray(schema.enum) && schema.enum.length) {
|
|
274
|
+
return schema.enum[0];
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
switch (schema.type) {
|
|
278
|
+
case 'boolean':
|
|
279
|
+
return false;
|
|
280
|
+
case 'integer':
|
|
281
|
+
case 'number':
|
|
282
|
+
if (typeof schema.minimum === 'number') {
|
|
283
|
+
return schema.minimum;
|
|
284
|
+
}
|
|
285
|
+
return 0;
|
|
286
|
+
case 'string': {
|
|
287
|
+
if (schema.format && FORMAT_PLACEHOLDERS[schema.format]) {
|
|
288
|
+
return FORMAT_PLACEHOLDERS[schema.format];
|
|
289
|
+
}
|
|
290
|
+
const xFormat = schema['x-format'];
|
|
291
|
+
if (xFormat && typeof xFormat === 'object') {
|
|
292
|
+
for (const key of Object.keys(xFormat)) {
|
|
293
|
+
if (FORMAT_PLACEHOLDERS[key]) {
|
|
294
|
+
return FORMAT_PLACEHOLDERS[key];
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return 'string';
|
|
299
|
+
}
|
|
300
|
+
default:
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function exampleFor(spec, rawSchema, depth, refStack) {
|
|
306
|
+
const resolved = resolve(spec, rawSchema, refStack);
|
|
307
|
+
const schema = resolved.schema;
|
|
308
|
+
|
|
309
|
+
if (!schema || typeof schema !== 'object') {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// An explicit example always wins, including on nested objects - those are the
|
|
314
|
+
// curated payloads the joi schemas carry.
|
|
315
|
+
if (typeof schema.example !== 'undefined') {
|
|
316
|
+
return schema.example;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (depth >= MAX_DEPTH) {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (Array.isArray(schema.anyOf) && schema.anyOf.length) {
|
|
324
|
+
return exampleFor(spec, schema.anyOf[0], depth + 1, resolved.refStack);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (schema.type === 'array') {
|
|
328
|
+
if (!schema.items) {
|
|
329
|
+
return [];
|
|
330
|
+
}
|
|
331
|
+
return [exampleFor(spec, schema.items, depth + 1, resolved.refStack)];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (schema.properties) {
|
|
335
|
+
const result = {};
|
|
336
|
+
for (const propertyName of Object.keys(schema.properties)) {
|
|
337
|
+
result[propertyName] = exampleFor(spec, schema.properties[propertyName], depth + 1, resolved.refStack);
|
|
338
|
+
}
|
|
339
|
+
return result;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (schema.type === 'object') {
|
|
343
|
+
return {};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return scalarExample(schema);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Public entry point: a JSON value illustrating the schema, or undefined when the
|
|
350
|
+
// schema carries nothing to build one from.
|
|
351
|
+
function buildExample(spec, schema) {
|
|
352
|
+
if (!schema) {
|
|
353
|
+
return undefined;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const value = exampleFor(spec, schema, 0, []);
|
|
357
|
+
return value === null && !schema.nullable ? undefined : value;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
module.exports = {
|
|
361
|
+
buildSchemaTree,
|
|
362
|
+
buildExample,
|
|
363
|
+
typeLabel,
|
|
364
|
+
enumInfo,
|
|
365
|
+
requiredFirst
|
|
366
|
+
};
|