Skip to content

Commit

Permalink
feat(editor): add automatic credential selection for new nodes (#2746)
Browse files Browse the repository at this point in the history
* ⚡ implemented automatic credential selection in nodes

* ⚡ fixed implementation

* ⚡ fixed linter error

* 🔨 in progress, removed watching for auth type, added check for more then one existing credential

* 🔨 removed console log

* 🔨 changing auth method for one that have default credential

* 🔨 credentials will be set only at node creation time

* 🔨 fixed authentication parameter assigment for nodes that does not have it

* ⚡ better properties checking

* 🔨 improvements

* 🔨 extracted into function, fix issue with assigning hidden credentials

* remove console log

* fix bug with multiple creds

* fix defaults issue

* remove import

* simplify to just auth

* Revert "simplify to just auth"

042c9cc

* fix get

Co-authored-by: Mutasem <mutdmour@gmail.com>
  • Loading branch information
michael-radency and mutdmour committed Oct 17, 2022
1 parent 353a28b commit d31fbbb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/editor-ui/src/components/NodeCredentials.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export default mixins(
return this.node.credentials || {};
},
},
methods: {
getCredentialOptions(type: string): ICredentialsResponse[] {
return (this.allCredentialsByType as Record<string, ICredentialsResponse[]>)[type].filter((credential) => {
Expand Down
81 changes: 70 additions & 11 deletions packages/editor-ui/src/views/NodeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,75 @@ export default mixins(
duration: 0,
});
},
async injectNode(nodeTypeName: string, options: AddNodeOptions = {}) {
async getNewNodeWithDefaultCredential(nodeTypeData: INodeTypeDescription) {
const newNodeData: INodeUi = {
id: uuid(),
name: nodeTypeData.defaults.name as string,
type: nodeTypeData.name,
typeVersion: Array.isArray(nodeTypeData.version)
? nodeTypeData.version.slice(-1)[0]
: nodeTypeData.version,
position: [0, 0],
parameters: {},
};
const credentialPerType = nodeTypeData.credentials && nodeTypeData.credentials
.map(type => this.$store.getters['credentials/getCredentialsByType'](type.name))
.flat();
if (credentialPerType && credentialPerType.length === 1) {
const defaultCredential = credentialPerType[0];
const selectedCredentials = this.$store.getters['credentials/getCredentialById'](defaultCredential.id);
const selected = { id: selectedCredentials.id, name: selectedCredentials.name };
const credentials = {
[defaultCredential.type]: selected,
};
await this.loadNodesProperties([newNodeData].map(node => ({name: node.type, version: node.typeVersion})));
const nodeType = this.$store.getters['nodeTypes/getNodeType'](newNodeData.type, newNodeData.typeVersion) as INodeTypeDescription;
const nodeParameters = NodeHelpers.getNodeParameters(nodeType.properties, {}, true, false, newNodeData);
if (nodeTypeData.credentials) {
const authentication = nodeTypeData.credentials.find(type => type.name === defaultCredential.type);
if (authentication?.displayOptions?.hide) {
return newNodeData;
}
const authDisplayOptions = authentication?.displayOptions?.show;
if (!authDisplayOptions) {
newNodeData.credentials = credentials;
return newNodeData;
}
if (Object.keys(authDisplayOptions).length === 1 && authDisplayOptions['authentication']) {
// ignore complex case when there's multiple dependencies
newNodeData.credentials = credentials;
let parameters: { [key:string]: string } = {};
for (const displayOption of Object.keys(authDisplayOptions)) {
if (nodeParameters && !nodeParameters[displayOption]) {
parameters = {};
newNodeData.credentials = undefined;
break;
}
const optionValue = authDisplayOptions[displayOption]?.[0];
if (optionValue && typeof optionValue === 'string') {
parameters[displayOption] = optionValue;
}
newNodeData.parameters = {
...newNodeData.parameters,
...parameters,
};
}
}
}
}
return newNodeData;
},
async injectNode (nodeTypeName: string, options: AddNodeOptions = {}) {
const nodeTypeData: INodeTypeDescription | null = this.$store.getters['nodeTypes/getNodeType'](nodeTypeName);
if (nodeTypeData === null) {
Expand All @@ -1385,16 +1453,7 @@ export default mixins(
return;
}
const newNodeData: INodeUi = {
id: uuid(),
name: nodeTypeData.defaults.name as string,
type: nodeTypeData.name,
typeVersion: Array.isArray(nodeTypeData.version)
? nodeTypeData.version.slice(-1)[0]
: nodeTypeData.version,
position: [0, 0],
parameters: {},
};
const newNodeData = await this.getNewNodeWithDefaultCredential(nodeTypeData);
// when pulling new connection from node or injecting into a connection
const lastSelectedNode = this.lastSelectedNode;
Expand Down

0 comments on commit d31fbbb

Please sign in to comment.