Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List mode search endpoint #3936

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ import { createHmac, randomBytes } from 'crypto';
// tested with all possible systems like Windows, Alpine on ARM, FreeBSD, ...
import { compare } from 'bcryptjs';

import { BinaryDataManager, Credentials, LoadNodeParameterOptions, UserSettings } from 'n8n-core';
import {
BinaryDataManager,
Credentials,
LoadNodeParameterOptions,
LoadNodeListSearch,
UserSettings,
} from 'n8n-core';

import {
ICredentialType,
IDataObject,
INodeCredentials,
INodeCredentialsDetails,
INodeListSearchResult,
INodeParameters,
INodePropertyOptions,
INodeType,
Expand Down Expand Up @@ -142,6 +149,7 @@ import { resolveJwt } from './UserManagement/auth/jwt';
import { User } from './databases/entities/User';
import type {
ExecutionRequest,
NodeListSearchRequest,
NodeParameterOptionsRequest,
OAuthRequest,
TagsRequest,
Expand Down Expand Up @@ -932,6 +940,51 @@ class App {
),
);

// Returns parameter values which normally get loaded from an external API or
// get generated dynamically
this.app.get(
`/${this.restEndpoint}/node-list-search`,
valya marked this conversation as resolved.
Show resolved Hide resolved
ResponseHelper.send(async (req: NodeListSearchRequest): Promise<INodeListSearchResult> => {
const nodeTypeAndVersion = JSON.parse(req.query.nodeTypeAndVersion) as INodeTypeNameVersion;

const { path, methodName } = req.query;

const currentNodeParameters = JSON.parse(
req.query.currentNodeParameters,
) as INodeParameters;

let credentials: INodeCredentials | undefined;

if (req.query.credentials) {
credentials = JSON.parse(req.query.credentials);
}
BHesseldieck marked this conversation as resolved.
Show resolved Hide resolved

const listSearchInstance = new LoadNodeListSearch(
nodeTypeAndVersion,
NodeTypes(),
path,
currentNodeParameters,
credentials,
);

const additionalData = await WorkflowExecuteAdditionalData.getBase(
req.user.id,
currentNodeParameters,
);

if (methodName) {
return listSearchInstance.getOptionsViaMethodName(
methodName,
additionalData,
req.query.filter,
req.query.paginationToken,
);
}

return { results: [] };
valya marked this conversation as resolved.
Show resolved Hide resolved
}),
);

// Returns all the node-types
this.app.get(
`/${this.restEndpoint}/node-types`,
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/requests.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,25 @@ export type NodeParameterOptionsRequest = AuthenticatedRequest<
}
>;

// ----------------------------------
// /node-list-search
// ----------------------------------

export type NodeListSearchRequest = AuthenticatedRequest<
{},
{},
{},
{
nodeTypeAndVersion: string;
methodName: string;
path: string;
currentNodeParameters: string;
credentials: string;
filter?: string;
paginationToken?: string;
valya marked this conversation as resolved.
Show resolved Hide resolved
}
>;

// ----------------------------------
// /tags
// ----------------------------------
Expand Down
224 changes: 224 additions & 0 deletions packages/core/src/LoadNodeListSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/* eslint-disable no-restricted-syntax */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import {
INode,
INodeCredentials,
INodeListSearchResult,
INodeParameters,
INodeTypeNameVersion,
INodeTypes,
IWorkflowExecuteAdditionalData,
Workflow,
} from 'n8n-workflow';

// eslint-disable-next-line import/no-cycle
import { NodeExecuteFunctions } from '.';

const TEMP_NODE_NAME = 'Temp-Node';
const TEMP_WORKFLOW_NAME = 'Temp-Workflow';

export class LoadNodeListSearch {
currentNodeParameters: INodeParameters;

path: string;

workflow: Workflow;

constructor(
nodeTypeNameAndVersion: INodeTypeNameVersion,
nodeTypes: INodeTypes,
path: string,
currentNodeParameters: INodeParameters,
credentials?: INodeCredentials,
) {
const nodeType = nodeTypes.getByNameAndVersion(
nodeTypeNameAndVersion.name,
nodeTypeNameAndVersion.version,
);
this.currentNodeParameters = currentNodeParameters;
this.path = path;
if (nodeType === undefined) {
throw new Error(
`The node-type "${nodeTypeNameAndVersion.name} v${nodeTypeNameAndVersion.version}" is not known!`,
);
}

const nodeData: INode = {
parameters: currentNodeParameters,
id: 'uuid-1234',
name: TEMP_NODE_NAME,
type: nodeTypeNameAndVersion.name,
typeVersion: nodeTypeNameAndVersion.version,
position: [0, 0],
};
if (credentials) {
nodeData.credentials = credentials;
}

const workflowData = {
nodes: [nodeData],
connections: {},
};

this.workflow = new Workflow({
nodes: workflowData.nodes,
connections: workflowData.connections,
active: false,
nodeTypes,
});
}

/**
* Returns data of a fake workflow
*
* @returns
* @memberof LoadNodeParameterOptions
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
getWorkflowData() {
return {
name: TEMP_WORKFLOW_NAME,
active: false,
connections: {},
nodes: Object.values(this.workflow.nodes),
createdAt: new Date(),
updatedAt: new Date(),
};
}

/**
* Returns the available options via a predefined method
*
* @param {string} methodName The name of the method of which to get the data from
* @param {IWorkflowExecuteAdditionalData} additionalData
* @returns {Promise<INodePropertyOptions[]>}
* @memberof LoadNodeParameterOptions
*/
async getOptionsViaMethodName(
methodName: string,
additionalData: IWorkflowExecuteAdditionalData,
filter?: string,
paginationToken?: unknown,
): Promise<INodeListSearchResult> {
const node = this.workflow.getNode(TEMP_NODE_NAME);

const nodeType = this.workflow.nodeTypes.getByNameAndVersion(node!.type, node?.typeVersion);

if (
!nodeType ||
nodeType.methods === undefined ||
nodeType.methods.listSearch === undefined ||
nodeType.methods.listSearch[methodName] === undefined
) {
throw new Error(
`The node-type "${node!.type}" does not have the method "${methodName}" defined!`,
);
}

const thisArgs = NodeExecuteFunctions.getLoadOptionsFunctions(
this.workflow,
node!,
this.path,
additionalData,
);

return nodeType.methods.listSearch[methodName].call(thisArgs, filter, paginationToken);
}

// Disable for now
valya marked this conversation as resolved.
Show resolved Hide resolved
// /**
// * Returns the available options via a load request informatoin
// *
// * @param {ILoadOptions} loadOptions The load options which also contain the request information
// * @param {IWorkflowExecuteAdditionalData} additionalData
// * @returns {Promise<INodePropertyOptions[]>}
// * @memberof LoadNodeParameterOptions
// */
// async getOptionsViaRequestProperty(
// loadOptions: ILoadOptions,
// additionalData: IWorkflowExecuteAdditionalData,
// ): Promise<INodePropertyOptions[]> {
// const node = this.workflow.getNode(TEMP_NODE_NAME);

// const nodeType = this.workflow.nodeTypes.getByNameAndVersion(node!.type, node?.typeVersion);

// if (
// nodeType === undefined ||
// !nodeType.description.requestDefaults ||
// !nodeType.description.requestDefaults.baseURL
// ) {
// // This in in here for now for security reasons.
// // Background: As the full data for the request to make does get send, and the auth data
// // will then be applied, would it be possible to retrieve that data like that. By at least
// // requiring a baseURL to be defined can at least not a random server be called.
// // In the future this code has to get improved that it does not use the request information from
// // the request rather resolves it via the parameter-path and nodeType data.
// throw new Error(
// `The node-type "${
// node!.type
// }" does not exist or does not have "requestDefaults.baseURL" defined!`,
// );
// }

// const mode = 'internal';
// const runIndex = 0;
// const connectionInputData: INodeExecutionData[] = [];
// const runExecutionData: IRunExecutionData = { resultData: { runData: {} } };

// const routingNode = new RoutingNode(
// this.workflow,
// node!,
// connectionInputData,
// runExecutionData ?? null,
// additionalData,
// mode,
// );

// // Create copy of node-type with the single property we want to get the data off
// const tempNode: INodeType = {
// ...nodeType,
// ...{
// description: {
// ...nodeType.description,
// properties: [
// {
// displayName: '',
// type: 'string',
// name: '',
// default: '',
// routing: loadOptions.routing,
// } as INodeProperties,
// ],
// },
// },
// };

// const inputData: ITaskDataConnections = {
// main: [[{ json: {} }]],
// };

// const optionsData = await routingNode.runNode(
// inputData,
// runIndex,
// tempNode,
// { node: node!, source: null, data: {} },
// NodeExecuteFunctions,
// );

// if (optionsData?.length === 0) {
// return [];
// }

// if (!Array.isArray(optionsData)) {
// throw new Error('The returned data is not an array!');
// }

// return optionsData[0].map((item) => item.json) as unknown as INodePropertyOptions[];
// }
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from './Constants';
export * from './Credentials';
export * from './Interfaces';
export * from './LoadNodeParameterOptions';
export * from './LoadNodeListSearch';
export * from './NodeExecuteFunctions';
export * from './WorkflowExecute';
export { NodeExecuteFunctions, UserSettings };
Loading