ToolJet/frontend/src/_helpers/ref_resolver.js
Muhsin Shah C P dcee0885d6
Feature: OpenApi import plugin (#2728)
* Initialized OpenApi plugin

* Added definition field to manifest file

* Added an openapi parser

* Implemented JSON/YAML resolver
- Removed swagger parser lib
- Added a field for choosing definition format type
- Implemented a resolver for resolving $ref

* Implemented a basic operations drop down from spec paths

* Added params and body ui

* Now, can view general parameters and servers

* Added an option to override general params
- Resolved a bug

* Implemented basic openapi plugin
- Added got lib
- Tested basic queries

* Added one more method

* Resolved a common bug

* Working on Parsing definition from datasource modal
- Created new form component for openapi
- Populated select array from security array

* Added ui for authentication

* Added bearer & basic plugin code

* Resolved some ui issues of Apikeys auth option

* Added apikey auth

* Resolved an issue
- Also replaced validate btn with auto validation
- Now spec will be added to db from data-source modal

* Added error text to show validation error

* Now spec will load from props for query manager

* Added some padding to operation description

* Added ui for openapi oauth2 auth

* Implemented oauth2

* Fixed a state issue with a temporary hack

* Now we can show multiple same auth types

* added the icon

* Resolved a bug & improved multiple apikeys process

* Resolved some realtime parsing error

* Resolved a bug from options fn

* Resolved PR changes

* Updated documentation with brief details about the open API plugin

* Removed unwanted package

* Changed if-else to switch

* Code changes

* added setLoadingSpec fn to catch section

* Solved typos and error in doc

* Resolved dropdown issue

* Resolved a bug

* updated openapi icon
2022-04-26 14:14:12 +05:30

35 lines
957 B
JavaScript

import _ from 'lodash';
export default class RefResolver {
constructor(originalJSON) {
this.originalJSON = originalJSON;
}
resolve(inputJSON) {
const outputJSON = _.cloneDeep(inputJSON || this.originalJSON);
if (typeof outputJSON === 'object') {
Object.keys(outputJSON).forEach((key) => {
outputJSON[key] = this.evaluateObject(outputJSON[key]);
});
}
return { result: outputJSON };
}
evaluateObject(inputObject) {
let outputObject = _.cloneDeep(inputObject);
if (typeof outputObject === 'object') {
if (outputObject.$ref) {
const array = outputObject.$ref.split('/');
const cleanedPath = array.slice(1, array.length).join('.');
const value = _.get(this.originalJSON, cleanedPath);
return value;
} else {
const resolvedJSON = this.resolve(outputObject).result;
outputObject = resolvedJSON;
}
}
return outputObject;
}
}