@unispechq/unispec-core 0.1.2 → 0.2.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/README.md +223 -223
- package/dist/cjs/converters/index.js +206 -0
- package/dist/cjs/diff/index.js +236 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/loader/index.js +22 -0
- package/dist/cjs/normalizer/index.js +107 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/types/index.js +3 -0
- package/dist/cjs/validator/index.js +81 -0
- package/dist/converters/index.js +135 -23
- package/dist/diff/index.js +80 -42
- package/dist/index.cjs +22 -0
- package/dist/normalizer/index.js +41 -53
- package/dist/types/index.d.ts +161 -20
- package/dist/types/index.js +1 -0
- package/dist/validator/index.d.ts +5 -1
- package/dist/validator/index.js +32 -5
- package/package.json +12 -3
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.diffUniSpec = diffUniSpec;
|
|
4
|
+
function isPlainObject(value) {
|
|
5
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
6
|
+
}
|
|
7
|
+
function diffValues(oldVal, newVal, basePath, out) {
|
|
8
|
+
if (oldVal === newVal) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
// Both plain objects → recurse by keys
|
|
12
|
+
if (isPlainObject(oldVal) && isPlainObject(newVal)) {
|
|
13
|
+
const oldKeys = new Set(Object.keys(oldVal));
|
|
14
|
+
const newKeys = new Set(Object.keys(newVal));
|
|
15
|
+
// Removed keys
|
|
16
|
+
for (const key of oldKeys) {
|
|
17
|
+
if (!newKeys.has(key)) {
|
|
18
|
+
out.push({
|
|
19
|
+
path: `${basePath}/${key}`,
|
|
20
|
+
description: "Field removed",
|
|
21
|
+
severity: "unknown",
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Added / changed keys
|
|
26
|
+
for (const key of newKeys) {
|
|
27
|
+
const childPath = `${basePath}/${key}`;
|
|
28
|
+
if (!oldKeys.has(key)) {
|
|
29
|
+
out.push({
|
|
30
|
+
path: childPath,
|
|
31
|
+
description: "Field added",
|
|
32
|
+
severity: "unknown",
|
|
33
|
+
});
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
diffValues(oldVal[key], newVal[key], childPath, out);
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// Arrays
|
|
41
|
+
if (Array.isArray(oldVal) && Array.isArray(newVal)) {
|
|
42
|
+
// Special handling for UniSpec collections identified by "name"
|
|
43
|
+
const isNamedCollection = basePath === "/service/protocols/rest/routes" ||
|
|
44
|
+
basePath === "/service/protocols/websocket/channels" ||
|
|
45
|
+
basePath === "/service/protocols/graphql/queries" ||
|
|
46
|
+
basePath === "/service/protocols/graphql/mutations" ||
|
|
47
|
+
basePath === "/service/protocols/graphql/subscriptions";
|
|
48
|
+
if (isNamedCollection) {
|
|
49
|
+
const oldByName = new Map();
|
|
50
|
+
const newByName = new Map();
|
|
51
|
+
for (const item of oldVal) {
|
|
52
|
+
if (item && typeof item === "object" && typeof item.name === "string") {
|
|
53
|
+
oldByName.set(item.name, item);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const item of newVal) {
|
|
57
|
+
if (item && typeof item === "object" && typeof item.name === "string") {
|
|
58
|
+
newByName.set(item.name, item);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Removed
|
|
62
|
+
for (const [name, oldItem] of oldByName.entries()) {
|
|
63
|
+
if (!newByName.has(name)) {
|
|
64
|
+
out.push({
|
|
65
|
+
path: `${basePath}/${name}`,
|
|
66
|
+
description: "Item removed",
|
|
67
|
+
severity: "unknown",
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
const newItem = newByName.get(name);
|
|
72
|
+
diffValues(oldItem, newItem, `${basePath}/${name}`, out);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Added
|
|
76
|
+
for (const [name] of newByName.entries()) {
|
|
77
|
+
if (!oldByName.has(name)) {
|
|
78
|
+
out.push({
|
|
79
|
+
path: `${basePath}/${name}`,
|
|
80
|
+
description: "Item added",
|
|
81
|
+
severity: "unknown",
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// Generic shallow index-based compare
|
|
88
|
+
const maxLen = Math.max(oldVal.length, newVal.length);
|
|
89
|
+
for (let i = 0; i < maxLen; i++) {
|
|
90
|
+
const childPath = `${basePath}/${i}`;
|
|
91
|
+
if (i >= oldVal.length) {
|
|
92
|
+
out.push({
|
|
93
|
+
path: childPath,
|
|
94
|
+
description: "Item added",
|
|
95
|
+
severity: "unknown",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
else if (i >= newVal.length) {
|
|
99
|
+
out.push({
|
|
100
|
+
path: childPath,
|
|
101
|
+
description: "Item removed",
|
|
102
|
+
severity: "unknown",
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
diffValues(oldVal[i], newVal[i], childPath, out);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
// Primitive or mismatched types → treat as value change
|
|
112
|
+
out.push({
|
|
113
|
+
path: basePath,
|
|
114
|
+
description: "Value changed",
|
|
115
|
+
severity: "unknown",
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function annotateRestChange(change) {
|
|
119
|
+
if (!change.path.startsWith("/service/protocols/rest/routes/")) {
|
|
120
|
+
return change;
|
|
121
|
+
}
|
|
122
|
+
const segments = change.path.split("/").filter(Boolean);
|
|
123
|
+
// Expected shape: ["service", "protocols", "rest", "routes", index]
|
|
124
|
+
if (segments[0] !== "service" || segments[1] !== "protocols" || segments[2] !== "rest" || segments[3] !== "routes") {
|
|
125
|
+
return change;
|
|
126
|
+
}
|
|
127
|
+
const index = segments[4];
|
|
128
|
+
if (typeof index === "undefined") {
|
|
129
|
+
return change;
|
|
130
|
+
}
|
|
131
|
+
const annotated = {
|
|
132
|
+
...change,
|
|
133
|
+
protocol: "rest",
|
|
134
|
+
};
|
|
135
|
+
if (change.description === "Item removed" || change.description === "Field removed") {
|
|
136
|
+
annotated.kind = "rest.route.removed";
|
|
137
|
+
annotated.severity = "breaking";
|
|
138
|
+
}
|
|
139
|
+
else if (change.description === "Item added" || change.description === "Field added") {
|
|
140
|
+
annotated.kind = "rest.route.added";
|
|
141
|
+
annotated.severity = "non-breaking";
|
|
142
|
+
}
|
|
143
|
+
return annotated;
|
|
144
|
+
}
|
|
145
|
+
function annotateWebSocketChange(change) {
|
|
146
|
+
if (!change.path.startsWith("/service/protocols/websocket/channels/")) {
|
|
147
|
+
return change;
|
|
148
|
+
}
|
|
149
|
+
const segments = change.path.split("/").filter(Boolean);
|
|
150
|
+
// Expected base: ["service","protocols","websocket","channels", channelIndex, ...]
|
|
151
|
+
if (segments[0] !== "service" || segments[1] !== "protocols" || segments[2] !== "websocket" || segments[3] !== "channels") {
|
|
152
|
+
return change;
|
|
153
|
+
}
|
|
154
|
+
const channelIndex = segments[4];
|
|
155
|
+
const next = segments[5];
|
|
156
|
+
const annotated = {
|
|
157
|
+
...change,
|
|
158
|
+
protocol: "websocket",
|
|
159
|
+
};
|
|
160
|
+
if (typeof channelIndex === "undefined") {
|
|
161
|
+
return annotated;
|
|
162
|
+
}
|
|
163
|
+
// Channel-level changes: /service/protocols/websocket/channels/{index}
|
|
164
|
+
if (!next) {
|
|
165
|
+
if (change.description === "Item removed" || change.description === "Field removed") {
|
|
166
|
+
annotated.kind = "websocket.channel.removed";
|
|
167
|
+
annotated.severity = "breaking";
|
|
168
|
+
}
|
|
169
|
+
else if (change.description === "Item added" || change.description === "Field added") {
|
|
170
|
+
annotated.kind = "websocket.channel.added";
|
|
171
|
+
annotated.severity = "non-breaking";
|
|
172
|
+
}
|
|
173
|
+
return annotated;
|
|
174
|
+
}
|
|
175
|
+
// Message-level changes: /service/protocols/websocket/channels/{index}/messages/{msgIndex}
|
|
176
|
+
if (next === "messages") {
|
|
177
|
+
const messageIndex = segments[6];
|
|
178
|
+
if (typeof messageIndex === "undefined") {
|
|
179
|
+
return annotated;
|
|
180
|
+
}
|
|
181
|
+
if (change.description === "Item removed") {
|
|
182
|
+
annotated.kind = "websocket.message.removed";
|
|
183
|
+
annotated.severity = "breaking";
|
|
184
|
+
}
|
|
185
|
+
else if (change.description === "Item added") {
|
|
186
|
+
annotated.kind = "websocket.message.added";
|
|
187
|
+
annotated.severity = "non-breaking";
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return annotated;
|
|
191
|
+
}
|
|
192
|
+
function annotateGraphQLChange(change) {
|
|
193
|
+
if (!change.path.startsWith("/service/protocols/graphql/")) {
|
|
194
|
+
return change;
|
|
195
|
+
}
|
|
196
|
+
const segments = change.path.split("/").filter(Boolean);
|
|
197
|
+
// Expected: ["service","protocols","graphql", kind, index, ...]
|
|
198
|
+
if (segments[0] !== "service" || segments[1] !== "protocols" || segments[2] !== "graphql") {
|
|
199
|
+
return change;
|
|
200
|
+
}
|
|
201
|
+
const opKind = segments[3];
|
|
202
|
+
const index = segments[4];
|
|
203
|
+
if (!opKind || typeof index === "undefined") {
|
|
204
|
+
return change;
|
|
205
|
+
}
|
|
206
|
+
if (opKind !== "queries" && opKind !== "mutations" && opKind !== "subscriptions") {
|
|
207
|
+
return change;
|
|
208
|
+
}
|
|
209
|
+
const annotated = {
|
|
210
|
+
...change,
|
|
211
|
+
protocol: "graphql",
|
|
212
|
+
};
|
|
213
|
+
if (change.description === "Item removed" || change.description === "Field removed") {
|
|
214
|
+
annotated.kind = "graphql.operation.removed";
|
|
215
|
+
annotated.severity = "breaking";
|
|
216
|
+
}
|
|
217
|
+
else if (change.description === "Item added" || change.description === "Field added") {
|
|
218
|
+
annotated.kind = "graphql.operation.added";
|
|
219
|
+
annotated.severity = "non-breaking";
|
|
220
|
+
}
|
|
221
|
+
return annotated;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Compute a structural diff between two UniSpec documents.
|
|
225
|
+
*
|
|
226
|
+
* Current behavior:
|
|
227
|
+
* - Tracks added, removed, and changed fields and array items.
|
|
228
|
+
* - Uses JSON Pointer-like paths rooted at "" (e.g., "/info/title").
|
|
229
|
+
* - Marks all changes with severity "unknown" for now.
|
|
230
|
+
*/
|
|
231
|
+
function diffUniSpec(oldDoc, newDoc) {
|
|
232
|
+
const changes = [];
|
|
233
|
+
diffValues(oldDoc, newDoc, "", changes);
|
|
234
|
+
const annotated = changes.map((change) => annotateWebSocketChange(annotateGraphQLChange(annotateRestChange(change))));
|
|
235
|
+
return { changes: annotated };
|
|
236
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types/index.js"), exports);
|
|
18
|
+
__exportStar(require("./loader/index.js"), exports);
|
|
19
|
+
__exportStar(require("./validator/index.js"), exports);
|
|
20
|
+
__exportStar(require("./normalizer/index.js"), exports);
|
|
21
|
+
__exportStar(require("./diff/index.js"), exports);
|
|
22
|
+
__exportStar(require("./converters/index.js"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadUniSpec = loadUniSpec;
|
|
4
|
+
/**
|
|
5
|
+
* Load a UniSpec document from a raw input value.
|
|
6
|
+
* Currently supports:
|
|
7
|
+
* - JavaScript objects (treated as already parsed UniSpec)
|
|
8
|
+
* - JSON strings
|
|
9
|
+
*
|
|
10
|
+
* YAML and filesystem helpers will be added later, keeping this API stable.
|
|
11
|
+
*/
|
|
12
|
+
async function loadUniSpec(input, _options = {}) {
|
|
13
|
+
if (typeof input === "string") {
|
|
14
|
+
const trimmed = input.trim();
|
|
15
|
+
if (!trimmed) {
|
|
16
|
+
throw new Error("Cannot load UniSpec: input string is empty");
|
|
17
|
+
}
|
|
18
|
+
// For now we assume JSON; YAML support will be added later.
|
|
19
|
+
return JSON.parse(trimmed);
|
|
20
|
+
}
|
|
21
|
+
return input;
|
|
22
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeUniSpec = normalizeUniSpec;
|
|
4
|
+
function isPlainObject(value) {
|
|
5
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
6
|
+
}
|
|
7
|
+
function normalizeValue(value) {
|
|
8
|
+
if (Array.isArray(value)) {
|
|
9
|
+
return value.map((item) => normalizeValue(item));
|
|
10
|
+
}
|
|
11
|
+
if (isPlainObject(value)) {
|
|
12
|
+
const entries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b));
|
|
13
|
+
const normalized = {};
|
|
14
|
+
for (const [key, val] of entries) {
|
|
15
|
+
normalized[key] = normalizeValue(val);
|
|
16
|
+
}
|
|
17
|
+
return normalized;
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function normalizeRestRoutes(doc) {
|
|
22
|
+
if (!doc || !doc.service || !doc.service.protocols) {
|
|
23
|
+
return doc;
|
|
24
|
+
}
|
|
25
|
+
const protocols = doc.service.protocols;
|
|
26
|
+
const rest = protocols.rest;
|
|
27
|
+
if (!rest || !Array.isArray(rest.routes)) {
|
|
28
|
+
return doc;
|
|
29
|
+
}
|
|
30
|
+
const routes = [...rest.routes];
|
|
31
|
+
routes.sort((a, b) => {
|
|
32
|
+
const keyA = a.name || `${a.path} ${a.method}`;
|
|
33
|
+
const keyB = b.name || `${b.path} ${b.method}`;
|
|
34
|
+
return keyA.localeCompare(keyB);
|
|
35
|
+
});
|
|
36
|
+
rest.routes = routes;
|
|
37
|
+
return doc;
|
|
38
|
+
}
|
|
39
|
+
function normalizeWebSocket(doc) {
|
|
40
|
+
if (!doc || !doc.service || !doc.service.protocols) {
|
|
41
|
+
return doc;
|
|
42
|
+
}
|
|
43
|
+
const protocols = doc.service.protocols;
|
|
44
|
+
const websocket = protocols.websocket;
|
|
45
|
+
if (!websocket || !Array.isArray(websocket.channels)) {
|
|
46
|
+
return doc;
|
|
47
|
+
}
|
|
48
|
+
const channels = websocket.channels.map((channel) => {
|
|
49
|
+
if (!channel || !Array.isArray(channel.messages)) {
|
|
50
|
+
return channel;
|
|
51
|
+
}
|
|
52
|
+
const sortedMessages = [...channel.messages].sort((a, b) => {
|
|
53
|
+
const aName = a?.name ?? "";
|
|
54
|
+
const bName = b?.name ?? "";
|
|
55
|
+
return aName.localeCompare(bName);
|
|
56
|
+
});
|
|
57
|
+
return {
|
|
58
|
+
...channel,
|
|
59
|
+
messages: sortedMessages,
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
channels.sort((a, b) => {
|
|
63
|
+
const aName = a?.name ?? "";
|
|
64
|
+
const bName = b?.name ?? "";
|
|
65
|
+
return aName.localeCompare(bName);
|
|
66
|
+
});
|
|
67
|
+
websocket.channels = channels;
|
|
68
|
+
return doc;
|
|
69
|
+
}
|
|
70
|
+
function normalizeGraphqlOperations(doc) {
|
|
71
|
+
if (!doc || !doc.service || !doc.service.protocols) {
|
|
72
|
+
return doc;
|
|
73
|
+
}
|
|
74
|
+
const protocols = doc.service.protocols;
|
|
75
|
+
const graphql = protocols.graphql;
|
|
76
|
+
if (!graphql) {
|
|
77
|
+
return doc;
|
|
78
|
+
}
|
|
79
|
+
const kinds = [
|
|
80
|
+
"queries",
|
|
81
|
+
"mutations",
|
|
82
|
+
"subscriptions",
|
|
83
|
+
];
|
|
84
|
+
for (const kind of kinds) {
|
|
85
|
+
const ops = graphql[kind];
|
|
86
|
+
if (!Array.isArray(ops)) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
graphql[kind] = [...ops].sort((a, b) => {
|
|
90
|
+
const aName = a?.name ?? "";
|
|
91
|
+
const bName = b?.name ?? "";
|
|
92
|
+
return aName.localeCompare(bName);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return doc;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Normalize a UniSpec document into a canonical, deterministic form.
|
|
99
|
+
*
|
|
100
|
+
* Current behavior:
|
|
101
|
+
* - Recursively sorts object keys lexicographically.
|
|
102
|
+
* - Preserves values as-is.
|
|
103
|
+
*/
|
|
104
|
+
function normalizeUniSpec(doc, _options = {}) {
|
|
105
|
+
const normalized = normalizeValue(doc);
|
|
106
|
+
return normalizeWebSocket(normalizeGraphqlOperations(normalizeRestRoutes(normalized)));
|
|
107
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateUniSpec = validateUniSpec;
|
|
7
|
+
exports.validateUniSpecTests = validateUniSpecTests;
|
|
8
|
+
const _2020_js_1 = __importDefault(require("ajv/dist/2020.js"));
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const unispec_schema_1 = require("@unispechq/unispec-schema");
|
|
12
|
+
const ajv = new _2020_js_1.default({
|
|
13
|
+
allErrors: true,
|
|
14
|
+
strict: true,
|
|
15
|
+
});
|
|
16
|
+
// Register minimal URI format to satisfy UniSpec schemas (service.environments[*].baseUrl)
|
|
17
|
+
ajv.addFormat("uri", true);
|
|
18
|
+
// Register all UniSpec subschemas so that Ajv can resolve internal $ref links
|
|
19
|
+
try {
|
|
20
|
+
const schemaDir = path_1.default.join(process.cwd(), "node_modules", "@unispechq", "unispec-schema", "schema");
|
|
21
|
+
const types = unispec_schema_1.manifest?.types ?? {};
|
|
22
|
+
const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
|
|
23
|
+
const loadedTypeSchemas = typeSchemaPaths
|
|
24
|
+
.map((relPath) => path_1.default.join(schemaDir, relPath))
|
|
25
|
+
.filter((filePath) => fs_1.default.existsSync(filePath))
|
|
26
|
+
.map((filePath) => JSON.parse(fs_1.default.readFileSync(filePath, "utf8")));
|
|
27
|
+
ajv.addSchema(loadedTypeSchemas);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// If subschemas cannot be loaded for some reason, validation will still work for
|
|
31
|
+
// parts of the schema that do not rely on those $ref references.
|
|
32
|
+
}
|
|
33
|
+
const validateFn = ajv.compile(unispec_schema_1.unispec);
|
|
34
|
+
let validateTestsFn;
|
|
35
|
+
function mapAjvErrors(errors) {
|
|
36
|
+
if (!errors)
|
|
37
|
+
return [];
|
|
38
|
+
return errors.map((error) => ({
|
|
39
|
+
message: error.message || "UniSpec validation error",
|
|
40
|
+
path: error.instancePath || error.schemaPath,
|
|
41
|
+
code: error.keyword,
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Validate a UniSpec document against the UniSpec JSON Schema.
|
|
46
|
+
*/
|
|
47
|
+
async function validateUniSpec(doc, _options = {}) {
|
|
48
|
+
const valid = validateFn(doc);
|
|
49
|
+
if (valid) {
|
|
50
|
+
return {
|
|
51
|
+
valid: true,
|
|
52
|
+
errors: [],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
valid: false,
|
|
57
|
+
errors: mapAjvErrors(validateFn.errors),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Validate a UniSpec Tests document against the UniSpec Tests JSON Schema.
|
|
62
|
+
*/
|
|
63
|
+
async function validateUniSpecTests(doc, _options = {}) {
|
|
64
|
+
if (!validateTestsFn) {
|
|
65
|
+
const schemaDir = path_1.default.join(process.cwd(), "node_modules", "@unispechq", "unispec-schema", "schema");
|
|
66
|
+
const testsSchemaPath = path_1.default.join(schemaDir, "unispec-tests.schema.json");
|
|
67
|
+
const testsSchema = JSON.parse(fs_1.default.readFileSync(testsSchemaPath, "utf8"));
|
|
68
|
+
validateTestsFn = ajv.compile(testsSchema);
|
|
69
|
+
}
|
|
70
|
+
const valid = validateTestsFn(doc);
|
|
71
|
+
if (valid) {
|
|
72
|
+
return {
|
|
73
|
+
valid: true,
|
|
74
|
+
errors: [],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
valid: false,
|
|
79
|
+
errors: mapAjvErrors(validateTestsFn.errors),
|
|
80
|
+
};
|
|
81
|
+
}
|
package/dist/converters/index.js
CHANGED
|
@@ -1,22 +1,137 @@
|
|
|
1
1
|
export function toOpenAPI(doc) {
|
|
2
2
|
const service = doc.service;
|
|
3
|
-
const rest = service.protocols?.rest;
|
|
3
|
+
const rest = (service.protocols?.rest ?? {});
|
|
4
4
|
const info = {
|
|
5
5
|
title: service.title ?? service.name,
|
|
6
6
|
description: service.description,
|
|
7
7
|
};
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
// Derive OpenAPI servers from UniSpec environments when available.
|
|
9
|
+
const servers = Array.isArray(service.environments)
|
|
10
|
+
? service.environments.map((env) => ({
|
|
11
|
+
url: env.baseUrl,
|
|
12
|
+
description: env.name,
|
|
13
|
+
}))
|
|
14
|
+
: [];
|
|
15
|
+
const paths = {};
|
|
16
|
+
const components = {};
|
|
17
|
+
// Map service.schemas into OpenAPI components.schemas
|
|
18
|
+
const schemas = (service.schemas ?? {});
|
|
19
|
+
const componentsSchemas = {};
|
|
20
|
+
for (const [name, def] of Object.entries(schemas)) {
|
|
21
|
+
componentsSchemas[name] = def.jsonSchema;
|
|
22
|
+
}
|
|
23
|
+
if (Object.keys(componentsSchemas).length > 0) {
|
|
24
|
+
components.schemas = componentsSchemas;
|
|
25
|
+
}
|
|
26
|
+
// Helper to build a $ref into components.schemas when schemaRef is present.
|
|
27
|
+
function schemaRefToOpenAPI(schemaRef) {
|
|
28
|
+
if (!schemaRef) {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
return { $ref: `#/components/schemas/${schemaRef}` };
|
|
32
|
+
}
|
|
33
|
+
// Build paths from REST routes.
|
|
34
|
+
if (Array.isArray(rest.routes)) {
|
|
35
|
+
for (const route of rest.routes) {
|
|
36
|
+
const pathItem = (paths[route.path] ?? {});
|
|
37
|
+
const method = route.method.toLowerCase();
|
|
38
|
+
const parameters = [];
|
|
39
|
+
// Path params
|
|
40
|
+
for (const param of route.pathParams ?? []) {
|
|
41
|
+
parameters.push({
|
|
42
|
+
name: param.name,
|
|
43
|
+
in: "path",
|
|
44
|
+
required: param.required ?? true,
|
|
45
|
+
description: param.description,
|
|
46
|
+
schema: schemaRefToOpenAPI(param.schemaRef),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
// Query params
|
|
50
|
+
for (const param of route.queryParams ?? []) {
|
|
51
|
+
parameters.push({
|
|
52
|
+
name: param.name,
|
|
53
|
+
in: "query",
|
|
54
|
+
required: param.required ?? false,
|
|
55
|
+
description: param.description,
|
|
56
|
+
schema: schemaRefToOpenAPI(param.schemaRef),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
// Header params
|
|
60
|
+
for (const param of route.headers ?? []) {
|
|
61
|
+
parameters.push({
|
|
62
|
+
name: param.name,
|
|
63
|
+
in: "header",
|
|
64
|
+
required: param.required ?? false,
|
|
65
|
+
description: param.description,
|
|
66
|
+
schema: schemaRefToOpenAPI(param.schemaRef),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// Request body
|
|
70
|
+
let requestBody;
|
|
71
|
+
if (route.requestBody && route.requestBody.content) {
|
|
72
|
+
const content = {};
|
|
73
|
+
for (const [mediaType, media] of Object.entries(route.requestBody.content)) {
|
|
74
|
+
content[mediaType] = {
|
|
75
|
+
schema: schemaRefToOpenAPI(media.schemaRef),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
requestBody = {
|
|
79
|
+
description: route.requestBody.description,
|
|
80
|
+
required: route.requestBody.required,
|
|
81
|
+
content,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// Responses
|
|
85
|
+
const responses = {};
|
|
86
|
+
for (const [status, resp] of Object.entries(route.responses ?? {})) {
|
|
87
|
+
const content = {};
|
|
88
|
+
if (resp.content) {
|
|
89
|
+
for (const [mediaType, media] of Object.entries(resp.content)) {
|
|
90
|
+
content[mediaType] = {
|
|
91
|
+
schema: schemaRefToOpenAPI(media.schemaRef),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
responses[status] = {
|
|
96
|
+
description: resp.description ?? "",
|
|
97
|
+
...(Object.keys(content).length > 0 ? { content } : {}),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const operation = {
|
|
101
|
+
operationId: route.name,
|
|
102
|
+
summary: route.summary,
|
|
103
|
+
description: route.description,
|
|
104
|
+
...(parameters.length > 0 ? { parameters } : {}),
|
|
105
|
+
responses: Object.keys(responses).length > 0 ? responses : { default: { description: "" } },
|
|
106
|
+
};
|
|
107
|
+
if (requestBody) {
|
|
108
|
+
operation.requestBody = requestBody;
|
|
109
|
+
}
|
|
110
|
+
// Security requirements
|
|
111
|
+
if (Array.isArray(route.security) && route.security.length > 0) {
|
|
112
|
+
operation.security = route.security.map((req) => {
|
|
113
|
+
const obj = {};
|
|
114
|
+
for (const schemeName of req) {
|
|
115
|
+
obj[schemeName] = [];
|
|
116
|
+
}
|
|
117
|
+
return obj;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
pathItem[method] = operation;
|
|
121
|
+
paths[route.path] = pathItem;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Security schemes pass-through from REST protocol
|
|
125
|
+
const componentsSecuritySchemes = rest.securitySchemes ?? {};
|
|
126
|
+
if (Object.keys(componentsSecuritySchemes).length > 0) {
|
|
127
|
+
components.securitySchemes = componentsSecuritySchemes;
|
|
128
|
+
}
|
|
14
129
|
return {
|
|
15
130
|
openapi: "3.1.0",
|
|
16
131
|
info,
|
|
17
132
|
servers,
|
|
18
133
|
paths,
|
|
19
|
-
...
|
|
134
|
+
...(Object.keys(components).length > 0 ? { components } : {}),
|
|
20
135
|
"x-unispec": doc,
|
|
21
136
|
};
|
|
22
137
|
}
|
|
@@ -26,7 +141,7 @@ export function toGraphQLSDL(doc) {
|
|
|
26
141
|
// protocol structure yet, but provides a stable, deterministic SDL shape
|
|
27
142
|
// based on top-level UniSpec document fields.
|
|
28
143
|
const graphql = doc.service.protocols?.graphql;
|
|
29
|
-
const customSDL = graphql?.schema
|
|
144
|
+
const customSDL = graphql?.schema;
|
|
30
145
|
if (typeof customSDL === "string" && customSDL.trim()) {
|
|
31
146
|
return { sdl: customSDL };
|
|
32
147
|
}
|
|
@@ -62,20 +177,17 @@ export function toWebSocketModel(doc) {
|
|
|
62
177
|
// document under a technical key for debugging and introspection.
|
|
63
178
|
const service = doc.service;
|
|
64
179
|
const websocket = (service.protocols?.websocket ?? {});
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
raw: channel,
|
|
77
|
-
};
|
|
78
|
-
});
|
|
180
|
+
const channelsArray = Array.isArray(websocket.channels) ? websocket.channels : [];
|
|
181
|
+
const channels = [...channelsArray]
|
|
182
|
+
.sort((a, b) => (a.name ?? "").localeCompare(b.name ?? ""))
|
|
183
|
+
.map((channel) => ({
|
|
184
|
+
name: channel.name,
|
|
185
|
+
summary: undefined,
|
|
186
|
+
description: channel.description,
|
|
187
|
+
direction: channel.direction,
|
|
188
|
+
messages: channel.messages,
|
|
189
|
+
raw: channel,
|
|
190
|
+
}));
|
|
79
191
|
return {
|
|
80
192
|
service: {
|
|
81
193
|
name: service.name,
|