@panomc/sdk 1.0.0-dev.9 → 2.0.0-dev.2
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/api/routes.core.json +3702 -0
- package/bin/api/__tests__/api-compat.test.mjs +97 -0
- package/bin/api/__tests__/check-paths.test.mjs +113 -0
- package/bin/api/__tests__/cli.test.mjs +44 -0
- package/bin/api/__tests__/extract-routes.test.mjs +132 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/api/paths.old-new.json +82 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
- package/bin/api/__tests__/fixtures/plugin-new/api/paths.old-new.json +66 -0
- package/bin/api/__tests__/fixtures/plugin-new/build.gradle.kts +42 -0
- package/bin/api/__tests__/fixtures/plugin-new/gradle.properties +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/panel/Things.js +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/theme/Logo.svelte +9 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/theme/api.js +29 -0
- package/bin/api/__tests__/fixtures/plugin-old/build.gradle.kts +22 -0
- package/bin/api/__tests__/fixtures/plugin-old/gradle.properties +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/panel/Things.js +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/theme/Logo.svelte +9 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/theme/api.js +29 -0
- package/bin/api/__tests__/fixtures/refusals/gradle.properties +1 -0
- package/bin/api/__tests__/fixtures/refusals/src/main/kotlin/x/Bad.kt +39 -0
- package/bin/api/__tests__/fixtures/unresolved/src/main/kotlin/x/Orphan.kt +19 -0
- package/bin/api/__tests__/helpers.mjs +48 -0
- package/bin/api/__tests__/migrate-v1.test.mjs +377 -0
- package/bin/api/__tests__/openapi-snapshot.test.mjs +120 -0
- package/bin/api/api-compat.mjs +204 -0
- package/bin/api/check-paths.mjs +135 -0
- package/bin/api/cli.mjs +83 -0
- package/bin/api/extract-routes.mjs +129 -0
- package/bin/api/js-scan.mjs +337 -0
- package/bin/api/kotlin.mjs +366 -0
- package/bin/api/migrate-v1.mjs +330 -0
- package/bin/api/openapi-snapshot.mjs +112 -0
- package/bin/api/project.mjs +172 -0
- package/bin/api/rename-table.mjs +66 -0
- package/bin/api/util.mjs +194 -0
- package/bin/pano-api.js +5 -0
- package/build/plugin-api-rollup.js +46 -0
- package/core/css/pano-tokens.css +225 -0
- package/core/favicon/apple-touch-icon.png +0 -0
- package/core/favicon/favicon-96x96.png +0 -0
- package/core/favicon/favicon.ico +0 -0
- package/core/favicon/favicon.svg +1 -0
- package/core/favicon/site.webmanifest +21 -0
- package/core/favicon/web-app-manifest-192x192.png +0 -0
- package/core/favicon/web-app-manifest-512x512.png +0 -0
- package/core/favicon_old/android-icon-144x144.png +0 -0
- package/core/favicon_old/android-icon-192x192.png +0 -0
- package/core/favicon_old/android-icon-36x36.png +0 -0
- package/core/favicon_old/android-icon-48x48.png +0 -0
- package/core/favicon_old/android-icon-72x72.png +0 -0
- package/core/favicon_old/android-icon-96x96.png +0 -0
- package/core/favicon_old/apple-icon-114x114.png +0 -0
- package/core/favicon_old/apple-icon-120x120.png +0 -0
- package/core/favicon_old/apple-icon-144x144.png +0 -0
- package/core/favicon_old/apple-icon-152x152.png +0 -0
- package/core/favicon_old/apple-icon-180x180.png +0 -0
- package/core/favicon_old/apple-icon-57x57.png +0 -0
- package/core/favicon_old/apple-icon-60x60.png +0 -0
- package/core/favicon_old/apple-icon-72x72.png +0 -0
- package/core/favicon_old/apple-icon-76x76.png +0 -0
- package/core/favicon_old/apple-icon-precomposed.png +0 -0
- package/core/favicon_old/apple-icon.png +0 -0
- package/core/favicon_old/browserconfig.xml +11 -0
- package/core/favicon_old/favicon-16x16.png +0 -0
- package/core/favicon_old/favicon-32x32.png +0 -0
- package/core/favicon_old/favicon-96x96.png +0 -0
- package/core/favicon_old/favicon.ico +0 -0
- package/core/favicon_old/manifest.json +41 -0
- package/core/favicon_old/ms-icon-144x144.png +0 -0
- package/core/favicon_old/ms-icon-150x150.png +0 -0
- package/core/favicon_old/ms-icon-310x310.png +0 -0
- package/core/favicon_old/ms-icon-70x70.png +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.svg +483 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.svg +486 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.svg +484 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.svg +486 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff2 +0 -0
- package/core/js/ControllerRegistry.js +421 -0
- package/core/js/NotificationManager.js +62 -0
- package/core/js/PluginAPI.js +39 -0
- package/core/js/PluginManager.js +851 -0
- package/core/js/RouteMatcher.js +142 -0
- package/core/js/__tests__/ControllerRegistry.test.js +378 -0
- package/core/js/__tests__/NotificationManager.test.js +57 -0
- package/core/js/__tests__/PluginAPI.test.js +25 -0
- package/core/js/__tests__/RouteMatcher.test.js +33 -0
- package/core/js/__tests__/accessClient.test.js +582 -0
- package/core/js/__tests__/api.util.v1.test.js +336 -0
- package/core/js/__tests__/appStores.test.js +47 -0
- package/core/js/__tests__/devBounce.test.js +28 -0
- package/core/js/__tests__/html.util.test.js +18 -0
- package/core/js/__tests__/pluginFolder.util.test.js +106 -0
- package/core/js/__tests__/pluginPackage.test.js +134 -0
- package/core/js/__tests__/pluginUiZip.test.js +47 -0
- package/core/js/__tests__/sdkLevel.test.js +34 -0
- package/core/js/api.util.js +433 -0
- package/core/js/appStores.js +93 -0
- package/core/js/basePath.js +10 -0
- package/core/js/html.util.js +26 -0
- package/core/js/pluginFolder.util.js +95 -0
- package/core/js/tooltip.util.js +59 -0
- package/core/js/variables.js +95 -0
- package/core/js/version.util.js +100 -0
- package/core/scss/_pano-tokens.scss +51 -0
- package/core/scss/custom-bootstrap.scss +210 -0
- package/core/scss/fonts.scss +85 -0
- package/core/scss/main.scss +389 -0
- package/core/scss/themes.scss +122 -0
- package/package.json +63 -11
- package/src/__tests__/controllers.test.js +220 -0
- package/src/__tests__/views.spike.test.js +472 -0
- package/src/components/{index.js → panel.js} +9 -7
- package/src/components/theme.js +18 -0
- package/src/controllers.js +127 -0
- package/src/index.js +1 -1
- package/src/runtime-specifiers.js +88 -0
- package/src/svelte.js +3 -2
- package/src/types.js +5 -3
- package/src/utils/api.js +42 -1
- package/src/utils/auth.js +8 -0
- package/src/utils/component.js +4 -2
- package/src/utils/route.js +16 -0
- package/src/utils/text.js +8 -0
- package/src/views.js +29 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// api-compat: the additive-only promise of doc 04 section 6, as a diff of two OpenAPI documents.
|
|
2
|
+
// `breaks(oldSpec, newSpec, today)` returns one message per break; an empty list means compatible.
|
|
3
|
+
|
|
4
|
+
const METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
|
|
5
|
+
const DAY = /^\d{4}-\d{2}-\d{2}/;
|
|
6
|
+
|
|
7
|
+
/** @param {any} spec @param {any} node */
|
|
8
|
+
function deref(spec, node, depth = 0) {
|
|
9
|
+
if (!node || typeof node !== 'object' || !node.$ref || depth > 12) return node;
|
|
10
|
+
const ref = String(node.$ref);
|
|
11
|
+
if (!ref.startsWith('#/')) return node;
|
|
12
|
+
let cur = spec;
|
|
13
|
+
for (const part of ref.slice(2).split('/')) cur = cur?.[part.replace(/~1/g, '/').replace(/~0/g, '~')];
|
|
14
|
+
return deref(spec, cur, depth + 1);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Resolves `$ref` and flattens `allOf` into one object-ish schema.
|
|
19
|
+
* @param {any} spec @param {any} node
|
|
20
|
+
*/
|
|
21
|
+
function flatten(spec, node, depth = 0) {
|
|
22
|
+
node = deref(spec, node);
|
|
23
|
+
if (!node || typeof node !== 'object' || depth > 12) return node || {};
|
|
24
|
+
if (!Array.isArray(node.allOf)) return node;
|
|
25
|
+
const merged = { ...node };
|
|
26
|
+
delete merged.allOf;
|
|
27
|
+
for (const part of node.allOf) {
|
|
28
|
+
const f = flatten(spec, part, depth + 1);
|
|
29
|
+
merged.properties = { ...(f.properties || {}), ...(merged.properties || {}) };
|
|
30
|
+
merged.required = [...new Set([...(f.required || []), ...(merged.required || [])])];
|
|
31
|
+
if (f.type && !merged.type) merged.type = f.type;
|
|
32
|
+
if (f.items && !merged.items) merged.items = f.items;
|
|
33
|
+
}
|
|
34
|
+
return merged;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const typesOf = (s) => (s && s.type ? [].concat(s.type).sort().join('|') : null);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Response side: nothing a client reads may disappear or change type.
|
|
41
|
+
* @returns {string[]}
|
|
42
|
+
*/
|
|
43
|
+
function responseBreaks(oldSpec, newSpec, a, b, at, seen = new Set(), depth = 0) {
|
|
44
|
+
a = flatten(oldSpec, a);
|
|
45
|
+
b = flatten(newSpec, b);
|
|
46
|
+
if (!a || !b || depth > 10) return [];
|
|
47
|
+
const key = `${at}`;
|
|
48
|
+
if (seen.has(key)) return [];
|
|
49
|
+
seen.add(key);
|
|
50
|
+
const out = [];
|
|
51
|
+
const ta = typesOf(a);
|
|
52
|
+
const tb = typesOf(b);
|
|
53
|
+
if (ta && tb && ta !== tb) {
|
|
54
|
+
// integer -> number widening still breaks typed clients; report every change
|
|
55
|
+
out.push(`${at}: type changed from ${ta} to ${tb}`);
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
if (ta && !tb && !b.oneOf && !b.anyOf) out.push(`${at}: type ${ta} was removed`);
|
|
59
|
+
if (a.properties) {
|
|
60
|
+
for (const [name, sub] of Object.entries(a.properties)) {
|
|
61
|
+
if (!b.properties || !(name in b.properties)) {
|
|
62
|
+
if (b.properties || tb === 'object') out.push(`${at}.${name}: response property was removed`);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
out.push(...responseBreaks(oldSpec, newSpec, sub, b.properties[name], `${at}.${name}`, seen, depth + 1));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (a.items && b.items) out.push(...responseBreaks(oldSpec, newSpec, a.items, b.items, `${at}[]`, seen, depth + 1));
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Request side: no field may become required. */
|
|
73
|
+
function requestBreaks(oldSpec, newSpec, a, b, at, depth = 0) {
|
|
74
|
+
a = flatten(oldSpec, a);
|
|
75
|
+
b = flatten(newSpec, b);
|
|
76
|
+
if (!b || depth > 10) return [];
|
|
77
|
+
const out = [];
|
|
78
|
+
const ra = new Set((a && a.required) || []);
|
|
79
|
+
for (const name of b.required || []) {
|
|
80
|
+
if (!ra.has(name)) out.push(`${at}.${name}: request property became required`);
|
|
81
|
+
}
|
|
82
|
+
const ta = typesOf(a);
|
|
83
|
+
const tb = typesOf(b);
|
|
84
|
+
if (ta && tb && ta !== tb) out.push(`${at}: request type changed from ${ta} to ${tb}`);
|
|
85
|
+
if (a && a.properties && b.properties) {
|
|
86
|
+
for (const [name, sub] of Object.entries(a.properties)) {
|
|
87
|
+
if (name in b.properties) out.push(...requestBreaks(oldSpec, newSpec, sub, b.properties[name], `${at}.${name}`, depth + 1));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const jsonSchemaOf = (spec, holder) => {
|
|
94
|
+
const content = deref(spec, holder)?.content;
|
|
95
|
+
if (!content) return undefined;
|
|
96
|
+
const entry = content['application/json'] || Object.values(content)[0];
|
|
97
|
+
return entry && entry.schema;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const paramKey = (p) => `${p.in}:${p.name}`;
|
|
101
|
+
|
|
102
|
+
/** Months between two ISO dates, fractional ignored. */
|
|
103
|
+
function addMonths(iso, months) {
|
|
104
|
+
const d = new Date(iso.slice(0, 10) + 'T00:00:00Z');
|
|
105
|
+
d.setUTCMonth(d.getUTCMonth() + months);
|
|
106
|
+
return d.toISOString().slice(0, 10);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Compares a committed spec with a fresh one.
|
|
111
|
+
* @param {any} oldSpec the snapshot
|
|
112
|
+
* @param {any} newSpec what the running Pano serves
|
|
113
|
+
* @param {string} today ISO date; a deprecated operation may disappear once its `x-pano-removal` is before it
|
|
114
|
+
* @returns {string[]}
|
|
115
|
+
*/
|
|
116
|
+
export function breaks(oldSpec, newSpec, today) {
|
|
117
|
+
const out = [];
|
|
118
|
+
const oldPaths = (oldSpec && oldSpec.paths) || {};
|
|
119
|
+
const newPaths = (newSpec && newSpec.paths) || {};
|
|
120
|
+
for (const [p, oldItem] of Object.entries(oldPaths)) {
|
|
121
|
+
for (const method of METHODS) {
|
|
122
|
+
const oldOp = oldItem[method];
|
|
123
|
+
if (!oldOp) continue;
|
|
124
|
+
const id = `${method.toUpperCase()} ${p}`;
|
|
125
|
+
if (oldOp['x-pano-stability'] === 'internal') continue; // internal operations carry no promise
|
|
126
|
+
const newOp = newPaths[p] && newPaths[p][method];
|
|
127
|
+
if (!newOp) {
|
|
128
|
+
const removal = oldOp['x-pano-removal'];
|
|
129
|
+
if (oldOp.deprecated === true && typeof removal === 'string' && DAY.test(removal) && removal.slice(0, 10) < today) continue;
|
|
130
|
+
out.push(
|
|
131
|
+
oldOp.deprecated === true && removal
|
|
132
|
+
? `${id}: removed before its removal date ${removal}`
|
|
133
|
+
: `${id}: operation was removed${oldOp.deprecated ? ' (deprecated without x-pano-removal)' : ' without being deprecated'}`,
|
|
134
|
+
);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
// parameters
|
|
138
|
+
const oldParams = new Map(((oldOp.parameters || oldItem.parameters || []).map((x) => deref(oldSpec, x))).map((x) => [paramKey(x), x]));
|
|
139
|
+
for (const raw of newOp.parameters || newItem(newPaths, p).parameters || []) {
|
|
140
|
+
const np = deref(newSpec, raw);
|
|
141
|
+
const op = oldParams.get(paramKey(np));
|
|
142
|
+
if (!op) {
|
|
143
|
+
if (np.required) out.push(`${id}: new required ${np.in} parameter "${np.name}"`);
|
|
144
|
+
} else {
|
|
145
|
+
if (!op.required && np.required) out.push(`${id}: ${np.in} parameter "${np.name}" became required`);
|
|
146
|
+
const ta = typesOf(flatten(oldSpec, op.schema));
|
|
147
|
+
const tb = typesOf(flatten(newSpec, np.schema));
|
|
148
|
+
if (ta && tb && ta !== tb) out.push(`${id}: ${np.in} parameter "${np.name}" changed type from ${ta} to ${tb}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// request body
|
|
152
|
+
const rbNew = deref(newSpec, newOp.requestBody);
|
|
153
|
+
if (rbNew) {
|
|
154
|
+
const rbOld = deref(oldSpec, oldOp.requestBody);
|
|
155
|
+
if (rbNew.required && !(rbOld && rbOld.required)) out.push(`${id}: request body became required`);
|
|
156
|
+
const sNew = jsonSchemaOf(newSpec, rbNew);
|
|
157
|
+
if (sNew) out.push(...requestBreaks(oldSpec, newSpec, jsonSchemaOf(oldSpec, rbOld), sNew, `${id} request`).map((m) => m));
|
|
158
|
+
}
|
|
159
|
+
// responses
|
|
160
|
+
for (const [status, oldRes] of Object.entries(oldOp.responses || {})) {
|
|
161
|
+
const newRes = (newOp.responses || {})[status];
|
|
162
|
+
if (!newRes) {
|
|
163
|
+
out.push(`${id}: response ${status} was removed`);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const so = jsonSchemaOf(oldSpec, oldRes);
|
|
167
|
+
const sn = jsonSchemaOf(newSpec, newRes);
|
|
168
|
+
if (so && sn) out.push(...responseBreaks(oldSpec, newSpec, so, sn, `${id} ${status}`));
|
|
169
|
+
else if (so && !sn) out.push(`${id}: response ${status} lost its body schema`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// the deprecation floor: removal at least 6 months after the deprecating release
|
|
174
|
+
for (const [p, item] of Object.entries(newPaths)) {
|
|
175
|
+
for (const method of METHODS) {
|
|
176
|
+
const op = item[method];
|
|
177
|
+
if (!op || op.deprecated !== true) continue;
|
|
178
|
+
const removal = op['x-pano-removal'];
|
|
179
|
+
const since = op['x-pano-deprecated-on'];
|
|
180
|
+
if (typeof removal === 'string' && typeof since === 'string' && DAY.test(removal) && DAY.test(since) && removal.slice(0, 10) < addMonths(since, 6)) {
|
|
181
|
+
out.push(`${method.toUpperCase()} ${p}: x-pano-removal ${removal} is less than 6 months after x-pano-deprecated-on ${since}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function newItem(paths, p) {
|
|
189
|
+
return paths[p] || {};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* CLI entry: `api-compat <old.json> <new.json> [--today YYYY-MM-DD]`.
|
|
194
|
+
* @param {{ oldFile: string, newFile: string, today?: string }} o
|
|
195
|
+
* @param {(p: string) => any} read
|
|
196
|
+
* @returns {number}
|
|
197
|
+
*/
|
|
198
|
+
export function runApiCompat(o, read) {
|
|
199
|
+
const today = o.today || new Date().toISOString().slice(0, 10);
|
|
200
|
+
const list = breaks(read(o.oldFile), read(o.newFile), today);
|
|
201
|
+
for (const b of list) console.error(`break: ${b}`);
|
|
202
|
+
console.error(`api-compat: ${list.length} break(s)`);
|
|
203
|
+
return list.length ? 1 : 0;
|
|
204
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// check-paths: every path passed to ApiUtil / api must be a Pano route (doc 04 section 9).
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { CORE_ROUTES_FILE, extractRoutes, API_ROOT, PLUGINS_ROOT } from './extract-routes.mjs';
|
|
5
|
+
import { findApiCalls, splitTail } from './js-scan.mjs';
|
|
6
|
+
import { segments, segMatch } from './rename-table.mjs';
|
|
7
|
+
import { walk, readText, rel, lineIndex, distance, pluginIdFor } from './util.mjs';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Pattern form of a route path relative to /api/v1 (`/posts/:id`).
|
|
11
|
+
* @param {string} full
|
|
12
|
+
*/
|
|
13
|
+
const relative = (full) =>
|
|
14
|
+
full.startsWith(API_ROOT) ? full.slice(API_ROOT.length) || '/' : full.startsWith(PLUGINS_ROOT + '/') ? full.slice('/api'.length) : full;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Does a literal (template holes allowed) fit one of the route patterns?
|
|
18
|
+
* @param {string} lit @param {string} pattern
|
|
19
|
+
*/
|
|
20
|
+
export function fits(lit, pattern) {
|
|
21
|
+
const a = segments(lit);
|
|
22
|
+
const b = segments(pattern);
|
|
23
|
+
if (a.length !== b.length) return false;
|
|
24
|
+
return a.every((s, i) => segMatch(s, b[i]));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @param {string} lit */
|
|
28
|
+
const normalise = (lit) => lit.replace(/\$\{[^}]*\}/g, ':_').replace(/:[A-Za-z_]\w*/g, ':_');
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {string} lit @param {string[]} patterns
|
|
32
|
+
* @returns {string | null}
|
|
33
|
+
*/
|
|
34
|
+
export function suggest(lit, patterns) {
|
|
35
|
+
const n = normalise(lit);
|
|
36
|
+
let best = null;
|
|
37
|
+
let bestD = Infinity;
|
|
38
|
+
for (const p of patterns) {
|
|
39
|
+
const d = distance(n, normalise(p));
|
|
40
|
+
if (d < bestD) {
|
|
41
|
+
bestD = d;
|
|
42
|
+
best = p;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return best !== null && bestD <= Math.max(3, Math.floor(n.length * 0.25)) ? best : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Collects the literal path arguments of API client calls in a source.
|
|
50
|
+
* @param {string} src
|
|
51
|
+
*/
|
|
52
|
+
export function collectCalls(src) {
|
|
53
|
+
const lineOf = lineIndex(src);
|
|
54
|
+
const out = [];
|
|
55
|
+
for (const call of findApiCalls(src)) {
|
|
56
|
+
const args = src.slice(call.open + 1, call.close);
|
|
57
|
+
const m = /\bpath\s*:\s*(['"`])/.exec(args);
|
|
58
|
+
if (!m) continue;
|
|
59
|
+
const q = call.open + 1 + m.index + m[0].length - 1;
|
|
60
|
+
const quote = src[q];
|
|
61
|
+
let e = q + 1;
|
|
62
|
+
while (e < src.length && src[e] !== quote && !(quote !== '`' && src[e] === '\n')) e += src[e] === '\\' ? 2 : 1;
|
|
63
|
+
if (src[e] !== quote) continue;
|
|
64
|
+
out.push({ ...call, line: lineOf(q), literal: src.slice(q + 1, e) });
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {{ root: string, routes?: string, pluginId?: string | null }} o
|
|
71
|
+
* @returns {{ problems: string[], checked: number, errors: string[] }}
|
|
72
|
+
*/
|
|
73
|
+
export function checkPaths(o) {
|
|
74
|
+
const rootAbs = path.resolve(o.root);
|
|
75
|
+
const errors = [];
|
|
76
|
+
const routesFile = o.routes ? path.resolve(o.routes) : CORE_ROUTES_FILE;
|
|
77
|
+
/** @type {string[]} */
|
|
78
|
+
let corePatterns = [];
|
|
79
|
+
if (fs.existsSync(routesFile)) {
|
|
80
|
+
corePatterns = JSON.parse(readText(routesFile)).map((r) => relative(r.path));
|
|
81
|
+
} else {
|
|
82
|
+
errors.push(`routes file ${routesFile} not found; generate it with "pano-api extract-routes --core <pano-web-platform>"`);
|
|
83
|
+
}
|
|
84
|
+
// the repository's own routes (a plugin repo has Kotlin endpoints; a UI repo has none)
|
|
85
|
+
const own = extractRoutes(rootAbs, { pluginId: o.pluginId ?? undefined }).routes;
|
|
86
|
+
const ownId = o.pluginId ?? pluginIdFor(rootAbs, rootAbs);
|
|
87
|
+
const ownMounted = own.map((r) => relative(r.path));
|
|
88
|
+
const ownSite = own.filter((r) => r.namespace === 'SITE').map((r) => r.declared);
|
|
89
|
+
const ownPanel = own.filter((r) => r.namespace === 'PANEL').map((r) => r.declared);
|
|
90
|
+
|
|
91
|
+
const problems = [];
|
|
92
|
+
let checked = 0;
|
|
93
|
+
for (const p of walk(rootAbs, { exts: ['.js', '.mjs', '.cjs', '.svelte', '.ts'] })) {
|
|
94
|
+
if (path.basename(p) === 'pano-api.js') continue;
|
|
95
|
+
const src = readText(p);
|
|
96
|
+
for (const c of collectCalls(src)) {
|
|
97
|
+
const at = `${rel(rootAbs, p)}:${c.line}`;
|
|
98
|
+
const [lit] = splitTail(c.literal);
|
|
99
|
+
if (/^\/api(\/|$)/.test(lit)) {
|
|
100
|
+
problems.push(`${at} '${lit}' must not start with /api: write '${lit.replace(/^\/api/, '') || '/'}'`);
|
|
101
|
+
checked++;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (!lit.startsWith('/')) continue; // a variable or a computed prefix: nothing to compare
|
|
105
|
+
checked++;
|
|
106
|
+
let patterns;
|
|
107
|
+
if (c.kind === 'core') {
|
|
108
|
+
if (c.panel) continue;
|
|
109
|
+
patterns = [...corePatterns, ...ownMounted];
|
|
110
|
+
} else {
|
|
111
|
+
if (c.pluginId && ownId && c.pluginId !== ownId) continue; // another plugin's API: not ours to verify
|
|
112
|
+
if (!own.length) continue;
|
|
113
|
+
patterns = c.panel ? ownPanel : ownSite;
|
|
114
|
+
}
|
|
115
|
+
if (!patterns.length) continue;
|
|
116
|
+
if (patterns.some((pat) => fits(lit, pat))) continue;
|
|
117
|
+
const hint = suggest(lit, patterns);
|
|
118
|
+
problems.push(`${at} '${lit}' is not a Pano route${hint ? ` (did you mean '${hint}'?)` : ''}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return { problems, checked, errors };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {{ root: string, routes?: string, pluginId?: string | null }} o
|
|
126
|
+
* @returns {number}
|
|
127
|
+
*/
|
|
128
|
+
export function runCheckPaths(o) {
|
|
129
|
+
const r = checkPaths(o);
|
|
130
|
+
for (const e of r.errors) console.error(`error: ${e}`);
|
|
131
|
+
for (const p of r.problems) console.error(p);
|
|
132
|
+
console.error(`check-paths: ${r.checked} path(s) checked, ${r.problems.length} problem(s)`);
|
|
133
|
+
if (r.errors.length) return 2;
|
|
134
|
+
return r.problems.length ? 1 : 0;
|
|
135
|
+
}
|
package/bin/api/cli.mjs
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Argument handling and dispatch of the pano-api CLI.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { parseArgs } from './util.mjs';
|
|
5
|
+
|
|
6
|
+
const HELP = `pano-api - Pano API v1 tooling (migration, route extraction, path check, OpenAPI snapshots)
|
|
7
|
+
|
|
8
|
+
Usage: pano-api <command> [options] [dir]
|
|
9
|
+
|
|
10
|
+
Commands
|
|
11
|
+
extract-routes [--core] [--check] [--out <file>] [--plugin-id <id>] [dir]
|
|
12
|
+
Static scan of @Endpoint Kotlin classes. Prints the route list as JSON; --core writes sdk/api/routes.core.json.
|
|
13
|
+
--check validates every declared path (doc 04 section 2) and, with --core / --out, that the file is current.
|
|
14
|
+
migrate-v1 [--strip <segment>] [--only a,b,..] [--check] [--plugin-id <id>] [dir]
|
|
15
|
+
Steps: a Kotlin paths, b Error codes, c validation imports, d api-level in build.gradle.kts,
|
|
16
|
+
e client path literals, f client result reads. --check changes nothing and exits 1 when something would change
|
|
17
|
+
or a leftover (/api/ literal, .result comparison, totalPage) remains.
|
|
18
|
+
check-paths [--routes <routes.core.json>] [--plugin-id <id>] [dir]
|
|
19
|
+
Every path passed to ApiUtil / api must be a Pano route.
|
|
20
|
+
openapi-snapshot --url <pano> (--write | --check) [--core-dir <dir>] [--plugins-dir <dir>] [--repo <dir>]
|
|
21
|
+
[--plugin <id>]... [--today <date>] [--allow-breaks]
|
|
22
|
+
Fetches the OpenAPI documents of a running Pano and writes / compares the committed snapshots.
|
|
23
|
+
api-compat <old.json> <new.json> [--today <date>]
|
|
24
|
+
Lists breaking differences between two OpenAPI documents (exit 1 when there are any).
|
|
25
|
+
|
|
26
|
+
dir defaults to the current directory. All commands are idempotent.
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
/** @param {string[]} argv process.argv.slice(2) @returns {Promise<number>} */
|
|
30
|
+
export async function main(argv) {
|
|
31
|
+
const [command, ...rest] = argv;
|
|
32
|
+
if (!command || command === '--help' || command === '-h' || command === 'help') {
|
|
33
|
+
process.stdout.write(HELP);
|
|
34
|
+
return command ? 0 : 1;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
switch (command) {
|
|
38
|
+
case 'extract-routes': {
|
|
39
|
+
const { flags, positional } = parseArgs(rest, { bool: ['core', 'check'], value: ['out', 'plugin-id'] });
|
|
40
|
+
const { runExtractRoutes } = await import('./extract-routes.mjs');
|
|
41
|
+
return runExtractRoutes({ root: positional[0] || '.', core: !!flags.core, check: !!flags.check, out: flags.out, pluginId: flags['plugin-id'] });
|
|
42
|
+
}
|
|
43
|
+
case 'migrate-v1': {
|
|
44
|
+
const { flags, positional } = parseArgs(rest, { bool: ['check'], value: ['strip', 'only', 'plugin-id'] });
|
|
45
|
+
const only = flags.only ? String(flags.only).split(',').map((s) => s.trim()).filter(Boolean) : [];
|
|
46
|
+
const bad = only.filter((s) => !/^[a-f]$/.test(s));
|
|
47
|
+
if (bad.length) throw new Error(`--only takes letters a-f, got "${bad.join(',')}"`);
|
|
48
|
+
const { runMigrateV1 } = await import('./migrate-v1.mjs');
|
|
49
|
+
return runMigrateV1({ root: positional[0] || '.', check: !!flags.check, only, strip: flags.strip || null, pluginId: flags['plugin-id'] || null });
|
|
50
|
+
}
|
|
51
|
+
case 'check-paths': {
|
|
52
|
+
const { flags, positional } = parseArgs(rest, { value: ['routes', 'plugin-id'] });
|
|
53
|
+
const { runCheckPaths } = await import('./check-paths.mjs');
|
|
54
|
+
return runCheckPaths({ root: positional[0] || '.', routes: flags.routes, pluginId: flags['plugin-id'] || null });
|
|
55
|
+
}
|
|
56
|
+
case 'openapi-snapshot': {
|
|
57
|
+
const { flags } = parseArgs(rest, { bool: ['write', 'check', 'allow-breaks'], value: ['url', 'core-dir', 'plugins-dir', 'repo', 'plugin', 'today'] });
|
|
58
|
+
if (!flags.url) throw new Error('--url <pano> is required');
|
|
59
|
+
if (!flags.write && !flags.check) throw new Error('pass --write or --check');
|
|
60
|
+
const { runOpenapiSnapshot } = await import('./openapi-snapshot.mjs');
|
|
61
|
+
return await runOpenapiSnapshot({
|
|
62
|
+
url: flags.url, write: !!flags.write, check: !!flags.check, allowBreaks: !!flags['allow-breaks'], today: flags.today,
|
|
63
|
+
coreDir: path.resolve(flags['core-dir'] || path.join('Pano', 'api')),
|
|
64
|
+
pluginsDir: flags['plugins-dir'] ? path.resolve(flags['plugins-dir']) : undefined,
|
|
65
|
+
repo: flags.repo, plugins: flags.plugin ? [].concat(flags.plugin) : undefined,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
case 'api-compat': {
|
|
69
|
+
const { flags, positional } = parseArgs(rest, { value: ['today'] });
|
|
70
|
+
if (positional.length !== 2) throw new Error('api-compat takes <old.json> <new.json>');
|
|
71
|
+
const { runApiCompat } = await import('./api-compat.mjs');
|
|
72
|
+
return runApiCompat({ oldFile: positional[0], newFile: positional[1], today: flags.today }, (p) => JSON.parse(fs.readFileSync(p, 'utf8')));
|
|
73
|
+
}
|
|
74
|
+
default:
|
|
75
|
+
console.error(`pano-api: unknown command "${command}"\n`);
|
|
76
|
+
process.stdout.write(HELP);
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
} catch (e) {
|
|
80
|
+
console.error(`pano-api ${command}: ${e.message}`);
|
|
81
|
+
return 2;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// extract-routes: static scan of @Endpoint Kotlin classes -> route list (doc 04 section 9).
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { loadKotlinProject, layoutOf } from './project.mjs';
|
|
6
|
+
import { prettyJson } from './util.mjs';
|
|
7
|
+
|
|
8
|
+
export const API_ROOT = '/api/v1';
|
|
9
|
+
/** Plugin endpoints are unversioned (decision 81): `/api/plugins/<id>/...`, panel ones `/api/plugins/<id>/panel/...`. */
|
|
10
|
+
export const PLUGINS_ROOT = '/api/plugins';
|
|
11
|
+
/** Where `--core` writes: `packages/sdk/api/routes.core.json`, shipped in the sdk package. */
|
|
12
|
+
export const CORE_ROUTES_FILE = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'api', 'routes.core.json');
|
|
13
|
+
|
|
14
|
+
/** Mirror of ApiPaths.resolve (doc 04 section 2). */
|
|
15
|
+
export function resolvePath(declared, mount, namespace, pluginId) {
|
|
16
|
+
if (mount === 'ROOT') return declared;
|
|
17
|
+
if (namespace === 'PANEL') return pluginId ? `${PLUGINS_ROOT}/${pluginId}/panel${declared}` : `${API_ROOT}/panel${declared}`;
|
|
18
|
+
return pluginId ? `${PLUGINS_ROOT}/${pluginId}${declared}` : `${API_ROOT}${declared}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Declared path as seen by the router before the cutover (it still carries `/api`). */
|
|
22
|
+
export const isLegacy = (declared) => declared === '/api' || declared.startsWith('/api/');
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {{
|
|
26
|
+
* method: string, path: string, declared: string, pluginId: string | null, namespace: 'SITE' | 'PANEL',
|
|
27
|
+
* mount: 'API' | 'ROOT', class: string, file: string, line: number, legacy: boolean
|
|
28
|
+
* }} Route
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Scans `root` (a plugin repo, a Kotlin source folder or a pano-web-platform checkout) and returns the routes.
|
|
33
|
+
* @param {string} root
|
|
34
|
+
* @param {{ pluginId?: string | null }} [opts]
|
|
35
|
+
* @returns {{ routes: Route[], errors: { file: string, line: number, message: string }[] }}
|
|
36
|
+
*/
|
|
37
|
+
export function extractRoutes(root, opts = {}) {
|
|
38
|
+
const { kotlinRoot } = layoutOf(root);
|
|
39
|
+
const project = loadKotlinProject(kotlinRoot, { ...opts, labelRoot: path.resolve(root) });
|
|
40
|
+
/** @type {Route[]} */
|
|
41
|
+
const routes = [];
|
|
42
|
+
for (const ep of project.endpoints) {
|
|
43
|
+
for (const call of ep.calls) {
|
|
44
|
+
routes.push({
|
|
45
|
+
method: call.method,
|
|
46
|
+
path: isLegacy(call.value) ? call.value : resolvePath(call.value, ep.mount, ep.namespace, ep.pluginId),
|
|
47
|
+
declared: call.value,
|
|
48
|
+
pluginId: ep.pluginId,
|
|
49
|
+
namespace: ep.namespace,
|
|
50
|
+
mount: ep.mount,
|
|
51
|
+
class: ep.cls.name,
|
|
52
|
+
file: ep.file.rel,
|
|
53
|
+
line: call.line,
|
|
54
|
+
legacy: ep.mount === 'API' && isLegacy(call.value),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
routes.sort((a, b) => a.path.localeCompare(b.path) || a.method.localeCompare(b.method) || a.class.localeCompare(b.class));
|
|
59
|
+
return { routes, errors: project.errors };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The refusal table of doc 04 section 2 applied to extracted routes, plus duplicate detection.
|
|
64
|
+
* @param {Route[]} routes
|
|
65
|
+
* @returns {string[]} one message per problem, `file:line Class: ... ; fix`
|
|
66
|
+
*/
|
|
67
|
+
export function checkRoutes(routes) {
|
|
68
|
+
const problems = [];
|
|
69
|
+
const seen = new Map();
|
|
70
|
+
const ordered = [...routes].sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line);
|
|
71
|
+
for (const r of ordered) {
|
|
72
|
+
const at = `${r.file}:${r.line} ${r.class}`;
|
|
73
|
+
const d = r.declared;
|
|
74
|
+
if (r.mount === 'API') {
|
|
75
|
+
if (!d || !d.startsWith('/')) problems.push(`${at}: path "${d}" must start with "/"; declare "/hello"`);
|
|
76
|
+
else if (r.legacy || d === '/api' || d.startsWith('/api/'))
|
|
77
|
+
problems.push(`${at}: path "${d}" starts with /api; declare "${d.replace(/^\/api/, '') || '/posts'}", not "${d}"; Pano adds /api/v1 itself`);
|
|
78
|
+
else if (d === '/panel' || d.startsWith('/panel/'))
|
|
79
|
+
problems.push(`${at}: path "${d}" starts with /panel; extend PanelApi and declare "${d.replace(/^\/panel/, '') || '/settings'}"; Pano adds /panel itself`);
|
|
80
|
+
else if (r.pluginId) {
|
|
81
|
+
const first = d.split('/')[1] ?? '';
|
|
82
|
+
if (first === '_' || first.startsWith(':'))
|
|
83
|
+
problems.push(`${at}: plugin path "${d}" starts with "${first}"; put a resource name first: "/items/:id"`);
|
|
84
|
+
} else if (d.startsWith('/plugins/') && !/(^|\/)route\/api\/plugins\//.test(r.file))
|
|
85
|
+
problems.push(`${at}: core path "${d}" starts with /plugins/; reserved for plugins`);
|
|
86
|
+
}
|
|
87
|
+
const key = `${r.method} ${r.path.replace(/:[A-Za-z_]\w*/g, ':_')}`;
|
|
88
|
+
if (seen.has(key)) problems.push(`${at}: duplicate of ${seen.get(key)} for ${r.method} ${r.path}`);
|
|
89
|
+
else seen.set(key, `${r.file}:${r.line} ${r.class}`);
|
|
90
|
+
}
|
|
91
|
+
return problems;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Output form: stable, without line numbers. */
|
|
95
|
+
export const toJson = (routes) =>
|
|
96
|
+
prettyJson(routes.map(({ method, path: p, pluginId, namespace, class: cls, file, declared, mount }) => ({ method, path: p, pluginId, namespace, mount, class: cls, file, declared })));
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* CLI entry.
|
|
100
|
+
* @param {{ root: string, core: boolean, check: boolean, out?: string, pluginId?: string }} o
|
|
101
|
+
* @returns {number} exit code
|
|
102
|
+
*/
|
|
103
|
+
export function runExtractRoutes(o) {
|
|
104
|
+
const { routes, errors } = extractRoutes(o.root, { pluginId: o.pluginId });
|
|
105
|
+
if (errors.length) {
|
|
106
|
+
for (const e of errors) console.error(`${e.file}:${e.line} ${e.message}`);
|
|
107
|
+
console.error(`extract-routes: ${errors.length} endpoint class(es) could not be read`);
|
|
108
|
+
return 2;
|
|
109
|
+
}
|
|
110
|
+
const problems = checkRoutes(routes);
|
|
111
|
+
const json = toJson(routes);
|
|
112
|
+
const target = o.out ? path.resolve(o.out) : o.core ? CORE_ROUTES_FILE : null;
|
|
113
|
+
if (o.check) {
|
|
114
|
+
for (const p of problems) console.error(p);
|
|
115
|
+
let stale = false;
|
|
116
|
+
if (target && fs.existsSync(target) && fs.readFileSync(target, 'utf8') !== json) stale = true;
|
|
117
|
+
if (target && !fs.existsSync(target)) stale = true;
|
|
118
|
+
if (stale) console.error(`extract-routes: ${path.relative(process.cwd(), target)} is out of date; run "pano-api extract-routes ${o.core ? '--core ' : ''}${o.root}"`);
|
|
119
|
+
console.error(`extract-routes: ${routes.length} routes, ${problems.length} problem(s)${stale ? ', snapshot stale' : ''}`);
|
|
120
|
+
return problems.length || stale ? 1 : 0;
|
|
121
|
+
}
|
|
122
|
+
if (target) {
|
|
123
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
124
|
+
fs.writeFileSync(target, json);
|
|
125
|
+
console.error(`extract-routes: wrote ${routes.length} routes to ${target}`);
|
|
126
|
+
} else process.stdout.write(json);
|
|
127
|
+
if (problems.length) console.error(`extract-routes: ${problems.length} path(s) do not fit the /api/v1 scheme yet; run with --check to list them`);
|
|
128
|
+
return 0;
|
|
129
|
+
}
|