@unispechq/unispec-core 0.1.1 → 0.1.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/dist/converters/index.d.ts +13 -0
- package/dist/converters/index.js +89 -0
- package/dist/diff/index.d.ts +21 -0
- package/dist/diff/index.js +195 -0
- package/dist/index.js +6 -0
- package/dist/loader/index.d.ts +13 -0
- package/dist/loader/index.js +19 -0
- package/dist/normalizer/index.d.ts +11 -0
- package/dist/normalizer/index.js +116 -0
- package/dist/types/index.d.ts +57 -0
- package/dist/types/index.js +1 -0
- package/dist/validator/index.d.ts +7 -0
- package/dist/validator/index.js +47 -0
- package/package.json +5 -1
- package/.github/workflows/npm-publish.yml +0 -74
- package/.windsurfrules +0 -138
- package/scripts/release.js +0 -51
- package/src/converters/index.ts +0 -120
- package/src/diff/index.ts +0 -235
- package/src/loader/index.ts +0 -25
- package/src/normalizer/index.ts +0 -156
- package/src/types/index.ts +0 -67
- package/src/validator/index.ts +0 -61
- package/tests/converters.test.mjs +0 -126
- package/tests/diff.test.mjs +0 -240
- package/tests/loader-validator.test.mjs +0 -19
- package/tests/normalizer.test.mjs +0 -115
- package/tsconfig.json +0 -15
- /package/{src/index.ts → dist/index.d.ts} +0 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { UniSpecDocument } from "../types";
|
|
2
|
+
export interface OpenAPIDocument {
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
}
|
|
5
|
+
export interface GraphQLSDLOutput {
|
|
6
|
+
sdl: string;
|
|
7
|
+
}
|
|
8
|
+
export interface WebSocketModel {
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
export declare function toOpenAPI(doc: UniSpecDocument): OpenAPIDocument;
|
|
12
|
+
export declare function toGraphQLSDL(doc: UniSpecDocument): GraphQLSDLOutput;
|
|
13
|
+
export declare function toWebSocketModel(doc: UniSpecDocument): WebSocketModel;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export function toOpenAPI(doc) {
|
|
2
|
+
const service = doc.service;
|
|
3
|
+
const rest = service.protocols?.rest;
|
|
4
|
+
const info = {
|
|
5
|
+
title: service.title ?? service.name,
|
|
6
|
+
description: service.description,
|
|
7
|
+
};
|
|
8
|
+
const servers = rest?.servers ?? [];
|
|
9
|
+
const paths = rest?.paths ?? {};
|
|
10
|
+
// Transparently forward additional REST protocol fields into the OpenAPI document.
|
|
11
|
+
// This allows users to describe components, security, tags and other structures
|
|
12
|
+
// without forcing a specific REST model at the core layer.
|
|
13
|
+
const { servers: _omitServers, paths: _omitPaths, ...restExtras } = rest ?? {};
|
|
14
|
+
return {
|
|
15
|
+
openapi: "3.1.0",
|
|
16
|
+
info,
|
|
17
|
+
servers,
|
|
18
|
+
paths,
|
|
19
|
+
...restExtras,
|
|
20
|
+
"x-unispec": doc,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function toGraphQLSDL(doc) {
|
|
24
|
+
// Minimal implementation: generate a basic SDL that exposes service metadata
|
|
25
|
+
// via a Query field. This does not attempt to interpret the full GraphQL
|
|
26
|
+
// protocol structure yet, but provides a stable, deterministic SDL shape
|
|
27
|
+
// based on top-level UniSpec document fields.
|
|
28
|
+
const graphql = doc.service.protocols?.graphql;
|
|
29
|
+
const customSDL = graphql?.schema?.sdl;
|
|
30
|
+
if (typeof customSDL === "string" && customSDL.trim()) {
|
|
31
|
+
return { sdl: customSDL };
|
|
32
|
+
}
|
|
33
|
+
const service = doc.service;
|
|
34
|
+
const title = service.title ?? service.name;
|
|
35
|
+
const description = service.description ?? "";
|
|
36
|
+
const lines = [];
|
|
37
|
+
if (title || description) {
|
|
38
|
+
lines.push("\"\"");
|
|
39
|
+
if (title) {
|
|
40
|
+
lines.push(title);
|
|
41
|
+
}
|
|
42
|
+
if (description) {
|
|
43
|
+
lines.push("");
|
|
44
|
+
lines.push(description);
|
|
45
|
+
}
|
|
46
|
+
lines.push("\"\"");
|
|
47
|
+
}
|
|
48
|
+
lines.push("schema {");
|
|
49
|
+
lines.push(" query: Query");
|
|
50
|
+
lines.push("}");
|
|
51
|
+
lines.push("");
|
|
52
|
+
lines.push("type Query {");
|
|
53
|
+
lines.push(" _serviceInfo: String!\n");
|
|
54
|
+
lines.push("}");
|
|
55
|
+
const sdl = lines.join("\n");
|
|
56
|
+
return { sdl };
|
|
57
|
+
}
|
|
58
|
+
export function toWebSocketModel(doc) {
|
|
59
|
+
// Base WebSocket model intended for a modern, dashboard-oriented UI.
|
|
60
|
+
// It exposes service metadata, a normalized list of channels and the raw
|
|
61
|
+
// websocket protocol object, while also embedding the original UniSpec
|
|
62
|
+
// document under a technical key for debugging and introspection.
|
|
63
|
+
const service = doc.service;
|
|
64
|
+
const websocket = (service.protocols?.websocket ?? {});
|
|
65
|
+
const channelsRecord = (websocket && typeof websocket === "object" && websocket.channels && typeof websocket.channels === "object")
|
|
66
|
+
? websocket.channels
|
|
67
|
+
: {};
|
|
68
|
+
const channels = Object.keys(channelsRecord).sort().map((name) => {
|
|
69
|
+
const channel = channelsRecord[name] ?? {};
|
|
70
|
+
return {
|
|
71
|
+
name,
|
|
72
|
+
summary: channel.summary ?? channel.title,
|
|
73
|
+
description: channel.description,
|
|
74
|
+
direction: channel.direction,
|
|
75
|
+
messages: channel.messages,
|
|
76
|
+
raw: channel,
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
return {
|
|
80
|
+
service: {
|
|
81
|
+
name: service.name,
|
|
82
|
+
title: service.title,
|
|
83
|
+
description: service.description,
|
|
84
|
+
},
|
|
85
|
+
channels,
|
|
86
|
+
rawProtocol: websocket,
|
|
87
|
+
"x-unispec-ws": doc,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { UniSpecDocument } from "../types";
|
|
2
|
+
export type ChangeSeverity = "breaking" | "non-breaking" | "unknown";
|
|
3
|
+
export interface UniSpecChange {
|
|
4
|
+
path: string;
|
|
5
|
+
description: string;
|
|
6
|
+
severity: ChangeSeverity;
|
|
7
|
+
protocol?: "rest" | "graphql" | "websocket";
|
|
8
|
+
kind?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface DiffResult {
|
|
11
|
+
changes: UniSpecChange[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Compute a structural diff between two UniSpec documents.
|
|
15
|
+
*
|
|
16
|
+
* Current behavior:
|
|
17
|
+
* - Tracks added, removed, and changed fields and array items.
|
|
18
|
+
* - Uses JSON Pointer-like paths rooted at "" (e.g., "/info/title").
|
|
19
|
+
* - Marks all changes with severity "unknown" for now.
|
|
20
|
+
*/
|
|
21
|
+
export declare function diffUniSpec(oldDoc: UniSpecDocument, newDoc: UniSpecDocument): DiffResult;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
function isPlainObject(value) {
|
|
2
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
3
|
+
}
|
|
4
|
+
function diffValues(oldVal, newVal, basePath, out) {
|
|
5
|
+
if (oldVal === newVal) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
// Both plain objects → recurse by keys
|
|
9
|
+
if (isPlainObject(oldVal) && isPlainObject(newVal)) {
|
|
10
|
+
const oldKeys = new Set(Object.keys(oldVal));
|
|
11
|
+
const newKeys = new Set(Object.keys(newVal));
|
|
12
|
+
// Removed keys
|
|
13
|
+
for (const key of oldKeys) {
|
|
14
|
+
if (!newKeys.has(key)) {
|
|
15
|
+
out.push({
|
|
16
|
+
path: `${basePath}/${key}`,
|
|
17
|
+
description: "Field removed",
|
|
18
|
+
severity: "unknown",
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// Added / changed keys
|
|
23
|
+
for (const key of newKeys) {
|
|
24
|
+
const childPath = `${basePath}/${key}`;
|
|
25
|
+
if (!oldKeys.has(key)) {
|
|
26
|
+
out.push({
|
|
27
|
+
path: childPath,
|
|
28
|
+
description: "Field added",
|
|
29
|
+
severity: "unknown",
|
|
30
|
+
});
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
diffValues(oldVal[key], newVal[key], childPath, out);
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// Arrays → shallow compare by index for now
|
|
38
|
+
if (Array.isArray(oldVal) && Array.isArray(newVal)) {
|
|
39
|
+
const maxLen = Math.max(oldVal.length, newVal.length);
|
|
40
|
+
for (let i = 0; i < maxLen; i++) {
|
|
41
|
+
const childPath = `${basePath}/${i}`;
|
|
42
|
+
if (i >= oldVal.length) {
|
|
43
|
+
out.push({
|
|
44
|
+
path: childPath,
|
|
45
|
+
description: "Item added",
|
|
46
|
+
severity: "unknown",
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else if (i >= newVal.length) {
|
|
50
|
+
out.push({
|
|
51
|
+
path: childPath,
|
|
52
|
+
description: "Item removed",
|
|
53
|
+
severity: "unknown",
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
diffValues(oldVal[i], newVal[i], childPath, out);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
// Primitive or mismatched types → treat as value change
|
|
63
|
+
out.push({
|
|
64
|
+
path: basePath,
|
|
65
|
+
description: "Value changed",
|
|
66
|
+
severity: "unknown",
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function annotateRestChange(change) {
|
|
70
|
+
if (!change.path.startsWith("/service/protocols/rest/paths/")) {
|
|
71
|
+
return change;
|
|
72
|
+
}
|
|
73
|
+
const segments = change.path.split("/").filter(Boolean);
|
|
74
|
+
// Expected shape: ["service", "protocols", "rest", "paths", pathKey?, method?]
|
|
75
|
+
if (segments[0] !== "service" || segments[1] !== "protocols" || segments[2] !== "rest" || segments[3] !== "paths") {
|
|
76
|
+
return change;
|
|
77
|
+
}
|
|
78
|
+
const pathKey = segments[4];
|
|
79
|
+
const method = segments[5];
|
|
80
|
+
const httpMethods = new Set(["get", "head", "options", "post", "put", "patch", "delete"]);
|
|
81
|
+
const annotated = {
|
|
82
|
+
...change,
|
|
83
|
+
protocol: "rest",
|
|
84
|
+
};
|
|
85
|
+
if (change.description === "Field removed") {
|
|
86
|
+
if (pathKey && !method) {
|
|
87
|
+
annotated.kind = "rest.path.removed";
|
|
88
|
+
annotated.severity = "breaking";
|
|
89
|
+
}
|
|
90
|
+
else if (pathKey && method && httpMethods.has(method)) {
|
|
91
|
+
annotated.kind = "rest.operation.removed";
|
|
92
|
+
annotated.severity = "breaking";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (change.description === "Field added") {
|
|
96
|
+
if (pathKey && !method) {
|
|
97
|
+
annotated.kind = "rest.path.added";
|
|
98
|
+
annotated.severity = "non-breaking";
|
|
99
|
+
}
|
|
100
|
+
else if (pathKey && method && httpMethods.has(method)) {
|
|
101
|
+
annotated.kind = "rest.operation.added";
|
|
102
|
+
annotated.severity = "non-breaking";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return annotated;
|
|
106
|
+
}
|
|
107
|
+
function annotateWebSocketChange(change) {
|
|
108
|
+
if (!change.path.startsWith("/service/protocols/websocket/channels/")) {
|
|
109
|
+
return change;
|
|
110
|
+
}
|
|
111
|
+
const segments = change.path.split("/").filter(Boolean);
|
|
112
|
+
// Expected: ["service","protocols","websocket","channels", channelName, ...]
|
|
113
|
+
if (segments[0] !== "service" || segments[1] !== "protocols" || segments[2] !== "websocket" || segments[3] !== "channels") {
|
|
114
|
+
return change;
|
|
115
|
+
}
|
|
116
|
+
const channelName = segments[4];
|
|
117
|
+
const next = segments[5];
|
|
118
|
+
const annotated = {
|
|
119
|
+
...change,
|
|
120
|
+
protocol: "websocket",
|
|
121
|
+
};
|
|
122
|
+
if (!channelName) {
|
|
123
|
+
return annotated;
|
|
124
|
+
}
|
|
125
|
+
// Channel-level changes
|
|
126
|
+
if (!next) {
|
|
127
|
+
if (change.description === "Field removed") {
|
|
128
|
+
annotated.kind = "websocket.channel.removed";
|
|
129
|
+
annotated.severity = "breaking";
|
|
130
|
+
}
|
|
131
|
+
else if (change.description === "Field added") {
|
|
132
|
+
annotated.kind = "websocket.channel.added";
|
|
133
|
+
annotated.severity = "non-breaking";
|
|
134
|
+
}
|
|
135
|
+
return annotated;
|
|
136
|
+
}
|
|
137
|
+
// Message-level changes (channels/{channelName}/messages/{index})
|
|
138
|
+
if (next === "messages") {
|
|
139
|
+
const index = segments[6];
|
|
140
|
+
if (typeof index === "undefined") {
|
|
141
|
+
return annotated;
|
|
142
|
+
}
|
|
143
|
+
if (change.description === "Item removed") {
|
|
144
|
+
annotated.kind = "websocket.message.removed";
|
|
145
|
+
annotated.severity = "breaking";
|
|
146
|
+
}
|
|
147
|
+
else if (change.description === "Item added") {
|
|
148
|
+
annotated.kind = "websocket.message.added";
|
|
149
|
+
annotated.severity = "non-breaking";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return annotated;
|
|
153
|
+
}
|
|
154
|
+
function annotateGraphQLChange(change) {
|
|
155
|
+
if (!change.path.startsWith("/service/protocols/graphql/operations/")) {
|
|
156
|
+
return change;
|
|
157
|
+
}
|
|
158
|
+
const segments = change.path.split("/").filter(Boolean);
|
|
159
|
+
// Expected: ["service","protocols","graphql","operations", kind, opName?]
|
|
160
|
+
if (segments[0] !== "service" || segments[1] !== "protocols" || segments[2] !== "graphql" || segments[3] !== "operations") {
|
|
161
|
+
return change;
|
|
162
|
+
}
|
|
163
|
+
const opKind = segments[4];
|
|
164
|
+
const opName = segments[5];
|
|
165
|
+
if (!opKind || !opName) {
|
|
166
|
+
return change;
|
|
167
|
+
}
|
|
168
|
+
const annotated = {
|
|
169
|
+
...change,
|
|
170
|
+
protocol: "graphql",
|
|
171
|
+
};
|
|
172
|
+
if (change.description === "Field removed") {
|
|
173
|
+
annotated.kind = "graphql.operation.removed";
|
|
174
|
+
annotated.severity = "breaking";
|
|
175
|
+
}
|
|
176
|
+
else if (change.description === "Field added") {
|
|
177
|
+
annotated.kind = "graphql.operation.added";
|
|
178
|
+
annotated.severity = "non-breaking";
|
|
179
|
+
}
|
|
180
|
+
return annotated;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Compute a structural diff between two UniSpec documents.
|
|
184
|
+
*
|
|
185
|
+
* Current behavior:
|
|
186
|
+
* - Tracks added, removed, and changed fields and array items.
|
|
187
|
+
* - Uses JSON Pointer-like paths rooted at "" (e.g., "/info/title").
|
|
188
|
+
* - Marks all changes with severity "unknown" for now.
|
|
189
|
+
*/
|
|
190
|
+
export function diffUniSpec(oldDoc, newDoc) {
|
|
191
|
+
const changes = [];
|
|
192
|
+
diffValues(oldDoc, newDoc, "", changes);
|
|
193
|
+
const annotated = changes.map((change) => annotateWebSocketChange(annotateGraphQLChange(annotateRestChange(change))));
|
|
194
|
+
return { changes: annotated };
|
|
195
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { UniSpecDocument } from "../types";
|
|
2
|
+
export interface LoadOptions {
|
|
3
|
+
filename?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Load a UniSpec document from a raw input value.
|
|
7
|
+
* Currently supports:
|
|
8
|
+
* - JavaScript objects (treated as already parsed UniSpec)
|
|
9
|
+
* - JSON strings
|
|
10
|
+
*
|
|
11
|
+
* YAML and filesystem helpers will be added later, keeping this API stable.
|
|
12
|
+
*/
|
|
13
|
+
export declare function loadUniSpec(input: string | object, _options?: LoadOptions): Promise<UniSpecDocument>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load a UniSpec document from a raw input value.
|
|
3
|
+
* Currently supports:
|
|
4
|
+
* - JavaScript objects (treated as already parsed UniSpec)
|
|
5
|
+
* - JSON strings
|
|
6
|
+
*
|
|
7
|
+
* YAML and filesystem helpers will be added later, keeping this API stable.
|
|
8
|
+
*/
|
|
9
|
+
export async function loadUniSpec(input, _options = {}) {
|
|
10
|
+
if (typeof input === "string") {
|
|
11
|
+
const trimmed = input.trim();
|
|
12
|
+
if (!trimmed) {
|
|
13
|
+
throw new Error("Cannot load UniSpec: input string is empty");
|
|
14
|
+
}
|
|
15
|
+
// For now we assume JSON; YAML support will be added later.
|
|
16
|
+
return JSON.parse(trimmed);
|
|
17
|
+
}
|
|
18
|
+
return input;
|
|
19
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UniSpecDocument } from "../types";
|
|
2
|
+
export interface NormalizeOptions {
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Normalize a UniSpec document into a canonical, deterministic form.
|
|
6
|
+
*
|
|
7
|
+
* Current behavior:
|
|
8
|
+
* - Recursively sorts object keys lexicographically.
|
|
9
|
+
* - Preserves values as-is.
|
|
10
|
+
*/
|
|
11
|
+
export declare function normalizeUniSpec(doc: UniSpecDocument, _options?: NormalizeOptions): UniSpecDocument;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
function isPlainObject(value) {
|
|
2
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
3
|
+
}
|
|
4
|
+
function normalizeValue(value) {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
return value.map((item) => normalizeValue(item));
|
|
7
|
+
}
|
|
8
|
+
if (isPlainObject(value)) {
|
|
9
|
+
const entries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b));
|
|
10
|
+
const normalized = {};
|
|
11
|
+
for (const [key, val] of entries) {
|
|
12
|
+
normalized[key] = normalizeValue(val);
|
|
13
|
+
}
|
|
14
|
+
return normalized;
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
function normalizeRestPaths(doc) {
|
|
19
|
+
if (!doc || !doc.service || !doc.service.protocols) {
|
|
20
|
+
return doc;
|
|
21
|
+
}
|
|
22
|
+
const protocols = doc.service.protocols;
|
|
23
|
+
const rest = protocols.rest;
|
|
24
|
+
if (!rest || !rest.paths || typeof rest.paths !== "object") {
|
|
25
|
+
return doc;
|
|
26
|
+
}
|
|
27
|
+
const httpMethodOrder = ["get", "head", "options", "post", "put", "patch", "delete"];
|
|
28
|
+
const normalizedPaths = {};
|
|
29
|
+
for (const path of Object.keys(rest.paths).sort()) {
|
|
30
|
+
const pathItem = rest.paths[path];
|
|
31
|
+
if (!pathItem || typeof pathItem !== "object") {
|
|
32
|
+
normalizedPaths[path] = pathItem;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const pathItemObj = pathItem;
|
|
36
|
+
const ordered = {};
|
|
37
|
+
for (const method of httpMethodOrder) {
|
|
38
|
+
if (Object.prototype.hasOwnProperty.call(pathItemObj, method)) {
|
|
39
|
+
ordered[method] = pathItemObj[method];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const remainingKeys = Object.keys(pathItemObj).filter((k) => !httpMethodOrder.includes(k)).sort();
|
|
43
|
+
for (const key of remainingKeys) {
|
|
44
|
+
ordered[key] = pathItemObj[key];
|
|
45
|
+
}
|
|
46
|
+
normalizedPaths[path] = ordered;
|
|
47
|
+
}
|
|
48
|
+
rest.paths = normalizedPaths;
|
|
49
|
+
return doc;
|
|
50
|
+
}
|
|
51
|
+
function normalizeWebSocket(doc) {
|
|
52
|
+
if (!doc || !doc.service || !doc.service.protocols) {
|
|
53
|
+
return doc;
|
|
54
|
+
}
|
|
55
|
+
const protocols = doc.service.protocols;
|
|
56
|
+
const websocket = protocols.websocket;
|
|
57
|
+
if (!websocket || !websocket.channels || typeof websocket.channels !== "object") {
|
|
58
|
+
return doc;
|
|
59
|
+
}
|
|
60
|
+
const originalChannels = websocket.channels;
|
|
61
|
+
const sortedChannels = {};
|
|
62
|
+
for (const name of Object.keys(originalChannels).sort()) {
|
|
63
|
+
const channel = originalChannels[name];
|
|
64
|
+
if (!channel) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
let messages = channel.messages;
|
|
68
|
+
if (Array.isArray(messages)) {
|
|
69
|
+
messages = [...messages].sort((a, b) => {
|
|
70
|
+
const aName = a?.name ?? "";
|
|
71
|
+
const bName = b?.name ?? "";
|
|
72
|
+
return aName.localeCompare(bName);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
sortedChannels[name] = {
|
|
76
|
+
...channel,
|
|
77
|
+
...(messages ? { messages } : {}),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
websocket.channels = sortedChannels;
|
|
81
|
+
return doc;
|
|
82
|
+
}
|
|
83
|
+
function normalizeGraphqlOperations(doc) {
|
|
84
|
+
if (!doc || !doc.service || !doc.service.protocols) {
|
|
85
|
+
return doc;
|
|
86
|
+
}
|
|
87
|
+
const protocols = doc.service.protocols;
|
|
88
|
+
const graphql = protocols.graphql;
|
|
89
|
+
if (!graphql || !graphql.operations) {
|
|
90
|
+
return doc;
|
|
91
|
+
}
|
|
92
|
+
const kinds = ["queries", "mutations", "subscriptions"];
|
|
93
|
+
for (const kind of kinds) {
|
|
94
|
+
const bucket = graphql.operations[kind];
|
|
95
|
+
if (!bucket || typeof bucket !== "object") {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const sorted = {};
|
|
99
|
+
for (const name of Object.keys(bucket).sort()) {
|
|
100
|
+
sorted[name] = bucket[name];
|
|
101
|
+
}
|
|
102
|
+
graphql.operations[kind] = sorted;
|
|
103
|
+
}
|
|
104
|
+
return doc;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Normalize a UniSpec document into a canonical, deterministic form.
|
|
108
|
+
*
|
|
109
|
+
* Current behavior:
|
|
110
|
+
* - Recursively sorts object keys lexicographically.
|
|
111
|
+
* - Preserves values as-is.
|
|
112
|
+
*/
|
|
113
|
+
export function normalizeUniSpec(doc, _options = {}) {
|
|
114
|
+
const normalized = normalizeValue(doc);
|
|
115
|
+
return normalizeWebSocket(normalizeGraphqlOperations(normalizeRestPaths(normalized)));
|
|
116
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export interface UniSpecGraphQLSchema {
|
|
2
|
+
sdl?: string;
|
|
3
|
+
}
|
|
4
|
+
export interface UniSpecGraphQLOperations {
|
|
5
|
+
queries?: Record<string, unknown>;
|
|
6
|
+
mutations?: Record<string, unknown>;
|
|
7
|
+
subscriptions?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
export interface UniSpecGraphQLProtocol {
|
|
10
|
+
schema?: UniSpecGraphQLSchema;
|
|
11
|
+
operations?: UniSpecGraphQLOperations;
|
|
12
|
+
extensions?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
export interface UniSpecWebSocketMessage {
|
|
15
|
+
name?: string;
|
|
16
|
+
summary?: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
payload?: unknown;
|
|
19
|
+
extensions?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
export interface UniSpecWebSocketChannel {
|
|
22
|
+
summary?: string;
|
|
23
|
+
title?: string;
|
|
24
|
+
description?: string;
|
|
25
|
+
direction?: string;
|
|
26
|
+
messages?: UniSpecWebSocketMessage[];
|
|
27
|
+
extensions?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
export interface UniSpecWebSocketProtocol {
|
|
30
|
+
channels?: Record<string, UniSpecWebSocketChannel>;
|
|
31
|
+
extensions?: Record<string, unknown>;
|
|
32
|
+
}
|
|
33
|
+
export interface UniSpecServiceProtocols {
|
|
34
|
+
rest?: unknown;
|
|
35
|
+
graphql?: UniSpecGraphQLProtocol;
|
|
36
|
+
websocket?: UniSpecWebSocketProtocol;
|
|
37
|
+
}
|
|
38
|
+
export interface UniSpecService {
|
|
39
|
+
name: string;
|
|
40
|
+
title?: string;
|
|
41
|
+
description?: string;
|
|
42
|
+
protocols?: UniSpecServiceProtocols;
|
|
43
|
+
}
|
|
44
|
+
export interface UniSpecDocument {
|
|
45
|
+
unispecVersion: string;
|
|
46
|
+
service: UniSpecService;
|
|
47
|
+
extensions?: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
export interface ValidationError {
|
|
50
|
+
message: string;
|
|
51
|
+
path?: string;
|
|
52
|
+
code?: string;
|
|
53
|
+
}
|
|
54
|
+
export interface ValidationResult {
|
|
55
|
+
valid: boolean;
|
|
56
|
+
errors: ValidationError[];
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { UniSpecDocument, ValidationResult } from "../types";
|
|
2
|
+
export interface ValidateOptions {
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Validate a UniSpec document against the UniSpec JSON Schema.
|
|
6
|
+
*/
|
|
7
|
+
export declare function validateUniSpec(doc: UniSpecDocument, _options?: ValidateOptions): Promise<ValidationResult>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Ajv2020 from "ajv/dist/2020.js";
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import { unispec as unispecSchema, manifest as unispecManifest } from "@unispechq/unispec-schema";
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
const ajv = new Ajv2020({
|
|
6
|
+
allErrors: true,
|
|
7
|
+
strict: true,
|
|
8
|
+
});
|
|
9
|
+
// Register all UniSpec subschemas so that Ajv can resolve internal $ref links
|
|
10
|
+
try {
|
|
11
|
+
const schemaRootPath = require.resolve("@unispechq/unispec-schema");
|
|
12
|
+
const schemaDir = schemaRootPath.replace(/index\.(cjs|mjs|js)$/u, "schema/");
|
|
13
|
+
const types = unispecManifest?.types ?? {};
|
|
14
|
+
const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
|
|
15
|
+
const loadedTypeSchemas = typeSchemaPaths.map((relPath) => require(schemaDir + relPath));
|
|
16
|
+
ajv.addSchema(loadedTypeSchemas);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// If subschemas cannot be loaded for some reason, validation will still work for
|
|
20
|
+
// parts of the schema that do not rely on those $ref references.
|
|
21
|
+
}
|
|
22
|
+
const validateFn = ajv.compile(unispecSchema);
|
|
23
|
+
function mapAjvErrors(errors) {
|
|
24
|
+
if (!errors)
|
|
25
|
+
return [];
|
|
26
|
+
return errors.map((error) => ({
|
|
27
|
+
message: error.message || "UniSpec validation error",
|
|
28
|
+
path: error.instancePath || error.schemaPath,
|
|
29
|
+
code: error.keyword,
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Validate a UniSpec document against the UniSpec JSON Schema.
|
|
34
|
+
*/
|
|
35
|
+
export async function validateUniSpec(doc, _options = {}) {
|
|
36
|
+
const valid = validateFn(doc);
|
|
37
|
+
if (valid) {
|
|
38
|
+
return {
|
|
39
|
+
valid: true,
|
|
40
|
+
errors: [],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
valid: false,
|
|
45
|
+
errors: mapAjvErrors(validateFn.errors),
|
|
46
|
+
};
|
|
47
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unispechq/unispec-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Central UniSpec Core Engine providing parsing, validation, normalization, diffing, and conversion of UniSpec specs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.cjs",
|
|
8
8
|
"module": "dist/index.js",
|
|
9
9
|
"types": "dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
10
14
|
"scripts": {
|
|
11
15
|
"build": "tsc -p tsconfig.json",
|
|
12
16
|
"test": "npm run build && node --test tests/*.test.mjs",
|