@unispechq/unispec-core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-publish.yml +74 -0
- package/.windsurfrules +138 -0
- package/README.md +223 -0
- 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.d.ts +6 -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 +28 -0
- package/scripts/release.js +51 -0
- package/src/converters/index.ts +120 -0
- package/src/diff/index.ts +235 -0
- package/src/index.ts +6 -0
- package/src/loader/index.ts +25 -0
- package/src/normalizer/index.ts +156 -0
- package/src/types/index.ts +67 -0
- package/src/validator/index.ts +61 -0
- package/tests/converters.test.mjs +126 -0
- package/tests/diff.test.mjs +240 -0
- package/tests/loader-validator.test.mjs +19 -0
- package/tests/normalizer.test.mjs +115 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import * as core from "../dist/index.js";
|
|
5
|
+
|
|
6
|
+
const { diffUniSpec } = core;
|
|
7
|
+
|
|
8
|
+
test("diffUniSpec detects added and removed fields", () => {
|
|
9
|
+
const oldDoc = { info: { title: "Old" } };
|
|
10
|
+
const newDoc = { info: { title: "New", description: "Desc" } };
|
|
11
|
+
|
|
12
|
+
const result = diffUniSpec(oldDoc, newDoc);
|
|
13
|
+
|
|
14
|
+
const paths = result.changes.map((c) => c.path).sort();
|
|
15
|
+
|
|
16
|
+
assert.ok(paths.includes("/info/title"));
|
|
17
|
+
assert.ok(paths.includes("/info/description"));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("diffUniSpec marks REST path and operation changes with severity and kind", () => {
|
|
21
|
+
const oldDoc = {
|
|
22
|
+
unispecVersion: "0.1.0",
|
|
23
|
+
service: {
|
|
24
|
+
name: "test-service",
|
|
25
|
+
protocols: {
|
|
26
|
+
rest: {
|
|
27
|
+
paths: {
|
|
28
|
+
"/ping": {
|
|
29
|
+
get: {},
|
|
30
|
+
},
|
|
31
|
+
"/status": {
|
|
32
|
+
get: {},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const newDoc = {
|
|
41
|
+
unispecVersion: "0.1.0",
|
|
42
|
+
service: {
|
|
43
|
+
name: "test-service",
|
|
44
|
+
protocols: {
|
|
45
|
+
rest: {
|
|
46
|
+
paths: {
|
|
47
|
+
"/ping": {
|
|
48
|
+
get: {},
|
|
49
|
+
post: {},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const result = diffUniSpec(oldDoc, newDoc);
|
|
58
|
+
|
|
59
|
+
const restChanges = result.changes.filter((c) => c.protocol === "rest");
|
|
60
|
+
|
|
61
|
+
const pathRemoved = restChanges.find((c) => c.kind === "rest.path.removed");
|
|
62
|
+
const opAdded = restChanges.find((c) => c.kind === "rest.operation.added");
|
|
63
|
+
|
|
64
|
+
assert.ok(pathRemoved);
|
|
65
|
+
assert.equal(pathRemoved.severity, "breaking");
|
|
66
|
+
|
|
67
|
+
assert.ok(opAdded);
|
|
68
|
+
assert.equal(opAdded.severity, "non-breaking");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("diffUniSpec marks WebSocket channel changes with severity and kind", () => {
|
|
72
|
+
const oldDoc = {
|
|
73
|
+
unispecVersion: "0.1.0",
|
|
74
|
+
service: {
|
|
75
|
+
name: "test-service",
|
|
76
|
+
protocols: {
|
|
77
|
+
websocket: {
|
|
78
|
+
channels: {
|
|
79
|
+
chat: {},
|
|
80
|
+
metrics: {},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const newDoc = {
|
|
88
|
+
unispecVersion: "0.1.0",
|
|
89
|
+
service: {
|
|
90
|
+
name: "test-service",
|
|
91
|
+
protocols: {
|
|
92
|
+
websocket: {
|
|
93
|
+
channels: {
|
|
94
|
+
chat: {},
|
|
95
|
+
notifications: {},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const result = diffUniSpec(oldDoc, newDoc);
|
|
103
|
+
|
|
104
|
+
const wsChanges = result.changes.filter((c) => c.protocol === "websocket");
|
|
105
|
+
|
|
106
|
+
const channelRemoved = wsChanges.find((c) => c.kind === "websocket.channel.removed");
|
|
107
|
+
const channelAdded = wsChanges.find((c) => c.kind === "websocket.channel.added");
|
|
108
|
+
|
|
109
|
+
assert.ok(channelRemoved);
|
|
110
|
+
assert.equal(channelRemoved.severity, "breaking");
|
|
111
|
+
|
|
112
|
+
assert.ok(channelAdded);
|
|
113
|
+
assert.equal(channelAdded.severity, "non-breaking");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("diffUniSpec marks WebSocket message changes with severity and kind", () => {
|
|
117
|
+
const oldDoc = {
|
|
118
|
+
unispecVersion: "0.1.0",
|
|
119
|
+
service: {
|
|
120
|
+
name: "test-service",
|
|
121
|
+
protocols: {
|
|
122
|
+
websocket: {
|
|
123
|
+
channels: {
|
|
124
|
+
chat: {
|
|
125
|
+
messages: [
|
|
126
|
+
{ name: "messageA" },
|
|
127
|
+
{ name: "messageB" },
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const newDoc = {
|
|
137
|
+
unispecVersion: "0.1.0",
|
|
138
|
+
service: {
|
|
139
|
+
name: "test-service",
|
|
140
|
+
protocols: {
|
|
141
|
+
websocket: {
|
|
142
|
+
channels: {
|
|
143
|
+
chat: {
|
|
144
|
+
messages: [
|
|
145
|
+
{ name: "messageA" },
|
|
146
|
+
{ name: "messageB" },
|
|
147
|
+
{ name: "messageC" },
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const result = diffUniSpec(oldDoc, newDoc);
|
|
157
|
+
|
|
158
|
+
const wsChangesAdd = result.changes.filter((c) => c.protocol === "websocket" && c.kind === "websocket.message.added");
|
|
159
|
+
|
|
160
|
+
assert.ok(wsChangesAdd.length > 0);
|
|
161
|
+
wsChangesAdd.forEach((change) => {
|
|
162
|
+
assert.equal(change.severity, "non-breaking");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const newDoc2 = {
|
|
166
|
+
unispecVersion: "0.1.0",
|
|
167
|
+
service: {
|
|
168
|
+
name: "test-service",
|
|
169
|
+
protocols: {
|
|
170
|
+
websocket: {
|
|
171
|
+
channels: {
|
|
172
|
+
chat: {
|
|
173
|
+
messages: [
|
|
174
|
+
{ name: "messageA" },
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const result2 = diffUniSpec(oldDoc, newDoc2);
|
|
184
|
+
|
|
185
|
+
const wsChangesRemove = result2.changes.filter((c) => c.protocol === "websocket" && c.kind === "websocket.message.removed");
|
|
186
|
+
|
|
187
|
+
assert.ok(wsChangesRemove.length > 0);
|
|
188
|
+
wsChangesRemove.forEach((change) => {
|
|
189
|
+
assert.equal(change.severity, "breaking");
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("diffUniSpec marks GraphQL operation changes with severity and kind", () => {
|
|
194
|
+
const oldDoc = {
|
|
195
|
+
unispecVersion: "0.1.0",
|
|
196
|
+
service: {
|
|
197
|
+
name: "test-service",
|
|
198
|
+
protocols: {
|
|
199
|
+
graphql: {
|
|
200
|
+
operations: {
|
|
201
|
+
queries: {
|
|
202
|
+
me: {},
|
|
203
|
+
ping: {},
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const newDoc = {
|
|
212
|
+
unispecVersion: "0.1.0",
|
|
213
|
+
service: {
|
|
214
|
+
name: "test-service",
|
|
215
|
+
protocols: {
|
|
216
|
+
graphql: {
|
|
217
|
+
operations: {
|
|
218
|
+
queries: {
|
|
219
|
+
ping: {},
|
|
220
|
+
status: {},
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const result = diffUniSpec(oldDoc, newDoc);
|
|
229
|
+
|
|
230
|
+
const graphqlChanges = result.changes.filter((c) => c.protocol === "graphql");
|
|
231
|
+
|
|
232
|
+
const opRemoved = graphqlChanges.find((c) => c.kind === "graphql.operation.removed");
|
|
233
|
+
const opAdded = graphqlChanges.find((c) => c.kind === "graphql.operation.added");
|
|
234
|
+
|
|
235
|
+
assert.ok(opRemoved);
|
|
236
|
+
assert.equal(opRemoved.severity, "breaking");
|
|
237
|
+
|
|
238
|
+
assert.ok(opAdded);
|
|
239
|
+
assert.equal(opAdded.severity, "non-breaking");
|
|
240
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import * as core from "../dist/index.js";
|
|
5
|
+
|
|
6
|
+
const { loadUniSpec, validateUniSpec } = core;
|
|
7
|
+
|
|
8
|
+
test("loadUniSpec loads JSON string into object", async () => {
|
|
9
|
+
const input = '{"info": {"title": "Test"}}';
|
|
10
|
+
const doc = await loadUniSpec(input);
|
|
11
|
+
assert.equal(typeof doc, "object");
|
|
12
|
+
assert.equal(doc.info.title, "Test");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("validateUniSpec returns ValidationResult shape", async () => {
|
|
16
|
+
const result = await validateUniSpec({});
|
|
17
|
+
assert.equal(typeof result.valid, "boolean");
|
|
18
|
+
assert.ok(Array.isArray(result.errors));
|
|
19
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import * as core from "../dist/index.js";
|
|
5
|
+
|
|
6
|
+
const { normalizeUniSpec } = core;
|
|
7
|
+
|
|
8
|
+
test("normalizeUniSpec sorts object keys deterministically", () => {
|
|
9
|
+
const doc = {
|
|
10
|
+
b: 1,
|
|
11
|
+
a: {
|
|
12
|
+
d: 3,
|
|
13
|
+
c: 2,
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const normalized = normalizeUniSpec(doc);
|
|
18
|
+
|
|
19
|
+
assert.deepEqual(Object.keys(normalized), ["a", "b"]);
|
|
20
|
+
assert.deepEqual(Object.keys(normalized.a), ["c", "d"]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("normalizeUniSpec normalizes REST paths and HTTP methods order", () => {
|
|
24
|
+
const doc = {
|
|
25
|
+
unispecVersion: "0.1.0",
|
|
26
|
+
service: {
|
|
27
|
+
name: "test-service",
|
|
28
|
+
protocols: {
|
|
29
|
+
rest: {
|
|
30
|
+
paths: {
|
|
31
|
+
"/z": {
|
|
32
|
+
post: {},
|
|
33
|
+
get: {},
|
|
34
|
+
},
|
|
35
|
+
"/a": {
|
|
36
|
+
delete: {},
|
|
37
|
+
get: {},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const normalized = normalizeUniSpec(doc);
|
|
46
|
+
|
|
47
|
+
const paths = normalized.service.protocols.rest.paths;
|
|
48
|
+
|
|
49
|
+
assert.deepEqual(Object.keys(paths), ["/a", "/z"]);
|
|
50
|
+
assert.deepEqual(Object.keys(paths["/z"]), ["get", "post"]);
|
|
51
|
+
assert.deepEqual(Object.keys(paths["/a"]), ["get", "delete"]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("normalizeUniSpec normalizes GraphQL operations order", () => {
|
|
55
|
+
const doc = {
|
|
56
|
+
unispecVersion: "0.1.0",
|
|
57
|
+
service: {
|
|
58
|
+
name: "test-service",
|
|
59
|
+
protocols: {
|
|
60
|
+
graphql: {
|
|
61
|
+
operations: {
|
|
62
|
+
queries: {
|
|
63
|
+
me: {},
|
|
64
|
+
ping: {},
|
|
65
|
+
a: {},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const normalized = normalizeUniSpec(doc);
|
|
74
|
+
|
|
75
|
+
const queries = normalized.service.protocols.graphql.operations.queries;
|
|
76
|
+
assert.deepEqual(Object.keys(queries), ["a", "me", "ping"]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("normalizeUniSpec normalizes WebSocket channels and messages order", () => {
|
|
80
|
+
const doc = {
|
|
81
|
+
unispecVersion: "0.1.0",
|
|
82
|
+
service: {
|
|
83
|
+
name: "test-service",
|
|
84
|
+
protocols: {
|
|
85
|
+
websocket: {
|
|
86
|
+
channels: {
|
|
87
|
+
zChannel: {
|
|
88
|
+
messages: [
|
|
89
|
+
{ name: "msgB" },
|
|
90
|
+
{ name: "msgA" },
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
aChannel: {
|
|
94
|
+
messages: [
|
|
95
|
+
{ name: "z" },
|
|
96
|
+
{ name: "a" },
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const normalized = normalizeUniSpec(doc);
|
|
106
|
+
|
|
107
|
+
const channels = normalized.service.protocols.websocket.channels;
|
|
108
|
+
assert.deepEqual(Object.keys(channels), ["aChannel", "zChannel"]);
|
|
109
|
+
|
|
110
|
+
const aMessages = channels.aChannel.messages;
|
|
111
|
+
const zMessages = channels.zChannel.messages;
|
|
112
|
+
|
|
113
|
+
assert.deepEqual(aMessages.map((m) => m.name), ["a", "z"]);
|
|
114
|
+
assert.deepEqual(zMessages.map((m) => m.name), ["msgA", "msgB"]);
|
|
115
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Node",
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"rootDir": "src",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"skipLibCheck": true
|
|
13
|
+
},
|
|
14
|
+
"include": ["src"]
|
|
15
|
+
}
|