sendproof-core 0.1.0.alpha.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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/lib/sendproof/assets.rb +70 -0
- data/lib/sendproof/attribution.rb +98 -0
- data/lib/sendproof/capture.rb +66 -0
- data/lib/sendproof/clock.rb +54 -0
- data/lib/sendproof/configuration.rb +241 -0
- data/lib/sendproof/data_store.rb +34 -0
- data/lib/sendproof/dynamodb/store.rb +183 -0
- data/lib/sendproof/dynamodb.rb +17 -0
- data/lib/sendproof/elasticsearch/store.rb +24 -0
- data/lib/sendproof/elasticsearch.rb +17 -0
- data/lib/sendproof/firestore/interceptor.rb +38 -0
- data/lib/sendproof/firestore/store.rb +150 -0
- data/lib/sendproof/firestore.rb +99 -0
- data/lib/sendproof/handler.rb +23 -0
- data/lib/sendproof/identity.rb +37 -0
- data/lib/sendproof/limits.rb +113 -0
- data/lib/sendproof/middleware.rb +68 -0
- data/lib/sendproof/mongo/engine.rb +692 -0
- data/lib/sendproof/mongo/guarded.rb +100 -0
- data/lib/sendproof/mongo/plain.rb +45 -0
- data/lib/sendproof/mongo/store.rb +54 -0
- data/lib/sendproof/mongo/subscriber.rb +21 -0
- data/lib/sendproof/mongo.rb +41 -0
- data/lib/sendproof/mongoid/store.rb +58 -0
- data/lib/sendproof/mongoid.rb +65 -0
- data/lib/sendproof/observed_request.rb +108 -0
- data/lib/sendproof/opensearch/store.rb +21 -0
- data/lib/sendproof/opensearch.rb +17 -0
- data/lib/sendproof/operation_matcher.rb +28 -0
- data/lib/sendproof/pool_hooks.rb +80 -0
- data/lib/sendproof/preview_request.rb +44 -0
- data/lib/sendproof/preview_transport.rb +331 -0
- data/lib/sendproof/protocol.rb +81 -0
- data/lib/sendproof/provider.rb +83 -0
- data/lib/sendproof/recorder.rb +298 -0
- data/lib/sendproof/redaction.rb +98 -0
- data/lib/sendproof/redis/commands.rb +76 -0
- data/lib/sendproof/redis/store.rb +205 -0
- data/lib/sendproof/redis.rb +18 -0
- data/lib/sendproof/search/middleware.rb +39 -0
- data/lib/sendproof/search/store.rb +213 -0
- data/lib/sendproof/search.rb +82 -0
- data/lib/sendproof/stores/classify.rb +346 -0
- data/lib/sendproof/stores/guard.rb +127 -0
- data/lib/sendproof/stores/tables.rb +1328 -0
- data/lib/sendproof/stores.rb +12 -0
- data/lib/sendproof/trace_store.rb +110 -0
- data/lib/sendproof/transaction_control.rb +187 -0
- data/lib/sendproof/ui/action-bar.js +91 -0
- data/lib/sendproof/ui/api.js +239 -0
- data/lib/sendproof/ui/app.css +848 -0
- data/lib/sendproof/ui/app.js +742 -0
- data/lib/sendproof/ui/autofill.js +137 -0
- data/lib/sendproof/ui/code-copy.js +71 -0
- data/lib/sendproof/ui/code-picker.js +149 -0
- data/lib/sendproof/ui/commands.js +197 -0
- data/lib/sendproof/ui/components.js +77 -0
- data/lib/sendproof/ui/context-bar.js +82 -0
- data/lib/sendproof/ui/copy.js +92 -0
- data/lib/sendproof/ui/coverage-popover.js +35 -0
- data/lib/sendproof/ui/crumb-fit.js +80 -0
- data/lib/sendproof/ui/deprecation.js +164 -0
- data/lib/sendproof/ui/display-settings.js +175 -0
- data/lib/sendproof/ui/doc-diff.js +209 -0
- data/lib/sendproof/ui/docs-view.js +347 -0
- data/lib/sendproof/ui/favicon.png +0 -0
- data/lib/sendproof/ui/favicon.svg +1 -0
- data/lib/sendproof/ui/field-help.js +52 -0
- data/lib/sendproof/ui/field-modules.js +23 -0
- data/lib/sendproof/ui/fields/controls.js +179 -0
- data/lib/sendproof/ui/fields/dom.js +36 -0
- data/lib/sendproof/ui/fields/infer.js +71 -0
- data/lib/sendproof/ui/fields/lookup-source.js +63 -0
- data/lib/sendproof/ui/fields/lookup.js +285 -0
- data/lib/sendproof/ui/fields/render.js +496 -0
- data/lib/sendproof/ui/fields/select.js +175 -0
- data/lib/sendproof/ui/fields/validate.js +363 -0
- data/lib/sendproof/ui/fields/validation-formats.js +164 -0
- data/lib/sendproof/ui/fields/validation-references.js +68 -0
- data/lib/sendproof/ui/fields/validation-values.js +137 -0
- data/lib/sendproof/ui/generate-menu.js +44 -0
- data/lib/sendproof/ui/generate.js +184 -0
- data/lib/sendproof/ui/header-editor.js +777 -0
- data/lib/sendproof/ui/headers.js +188 -0
- data/lib/sendproof/ui/highlight.js +257 -0
- data/lib/sendproof/ui/history-popover.js +24 -0
- data/lib/sendproof/ui/i18n.js +253 -0
- data/lib/sendproof/ui/icons.js +87 -0
- data/lib/sendproof/ui/index.html +70 -0
- data/lib/sendproof/ui/issues.js +187 -0
- data/lib/sendproof/ui/json-view.js +166 -0
- data/lib/sendproof/ui/lang-icons.js +62 -0
- data/lib/sendproof/ui/layout.js +262 -0
- data/lib/sendproof/ui/listbox.js +334 -0
- data/lib/sendproof/ui/locales/ar-capture.js +366 -0
- data/lib/sendproof/ui/locales/ar-stores.js +116 -0
- data/lib/sendproof/ui/locales/ar.js +561 -0
- data/lib/sendproof/ui/locales/cs-capture.js +442 -0
- data/lib/sendproof/ui/locales/cs-stores.js +116 -0
- data/lib/sendproof/ui/locales/cs.js +689 -0
- data/lib/sendproof/ui/locales/da-capture.js +347 -0
- data/lib/sendproof/ui/locales/da-stores.js +116 -0
- data/lib/sendproof/ui/locales/da.js +522 -0
- data/lib/sendproof/ui/locales/de-capture.js +347 -0
- data/lib/sendproof/ui/locales/de-stores.js +116 -0
- data/lib/sendproof/ui/locales/de.js +519 -0
- data/lib/sendproof/ui/locales/el-capture.js +347 -0
- data/lib/sendproof/ui/locales/el-stores.js +116 -0
- data/lib/sendproof/ui/locales/el.js +520 -0
- data/lib/sendproof/ui/locales/en-capture.js +349 -0
- data/lib/sendproof/ui/locales/en-stores.js +95 -0
- data/lib/sendproof/ui/locales/en.js +522 -0
- data/lib/sendproof/ui/locales/es-capture.js +353 -0
- data/lib/sendproof/ui/locales/es-stores.js +116 -0
- data/lib/sendproof/ui/locales/es.js +525 -0
- data/lib/sendproof/ui/locales/fa-capture.js +348 -0
- data/lib/sendproof/ui/locales/fa-stores.js +116 -0
- data/lib/sendproof/ui/locales/fa.js +521 -0
- data/lib/sendproof/ui/locales/fi-capture.js +347 -0
- data/lib/sendproof/ui/locales/fi-stores.js +116 -0
- data/lib/sendproof/ui/locales/fi.js +523 -0
- data/lib/sendproof/ui/locales/fr-capture.js +353 -0
- data/lib/sendproof/ui/locales/fr-stores.js +116 -0
- data/lib/sendproof/ui/locales/fr.js +525 -0
- data/lib/sendproof/ui/locales/he-capture.js +358 -0
- data/lib/sendproof/ui/locales/he-stores.js +116 -0
- data/lib/sendproof/ui/locales/he.js +529 -0
- data/lib/sendproof/ui/locales/hi-capture.js +348 -0
- data/lib/sendproof/ui/locales/hi-stores.js +116 -0
- data/lib/sendproof/ui/locales/hi.js +523 -0
- data/lib/sendproof/ui/locales/id-capture.js +347 -0
- data/lib/sendproof/ui/locales/id-stores.js +116 -0
- data/lib/sendproof/ui/locales/id.js +521 -0
- data/lib/sendproof/ui/locales/it-capture.js +361 -0
- data/lib/sendproof/ui/locales/it-stores.js +116 -0
- data/lib/sendproof/ui/locales/it.js +523 -0
- data/lib/sendproof/ui/locales/ja-capture.js +346 -0
- data/lib/sendproof/ui/locales/ja-stores.js +116 -0
- data/lib/sendproof/ui/locales/ja.js +521 -0
- data/lib/sendproof/ui/locales/ko-capture.js +346 -0
- data/lib/sendproof/ui/locales/ko-stores.js +116 -0
- data/lib/sendproof/ui/locales/ko.js +522 -0
- data/lib/sendproof/ui/locales/nb-capture.js +349 -0
- data/lib/sendproof/ui/locales/nb-stores.js +116 -0
- data/lib/sendproof/ui/locales/nb.js +521 -0
- data/lib/sendproof/ui/locales/nl-capture.js +355 -0
- data/lib/sendproof/ui/locales/nl-stores.js +116 -0
- data/lib/sendproof/ui/locales/nl.js +520 -0
- data/lib/sendproof/ui/locales/pl-capture.js +432 -0
- data/lib/sendproof/ui/locales/pl-stores.js +116 -0
- data/lib/sendproof/ui/locales/pl.js +528 -0
- data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-BR.js +523 -0
- data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-PT.js +523 -0
- data/lib/sendproof/ui/locales/ru-capture.js +437 -0
- data/lib/sendproof/ui/locales/ru-stores.js +116 -0
- data/lib/sendproof/ui/locales/ru.js +624 -0
- data/lib/sendproof/ui/locales/sv-capture.js +349 -0
- data/lib/sendproof/ui/locales/sv-stores.js +116 -0
- data/lib/sendproof/ui/locales/sv.js +518 -0
- data/lib/sendproof/ui/locales/th-capture.js +347 -0
- data/lib/sendproof/ui/locales/th-stores.js +116 -0
- data/lib/sendproof/ui/locales/th.js +521 -0
- data/lib/sendproof/ui/locales/tr-capture.js +348 -0
- data/lib/sendproof/ui/locales/tr-stores.js +116 -0
- data/lib/sendproof/ui/locales/tr.js +521 -0
- data/lib/sendproof/ui/locales/uk-capture.js +442 -0
- data/lib/sendproof/ui/locales/uk-stores.js +116 -0
- data/lib/sendproof/ui/locales/uk.js +630 -0
- data/lib/sendproof/ui/locales/vi-capture.js +346 -0
- data/lib/sendproof/ui/locales/vi-stores.js +116 -0
- data/lib/sendproof/ui/locales/vi.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
- data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
- data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
- data/lib/sendproof/ui/manifest.json +866 -0
- data/lib/sendproof/ui/markdown.js +39 -0
- data/lib/sendproof/ui/model-view.js +182 -0
- data/lib/sendproof/ui/navigation.js +233 -0
- data/lib/sendproof/ui/operation-header.js +76 -0
- data/lib/sendproof/ui/operations.js +7 -0
- data/lib/sendproof/ui/overview-view.js +63 -0
- data/lib/sendproof/ui/personas.js +59 -0
- data/lib/sendproof/ui/popover.js +71 -0
- data/lib/sendproof/ui/preview-false.js +108 -0
- data/lib/sendproof/ui/request-editor.js +496 -0
- data/lib/sendproof/ui/response-metadata.js +354 -0
- data/lib/sendproof/ui/response-viewer.js +618 -0
- data/lib/sendproof/ui/result-blocked.js +155 -0
- data/lib/sendproof/ui/result-capture.js +25 -0
- data/lib/sendproof/ui/result-database.js +185 -0
- data/lib/sendproof/ui/result-effects.js +176 -0
- data/lib/sendproof/ui/result-headers.js +106 -0
- data/lib/sendproof/ui/result-logs.js +408 -0
- data/lib/sendproof/ui/result-states.js +213 -0
- data/lib/sendproof/ui/results.js +371 -0
- data/lib/sendproof/ui/router.js +118 -0
- data/lib/sendproof/ui/schema-model.js +336 -0
- data/lib/sendproof/ui/schema-tree.js +344 -0
- data/lib/sendproof/ui/schema.js +397 -0
- data/lib/sendproof/ui/security.js +31 -0
- data/lib/sendproof/ui/seeded-menu.js +69 -0
- data/lib/sendproof/ui/settings.js +93 -0
- data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
- data/lib/sendproof/ui/shortcuts.js +190 -0
- data/lib/sendproof/ui/sidebar.js +289 -0
- data/lib/sendproof/ui/signin.js +259 -0
- data/lib/sendproof/ui/snippets.js +530 -0
- data/lib/sendproof/ui/store-marks.js +47 -0
- data/lib/sendproof/ui/stores.js +131 -0
- data/lib/sendproof/ui/styles.css +971 -0
- data/lib/sendproof/ui/toggletip.js +53 -0
- data/lib/sendproof/ui/uuid.js +10 -0
- data/lib/sendproof/ui/view.js +122 -0
- data/lib/sendproof/ui/views.css +653 -0
- data/lib/sendproof/version.rb +9 -0
- data/lib/sendproof/workbench.rb +102 -0
- data/lib/sendproof-core.rb +3 -0
- data/lib/sendproof.rb +121 -0
- metadata +292 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { operations as discoverOperations } from './api.js';
|
|
2
|
+
import { validateSchema } from './fields/validate.js';
|
|
3
|
+
import { createReferenceResolver } from './fields/validation-references.js';
|
|
4
|
+
import { t } from './i18n.js';
|
|
5
|
+
|
|
6
|
+
// Server-Timing metrics the workbench names itself: [label key, colour]. Any other metric
|
|
7
|
+
// keeps the name the API sent.
|
|
8
|
+
const knownTimings = new Map([
|
|
9
|
+
['auth', ['resp.timing.auth', 'var(--text-3)']],
|
|
10
|
+
['handler', ['resp.timing.handler', 'var(--accent)']],
|
|
11
|
+
['db', ['resp.timing.db', 'var(--ok)']],
|
|
12
|
+
['serialize', ['resp.timing.serialize', 'var(--warn)']],
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
function header(headers, name) {
|
|
16
|
+
if (typeof headers?.get === 'function') return headers.get(name) ?? undefined;
|
|
17
|
+
return Object.entries(headers ?? {}).find(([key]) => key.toLowerCase() === name.toLowerCase())?.[1];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function timingMetric(name, duration, description) {
|
|
21
|
+
const known = knownTimings.get(name.toLowerCase());
|
|
22
|
+
const [label, color] = known ? [t(known[0]), known[1]] : [name, 'var(--text-3)'];
|
|
23
|
+
return { label, color, duration, description };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Split HTTP list fields without treating punctuation in a quoted description as a separator. */
|
|
27
|
+
function splitTiming(value, separator) {
|
|
28
|
+
const parts = [];
|
|
29
|
+
let start = 0;
|
|
30
|
+
let quoted = false;
|
|
31
|
+
for (let index = 0; index < value.length; index++) {
|
|
32
|
+
const char = value[index];
|
|
33
|
+
if (quoted && char === '\\') index++;
|
|
34
|
+
else if (char === '"') quoted = !quoted;
|
|
35
|
+
else if (!quoted && char === separator) {
|
|
36
|
+
parts.push(value.slice(start, index));
|
|
37
|
+
start = index + 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
parts.push(value.slice(start));
|
|
41
|
+
return parts;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Prefer measured Server-Timing metrics, falling back to the preview or trace durations. */
|
|
45
|
+
export function responseTimings(result) {
|
|
46
|
+
const fields = splitTiming(result.serverTiming ?? header(result.headers, 'Server-Timing') ?? '', ',');
|
|
47
|
+
const timings = fields.flatMap(field => {
|
|
48
|
+
const [name, ...parameters] = splitTiming(field, ';').map(part => part.trim());
|
|
49
|
+
if (!name || !/^[!#$%&'*+.^_`|~\w-]+$/.test(name)) return [];
|
|
50
|
+
const values = new Map();
|
|
51
|
+
for (const parameter of parameters) {
|
|
52
|
+
const match = /^([^=\s]+)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s"]+))$/.exec(parameter);
|
|
53
|
+
if (!match || values.has(match[1].toLowerCase())) continue;
|
|
54
|
+
values.set(match[1].toLowerCase(), match[2]?.replace(/\\(.)/g, '$1') ?? match[3]);
|
|
55
|
+
}
|
|
56
|
+
const duration = values.get('dur');
|
|
57
|
+
if (!/^(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(duration ?? '') || !Number.isFinite(Number(duration))) return [];
|
|
58
|
+
return [timingMetric(name, Number(duration), values.get('desc'))];
|
|
59
|
+
});
|
|
60
|
+
if (timings.length) return timings;
|
|
61
|
+
const trace = result.timing;
|
|
62
|
+
return [...knownTimings.keys()].flatMap(name => {
|
|
63
|
+
const duration = trace?.[name];
|
|
64
|
+
const count = trace?.queryCount;
|
|
65
|
+
const description = name === 'db' && Number.isInteger(count) && count >= 0
|
|
66
|
+
? t('resp.queries', { n: count }) : undefined;
|
|
67
|
+
return Number.isFinite(duration) && duration >= 0 ? [timingMetric(name, duration, description)] : [];
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function resolveComponent(value, document) {
|
|
72
|
+
const resolve = createReferenceResolver(document);
|
|
73
|
+
const visited = new Set();
|
|
74
|
+
while (value?.$ref) {
|
|
75
|
+
if (visited.has(value)) return;
|
|
76
|
+
visited.add(value);
|
|
77
|
+
value = resolve(value.$ref);
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Select the response documented for the wire status and content type. */
|
|
83
|
+
export function responseDefinition(result, { document, operation }) {
|
|
84
|
+
const responses = operation?.operation?.responses;
|
|
85
|
+
const status = String(result.status ?? '');
|
|
86
|
+
return resolveComponent(responses?.[status] ?? responses?.[status[0] + 'XX'] ?? responses?.default, document);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The response's media type without parameters, lowercased; empty when none was sent. */
|
|
90
|
+
export function responseMediaType(result) {
|
|
91
|
+
return header(result.headers, 'Content-Type')?.split(';')[0].trim().toLowerCase() ?? '';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Whether the body matches its documented schema, as { name, matches }; undefined when it can't be told. */
|
|
95
|
+
export function responseSchema(result, context, definition, body) {
|
|
96
|
+
const content = definition?.content;
|
|
97
|
+
if (!content || body === undefined) return;
|
|
98
|
+
const type = responseMediaType(result);
|
|
99
|
+
const available = Object.keys(content);
|
|
100
|
+
const candidate = type
|
|
101
|
+
? [type, type.split('/')[0] + '/*', '*/*'].find(key => available.some(media => media.toLowerCase() === key))
|
|
102
|
+
: available.length === 1 ? available[0].toLowerCase() : undefined;
|
|
103
|
+
const schema = content[available.find(media => media.toLowerCase() === candidate)]?.schema;
|
|
104
|
+
if (schema === undefined) return;
|
|
105
|
+
|
|
106
|
+
const resolve = createReferenceResolver(context.document, schema);
|
|
107
|
+
const target = schema?.$ref ? resolve(schema.$ref) : schema;
|
|
108
|
+
if (target === undefined) return;
|
|
109
|
+
const unresolvedReferences = new Set();
|
|
110
|
+
const errors = validateSchema({ document: context.document, parseErrors: new Map(), response: true, unresolvedReferences }, schema, body, true);
|
|
111
|
+
if (unresolvedReferences.size) return;
|
|
112
|
+
const name = schema?.title || target?.title || schema?.$ref?.split('/').at(-1)?.replace(/~1/g, '/').replace(/~0/g, '~') || t('resp.schemaFallback');
|
|
113
|
+
const extra = undocumented(body, target, resolve);
|
|
114
|
+
// A required property the body left out reads as a fact about the response, not a form prompt.
|
|
115
|
+
const missing = path => { const parts = String(path).split('.'); const parent = parts.slice(0, -1).reduce((node, key) => node?.[key], body); return parts.length && parent && typeof parent === 'object' && !Object.hasOwn(parent, parts.at(-1)); };
|
|
116
|
+
const issues = [...errors.map(({ path, message }) => ({ path, message: path && missing(path) ? t('match.missing') : message })), ...extra.map(item => ({ path: item.name, message: t('match.extra', { model: name }), extra: true }))];
|
|
117
|
+
return {
|
|
118
|
+
name, matches: issues.length === 0, raw: schema, issues, extra, properties: countProperties(target, resolve),
|
|
119
|
+
ref: schema?.$ref?.startsWith('#/components/schemas/') ? decodeURIComponent(schema.$ref.split('/').at(-1)) : '',
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Properties a body returned at its top level that the (object) schema doesn't declare. */
|
|
124
|
+
function undocumented(body, schema, resolve) {
|
|
125
|
+
if (!body || typeof body !== 'object' || Array.isArray(body) || !schema?.properties || schema.additionalProperties === true) return [];
|
|
126
|
+
const declared = new Set(Object.keys(schema.properties));
|
|
127
|
+
for (const part of schema.allOf || []) for (const key of Object.keys((part.$ref ? resolve(part.$ref) : part)?.properties || {})) declared.add(key);
|
|
128
|
+
return Object.keys(body).filter(key => !declared.has(key)).map(name => ({ name, value: body[name] }));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** How many properties a schema documents, counting one level into objects and list items. */
|
|
132
|
+
function countProperties(schema, resolve, depth = 0) {
|
|
133
|
+
const target = schema?.$ref ? resolve(schema.$ref) : schema;
|
|
134
|
+
if (!target || depth > 2) return 0;
|
|
135
|
+
const props = Object.values(target.properties || {});
|
|
136
|
+
return props.length + props.reduce((sum, child) => {
|
|
137
|
+
const inner = child?.$ref ? resolve(child.$ref) : child;
|
|
138
|
+
const items = inner?.items?.$ref ? resolve(inner.items.$ref) : inner?.items;
|
|
139
|
+
return sum + (inner?.properties ? countProperties(inner, resolve, depth + 1) : items?.properties ? countProperties(items, resolve, depth + 1) : 0);
|
|
140
|
+
}, 0);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function pointerPath(pointer) {
|
|
144
|
+
if (pointer === '') return [];
|
|
145
|
+
if (!pointer.startsWith('/') || /~(?![01])/.test(pointer)) return;
|
|
146
|
+
return pointer.slice(1).split('/').map(part => part.replace(/~1/g, '/').replace(/~0/g, '~'));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function pointerValue(value, path) {
|
|
150
|
+
return path?.reduce((item, key) => item !== null && typeof item === 'object' && Object.hasOwn(item, key) ? item[key] : undefined, value);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function parseUrl(value, base) {
|
|
154
|
+
try { return value ? new URL(value, base) : undefined; }
|
|
155
|
+
catch { return undefined; }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const pathParts = path => path.replace(/\/$/, '').split('/').slice(1);
|
|
159
|
+
const pathKey = part => /^\{([^{}]+)\}$/.exec(part)?.[1];
|
|
160
|
+
const supplied = value => value !== undefined && value !== null && value !== '';
|
|
161
|
+
const scalar = value => supplied(value) && ['string', 'number', 'boolean'].includes(typeof value);
|
|
162
|
+
const pathNames = path => [...path.matchAll(/\{([^{}]+)\}/g)].map(match => match[1]);
|
|
163
|
+
|
|
164
|
+
function matchPath(operation, path) {
|
|
165
|
+
if (!path) return;
|
|
166
|
+
const template = pathParts(operation.path);
|
|
167
|
+
const actual = pathParts(path);
|
|
168
|
+
if (template.length !== actual.length) return;
|
|
169
|
+
const values = Object.create(null);
|
|
170
|
+
for (const [index, part] of template.entries()) {
|
|
171
|
+
const names = pathNames(part);
|
|
172
|
+
try {
|
|
173
|
+
if (!names.length) {
|
|
174
|
+
if (decodeURIComponent(part) !== decodeURIComponent(actual[index])) return;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const pattern = part.split(/\{[^{}]+\}/).map(literal => literal.replace(/[.*+?^$(){}|[\]\\]/g, '\\$&')).join('(.+?)');
|
|
178
|
+
const matched = new RegExp('^' + pattern + '$').exec(actual[index]);
|
|
179
|
+
if (!matched) return;
|
|
180
|
+
names.forEach((name, i) => { values[name] = decodeURIComponent(matched[i + 1]); });
|
|
181
|
+
} catch {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return values;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function operationPath(url, basePath = '/') {
|
|
189
|
+
const mount = basePath.endsWith('/') ? basePath : basePath + '/';
|
|
190
|
+
return url?.pathname.startsWith(mount) ? '/' + url.pathname.slice(mount.length) : undefined;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function runtimeExpression(expression, result, context, body, requestUrl, requestPath) {
|
|
194
|
+
if (expression === '$url') return { value: result.requestUrl };
|
|
195
|
+
if (expression === '$method') return { value: context.operation.method };
|
|
196
|
+
if (expression === '$statusCode') return { value: result.status === undefined ? undefined : String(result.status) };
|
|
197
|
+
const responseBody = /^\$response\.body(?:#(.*))?$/.exec(expression);
|
|
198
|
+
if (responseBody) {
|
|
199
|
+
const path = pointerPath(responseBody[1] ?? '');
|
|
200
|
+
return { value: pointerValue(body, path), path };
|
|
201
|
+
}
|
|
202
|
+
if (expression.startsWith('$response.header.')) {
|
|
203
|
+
return { value: header(result.headers, expression.slice('$response.header.'.length)) };
|
|
204
|
+
}
|
|
205
|
+
const requestBody = /^\$request\.body(?:#(.*))?$/.exec(expression);
|
|
206
|
+
if (requestBody && typeof result.wireRequest?.body === 'string') {
|
|
207
|
+
const raw = result.wireRequest.body;
|
|
208
|
+
const type = header(result.requestHeaders, 'Content-Type')?.split(';')[0].trim().toLowerCase() ?? '';
|
|
209
|
+
const path = pointerPath(requestBody[1] ?? '');
|
|
210
|
+
if (!(type.endsWith('/json') || type.endsWith('+json'))) return { value: pointerValue(raw, path) };
|
|
211
|
+
try { return { value: pointerValue(JSON.parse(raw), path) }; }
|
|
212
|
+
catch { return { value: undefined }; }
|
|
213
|
+
}
|
|
214
|
+
const request = /^\$request\.(path|query|header)\.(.+)$/.exec(expression);
|
|
215
|
+
if (request && context.operation.parameters?.some(parameter => parameter.in === request[1] &&
|
|
216
|
+
(request[1] === 'header' ? parameter.name.toLowerCase() === request[2].toLowerCase() : parameter.name === request[2]))) {
|
|
217
|
+
const [, location, name] = request;
|
|
218
|
+
if (location === 'path') return { value: requestPath?.[name] };
|
|
219
|
+
if (location === 'header') return { value: header(result.requestHeaders, name) };
|
|
220
|
+
const values = requestUrl?.searchParams.getAll(name) ?? [];
|
|
221
|
+
return { value: values.length > 1 ? values : values[0] };
|
|
222
|
+
}
|
|
223
|
+
return { value: undefined };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function resolveParameter(value, result, context, body, requestUrl, requestPath) {
|
|
227
|
+
if (typeof value !== 'string') return { value };
|
|
228
|
+
const evaluate = expression => runtimeExpression(expression, result, context, body, requestUrl, requestPath);
|
|
229
|
+
if (/^\$(?:url$|method$|statusCode$|request\.|response\.)/.test(value)) return evaluate(value);
|
|
230
|
+
const paths = [];
|
|
231
|
+
let complete = true;
|
|
232
|
+
const interpolated = value.replace(/\{(\$[^{}]+)\}/g, (_, expression) => {
|
|
233
|
+
const { value, path } = evaluate(expression);
|
|
234
|
+
if (value === undefined) complete = false;
|
|
235
|
+
if (path) paths.push(path);
|
|
236
|
+
return value;
|
|
237
|
+
});
|
|
238
|
+
return { value: complete ? interpolated : undefined, path: paths.length === 1 ? paths[0] : undefined };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function linkOperation(link, operations, document) {
|
|
242
|
+
if (typeof link?.operationRef === 'string' && link.operationRef.startsWith('#/')) {
|
|
243
|
+
const target = createReferenceResolver(document)(link.operationRef);
|
|
244
|
+
// A Path Item can be a reference, so the discovered path and method are also checked.
|
|
245
|
+
let parts;
|
|
246
|
+
try { parts = pointerPath(decodeURIComponent(link.operationRef.slice(1))); }
|
|
247
|
+
catch { return; }
|
|
248
|
+
if (parts?.length === 3 && parts[0] === 'paths') {
|
|
249
|
+
return operations.find(operation => operation.path === parts[1] && operation.method.toLowerCase() === parts[2]);
|
|
250
|
+
}
|
|
251
|
+
return operations.find(operation => operation.operation === target);
|
|
252
|
+
}
|
|
253
|
+
if (link?.operationId) return operations.find(operation => operation.operation?.operationId === link.operationId);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function findParameter(operation, key) {
|
|
257
|
+
const parameters = operation.parameters ?? [];
|
|
258
|
+
const qualified = /^(path|query|header|cookie)[.:](.+)$/.exec(key);
|
|
259
|
+
if (qualified) return parameters.find(parameter => parameter.in === qualified[1] && parameter.name === qualified[2]);
|
|
260
|
+
const matches = parameters.filter(parameter => parameter.name === key);
|
|
261
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function missingPathParams(operation, values) {
|
|
265
|
+
return pathNames(operation.path).filter(key => !supplied(values['path:' + key]));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function documentedFollowUps(definition, result, context, operations, body, requestUrl, requestPath) {
|
|
269
|
+
return Object.entries(definition?.links ?? {}).flatMap(([name, raw]) => {
|
|
270
|
+
const link = resolveComponent(raw, context.document);
|
|
271
|
+
const operation = linkOperation(link, operations, context.document);
|
|
272
|
+
if (!operation) return [];
|
|
273
|
+
const values = {};
|
|
274
|
+
const paths = [];
|
|
275
|
+
for (const [key, expression] of Object.entries(link.parameters ?? {})) {
|
|
276
|
+
const parameter = findParameter(operation, key);
|
|
277
|
+
if (!parameter || parameter.in === 'cookie') continue;
|
|
278
|
+
const { value, path } = resolveParameter(expression, result, context, body, requestUrl, requestPath);
|
|
279
|
+
if (!supplied(value)) continue;
|
|
280
|
+
values[parameter.in + ':' + parameter.name] = value;
|
|
281
|
+
if (path) paths.push(path);
|
|
282
|
+
}
|
|
283
|
+
return missingPathParams(operation, values).length ? [] : [{ name, operation, values, paths }];
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function isRelated(source, target) {
|
|
288
|
+
const from = pathParts(source.path);
|
|
289
|
+
const to = pathParts(target.path);
|
|
290
|
+
return to.length >= from.length && from.every((part, index) =>
|
|
291
|
+
pathKey(part) ? Boolean(pathKey(to[index])) : part === to[index]
|
|
292
|
+
) && (to.length === from.length || Boolean(pathKey(to[from.length])));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function inferredFollowUp(operation, source, body, requestPath, locationPath, locationUrl) {
|
|
296
|
+
if (operation.path === source.path && operation.method === source.method) return;
|
|
297
|
+
const fromLocation = matchPath(operation, locationPath);
|
|
298
|
+
if (!fromLocation && !isRelated(source, operation)) return;
|
|
299
|
+
const targetParts = pathParts(operation.path);
|
|
300
|
+
const sourceParts = pathParts(source.path);
|
|
301
|
+
const values = {};
|
|
302
|
+
const paths = [];
|
|
303
|
+
const pending = [];
|
|
304
|
+
for (const [index, part] of targetParts.entries()) {
|
|
305
|
+
for (const key of pathNames(part)) {
|
|
306
|
+
const exact = pointerValue(body, [key]);
|
|
307
|
+
const carried = requestPath?.[pathKey(sourceParts[index]) ?? ''] ?? requestPath?.[key];
|
|
308
|
+
const value = fromLocation?.[key] ?? carried ?? exact;
|
|
309
|
+
if (scalar(value)) {
|
|
310
|
+
values['path:' + key] = value;
|
|
311
|
+
if (scalar(exact) && String(value) === String(exact)) paths.push([key]);
|
|
312
|
+
else if (fromLocation && key !== 'id' && String(value) === String(pointerValue(body, ['id']))) paths.push(['id']);
|
|
313
|
+
} else {
|
|
314
|
+
pending.push(key);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
const id = pointerValue(body, ['id']);
|
|
319
|
+
if (pending.length === 1 && scalar(id) && !paths.some(path => path[0] === 'id')) {
|
|
320
|
+
values['path:' + pending.pop()] = id;
|
|
321
|
+
paths.push(['id']);
|
|
322
|
+
}
|
|
323
|
+
if (fromLocation) {
|
|
324
|
+
for (const { in: where, name } of operation.parameters ?? []) {
|
|
325
|
+
if (where !== 'query') continue;
|
|
326
|
+
const entries = locationUrl.searchParams.getAll(name);
|
|
327
|
+
if (entries.length) values['query:' + name] = entries.length === 1 ? entries[0] : entries;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (pending.length || (!paths.length && !Object.keys(values).length && !fromLocation)) return;
|
|
331
|
+
return { operation, values, paths };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Open documented operations with values from this result or its original URL. */
|
|
335
|
+
export function responseFollowUps(result, context, definition, body) {
|
|
336
|
+
const source = context.operation;
|
|
337
|
+
if (!source || typeof context.onFollowUp !== 'function') return [];
|
|
338
|
+
const operations = context.operations ?? discoverOperations(context.document ?? {});
|
|
339
|
+
const requestUrl = parseUrl(result.requestUrl, globalThis.location?.origin);
|
|
340
|
+
const requestPath = matchPath(source, operationPath(requestUrl, context.basePath));
|
|
341
|
+
const explicit = documentedFollowUps(definition, result, context, operations, body, requestUrl, requestPath);
|
|
342
|
+
if (!(result.status >= 200 && result.status < 300)) return explicit;
|
|
343
|
+
|
|
344
|
+
const origin = requestUrl?.origin ?? globalThis.location?.origin;
|
|
345
|
+
const location = parseUrl(header(result.headers, 'Location'), requestUrl ?? origin);
|
|
346
|
+
const locationPath = origin && location?.origin === origin ? operationPath(location, context.basePath) : undefined;
|
|
347
|
+
const handled = new Set(explicit.map(item => item.operation));
|
|
348
|
+
const inferred = operations.flatMap(operation => {
|
|
349
|
+
if (handled.has(operation)) return [];
|
|
350
|
+
const item = inferredFollowUp(operation, source, body, requestPath, locationPath, location);
|
|
351
|
+
return item ? [item] : [];
|
|
352
|
+
});
|
|
353
|
+
return [...explicit, ...inferred];
|
|
354
|
+
}
|