@unispechq/unispec-core 0.2.0 → 0.2.3

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 CHANGED
@@ -1,223 +1,223 @@
1
- # UniSpec Core Platform
2
- **The official UniSpec Core Engine package — parser, validator, normalizer, diff engine, and converters for the UniSpec format**
3
-
4
- ---
5
-
6
- ## 🚀 Overview
7
-
8
- **UniSpec Core Engine** is the central runtime package of the UniSpec ecosystem.
9
- It implements the mechanics of the **UniSpec Format**: loading, JSON Schema validation, normalization, diffing, and conversion to other formats.
10
-
11
- This repository contains **only the Core Engine**, which is consumed by other UniSpec platform components (CLI, Registry, Portal, adapters, SDKs) that live in separate repositories.
12
-
13
- Core Engine capabilities:
14
-
15
- - 🧠 **Core Engine** — parser, validator, normalizer, diff engine, converters
16
- - 🔄 **Format conversion** — UniSpec → OpenAPI, UniSpec → GraphQL SDL, UniSpec → WebSocket channel models
17
- - 🧩 **Shared types and utilities** — types and helper functions used by the CLI, adapters, Registry, and Portal
18
-
19
- UniSpec is designed to unify documentation for:
20
-
21
- - REST
22
- - GraphQL
23
- - WebSocket
24
- - Event-driven APIs (future)
25
- - Multi-service architectures
26
-
27
- ---
28
-
29
- ## 📦 Repository Structure
30
-
31
- ```pgsql
32
- unispec-core/
33
- ├─ src/
34
- │ ├─ loader/ # File loading (YAML/JSON → JS object)
35
- │ ├─ validator/ # JSON Schema validation
36
- │ ├─ normalizer/ # UniSpec structure normalization
37
- │ ├─ diff/ # Comparison of two UniSpec documents
38
- │ ├─ converters/ # OpenAPI, GraphQL SDL, WS, etc.
39
- │ ├─ types/ # UniSpec types/interfaces
40
- │ ├─ utils/ # Small helpers
41
- │ └─ index.ts # Public API
42
- ├─ tests/ # Unit tests
43
- ├─ package.json
44
- └─ README.md
45
- ```
46
-
47
- ---
48
-
49
- ## 🧠 Core Concepts
50
-
51
- ### 📐 1. UniSpec Format
52
- Defined in a separate repository: **`unispec-spec`**.
53
- UniSpec Core Engine *implements* this format but does **not** define it.
54
-
55
- ### 🧱 2. Core Engine
56
- Located in this repository, under the `src/` directory.
57
-
58
- Core Engine provides:
59
-
60
- - YAML/JSON loader
61
- - JSON Schema validator
62
- – Normalizer → canonical UniSpec output (REST routes, GraphQL operations, WebSocket channels/messages)
63
- – Diff engine (with basic breaking / non-breaking classification for REST, GraphQL and WebSocket)
64
- – Converters:
65
- - UniSpec → OpenAPI (REST-centric)
66
- - UniSpec → GraphQL SDL
67
- - UniSpec → WebSocket channel models for dashboards
68
-
69
- This is the foundation used by:
70
-
71
- - the CLI (in a separate repository/package)
72
- - framework adapters
73
- - Registry and Portal (as separate UniSpec platform services)
74
-
75
- ### 🌉 3. Protocol Coverage in Core
76
-
77
- At the level of the Core Engine, protocol support is intentionally minimal but deterministic:
78
-
79
- - **REST**
80
- - Typed REST surface defined by `service.protocols.rest.routes[]` and reusable `service.schemas`.
81
- - Normalizer orders routes by `name` (or `path + method`) for stable diffs.
82
- - Diff engine annotates route additions and removals with breaking/non-breaking severity.
83
- - Converter exposes REST as an OpenAPI 3.1 document built from routes, schemas and environments, while preserving the original UniSpec under `x-unispec`.
84
-
85
- - **GraphQL**
86
- - Typed protocol with `schema` (SDL string) and `queries`/`mutations`/`subscriptions` as operation lists.
87
- - Normalizer orders operation names within each bucket.
88
- - Diff engine annotates operation additions/removals as non-breaking/breaking.
89
- - Converter either passes through user-provided SDL or generates a minimal, deterministic SDL shell.
90
-
91
- - **WebSocket**
92
- - Typed protocol with channels and messages backed by reusable schemas and security schemes.
93
- - Normalizer orders channels by name and messages by `name` within each channel.
94
- - Diff engine annotates channel/message additions/removals as non-breaking/breaking changes.
95
- - Converter produces a dashboard-friendly model with service metadata, a normalized channel list and the raw protocol.
96
-
97
- ### 💻 4. CLI (separate platform component)
98
- The CLI uses UniSpec Core Engine for validation, conversion, and working with UniSpec specifications.
99
-
100
- Example UniSpec CLI commands:
101
-
102
- ```pgsql
103
- unispec validate
104
- unispec push
105
- unispec dev
106
- unispec open
107
- unispec diff
108
- unispec convert
109
- ```
110
-
111
-
112
- ### 🗂 5. Registry API (separate service)
113
- Stores UniSpec specs from multiple services and uses Core Engine for validation, normalization, and change analysis.
114
- Effectively acts as the "system of record" for all APIs in the company.
115
-
116
- ### 🌐 6. Portal Web + API (separate service)
117
- API documentation portal built on top of UniSpec and Core Engine:
118
-
119
- - service catalog
120
- - API endpoints
121
- - schemas
122
- - interactive playgrounds
123
- - version comparison
124
-
125
- ---
126
-
127
- ## 🛒 Use Cases
128
-
129
- ### 🟦 Monolith mode (Swagger-like)
130
- A backend service includes an adapter →
131
- `/unispec.json` + `/docs` are served automatically.
132
-
133
- ### 🟧 Microservice mode
134
- Each service pushes its UniSpec into Registry →
135
- Portal Web assembles your entire company's API landscape.
136
-
137
- ### 🟩 Enterprise mode
138
- Includes:
139
-
140
- - SSO / permissions
141
- - RBAC
142
- - auditing
143
- - topology map
144
- - advanced analytics
145
-
146
- (Handled via private repos.)
147
-
148
- ---
149
-
150
- ## 🏁 Getting Started
151
-
152
- ### 1. Clone the repo
153
-
154
- ```pgsql
155
- git clone https://github.com/unispec/unispec-core.git
156
- cd unispec-core
157
- ```
158
-
159
- ### 2. Install dependencies
160
-
161
- ```pgsql
162
- pnpm install
163
- ```
164
-
165
- ### 3. Build all packages
166
-
167
- ```pgsql
168
- pnpm build
169
- ```
170
-
171
- ### 4. Run locally (dev mode)
172
-
173
- ---
174
-
175
- ## 🤝 Contributing
176
-
177
- Contributions are welcome!
178
- Before contributing, please review:
179
-
180
- - `docs/development.md`
181
- - `.windsurfrules`
182
-
183
- All core changes must comply with:
184
-
185
- - the UniSpec format from `unispec-spec`
186
- - compatibility rules
187
- - test coverage
188
- - platform architecture
189
-
190
- ---
191
-
192
- ## Related Repositories
193
-
194
- | Repository | Purpose |
195
- |------------------------|---------|
196
- | `unispec-spec` | UniSpec format definition (schemas, examples) |
197
- | `unispec-core` | **This repo** — UniSpec Core Engine implementation |
198
- | `unispec-docs` | Documentation site |
199
- | `unispec-js-adapters` | Framework integrations (Express/Nest/Fastify) built on top of Core Engine |
200
- | `unispec-infra` | Helm charts, Docker, Terraform for deploying the UniSpec platform |
201
-
202
- ---
203
-
204
- ## License
205
-
206
- UniSpec Core Engine is open-source and free to use under the MIT License.
207
-
208
- ---
209
-
210
- ## 🔥 Summary
211
-
212
- `unispec-core` is the **heart of the UniSpec ecosystem** at the code level.
213
- It provides the Core Engine used to:
214
-
215
- - validate
216
- - normalize
217
- - diff
218
- - convert
219
- - and integrate
220
-
221
- API specifications in the UniSpec format.
222
-
223
- If you're building tools, adapters, or platform services that rely on UniSpec → **this is where the engine lives.**
1
+ # UniSpec Core Platform
2
+ **The official UniSpec Core Engine package — parser, validator, normalizer, diff engine, and converters for the UniSpec format**
3
+
4
+ ---
5
+
6
+ ## 🚀 Overview
7
+
8
+ **UniSpec Core Engine** is the central runtime package of the UniSpec ecosystem.
9
+ It implements the mechanics of the **UniSpec Format**: loading, JSON Schema validation, normalization, diffing, and conversion to other formats.
10
+
11
+ This repository contains **only the Core Engine**, which is consumed by other UniSpec platform components (CLI, Registry, Portal, adapters, SDKs) that live in separate repositories.
12
+
13
+ Core Engine capabilities:
14
+
15
+ - 🧠 **Core Engine** — parser, validator, normalizer, diff engine, converters
16
+ - 🔄 **Format conversion** — UniSpec → OpenAPI, UniSpec → GraphQL SDL, UniSpec → WebSocket channel models
17
+ - 🧩 **Shared types and utilities** — types and helper functions used by the CLI, adapters, Registry, and Portal
18
+
19
+ UniSpec is designed to unify documentation for:
20
+
21
+ - REST
22
+ - GraphQL
23
+ - WebSocket
24
+ - Event-driven APIs (future)
25
+ - Multi-service architectures
26
+
27
+ ---
28
+
29
+ ## 📦 Repository Structure
30
+
31
+ ```pgsql
32
+ unispec-core/
33
+ ├─ src/
34
+ │ ├─ loader/ # File loading (YAML/JSON → JS object)
35
+ │ ├─ validator/ # JSON Schema validation
36
+ │ ├─ normalizer/ # UniSpec structure normalization
37
+ │ ├─ diff/ # Comparison of two UniSpec documents
38
+ │ ├─ converters/ # OpenAPI, GraphQL SDL, WS, etc.
39
+ │ ├─ types/ # UniSpec types/interfaces
40
+ │ ├─ utils/ # Small helpers
41
+ │ └─ index.ts # Public API
42
+ ├─ tests/ # Unit tests
43
+ ├─ package.json
44
+ └─ README.md
45
+ ```
46
+
47
+ ---
48
+
49
+ ## 🧠 Core Concepts
50
+
51
+ ### 📐 1. UniSpec Format
52
+ Defined in a separate repository: **`unispec-spec`**.
53
+ UniSpec Core Engine *implements* this format but does **not** define it.
54
+
55
+ ### 🧱 2. Core Engine
56
+ Located in this repository, under the `src/` directory.
57
+
58
+ Core Engine provides:
59
+
60
+ - YAML/JSON loader
61
+ - JSON Schema validator
62
+ – Normalizer → canonical UniSpec output (REST routes, GraphQL operations, WebSocket channels/messages)
63
+ – Diff engine (with basic breaking / non-breaking classification for REST, GraphQL and WebSocket)
64
+ – Converters:
65
+ - UniSpec → OpenAPI (REST-centric)
66
+ - UniSpec → GraphQL SDL
67
+ - UniSpec → WebSocket channel models for dashboards
68
+
69
+ This is the foundation used by:
70
+
71
+ - the CLI (in a separate repository/package)
72
+ - framework adapters
73
+ - Registry and Portal (as separate UniSpec platform services)
74
+
75
+ ### 🌉 3. Protocol Coverage in Core
76
+
77
+ At the level of the Core Engine, protocol support is intentionally minimal but deterministic:
78
+
79
+ - **REST**
80
+ - Typed REST surface defined by `service.protocols.rest.routes[]` and reusable `service.schemas`.
81
+ - Normalizer orders routes by `name` (or `path + method`) for stable diffs.
82
+ - Diff engine annotates route additions and removals with breaking/non-breaking severity.
83
+ - Converter exposes REST as an OpenAPI 3.1 document built from routes, schemas and environments, while preserving the original UniSpec under `x-unispec`.
84
+
85
+ - **GraphQL**
86
+ - Typed protocol with `schema` (SDL string) and `queries`/`mutations`/`subscriptions` as operation lists.
87
+ - Normalizer orders operation names within each bucket.
88
+ - Diff engine annotates operation additions/removals as non-breaking/breaking.
89
+ - Converter either passes through user-provided SDL or generates a minimal, deterministic SDL shell.
90
+
91
+ - **WebSocket**
92
+ - Typed protocol with channels and messages backed by reusable schemas and security schemes.
93
+ - Normalizer orders channels by name and messages by `name` within each channel.
94
+ - Diff engine annotates channel/message additions/removals as non-breaking/breaking changes.
95
+ - Converter produces a dashboard-friendly model with service metadata, a normalized channel list and the raw protocol.
96
+
97
+ ### 💻 4. CLI (separate platform component)
98
+ The CLI uses UniSpec Core Engine for validation, conversion, and working with UniSpec specifications.
99
+
100
+ Example UniSpec CLI commands:
101
+
102
+ ```pgsql
103
+ unispec validate
104
+ unispec push
105
+ unispec dev
106
+ unispec open
107
+ unispec diff
108
+ unispec convert
109
+ ```
110
+
111
+
112
+ ### 🗂 5. Registry API (separate service)
113
+ Stores UniSpec specs from multiple services and uses Core Engine for validation, normalization, and change analysis.
114
+ Effectively acts as the "system of record" for all APIs in the company.
115
+
116
+ ### 🌐 6. Portal Web + API (separate service)
117
+ API documentation portal built on top of UniSpec and Core Engine:
118
+
119
+ - service catalog
120
+ - API endpoints
121
+ - schemas
122
+ - interactive playgrounds
123
+ - version comparison
124
+
125
+ ---
126
+
127
+ ## 🛒 Use Cases
128
+
129
+ ### 🟦 Monolith mode (Swagger-like)
130
+ A backend service includes an adapter →
131
+ `/unispec.json` + `/docs` are served automatically.
132
+
133
+ ### 🟧 Microservice mode
134
+ Each service pushes its UniSpec into Registry →
135
+ Portal Web assembles your entire company's API landscape.
136
+
137
+ ### 🟩 Enterprise mode
138
+ Includes:
139
+
140
+ - SSO / permissions
141
+ - RBAC
142
+ - auditing
143
+ - topology map
144
+ - advanced analytics
145
+
146
+ (Handled via private repos.)
147
+
148
+ ---
149
+
150
+ ## 🏁 Getting Started
151
+
152
+ ### 1. Clone the repo
153
+
154
+ ```pgsql
155
+ git clone https://github.com/unispec/unispec-core.git
156
+ cd unispec-core
157
+ ```
158
+
159
+ ### 2. Install dependencies
160
+
161
+ ```pgsql
162
+ pnpm install
163
+ ```
164
+
165
+ ### 3. Build all packages
166
+
167
+ ```pgsql
168
+ pnpm build
169
+ ```
170
+
171
+ ### 4. Run locally (dev mode)
172
+
173
+ ---
174
+
175
+ ## 🤝 Contributing
176
+
177
+ Contributions are welcome!
178
+ Before contributing, please review:
179
+
180
+ - `docs/development.md`
181
+ - `.windsurfrules`
182
+
183
+ All core changes must comply with:
184
+
185
+ - the UniSpec format from `unispec-spec`
186
+ - compatibility rules
187
+ - test coverage
188
+ - platform architecture
189
+
190
+ ---
191
+
192
+ ## Related Repositories
193
+
194
+ | Repository | Purpose |
195
+ |------------------------|---------|
196
+ | `unispec-spec` | UniSpec format definition (schemas, examples) |
197
+ | `unispec-core` | **This repo** — UniSpec Core Engine implementation |
198
+ | `unispec-docs` | Documentation site |
199
+ | `unispec-js-adapters` | Framework integrations (Express/Nest/Fastify) built on top of Core Engine |
200
+ | `unispec-infra` | Helm charts, Docker, Terraform for deploying the UniSpec platform |
201
+
202
+ ---
203
+
204
+ ## License
205
+
206
+ UniSpec Core Engine is open-source and free to use under the MIT License.
207
+
208
+ ---
209
+
210
+ ## 🔥 Summary
211
+
212
+ `unispec-core` is the **heart of the UniSpec ecosystem** at the code level.
213
+ It provides the Core Engine used to:
214
+
215
+ - validate
216
+ - normalize
217
+ - diff
218
+ - convert
219
+ - and integrate
220
+
221
+ API specifications in the UniSpec format.
222
+
223
+ If you're building tools, adapters, or platform services that rely on UniSpec → **this is where the engine lives.**
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toOpenAPI = toOpenAPI;
4
+ exports.toGraphQLSDL = toGraphQLSDL;
5
+ exports.toWebSocketModel = toWebSocketModel;
6
+ function toOpenAPI(doc) {
7
+ const rest = (doc.protocols?.rest ?? {});
8
+ const info = {
9
+ title: doc.name,
10
+ description: doc.description,
11
+ };
12
+ const servers = [];
13
+ const paths = {};
14
+ const components = {};
15
+ // Map doc.schemas into OpenAPI components.schemas
16
+ const schemas = (doc.schemas ?? {});
17
+ const componentsSchemas = {};
18
+ for (const [name, def] of Object.entries(schemas)) {
19
+ componentsSchemas[name] = def.jsonSchema;
20
+ }
21
+ if (Object.keys(componentsSchemas).length > 0) {
22
+ components.schemas = componentsSchemas;
23
+ }
24
+ // Helper to build a $ref into components.schemas when schemaRef is present.
25
+ function schemaRefToOpenAPI(schemaRef) {
26
+ if (!schemaRef) {
27
+ return {};
28
+ }
29
+ return { $ref: `#/components/schemas/${schemaRef}` };
30
+ }
31
+ // Build paths from REST routes.
32
+ if (Array.isArray(rest.routes)) {
33
+ for (const route of rest.routes) {
34
+ const pathItem = (paths[route.path] ?? {});
35
+ const method = route.method.toLowerCase();
36
+ const parameters = [];
37
+ // Path params
38
+ for (const param of route.pathParams ?? []) {
39
+ parameters.push({
40
+ name: param.name,
41
+ in: "path",
42
+ required: param.required ?? true,
43
+ description: param.description,
44
+ schema: schemaRefToOpenAPI(param.schemaRef),
45
+ });
46
+ }
47
+ // Query params
48
+ for (const param of route.queryParams ?? []) {
49
+ parameters.push({
50
+ name: param.name,
51
+ in: "query",
52
+ required: param.required ?? false,
53
+ description: param.description,
54
+ schema: schemaRefToOpenAPI(param.schemaRef),
55
+ });
56
+ }
57
+ // Header params
58
+ for (const param of route.headers ?? []) {
59
+ parameters.push({
60
+ name: param.name,
61
+ in: "header",
62
+ required: param.required ?? false,
63
+ description: param.description,
64
+ schema: schemaRefToOpenAPI(param.schemaRef),
65
+ });
66
+ }
67
+ // Request body
68
+ let requestBody;
69
+ if (route.requestBody && route.requestBody.content) {
70
+ const content = {};
71
+ for (const [mediaType, media] of Object.entries(route.requestBody.content)) {
72
+ content[mediaType] = {
73
+ schema: schemaRefToOpenAPI(media.schemaRef),
74
+ };
75
+ }
76
+ requestBody = {
77
+ description: route.requestBody.description,
78
+ required: route.requestBody.required,
79
+ content,
80
+ };
81
+ }
82
+ // Responses
83
+ const responses = {};
84
+ for (const [status, resp] of Object.entries(route.responses ?? {})) {
85
+ const content = {};
86
+ if (resp.content) {
87
+ for (const [mediaType, media] of Object.entries(resp.content)) {
88
+ content[mediaType] = {
89
+ schema: schemaRefToOpenAPI(media.schemaRef),
90
+ };
91
+ }
92
+ }
93
+ responses[status] = {
94
+ description: resp.description ?? "",
95
+ ...(Object.keys(content).length > 0 ? { content } : {}),
96
+ };
97
+ }
98
+ const operation = {
99
+ operationId: route.name,
100
+ summary: route.summary,
101
+ description: route.description,
102
+ ...(parameters.length > 0 ? { parameters } : {}),
103
+ responses: Object.keys(responses).length > 0 ? responses : { default: { description: "" } },
104
+ };
105
+ if (requestBody) {
106
+ operation.requestBody = requestBody;
107
+ }
108
+ // Security requirements
109
+ if (Array.isArray(route.security) && route.security.length > 0) {
110
+ operation.security = route.security.map((req) => {
111
+ const obj = {};
112
+ for (const schemeName of req) {
113
+ obj[schemeName] = [];
114
+ }
115
+ return obj;
116
+ });
117
+ }
118
+ pathItem[method] = operation;
119
+ paths[route.path] = pathItem;
120
+ }
121
+ }
122
+ // Security schemes pass-through from REST protocol
123
+ const componentsSecuritySchemes = rest.securitySchemes ?? {};
124
+ if (Object.keys(componentsSecuritySchemes).length > 0) {
125
+ components.securitySchemes = componentsSecuritySchemes;
126
+ }
127
+ return {
128
+ openapi: "3.1.0",
129
+ info,
130
+ servers,
131
+ paths,
132
+ ...(Object.keys(components).length > 0 ? { components } : {}),
133
+ "x-unispec": doc,
134
+ };
135
+ }
136
+ function toGraphQLSDL(doc) {
137
+ // Minimal implementation: generate a basic SDL that exposes service metadata
138
+ // via a Query field. This does not attempt to interpret the full GraphQL
139
+ // protocol structure yet, but provides a stable, deterministic SDL shape
140
+ // based on top-level UniSpec document fields.
141
+ const graphql = doc.protocols?.graphql;
142
+ const customSDL = graphql?.schema;
143
+ if (typeof customSDL === "string" && customSDL.trim()) {
144
+ return { sdl: customSDL };
145
+ }
146
+ const title = doc.name;
147
+ const description = doc.description ?? "";
148
+ const lines = [];
149
+ if (title || description) {
150
+ lines.push("\"\"");
151
+ if (title) {
152
+ lines.push(title);
153
+ }
154
+ if (description) {
155
+ lines.push("");
156
+ lines.push(description);
157
+ }
158
+ lines.push("\"\"");
159
+ }
160
+ lines.push("schema {");
161
+ lines.push(" query: Query");
162
+ lines.push("}");
163
+ lines.push("");
164
+ lines.push("type Query {");
165
+ lines.push(" _serviceInfo: String!\n");
166
+ lines.push("}");
167
+ const sdl = lines.join("\n");
168
+ return { sdl };
169
+ }
170
+ function toWebSocketModel(doc) {
171
+ // Base WebSocket model intended for a modern, dashboard-oriented UI.
172
+ // It exposes service metadata, a normalized list of channels and the raw
173
+ // websocket protocol object, while also embedding the original UniSpec
174
+ // document under a technical key for debugging and introspection.
175
+ const websocket = (doc.protocols?.websocket ?? {});
176
+ const channelsArray = Array.isArray(websocket.channels) ? websocket.channels : [];
177
+ const channels = [...channelsArray]
178
+ .sort((a, b) => (a.name ?? "").localeCompare(b.name ?? ""))
179
+ .map((channel) => ({
180
+ name: channel.name,
181
+ summary: undefined,
182
+ description: channel.description,
183
+ direction: channel.direction,
184
+ messages: channel.messages,
185
+ raw: channel,
186
+ }));
187
+ return {
188
+ service: {
189
+ name: doc.name,
190
+ title: undefined,
191
+ description: doc.description,
192
+ },
193
+ channels,
194
+ rawProtocol: websocket,
195
+ "x-unispec-ws": doc,
196
+ };
197
+ }