@unispechq/unispec-core 0.1.0 → 0.1.1

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.
@@ -1,116 +0,0 @@
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
- }
@@ -1,57 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,7 +0,0 @@
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>;
@@ -1,47 +0,0 @@
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
- }