import type { CompareOptions, CompareEngine, CompareEngineName } from "./types" import { compareJsonSchema } from "./jsonSchema" import { compareGraphApi, GRAPHAPI_VERSION_PATTERN, LEGACY_GRAPHAPI_VERSION_PATTERN, legacyGraphApiMessage, } from "./graphapi" import { compareAsyncApi } from "./asyncapi" import { compareOpenApi } from "./openapi" import { compareProtoApi, PROTOAPI_VERSION_PATTERN } from "./protoapi" import { UnsupportedSpecError } from "./core" import { isObject } from "./utils" export interface DetectedSpec { engine: CompareEngineName // specification version as written in the document (e.g. "3.1.0", "2.6.0", "1.0", "0.1") version?: string } const engines: Record = { openapi: compareOpenApi, asyncapi: compareAsyncApi, graphapi: compareGraphApi, protoapi: compareProtoApi, jsonschema: compareJsonSchema, } const versionOf = (value: unknown): string | undefined => typeof value === "string" || typeof value === "number" ? String(value) : undefined /** * Detects the specification of a document. Objects without a specification marker are JSON Schemas; documents of a * recognized but unsupported specification (Swagger 2, OpenAPI 4+, AsyncAPI 1.x/4+, GraphApi versions other than * 1.x (0.1.2 included), unknown ProtoApi versions) throw `UnsupportedSpecError`. */ export const detectSpec = (document: unknown): DetectedSpec => { if (!isObject(document)) { return { engine: "jsonschema" } } if ("openapi" in document) { const version = versionOf(document.openapi) if (version !== undefined && /^3(\.\d+)*$/.test(version)) { return { engine: "openapi", version } } throw new UnsupportedSpecError(`Unsupported OpenAPI version: ${String(document.openapi)}`, version) } if ("swagger" in document) { throw new UnsupportedSpecError(`Swagger ${String(document.swagger)} is not supported`, versionOf(document.swagger)) } if ("asyncapi" in document) { const version = versionOf(document.asyncapi) if (version !== undefined && /^[23](\.\d+)*$/.test(version)) { return { engine: "asyncapi", version } } throw new UnsupportedSpecError(`Unsupported AsyncAPI version: ${String(document.asyncapi)}`, version) } if ("graphapi" in document && document.graphapi) { const version = versionOf(document.graphapi) if (version !== undefined && GRAPHAPI_VERSION_PATTERN.test(version)) { return { engine: "graphapi", version } } if (version !== undefined && LEGACY_GRAPHAPI_VERSION_PATTERN.test(version)) { throw new UnsupportedSpecError(legacyGraphApiMessage(version), version) } throw new UnsupportedSpecError(`Unsupported GraphApi version: ${String(document.graphapi)}`, version) } if ("protoapi" in document && document.protoapi) { const version = versionOf(document.protoapi) if (version !== undefined && PROTOAPI_VERSION_PATTERN.test(version)) { return { engine: "protoapi", version } } throw new UnsupportedSpecError( `Unsupported ProtoApi version: ${version ?? JSON.stringify(document.protoapi)}`, version, ) } return { engine: "jsonschema" } } /** The compare engine for a document (throws `UnsupportedSpecError` for unsupported specifications). */ export const discoverCompareEngine = (data: unknown): CompareEngine => engines[detectSpec(data).engine] /** * Compares two API documents with the engine detected from them (`options.engine` forces one). Both documents must * belong to the same specification; the engines check the version compatibility themselves. */ export const apiCompare = (before: unknown, after: unknown, options: CompareOptions = {}) => { if (options.engine) { return engines[options.engine](before, after, options) } const b = detectSpec(before) const a = detectSpec(after) if (b.engine !== a.engine) { throw new UnsupportedSpecError( `Cannot compare a ${b.engine} document with a ${a.engine} document`, b.version ?? b.engine, a.version ?? a.engine, ) } return engines[b.engine](before, after, options) }