diff --git a/package-lock.json b/package-lock.json index 81c6ec26b..5a1550dd2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,8 @@ "dependencies": { "@asyncapi/specs": "^3.1.0", "@openapi-contrib/openapi-schema-to-json-schema": "^3.2.0", + "@stoplight/json-ref-readers": "^1.2.2", + "@stoplight/json-ref-resolver": "^3.1.4", "@stoplight/spectral-core": "^1.13.1", "@stoplight/spectral-functions": "^1.7.1", "@stoplight/spectral-parsers": "^1.0.2", @@ -2011,21 +2013,20 @@ "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" }, "node_modules/@stoplight/json-ref-resolver": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/@stoplight/json-ref-resolver/-/json-ref-resolver-3.1.3.tgz", - "integrity": "sha512-SgoKXwVnlpIZUyAFX4W79eeuTWvXmNlMfICZixL16GZXnkjcW+uZnfmAU0ZIjcnaTgaI4mjfxn8LAP2KR6Cr0A==", + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/@stoplight/json-ref-resolver/-/json-ref-resolver-3.1.4.tgz", + "integrity": "sha512-842JVmMsi++qpDuIX+JpQvK7YY8FXEZZb+/z4xuRfStOAVEryJT/tbgGOWxniSdxEl9Eni5D/I2afMyy6BuiNw==", "dependencies": { "@stoplight/json": "^3.17.0", "@stoplight/path": "^1.3.2", - "@stoplight/types": "^12.3.0", - "@types/urijs": "^1.19.16", + "@stoplight/types": "^12.3.0 || ^13.0.0", + "@types/urijs": "^1.19.19", "dependency-graph": "~0.11.0", "fast-memoize": "^2.5.2", "immer": "^9.0.6", - "lodash.get": "^4.4.2", - "lodash.set": "^4.3.2", + "lodash": "^4.17.21", "tslib": "^2.3.1", - "urijs": "^1.19.6" + "urijs": "^1.19.11" }, "engines": { "node": ">=8.3.0" @@ -2220,6 +2221,27 @@ "node": ">=12" } }, + "node_modules/@stoplight/spectral-ref-resolver/node_modules/@stoplight/json-ref-resolver": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/@stoplight/json-ref-resolver/-/json-ref-resolver-3.1.3.tgz", + "integrity": "sha512-SgoKXwVnlpIZUyAFX4W79eeuTWvXmNlMfICZixL16GZXnkjcW+uZnfmAU0ZIjcnaTgaI4mjfxn8LAP2KR6Cr0A==", + "dependencies": { + "@stoplight/json": "^3.17.0", + "@stoplight/path": "^1.3.2", + "@stoplight/types": "^12.3.0", + "@types/urijs": "^1.19.16", + "dependency-graph": "~0.11.0", + "fast-memoize": "^2.5.2", + "immer": "^9.0.6", + "lodash.get": "^4.4.2", + "lodash.set": "^4.3.2", + "tslib": "^2.3.1", + "urijs": "^1.19.6" + }, + "engines": { + "node": ">=8.3.0" + } + }, "node_modules/@stoplight/spectral-rulesets": { "version": "1.12.0", "resolved": "https://registry.npmjs.org/@stoplight/spectral-rulesets/-/spectral-rulesets-1.12.0.tgz", @@ -8016,7 +8038,7 @@ "node_modules/lodash.get": { "version": "4.4.2", "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", - "integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=" + "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==" }, "node_modules/lodash.ismatch": { "version": "4.4.0", @@ -8051,7 +8073,7 @@ "node_modules/lodash.set": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz", - "integrity": "sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=" + "integrity": "sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg==" }, "node_modules/lodash.template": { "version": "4.5.0", @@ -15336,9 +15358,9 @@ } }, "node_modules/urijs": { - "version": "1.19.10", - "resolved": "https://registry.npmjs.org/urijs/-/urijs-1.19.10.tgz", - "integrity": "sha512-EzauQlgKuJgsXOqoMrCiePBf4At5jVqRhXykF3Wfb8ZsOBMxPcfiVBcsHXug4Aepb/ICm2PIgqAUGMelgdrWEg==" + "version": "1.19.11", + "resolved": "https://registry.npmjs.org/urijs/-/urijs-1.19.11.tgz", + "integrity": "sha512-HXgFDgDommxn5/bIv0cnQZsPhHDA90NPHD6+c/v21U5+Sx5hoP8+dP9IZXBU1gIfvdRfhG8cel9QNPeionfcCQ==" }, "node_modules/url-join": { "version": "4.0.1", @@ -17225,21 +17247,20 @@ } }, "@stoplight/json-ref-resolver": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/@stoplight/json-ref-resolver/-/json-ref-resolver-3.1.3.tgz", - "integrity": "sha512-SgoKXwVnlpIZUyAFX4W79eeuTWvXmNlMfICZixL16GZXnkjcW+uZnfmAU0ZIjcnaTgaI4mjfxn8LAP2KR6Cr0A==", + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/@stoplight/json-ref-resolver/-/json-ref-resolver-3.1.4.tgz", + "integrity": "sha512-842JVmMsi++qpDuIX+JpQvK7YY8FXEZZb+/z4xuRfStOAVEryJT/tbgGOWxniSdxEl9Eni5D/I2afMyy6BuiNw==", "requires": { "@stoplight/json": "^3.17.0", "@stoplight/path": "^1.3.2", - "@stoplight/types": "^12.3.0", - "@types/urijs": "^1.19.16", + "@stoplight/types": "^12.3.0 || ^13.0.0", + "@types/urijs": "^1.19.19", "dependency-graph": "~0.11.0", "fast-memoize": "^2.5.2", "immer": "^9.0.6", - "lodash.get": "^4.4.2", - "lodash.set": "^4.3.2", + "lodash": "^4.17.21", "tslib": "^2.3.1", - "urijs": "^1.19.6" + "urijs": "^1.19.11" } }, "@stoplight/lifecycle": { @@ -17382,6 +17403,26 @@ "@stoplight/spectral-runtime": "^1.0.0", "dependency-graph": "0.11.0", "tslib": "^2.3.1" + }, + "dependencies": { + "@stoplight/json-ref-resolver": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/@stoplight/json-ref-resolver/-/json-ref-resolver-3.1.3.tgz", + "integrity": "sha512-SgoKXwVnlpIZUyAFX4W79eeuTWvXmNlMfICZixL16GZXnkjcW+uZnfmAU0ZIjcnaTgaI4mjfxn8LAP2KR6Cr0A==", + "requires": { + "@stoplight/json": "^3.17.0", + "@stoplight/path": "^1.3.2", + "@stoplight/types": "^12.3.0", + "@types/urijs": "^1.19.16", + "dependency-graph": "~0.11.0", + "fast-memoize": "^2.5.2", + "immer": "^9.0.6", + "lodash.get": "^4.4.2", + "lodash.set": "^4.3.2", + "tslib": "^2.3.1", + "urijs": "^1.19.6" + } + } } }, "@stoplight/spectral-rulesets": { @@ -21811,7 +21852,7 @@ "lodash.get": { "version": "4.4.2", "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", - "integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=" + "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==" }, "lodash.ismatch": { "version": "4.4.0", @@ -21846,7 +21887,7 @@ "lodash.set": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz", - "integrity": "sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=" + "integrity": "sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg==" }, "lodash.template": { "version": "4.5.0", @@ -27374,9 +27415,9 @@ } }, "urijs": { - "version": "1.19.10", - "resolved": "https://registry.npmjs.org/urijs/-/urijs-1.19.10.tgz", - "integrity": "sha512-EzauQlgKuJgsXOqoMrCiePBf4At5jVqRhXykF3Wfb8ZsOBMxPcfiVBcsHXug4Aepb/ICm2PIgqAUGMelgdrWEg==" + "version": "1.19.11", + "resolved": "https://registry.npmjs.org/urijs/-/urijs-1.19.11.tgz", + "integrity": "sha512-HXgFDgDommxn5/bIv0cnQZsPhHDA90NPHD6+c/v21U5+Sx5hoP8+dP9IZXBU1gIfvdRfhG8cel9QNPeionfcCQ==" }, "url-join": { "version": "4.0.1", diff --git a/package.json b/package.json index bfbf229f9..8e327613c 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,8 @@ "dependencies": { "@asyncapi/specs": "^3.1.0", "@openapi-contrib/openapi-schema-to-json-schema": "^3.2.0", + "@stoplight/json-ref-readers": "^1.2.2", + "@stoplight/json-ref-resolver": "^3.1.4", "@stoplight/spectral-core": "^1.13.1", "@stoplight/spectral-functions": "^1.7.1", "@stoplight/spectral-parsers": "^1.0.2", diff --git a/src/parser.ts b/src/parser.ts index 5681e5047..d1d6579d8 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1,18 +1,16 @@ -import { Spectral } from "@stoplight/spectral-core"; - import { parse } from "./parse"; import { lint, validate } from "./lint"; import { registerSchemaParser } from './schema-parser'; import { AsyncAPISchemaParser } from "./schema-parser/asyncapi-schema-parser"; -import { configureSpectral } from "./spectral"; +import { createSpectral } from "./spectral"; -import type { IConstructorOpts } from "@stoplight/spectral-core"; +import type { Spectral } from "@stoplight/spectral-core"; import type { ParseInput, ParseOptions } from "./parse"; import type { LintOptions, ValidateOptions } from "./lint"; import type { SchemaParser } from './schema-parser'; export interface ParserOptions { - spectral?: Spectral | IConstructorOpts; + schemaParsers?: Array; } export class Parser { @@ -22,15 +20,9 @@ export class Parser { constructor( private readonly options?: ParserOptions ) { - const { spectral } = this.options || {}; - if (spectral instanceof Spectral) { - this.spectral = spectral; - } else { - this.spectral = new Spectral(spectral); - } - + this.spectral = createSpectral(this); this.registerSchemaParser(AsyncAPISchemaParser()); - configureSpectral(this); + this.options?.schemaParsers?.forEach(parser => this.registerSchemaParser(parser)); } parse(asyncapi: ParseInput, options?: ParseOptions) { diff --git a/src/resolver.ts b/src/resolver.ts new file mode 100644 index 000000000..a207c9e8a --- /dev/null +++ b/src/resolver.ts @@ -0,0 +1,35 @@ +import { Resolver as SpectralResolver } from '@stoplight/json-ref-resolver'; +import { resolveFile, resolveHttp } from '@stoplight/json-ref-readers'; + +export interface Resolver { + order?: number; + canRead?: boolean | ((input: ResolverInput) => boolean); + read: (input: ResolverInput) => string | Buffer | Promise; +} + +export interface ResolverInput { + url: string; + extension: string; +} + +interface ResolverOptions { + resolvers: Array; +} + +export function createResolver(options?: ResolverOptions): SpectralResolver { + return new SpectralResolver({ + resolvers: { + https: { resolve: resolveHttp }, + http: { resolve: resolveHttp }, + file: { resolve: resolveFile }, + }, + }); +} + +export function createFileResolver() { + +} + +export function createHttpResolver() { + +} diff --git a/src/spectral.ts b/src/spectral.ts index a9d231a0d..cd809e834 100644 --- a/src/spectral.ts +++ b/src/spectral.ts @@ -1,3 +1,4 @@ +import { Spectral } from "@stoplight/spectral-core"; import { createRulesetFunction } from '@stoplight/spectral-core'; import { asyncapi as aasRuleset } from "@stoplight/spectral-rulesets"; @@ -9,9 +10,11 @@ import type { RuleDefinition, RulesetDefinition } from "@stoplight/spectral-core import type { Parser } from "./parser"; import type { MaybeAsyncAPI } from "./types"; -export function configureSpectral(parser: Parser) { +export function createSpectral(parser: Parser) { + const spectral = new Spectral(); const ruleset = configureRuleset(parser); - parser.spectral.setRuleset(ruleset); + spectral.setRuleset(ruleset); + return spectral; } function configureRuleset(parser: Parser): RulesetDefinition {