Skip to content

Commit

Permalink
feat(converter): add convenient toolbox utils (#3816)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Feb 12, 2024
1 parent e103eb4 commit ec8fa05
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Expand Up @@ -5,7 +5,6 @@ import {
import {
isOpenApi3_1Element,
mediaTypes as openAPI3_1MediaTypes,
createToolbox,
keyMap,
getNodeType,
} from '@swagger-api/apidom-ns-openapi-3-1';
Expand All @@ -20,6 +19,7 @@ import ConvertStrategy, { IFile } from '../ConvertStrategy';
import openAPIVersionRefractorPlugin from './refractor-plugins/openapi-version';
import webhooksRefractorPlugin from './refractor-plugins/webhooks';
import type { ConverterOptions } from '../../options';
import createToolbox from './toolbox';

// eslint-disable-next-line @typescript-eslint/naming-convention
const openAPI3_0_3MediaTypes = [
Expand Down
@@ -0,0 +1,36 @@
import { Element, Meta, Attributes, AnnotationElement } from '@swagger-api/apidom-core';
import { createToolbox as createToolboxOpenAPI31 } from '@swagger-api/apidom-ns-openapi-3-1';

const createToolbox = () => {
const openAPI31Toolbox = createToolboxOpenAPI31();

const copySourceMap = <T extends Element, U extends Element>(from: T, to: U): void => {
if (openAPI31Toolbox.predicates.hasElementSourceMap(from)) {
to.meta.set('sourceMap', from.meta.get('sourceMap'));
}
};

const createAnnotation = (content?: string, meta?: Meta, attributes?: Attributes) => {
return new AnnotationElement(content, meta, attributes);
};
createAnnotation.fromElement = <T extends Element>(
element: T,
content?: string,
meta?: Meta,
attributes?: Attributes,
) => {
const annotation = createAnnotation(content, meta, attributes);
copySourceMap(element, annotation);
return annotation;
};

return {
...openAPI31Toolbox,
copySourceMap,
createAnnotation,
};
};

export type Toolbox = ReturnType<typeof createToolbox>;

export default createToolbox;
1 change: 1 addition & 0 deletions packages/apidom-ns-openapi-3-0/src/index.ts
Expand Up @@ -20,6 +20,7 @@ export { default as refractorPluginReplaceEmptyElement } from './refractor/plugi

export { default as refract, createRefractor } from './refractor';
export { default as createToolbox } from './refractor/toolbox';
export type { Predicates as ToolboxPredicates } from './refractor/toolbox';
export { default as specificationObj } from './refractor/specification';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/apidom-ns-openapi-3-1/src/index.ts
Expand Up @@ -24,6 +24,7 @@ export { default as refractorPluginNormalizeOperationIds } from './refractor/plu
export { default as refractorPluginNormalizeParameterExamples } from './refractor/plugins/normalize-parameter-examples';
export { default as refractorPluginNormalizeHeaderExamples } from './refractor/plugins/normalize-header-examples';
export { default as createToolbox } from './refractor/toolbox';
export type { Predicates as ToolboxPredicates } from './refractor/toolbox';
export { default as specificationObj } from './refractor/specification';

export { AlternatingVisitor } from '@swagger-api/apidom-ns-openapi-3-0';
Expand Down

0 comments on commit ec8fa05

Please sign in to comment.