-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resolver): create ApiDOM path -> SwaggerClient path translator
Refs #2794
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...lpers/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/utils/to-path.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |