Skip to content

Commit

Permalink
feat(resolver): create ApiDOM path -> SwaggerClient path translator
Browse files Browse the repository at this point in the history
Refs #2794
  • Loading branch information
char0n committed Jan 27, 2023
1 parent ef9d20d commit 280f301
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { isMemberElement, isArrayElement } from '@swagger-api/apidom-core';

const trimParseResult = (elementPath) => elementPath.slice(2);

/**
* Transforms ApiDOM traversal meta information into
* SwaggerClient compatible path.
*
* SwaggerClient path is a list of JSON Pointer tokens.
*/
const toPath = (elementPath) => {
const elementPathSanitized = trimParseResult(elementPath);

return elementPathSanitized.reduce((path, element, index) => {
if (isMemberElement(element)) {
const token = String(element.key.toValue());
path.push(token);
} else if (isArrayElement(elementPathSanitized[index - 2])) {
const token = elementPathSanitized[index - 2].content.indexOf(element);
path.push(token);
}

return path;
}, []);
};

export default toPath;

0 comments on commit 280f301

Please sign in to comment.