@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/dist/index.cjs ADDED
@@ -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);
@@ -16,10 +16,10 @@ function normalizeValue(value) {
16
16
  return value;
17
17
  }
18
18
  function normalizeRestRoutes(doc) {
19
- if (!doc || !doc.service || !doc.service.protocols) {
19
+ if (!doc || !doc.protocols) {
20
20
  return doc;
21
21
  }
22
- const protocols = doc.service.protocols;
22
+ const protocols = doc.protocols;
23
23
  const rest = protocols.rest;
24
24
  if (!rest || !Array.isArray(rest.routes)) {
25
25
  return doc;
@@ -34,10 +34,10 @@ function normalizeRestRoutes(doc) {
34
34
  return doc;
35
35
  }
36
36
  function normalizeWebSocket(doc) {
37
- if (!doc || !doc.service || !doc.service.protocols) {
37
+ if (!doc || !doc.protocols) {
38
38
  return doc;
39
39
  }
40
- const protocols = doc.service.protocols;
40
+ const protocols = doc.protocols;
41
41
  const websocket = protocols.websocket;
42
42
  if (!websocket || !Array.isArray(websocket.channels)) {
43
43
  return doc;
@@ -65,10 +65,10 @@ function normalizeWebSocket(doc) {
65
65
  return doc;
66
66
  }
67
67
  function normalizeGraphqlOperations(doc) {
68
- if (!doc || !doc.service || !doc.service.protocols) {
68
+ if (!doc || !doc.protocols) {
69
69
  return doc;
70
70
  }
71
- const protocols = doc.service.protocols;
71
+ const protocols = doc.protocols;
72
72
  const graphql = protocols.graphql;
73
73
  if (!graphql) {
74
74
  return doc;
@@ -85,21 +85,12 @@ export interface UniSpecServiceProtocols {
85
85
  graphql?: UniSpecGraphQLProtocol;
86
86
  websocket?: UniSpecWebSocketProtocol;
87
87
  }
88
- export interface UniSpecService {
88
+ export interface UniSpecDocument {
89
89
  name: string;
90
- title?: string;
91
90
  description?: string;
92
91
  version?: string;
93
- owner?: string;
94
- tags?: string[];
95
- links?: Record<string, string>;
96
92
  protocols?: UniSpecServiceProtocols;
97
93
  schemas?: UniSpecSchemas;
98
- environments?: UniSpecEnvironment[];
99
- }
100
- export interface UniSpecDocument {
101
- unispecVersion: string;
102
- service: UniSpecService;
103
94
  extensions?: Record<string, unknown>;
104
95
  }
105
96
  export interface ValidationError {
@@ -1,7 +1,7 @@
1
1
  import Ajv2020 from "ajv/dist/2020.js";
2
- import { createRequire } from "module";
2
+ import fs from "fs";
3
+ import path from "path";
3
4
  import { unispec as unispecSchema, manifest as unispecManifest } from "@unispechq/unispec-schema";
4
- const require = createRequire(import.meta.url);
5
5
  const ajv = new Ajv2020({
6
6
  allErrors: true,
7
7
  strict: true,
@@ -10,11 +10,13 @@ const ajv = new Ajv2020({
10
10
  ajv.addFormat("uri", true);
11
11
  // Register all UniSpec subschemas so that Ajv can resolve internal $ref links
12
12
  try {
13
- const schemaRootPath = require.resolve("@unispechq/unispec-schema");
14
- const schemaDir = schemaRootPath.replace(/index\.(cjs|mjs|js)$/u, "schema/");
13
+ const schemaDir = path.join(process.cwd(), "node_modules", "@unispechq", "unispec-schema", "schema");
15
14
  const types = unispecManifest?.types ?? {};
16
15
  const typeSchemaPaths = Object.values(types).map((rel) => String(rel));
17
- const loadedTypeSchemas = typeSchemaPaths.map((relPath) => require(schemaDir + relPath));
16
+ const loadedTypeSchemas = typeSchemaPaths
17
+ .map((relPath) => path.join(schemaDir, relPath))
18
+ .filter((filePath) => fs.existsSync(filePath))
19
+ .map((filePath) => JSON.parse(fs.readFileSync(filePath, "utf8")));
18
20
  ajv.addSchema(loadedTypeSchemas);
19
21
  }
20
22
  catch {
@@ -36,7 +38,18 @@ function mapAjvErrors(errors) {
36
38
  * Validate a UniSpec document against the UniSpec JSON Schema.
37
39
  */
38
40
  export async function validateUniSpec(doc, _options = {}) {
39
- const valid = validateFn(doc);
41
+ const docForValidation = {
42
+ unispecVersion: "0.0.0",
43
+ service: {
44
+ name: doc.name,
45
+ description: doc.description,
46
+ version: doc.version,
47
+ protocols: doc.protocols,
48
+ schemas: doc.schemas,
49
+ },
50
+ extensions: doc.extensions,
51
+ };
52
+ const valid = validateFn(docForValidation);
40
53
  if (valid) {
41
54
  return {
42
55
  valid: true,
@@ -53,9 +66,9 @@ export async function validateUniSpec(doc, _options = {}) {
53
66
  */
54
67
  export async function validateUniSpecTests(doc, _options = {}) {
55
68
  if (!validateTestsFn) {
56
- const schemaRootPath = require.resolve("@unispechq/unispec-schema");
57
- const schemaDir = schemaRootPath.replace(/index\.(cjs|mjs|js)$/u, "schema/");
58
- const testsSchema = require(`${schemaDir}unispec-tests.schema.json`);
69
+ const schemaDir = path.join(process.cwd(), "node_modules", "@unispechq", "unispec-schema", "schema");
70
+ const testsSchemaPath = path.join(schemaDir, "unispec-tests.schema.json");
71
+ const testsSchema = JSON.parse(fs.readFileSync(testsSchemaPath, "utf8"));
59
72
  validateTestsFn = ajv.compile(testsSchema);
60
73
  }
61
74
  const valid = validateTestsFn(doc);
package/package.json CHANGED
@@ -1,25 +1,54 @@
1
1
  {
2
2
  "name": "@unispechq/unispec-core",
3
- "version": "0.2.0",
3
+ "version": "0.2.3",
4
4
  "description": "Central UniSpec Core Engine providing parsing, validation, normalization, diffing, and conversion of UniSpec specs.",
5
5
  "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/unispec/unispec-core.git"
9
+ },
10
+ "bugs": {
11
+ "url": "https://github.com/unispec/unispec-core/issues"
12
+ },
13
+ "homepage": "https://github.com/unispec/unispec-core#readme",
14
+ "keywords": [
15
+ "unispec",
16
+ "specification",
17
+ "schema",
18
+ "validator",
19
+ "normalizer",
20
+ "diff",
21
+ "converter"
22
+ ],
23
+ "engines": {
24
+ "node": ">=18.0.0"
25
+ },
6
26
  "type": "module",
7
27
  "main": "dist/index.cjs",
8
28
  "module": "dist/index.js",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "require": "./dist/index.cjs"
34
+ }
35
+ },
9
36
  "types": "dist/index.d.ts",
10
37
  "files": [
11
38
  "dist",
12
39
  "README.md"
13
40
  ],
14
41
  "scripts": {
15
- "build": "tsc -p tsconfig.json",
42
+ "build": "npm run build:esm && npm run build:cjs",
43
+ "build:esm": "tsc -p tsconfig.json",
44
+ "build:cjs": "tsc -p tsconfig.cjs.json && node scripts/postbuild-cjs.cjs",
16
45
  "test": "npm run build && node --test tests/*.test.mjs",
17
46
  "release:patch": "node scripts/release.js patch",
18
47
  "release:minor": "node scripts/release.js minor",
19
48
  "release:major": "node scripts/release.js major"
20
49
  },
21
50
  "dependencies": {
22
- "@unispechq/unispec-schema": "^0.3.1",
51
+ "@unispechq/unispec-schema": "^0.3.3",
23
52
  "ajv": "^8.12.0"
24
53
  },
25
54
  "devDependencies": {