feat(editor): Recommend and pre-select auth type with overrides (#5684)

* feat(editor): Recommend auth type with overrides and pre-select them when creating new credentials
*  Only auto-selecting credentials on cloud
This commit is contained in:
Milorad FIlipović 2023-03-17 09:01:39 +01:00 committed by GitHub
parent 7a352efff9
commit f59b591c93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View file

@ -283,6 +283,7 @@
"credentialEdit.credentialConfig.missingCredentialType": "This credential's type isn't available. This usually happens when a previously installed community or custom node was uninstalled.",
"credentialEdit.credentialConfig.authTypeSelectorLabel": "Connect using",
"credentialEdit.credentialConfig.authTypeSelectorTooltip": "The authentication method to use for the connection",
"credentialEdit.credentialConfig.recommendedAuthTypeSuffix": "(recommended)",
"credentialEdit.credentialEdit.confirmMessage.beforeClose1.cancelButtonText": "Keep Editing",
"credentialEdit.credentialEdit.confirmMessage.beforeClose1.confirmButtonText": "Close",
"credentialEdit.credentialEdit.confirmMessage.beforeClose1.headline": "Close without saving?",

View file

@ -36,6 +36,9 @@ import {
INodePropertyCollection,
} from 'n8n-workflow';
import { isResourceLocatorValue, isJsonKeyObject } from '@/utils';
import { useCredentialsStore } from '@/stores/credentials';
import { i18n as locale } from '@/plugins/i18n';
import { useSettingsStore } from '@/stores/settings';
/*
Constants and utility functions mainly used to get information about
@ -381,6 +384,9 @@ export const getNodeAuthOptions = (
if (!nodeType) {
return [];
}
const recommendedSuffix = locale.baseText(
'credentialEdit.credentialConfig.recommendedAuthTypeSuffix',
);
let options: NodeAuthenticationOption[] = [];
const authProp = getMainAuthField(nodeType);
// Some nodes have multiple auth fields with same name but different display options so need
@ -392,18 +398,34 @@ export const getNodeAuthOptions = (
authProps.forEach((field) => {
if (field.options) {
options = options.concat(
field.options.map((option) => ({
name: option.name,
value: option.value,
// Also add in the display options so we can hide/show the option if necessary
displayOptions: field.displayOptions,
})) || [],
field.options.map((option) => {
// Check if credential type associated with this auth option has overwritten properties
let hasOverrides = false;
if (useSettingsStore().isCloudDeployment) {
const cred = getNodeCredentialForSelectedAuthType(nodeType, option.value);
if (cred) {
hasOverrides =
useCredentialsStore().getCredentialTypeByName(cred.name).__overwrittenProperties !==
undefined;
}
}
return {
name:
// Add recommended suffix if credentials have overrides and option is not already recommended
hasOverrides && !option.name.endsWith(recommendedSuffix)
? `${option.name} ${recommendedSuffix}`
: option.name,
value: option.value,
// Also add in the display options so we can hide/show the option if necessary
displayOptions: field.displayOptions,
};
}) || [],
);
}
});
// sort so recommended options are first
options.forEach((item, i) => {
if (item.name.includes('(recommended)')) {
if (item.name.includes(recommendedSuffix)) {
options.splice(i, 1);
options.unshift(item);
}