Skip to content

Commit

Permalink
refactor: add possibility to pass custom resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Sep 5, 2022
1 parent cc46e8f commit 376ef2e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 41 deletions.
93 changes: 67 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 5 additions & 13 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -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<SchemaParser>;
}

export class Parser {
Expand All @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -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<string | Buffer>;
}

export interface ResolverInput {
url: string;
extension: string;
}

interface ResolverOptions {
resolvers: Array<Resolver>;
}

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() {

}
7 changes: 5 additions & 2 deletions src/spectral.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Spectral } from "@stoplight/spectral-core";
import { createRulesetFunction } from '@stoplight/spectral-core';
import { asyncapi as aasRuleset } from "@stoplight/spectral-rulesets";

Expand All @@ -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 {
Expand Down

0 comments on commit 376ef2e

Please sign in to comment.