emailengine-app 2.76.0 → 2.78.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 +80 -0
- package/.ncurc.js +14 -2
- package/.release-please-manifest.json +3 -0
- package/CHANGELOG.md +54 -0
- package/README.md +1 -1
- package/bin/emailengine.js +1 -1
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +72 -5
- package/lib/account/expected-identity.js +88 -0
- package/lib/account.js +75 -26
- package/lib/address-strategies.js +18 -0
- package/lib/api-reference/code-samples.js +194 -0
- package/lib/api-reference/format.js +193 -0
- package/lib/api-reference/index.js +222 -0
- package/lib/api-reference/model.js +338 -0
- package/lib/api-reference/schema-tree.js +366 -0
- package/lib/api-routes/account-routes.js +136 -39
- package/lib/api-routes/behavior-notes.js +57 -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/settings-routes.js +10 -9
- package/lib/api-routes/stats-routes.js +3 -3
- package/lib/api-routes/submit-routes.js +58 -8
- 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/append-list.js +3 -14
- package/lib/arf-detect.js +23 -1
- package/lib/autodetect-imap-settings.js +40 -16
- package/lib/base32.js +30 -0
- package/lib/bounce-detect.js +182 -62
- package/lib/consts.js +7 -0
- package/lib/egress-filter.js +89 -8
- package/lib/email-client/base-client.js +72 -82
- package/lib/email-client/gmail-client.js +3 -3
- package/lib/email-client/imap/listing-diff.js +3 -3
- package/lib/email-client/imap/mailbox.js +5 -69
- package/lib/email-client/imap-client.js +3 -2
- package/lib/email-client/message-builder.js +13 -13
- package/lib/email-client/outlook-client.js +1 -1
- package/lib/enum-descriptions.js +130 -0
- package/lib/export.js +1 -1
- package/lib/gateway.js +1 -1
- package/lib/get-raw-email.js +1 -1
- package/lib/imapproxy/imap-core/lib/imap-connection.js +2 -2
- package/lib/imapproxy/imap-core/lib/imap-server.js +2 -2
- package/lib/imapproxy/imap-server.js +42 -28
- package/lib/imapproxy/upstream-client.js +38 -0
- package/lib/license-beacon.js +1 -1
- package/lib/llm-pre-process.js +1 -1
- package/lib/msgpack.js +62 -0
- package/lib/oauth/decode-jwt-payload.js +39 -0
- package/lib/oauth/outlook-identity.js +61 -0
- package/lib/oauth/retry-setup.js +38 -0
- package/lib/oauth2-apps.js +10 -4
- 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/outbox.js +66 -3
- package/lib/pre-process.js +1 -1
- package/lib/read-env-value.js +37 -0
- package/lib/routes-ui.js +5 -1
- package/lib/schemas.js +88 -17
- package/lib/swagger-options.js +173 -0
- package/lib/templates.js +1 -1
- package/lib/tokens.js +70 -6
- package/lib/tools.js +117 -48
- package/lib/totp.js +67 -0
- package/lib/ui-routes/account-routes.js +91 -15
- package/lib/ui-routes/admin-config-routes.js +2 -7
- package/lib/ui-routes/admin-entities-routes.js +3 -0
- package/lib/ui-routes/auth-routes.js +6 -24
- package/lib/ui-routes/dashboard-routes.js +2 -26
- package/lib/ui-routes/reference-routes.js +198 -0
- package/lib/ui-routes/route-helpers.js +39 -0
- package/lib/utils/is-email.js +34 -0
- package/lib/utils/to-camel-case.js +17 -0
- package/lib/webhook-egress.js +37 -0
- package/lib/webhooks.js +1 -1
- package/package.json +27 -31
- package/prepare-bundle-deps.sh +28 -0
- package/release-please-config.json +11 -0
- package/sbom.json +1 -1
- package/server.js +11 -3
- package/static/css/flyonui.css +1 -1
- package/static/css/public.css +3 -0
- package/static/css/src/app.tailwind.css +113 -0
- package/static/js/app.js +69 -0
- package/static/js/ee-client.js +238 -8
- package/static/js/reference.js +1021 -0
- package/static/js/ui.js +61 -33
- package/static/licenses.html +658 -1560
- package/translations/README.md +64 -0
- package/translations/de.mo +0 -0
- package/translations/de.po +164 -120
- package/translations/en.po +200 -161
- package/translations/et.mo +0 -0
- package/translations/et.po +163 -120
- package/translations/fr.mo +0 -0
- package/translations/fr.po +163 -120
- package/translations/ja.mo +0 -0
- package/translations/ja.po +163 -120
- package/translations/messages.pot +84 -54
- package/translations/nl.mo +0 -0
- package/translations/nl.po +164 -120
- package/translations/pl.mo +0 -0
- package/translations/pl.po +163 -120
- package/views/account-identity-error.hbs +34 -0
- package/views/accounts/edit.hbs +1 -0
- package/views/accounts/register/imap.hbs +12 -2
- 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 +43 -8
- package/views/oauth-scope-error.hbs +1 -1
- 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 +120 -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 +49 -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 +194 -302
- package/workers/imap.js +1 -1
- package/workers/submit.js +1 -1
- package/workers/webhooks.js +2 -13
- package/views/swagger/index.hbs +0 -82
|
@@ -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
|
+
};
|