Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reference): separate abstractions from configuration #2409

Merged
merged 2 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package-lock.json

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

121 changes: 116 additions & 5 deletions packages/apidom-reference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,130 @@
"registry": "https://npm.pkg.github.com"
},
"type": "module",
"sideEffects": [
"./es/configuration/complete.js",
"./cjs/configuration/complete.cjs"
],
"unpkg": "./dist/apidom-reference.browser.min.js",
"browser": {
"./es/parse/parsers/binary/index-node.js": "./es/parse/parsers/binary/index-browser.js",
"./cjs/parse/parsers/binary/index-node.cjs": "./cjs/parse/parsers/binary/index-browser.cjs",
"./cjs/resolve/resolvers/file/index-node.cjs": "./cjs/resolve/resolvers/file/index-browser.cjs",
"./es/parse/parsers/binary/index-node.js": "./es/parse/parsers/binary/index-browser.js",
"./es/resolve/resolvers/file/index-node.js": "./es/resolve/resolvers/file/index-browser.js"
},
"main": "./cjs/index.cjs",
"exports": {
"types": "./types/dist.d.ts",
"import": "./es/index.js",
"require": "./cjs/index.cjs"
".": {
"types": "./types/dist.d.ts",
"import": "./es/configuration/saturated.js",
"require": "./cjs/configuration/saturated.cjs"
},
"./configuration/saturated": {
"types": "./types/dist.d.ts",
"import": "./es/configuration/saturated.js",
"require": "./cjs/configuration/saturated.cjs"
},
"./configuration/empty": {
"types": "./types/dist.d.ts",
"import": "./es/configuration/empty.js",
"require": "./cjs/configuration/empty.cjs"
},
"./resolve/resolvers/file": {
"browser": {
"import": "./es/resolve/resolvers/file/index-browser.js",
"require": "./cjs/resolve/resolvers/file/index-browser.cjs"
},
"default": {
"import": "./es/resolve/resolvers/file/index-node.js",
"require": "./cjs/resolve/resolvers/file/index-node.cjs"
}
},
"./resolve/resolvers/http-axios": {
"import": "./es/resolve/resolvers/http-axios/index.js",
"require": "./cjs/resolve/resolvers/http-axios/index.cjs"
},
"./resolve/resolvers/http-swagger-client": {
"import": "./es/resolve/resolvers/http-swagger-client/index.js",
"require": "./cjs/resolve/resolvers/http-swagger-client/index.cjs"
},
"./resolve/strategies/asyncapi-2": {
"import": "./es/resolve/strategies/asyncapi-2/index.js",
"require": "./cjs/resolve/strategies/asyncapi-2/index.cjs"
},
"./resolve/strategies/openapi-3-0": {
"import": "./es/resolve/strategies/openapi-3-0/index.js",
"require": "./cjs/resolve/strategies/openapi-3-0/index.cjs"
},
"./resolve/strategies/openapi-3-1": {
"import": "./es/resolve/strategies/openapi-3-1/index.js",
"require": "./cjs/resolve/strategies/openapi-3-1/index.cjs"
},
"./parse/parsers/api-design-systems-json": {
"import": "./es/parse/parsers/api-design-systems-json/index.js",
"require": "./cjs/parse/parsers/api-design-systems-json/index.cjs"
},
"./parse/parsers/api-design-systems-yaml": {
"import": "./es/parse/parsers/api-design-systems-yaml/index.js",
"require": "./cjs/parse/parsers/api-design-systems-yaml/index.cjs"
},
"./parse/parsers/asyncapi-json-2": {
"import": "./es/parse/parsers/asyncapi-json-2/index.js",
"require": "./cjs/parse/parsers/asyncapi-json-2/index.cjs"
},
"./parse/parsers/asyncapi-yaml-2": {
"import": "./es/parse/parsers/asyncapi-yaml-2/index.js",
"require": "./cjs/parse/parsers/asyncapi-yaml-2/index.cjs"
},
"./parse/parsers/binary": {
"browser": {
"import": "./es/parse/parsers/binary/index-browser.js",
"require": "./cjs/parse/parsers/binary/index-browser.cjs"
},
"default": {
"import": "./es/parse/parsers/binary/index-node.js",
"require": "./cjs/parse/parsers/binary/index-node.cjs"
}
},
"./parse/parsers/json": {
"import": "./es/parse/parsers/json/index.js",
"require": "./cjs/parse/parsers/json/index.cjs"
},
"./parse/parsers/openapi-json-3-0": {
"import": "./es/parse/parsers/openapi-json-3-0/index.js",
"require": "./cjs/parse/parsers/openapi-json-3-0/index.cjs"
},
"./parse/parsers/openapi-json-3-1": {
"import": "./es/parse/parsers/openapi-json-3-1/index.js",
"require": "./cjs/parse/parsers/openapi-json-3-1/index.cjs"
},
"./parse/parsers/openapi-yaml-3-0": {
"import": "./es/parse/parsers/openapi-yaml-3-0/index.js",
"require": "./cjs/parse/parsers/openapi-yaml-3-0/index.cjs"
},
"./parse/parsers/openapi-yaml-3-1": {
"import": "./es/parse/parsers/openapi-yaml-3-1/index.js",
"require": "./cjs/parse/parsers/openapi-yaml-3-1/index.cjs"
},
"./parse/parsers/yaml-1-2": {
"import": "./es/parse/parsers/yaml-1-2/index.js",
"require": "./cjs/parse/parsers/yaml-1-2/index.cjs"
},
"./dereference/strategies/asyncapi-2": {
"import": "./es/dereference/strategies/asyncapi-2/index.js",
"require": "./cjs/dereference/strategies/asyncapi-2/index.cjs"
},
"./dereference/strategies/openapi-3-0": {
"import": "./es/dereference/strategies/openapi-3-0/index.js",
"require": "./cjs/dereference/strategies/openapi-3-0/index.cjs"
},
"./dereference/strategies/openapi-3-1": {
"import": "./es/dereference/strategies/openapi-3-1/index.js",
"require": "./cjs/dereference/strategies/openapi-3-1/index.cjs"
},
"./dereference/strategies/openapi-3-1-swagger-client": {
"import": "./es/dereference/strategies/openapi-3-1-swagger-client/index.js",
"require": "./cjs/dereference/strategies/openapi-3-1-swagger-client/index.cjs"
}
},
"types": "./types/dist.d.ts",
"scripts": {
Expand All @@ -42,7 +154,6 @@
"license": "Apache-2.0",
"dependencies": {
"@babel/runtime-corejs3": "=7.20.7",
"@swagger-api/apidom-ast": "^0.58.0",
"@swagger-api/apidom-core": "^0.58.0",
"@swagger-api/apidom-json-pointer": "^0.58.0",
"@swagger-api/apidom-ns-asyncapi-2": "^0.58.0",
Expand Down
1 change: 1 addition & 0 deletions packages/apidom-reference/src/configuration/empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../index';
53 changes: 53 additions & 0 deletions packages/apidom-reference/src/configuration/saturated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import FileResolver from '../resolve/resolvers/file/index-node';
import HttpResolverAxios from '../resolve/resolvers/http-axios';
import OpenApi3_0ResolveStrategy from '../resolve/strategies/openapi-3-0';
import OpenApi3_1ResolveStrategy from '../resolve/strategies/openapi-3-1';
import AsyncApi2ResolveStrategy from '../resolve/strategies/asyncapi-2';
import ApiDesignSystemsJsonParser from '../parse/parsers/api-design-systems-json';
import ApiDesignSystemsYamlParser from '../parse/parsers/api-design-systems-yaml';
import OpenApiJson3_0Parser from '../parse/parsers/openapi-json-3-0';
import OpenApiYaml3_0Parser from '../parse/parsers/openapi-yaml-3-0';
import OpenApiJson3_1Parser from '../parse/parsers/openapi-json-3-1';
import OpenApiYaml3_1Parser from '../parse/parsers/openapi-yaml-3-1';
import AsyncApiJson2Parser from '../parse/parsers/asyncapi-json-2';
import AsyncApiYaml2Parser from '../parse/parsers/asyncapi-yaml-2';
import JsonParser from '../parse/parsers/json';
import YamlParser from '../parse/parsers/yaml-1-2';
import BinaryParser from '../parse/parsers/binary/index-node';
import OpenApi3_0DereferenceStrategy from '../dereference/strategies/openapi-3-0';
import OpenApi3_1DereferenceStrategy from '../dereference/strategies/openapi-3-1';
import AsyncApi2DereferenceStrategy from '../dereference/strategies/asyncapi-2';
import { options } from '../index';

options.parse.parsers = [
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
AsyncApiJson2Parser({ allowEmpty: true, sourceMap: false }),
AsyncApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
ApiDesignSystemsJsonParser({ allowEmpty: true, sourceMap: false }),
ApiDesignSystemsYamlParser({ allowEmpty: true, sourceMap: false }),
JsonParser({ allowEmpty: true, sourceMap: false }),
YamlParser({ allowEmpty: true, sourceMap: false }),
BinaryParser({ allowEmpty: true }),
];

options.resolve.resolvers = [
FileResolver(),
HttpResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
];

options.resolve.strategies = [
OpenApi3_0ResolveStrategy(),
OpenApi3_1ResolveStrategy(),
AsyncApi2ResolveStrategy(),
];

options.dereference.strategies = [
OpenApi3_0DereferenceStrategy(),
OpenApi3_1DereferenceStrategy(),
AsyncApi2DereferenceStrategy(),
];

export * from '../index';
53 changes: 17 additions & 36 deletions packages/apidom-reference/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,34 @@ import resolveFn, { resolveApiDOM as resolveApiDOMFn } from './resolve';
import { readFile as readFileFn } from './resolve/util';
import dereferenceFn, { dereferenceApiDOM as dereferenceApiDOMFn } from './dereference';

export { url };

export { default as Parser } from './parse/parsers/Parser';
export { default as ApiDesignSystemsJsonParser } from './parse/parsers/api-design-systems-json';
export { default as ApiDesignSystemsYamlParser } from './parse/parsers/api-design-systems-yaml';
export { default as OpenApiJson3_0Parser } from './parse/parsers/openapi-json-3-0';
export { default as OpenApiYaml3_0Parser } from './parse/parsers/openapi-yaml-3-0';
export { default as OpenApiJson3_1Parser } from './parse/parsers/openapi-json-3-1';
export { default as OpenApiYaml3_1Parser } from './parse/parsers/openapi-yaml-3-1';
export { default as AsyncApiJson2Parser } from './parse/parsers/asyncapi-json-2';
export { default as AsyncApiYaml2Parser } from './parse/parsers/asyncapi-yaml-2';
export { default as JsonParser } from './parse/parsers/json';
export { default as YamlParser } from './parse/parsers/yaml-1-2';
export { default as BinaryParser } from './parse/parsers/binary/index-node';

export { default as FileResolver } from './resolve/resolvers/file/index-node';
export { default as HttpResolverAxios } from './resolve/resolvers/http-axios';
export { default as HttpResolverSwaggerClient } from './resolve/resolvers/http-swagger-client';
export { default as HttpResolver } from './resolve/resolvers/HttpResolver';
export { default as Resolver } from './resolve/resolvers/Resolver';
export { default as ResolveStrategy } from './resolve/strategies/ResolveStrategy';
export { default as AsyncApi2ResolveStrategy } from './resolve/strategies/asyncapi-2';
export { default as OpenApi3_0ResolveStrategy } from './resolve/strategies/openapi-3-0';
export { default as OpenApi3_1ResolveStrategy } from './resolve/strategies/openapi-3-1';

export { default as AsyncApi2DereferenceStrategy } from './dereference/strategies/asyncapi-2';
export { default as OpenApi3_0DereferenceStrategy } from './dereference/strategies/openapi-3-0';
export { default as OpenApi3_1DereferenceStrategy } from './dereference/strategies/openapi-3-1';
export { default as ResolveStrategy } from './resolve/strategies/ResolveStrategy';

export { default as options } from './options';
export { merge as mergeOptions } from './options/util';

export { default as Reference } from './Reference';
export { default as ReferenceSet } from './ReferenceSet';

export {
DereferenceError,
InvalidSelectorError,
MaximumDereferenceDepthError,
MaximumResolverDepthError,
NotImplementedError,
ParserError,
PluginError,
ResolverError,
UnmatchedDereferenceStrategyError,
UnmatchedResolveStrategyError,
UnmatchedResolverError,
} from './util/errors';

export const readFile = async (uri: string, options = {}): Promise<Buffer> => {
const mergedOptions = mergeOptions(defaultOptions, options);
const file = File({ uri: url.sanitize(uri) });
Expand Down Expand Up @@ -80,17 +75,3 @@ export const dereferenceApiDOM = async <T extends Element>(
const mergedOptions = mergeOptions(defaultOptions, options);
return dereferenceApiDOMFn(element, mergedOptions);
};

export {
DereferenceError,
InvalidSelectorError,
MaximumDereferenceDepthError,
MaximumResolverDepthError,
NotImplementedError,
ParserError,
PluginError,
ResolverError,
UnmatchedDereferenceStrategyError,
UnmatchedResolveStrategyError,
UnmatchedResolverError,
} from './util/errors';
50 changes: 4 additions & 46 deletions packages/apidom-reference/src/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
import FileResolver from '../resolve/resolvers/file/index-node';
import HttpResolverAxios from '../resolve/resolvers/http-axios';
import OpenApi3_0ResolveStrategy from '../resolve/strategies/openapi-3-0';
import OpenApi3_1ResolveStrategy from '../resolve/strategies/openapi-3-1';
import AsyncApi2ResolveStrategy from '../resolve/strategies/asyncapi-2';
import ApiDesignSystemsJsonParser from '../parse/parsers/api-design-systems-json';
import ApiDesignSystemsYamlParser from '../parse/parsers/api-design-systems-yaml';
import OpenApiJson3_0Parser from '../parse/parsers/openapi-json-3-0';
import OpenApiYaml3_0Parser from '../parse/parsers/openapi-yaml-3-0';
import OpenApiJson3_1Parser from '../parse/parsers/openapi-json-3-1';
import OpenApiYaml3_1Parser from '../parse/parsers/openapi-yaml-3-1';
import AsyncApiJson2Parser from '../parse/parsers/asyncapi-json-2';
import AsyncApiYaml2Parser from '../parse/parsers/asyncapi-yaml-2';
import JsonParser from '../parse/parsers/json';
import YamlParser from '../parse/parsers/yaml-1-2';
import BinaryParser from '../parse/parsers/binary/index-node';
import OpenApi3_0DereferenceStrategy from '../dereference/strategies/openapi-3-0';
import OpenApi3_1DereferenceStrategy from '../dereference/strategies/openapi-3-1';
import AsyncApi2DereferenceStrategy from '../dereference/strategies/asyncapi-2';
import { ReferenceOptions as IReferenceOptions } from '../types';

const defaultOptions: IReferenceOptions = {
Expand All @@ -33,19 +14,7 @@ const defaultOptions: IReferenceOptions = {
* your own implementation, or remove any resolver by removing it from the list.
* It's recommended to keep the order of parser from most specific ones to most generic ones.
*/
parsers: [
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
AsyncApiJson2Parser({ allowEmpty: true, sourceMap: false }),
AsyncApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
ApiDesignSystemsJsonParser({ allowEmpty: true, sourceMap: false }),
ApiDesignSystemsYamlParser({ allowEmpty: true, sourceMap: false }),
JsonParser({ allowEmpty: true, sourceMap: false }),
YamlParser({ allowEmpty: true, sourceMap: false }),
BinaryParser({ allowEmpty: true }),
],
parsers: [],

/**
* These options are merged with parser plugin instance before the plugin is run.
Expand All @@ -63,10 +32,7 @@ const defaultOptions: IReferenceOptions = {
* You can add additional resolvers of your own, replace an existing one with
* your own implementation, or remove any resolver by removing it from the list.
*/
resolvers: [
FileResolver(),
HttpResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
],
resolvers: [],
/**
* These options are merged with resolver plugin instance before the plugin is run.
*/
Expand All @@ -78,11 +44,7 @@ const defaultOptions: IReferenceOptions = {
* You can add additional resolver strategies of your own, replace an existing one with
* your own implementation, or remove any resolve strategy by removing it from the list.
*/
strategies: [
OpenApi3_0ResolveStrategy(),
OpenApi3_1ResolveStrategy(),
AsyncApi2ResolveStrategy(),
],
strategies: [],
/**
* Determines whether external references will be resolved.
* If this option is disabled, then none of above resolvers will be called.
Expand Down Expand Up @@ -110,11 +72,7 @@ const defaultOptions: IReferenceOptions = {
* You can add additional dereference strategies of your own, replace an existing one with
* your own implementation, or remove any dereference strategy by removing it from the list.
*/
strategies: [
OpenApi3_0DereferenceStrategy(),
OpenApi3_1DereferenceStrategy(),
AsyncApi2DereferenceStrategy(),
],
strategies: [],
/**
* This option accepts an instance of pre-computed ReferenceSet.
* If provided it will speed up the dereferencing significantly as the external
Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-reference/test/mocha-bootstrap.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { jestSnapshotPlugin, addSerializer } = require('mocha-chai-jest-snapshot'

const jestApiDOMSerializer = require('../../../scripts/jest-serializer-apidom.cjs');
const jestStringSerializer = require('../../../scripts/jest-serializer-string.cjs');
const { options } = require('../src');
const { options } = require('../src/configuration/saturated');

// setup snapshot testing
chai.use(jestSnapshotPlugin());
Expand Down
3 changes: 2 additions & 1 deletion packages/apidom-reference/test/resolve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import path from 'node:path';
import { assert } from 'chai';
import { mediaTypes } from '@swagger-api/apidom-ns-openapi-3-1';

import { resolve, resolveApiDOM, parse, FileResolver } from '../../src';
import { resolve, resolveApiDOM, parse } from '../../src';
import FileResolver from '../../src/resolve/resolvers/file/index-node';
import { UnmatchedResolveStrategyError, ResolverError, ParserError } from '../../src/util/errors';
import OpenApiJson3_1Parser from '../../src/parse/parsers/openapi-json-3-1';

Expand Down