Skip to content

Commit

Permalink
fix(editor): Fix node authentication options ordering and hiding opti…
Browse files Browse the repository at this point in the history
…ons based on node version (#5268)

* 🐛 Fixing auth options order and hiding options that are not valid for current node version
* 🔨 Minor refactoring
  • Loading branch information
MiloradFilipovic authored Jan 27, 2023
1 parent 92ae988 commit 7d74181
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const activeNodeType = computed<INodeTypeDescription | null>(() => {
});
const authOptions = computed<NodeAuthenticationOption[]>(() => {
return getNodeAuthOptions(activeNodeType.value);
return getNodeAuthOptions(activeNodeType.value, ndvStore.activeNode?.typeVersion);
});
const filteredNodeAuthOptions = computed<NodeAuthenticationOption[]>(() => {
Expand Down
32 changes: 29 additions & 3 deletions packages/editor-ui/src/utils/nodeTypesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ const findAlternativeAuthField = (
// Gets all authentication types that a given node type supports
export const getNodeAuthOptions = (
nodeType: INodeTypeDescription | null,
nodeVersion?: number,
): NodeAuthenticationOption[] => {
if (!nodeType) {
return [];
Expand All @@ -392,7 +393,9 @@ export const getNodeAuthOptions = (
const authProp = getMainAuthField(nodeType);
// Some nodes have multiple auth fields with same name but different display options so need
// take them all into account
const authProps = getNodeAuthFields(nodeType).filter((prop) => prop.name === authProp?.name);
const authProps = getNodeAuthFields(nodeType, nodeVersion).filter(
(prop) => prop.name === authProp?.name,
);

authProps.forEach((field) => {
if (field.options) {
Expand All @@ -406,6 +409,13 @@ export const getNodeAuthOptions = (
);
}
});
// sort so recommended options are first
options.forEach((item, i) => {
if (item.name.includes('(recommended)')) {
options.splice(i, 1);
options.unshift(item);
}
});
return options;
};

Expand Down Expand Up @@ -474,7 +484,10 @@ export const isAuthRelatedParameter = (
return isRelated;
};

export const getNodeAuthFields = (nodeType: INodeTypeDescription | null): INodeProperties[] => {
export const getNodeAuthFields = (
nodeType: INodeTypeDescription | null,
nodeVersion?: number,
): INodeProperties[] => {
const authFields: INodeProperties[] = [];
if (nodeType && nodeType.credentials && nodeType.credentials.length > 0) {
nodeType.credentials.forEach((cred) => {
Expand All @@ -483,7 +496,10 @@ export const getNodeAuthFields = (nodeType: INodeTypeDescription | null): INodeP
const nodeFieldsForName = nodeType.properties.filter((prop) => prop.name === option);
if (nodeFieldsForName) {
nodeFieldsForName.forEach((nodeField) => {
if (!authFields.includes(nodeField)) {
if (
!authFields.includes(nodeField) &&
isNodeFieldMatchingNodeVersion(nodeField, nodeVersion)
) {
authFields.push(nodeField);
}
});
Expand All @@ -495,6 +511,16 @@ export const getNodeAuthFields = (nodeType: INodeTypeDescription | null): INodeP
return authFields;
};

export const isNodeFieldMatchingNodeVersion = (
nodeField: INodeProperties,
nodeVersion: number | undefined,
) => {
if (nodeVersion && nodeField.displayOptions?.show?.['@version']) {
return nodeField.displayOptions.show['@version']?.includes(nodeVersion);
}
return true;
};

export const getCredentialsRelatedFields = (
nodeType: INodeTypeDescription | null,
credentialType: INodeCredentialDescription | null,
Expand Down

0 comments on commit 7d74181

Please sign in to comment.