mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 21:19:43 -08:00
feat(editor): add automatic credential selection for new nodes (#2746)
* ⚡ 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"
042c9cc30f
* fix get
Co-authored-by: Mutasem <mutdmour@gmail.com>
This commit is contained in:
parent
353a28bfe7
commit
d31fbbba27
|
@ -147,6 +147,7 @@ export default mixins(
|
||||||
return this.node.credentials || {};
|
return this.node.credentials || {};
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
getCredentialOptions(type: string): ICredentialsResponse[] {
|
getCredentialOptions(type: string): ICredentialsResponse[] {
|
||||||
return (this.allCredentialsByType as Record<string, ICredentialsResponse[]>)[type].filter((credential) => {
|
return (this.allCredentialsByType as Record<string, ICredentialsResponse[]>)[type].filter((credential) => {
|
||||||
|
|
|
@ -1365,7 +1365,75 @@ export default mixins(
|
||||||
duration: 0,
|
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);
|
const nodeTypeData: INodeTypeDescription | null = this.$store.getters['nodeTypes/getNodeType'](nodeTypeName);
|
||||||
|
|
||||||
if (nodeTypeData === null) {
|
if (nodeTypeData === null) {
|
||||||
|
@ -1385,16 +1453,7 @@ export default mixins(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newNodeData: INodeUi = {
|
const newNodeData = await this.getNewNodeWithDefaultCredential(nodeTypeData);
|
||||||
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: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
// when pulling new connection from node or injecting into a connection
|
// when pulling new connection from node or injecting into a connection
|
||||||
const lastSelectedNode = this.lastSelectedNode;
|
const lastSelectedNode = this.lastSelectedNode;
|
||||||
|
|
Loading…
Reference in a new issue