Skip to content

Commit

Permalink
feat(reference): change all strategies from stamps to TypeScript clas…
Browse files Browse the repository at this point in the history
…ses (#4103)

Refs #3481

BREAKING CHANGE: all strategies from apidom-reference package became a class and requires to be instantiated with new operator.
  • Loading branch information
char0n committed May 14, 2024
1 parent e3d195d commit 7e6dad4
Show file tree
Hide file tree
Showing 28 changed files with 767 additions and 830 deletions.
4 changes: 2 additions & 2 deletions packages/apidom-reference/src/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import File from '../File';
import * as plugins from '../util/plugins';
import UnmatchedBundleStrategyError from '../errors/UnmatchedBundleStrategyError';
import BundleError from '../errors/BundleError';
import { ReferenceOptions as IReferenceOptions } from '../types';
import parse from '../parse';
import { merge as mergeOptions } from '../options/util';
import * as url from '../util/url';
import type { ReferenceOptions } from '../options';

/**
* Bundle a file with all its external references to a compound document.
*/
const bundle = async (uri: string, options: IReferenceOptions): Promise<ParseResultElement> => {
const bundle = async (uri: string, options: ReferenceOptions): Promise<ParseResultElement> => {
const { refSet } = options.bundle;
const sanitizedURI = url.sanitize(uri);
const mergedOptions = mergeOptions(options, { resolve: { baseURI: sanitizedURI } });
Expand Down
6 changes: 6 additions & 0 deletions packages/apidom-reference/src/bundle/strategies/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"class-methods-use-this": 0,
"@typescript-eslint/naming-convention": 0
}
}
34 changes: 16 additions & 18 deletions packages/apidom-reference/src/bundle/strategies/BundleStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import stampit from 'stampit';
import { NotImplementedError } from '@swagger-api/apidom-error';
import { ParseResultElement } from '@swagger-api/apidom-core';

import { BundleStrategy as IBundleStrategy } from '../../types';
import File from '../../File';
import type { ReferenceOptions } from '../../options';

const BundleStrategy: stampit.Stamp<IBundleStrategy> = stampit({
props: {
name: null,
},
methods: {
canBundle() {
return false;
},
export interface BundleStrategyOptions {
readonly name: string;
}

async bundle(): Promise<never> {
throw new NotImplementedError(
'bundle method in BundleStrategy stamp is not yet implemented.',
);
},
},
});
abstract class BundleStrategy {
public readonly name: string;

constructor({ name }: BundleStrategyOptions) {
this.name = name;
}

abstract canBundle(file: File, options: ReferenceOptions): boolean;
abstract bundle(file: File, options: ReferenceOptions): Promise<ParseResultElement>;
}

export default BundleStrategy;
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import stampit from 'stampit';
import { ParseResultElement } from '@swagger-api/apidom-core';
import { mediaTypes, isOpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';

import File from '../../../File';
import BundleStrategy from '../BundleStrategy';
import { BundleStrategy as IBundleStrategy } from '../../../types';
import BundleStrategy, { BundleStrategyOptions } from '../BundleStrategy';

// eslint-disable-next-line @typescript-eslint/naming-convention
const OpenApi3_1BundleStrategy: stampit.Stamp<IBundleStrategy> = stampit(BundleStrategy, {
init() {
this.name = 'openapi-3-1';
},
methods: {
canBundle(file: File): boolean {
// assert by media type
if (file.mediaType !== 'text/plain') {
return mediaTypes.includes(file.mediaType);
}
export interface OpenAPI3_1BundleStrategyOptions extends Omit<BundleStrategyOptions, 'name'> {}

// assert by inspecting ApiDOM
return isOpenApi3_1Element(file.parseResult?.result);
},
class OpenAPI3_1BundleStrategy extends BundleStrategy {
constructor(options?: OpenAPI3_1BundleStrategyOptions) {
super({ ...(options ?? {}), name: 'openapi-3-1' });
}

async bundle(file: File): Promise<ParseResultElement> {
return file.parseResult!;
},
},
});
canBundle(file: File): boolean {
// assert by media type
if (file.mediaType !== 'text/plain') {
return mediaTypes.includes(file.mediaType);
}

export default OpenApi3_1BundleStrategy;
// assert by inspecting ApiDOM
return isOpenApi3_1Element(file.parseResult?.result);
}

async bundle(file: File): Promise<ParseResultElement> {
return file.parseResult!;
}
}

export default OpenAPI3_1BundleStrategy;
40 changes: 20 additions & 20 deletions packages/apidom-reference/src/configuration/saturated.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import FileResolver from '../resolve/resolvers/file/index-node';
import HTTPResolverAxios from '../resolve/resolvers/http-axios';
import OpenApi2ResolveStrategy from '../resolve/strategies/openapi-2';
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 OpenAPI2ResolveStrategy from '../resolve/strategies/openapi-2';
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 ApiDOMResolveStrategy from '../resolve/strategies/apidom';
import APIDesignSystemsJSONParser from '../parse/parsers/api-design-systems-json';
import APIDesignSystemsYAMLParser from '../parse/parsers/api-design-systems-yaml';
Expand All @@ -22,11 +22,11 @@ import JSONParser from '../parse/parsers/json';
import YAMLParser from '../parse/parsers/yaml-1-2';
import BinaryParser from '../parse/parsers/binary/index-node';
import ApiDOMDereferenceStrategy from '../dereference/strategies/apidom';
import OpenApi2DereferenceStrategy from '../dereference/strategies/openapi-2';
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 OpenApi3_1BundleStrategy from '../bundle/strategies/openapi-3-1';
import OpenAPI2DereferenceStrategy from '../dereference/strategies/openapi-2';
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 OpenAPI3_1BundleStrategy from '../bundle/strategies/openapi-3-1';
import { options } from '../index';

options.parse.parsers = [
Expand Down Expand Up @@ -54,21 +54,21 @@ options.resolve.resolvers = [
];

options.resolve.strategies = [
OpenApi2ResolveStrategy(),
OpenApi3_0ResolveStrategy(),
OpenApi3_1ResolveStrategy(),
AsyncApi2ResolveStrategy(),
ApiDOMResolveStrategy(),
new OpenAPI2ResolveStrategy(),
new OpenAPI3_0ResolveStrategy(),
new OpenAPI3_1ResolveStrategy(),
new AsyncAPI2ResolveStrategy(),
new ApiDOMResolveStrategy(),
];

options.dereference.strategies = [
OpenApi2DereferenceStrategy(),
OpenApi3_0DereferenceStrategy(),
OpenApi3_1DereferenceStrategy(),
AsyncApi2DereferenceStrategy(),
ApiDOMDereferenceStrategy(),
new OpenAPI2DereferenceStrategy(),
new OpenAPI3_0DereferenceStrategy(),
new OpenAPI3_1DereferenceStrategy(),
new AsyncAPI2DereferenceStrategy(),
new ApiDOMDereferenceStrategy(),
];

options.bundle.strategies = [OpenApi3_1BundleStrategy()];
options.bundle.strategies = [new OpenAPI3_1BundleStrategy()];

export * from '../index';
9 changes: 3 additions & 6 deletions packages/apidom-reference/src/dereference/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import File from '../File';
import * as plugins from '../util/plugins';
import UnmatchedDereferenceStrategyError from '../errors/UnmatchedDereferenceStrategyError';
import DereferenceError from '../errors/DereferenceError';
import { ReferenceOptions as IReferenceOptions } from '../types';
import parse from '../parse';
import { merge as mergeOptions } from '../options/util';
import * as url from '../util/url';
import type { ReferenceOptions } from '../options';

/**
* Dereferences ApiDOM with all its external references.
*/
export const dereferenceApiDOM = async <T extends Element>(
element: T,
options: IReferenceOptions,
options: ReferenceOptions,
): Promise<T> => {
// @ts-ignore
let parseResult: ParseResultElement = element;
Expand Down Expand Up @@ -63,10 +63,7 @@ export const dereferenceApiDOM = async <T extends Element>(
/**
* Dereferences a file with all its external references.
*/
const dereference = async (
uri: string,
options: IReferenceOptions,
): Promise<ParseResultElement> => {
const dereference = async (uri: string, options: ReferenceOptions): Promise<ParseResultElement> => {
const { refSet } = options.dereference;
const sanitizedURI = url.sanitize(uri);
let parseResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"class-methods-use-this": 0,
"@typescript-eslint/naming-convention": 0
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import stampit from 'stampit';
import { NotImplementedError } from '@swagger-api/apidom-error';
import { Element } from 'minim';

import { DereferenceStrategy as IDereferenceStrategy } from '../../types';
import File from '../../File';
import type { ReferenceOptions } from '../../options';

const DereferenceStrategy: stampit.Stamp<IDereferenceStrategy> = stampit({
props: {
name: null,
},
methods: {
canDereference() {
return false;
},
export interface DereferenceStrategyOptions {
readonly name: string;
}

async dereference(): Promise<never> {
throw new NotImplementedError(
'dereference method in DereferenceStrategy stamp is not yet implemented.',
);
},
},
});
abstract class DereferenceStrategy {
public readonly name: string;

constructor({ name }: DereferenceStrategyOptions) {
this.name = name;
}

abstract canDereference(file: File, options: ReferenceOptions): boolean;
abstract dereference(file: File, options: ReferenceOptions): Promise<Element>;
}

export default DereferenceStrategy;
Loading

0 comments on commit 7e6dad4

Please sign in to comment.