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 all 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
67 changes: 66 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 All @@ -162,6 +170,7 @@ import {
} from './UserManagement/UserManagementHelper';
import { loadPublicApiVersions } from './PublicApi';
import * as telemetryScripts from './telemetry/scripts';
import { ResponseError } from './ResponseHelper';

require('body-parser-xml')(bodyParser);

Expand Down Expand Up @@ -932,6 +941,62 @@ class App {
),
);

// Returns parameter values which normally get loaded from an external API or
// get generated dynamically
this.app.get(
`/${this.restEndpoint}/nodes-list-search`,
ResponseHelper.send(
async (
req: NodeListSearchRequest,
res: express.Response,
): Promise<INodeListSearchResult | undefined> => {
const nodeTypeAndVersion = JSON.parse(
req.query.nodeTypeAndVersion,
) as INodeTypeNameVersion;

const { path, methodName } = req.query;

if (!req.query.currentNodeParameters) {
throw new ResponseError('Parameter currentNodeParameters is required.', undefined, 400);
}

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

let credentials: INodeCredentials | undefined;

if (req.query.credentials) {
credentials = JSON.parse(req.query.credentials);
}

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,
);
}

throw new ResponseError('Parameter methodName is required.', undefined, 400);
},
),
);

// 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
133 changes: 133 additions & 0 deletions packages/core/src/LoadNodeListSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* 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?: string,
): 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);
}
}
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