-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): Convert dynamic node-parameter routes to a decorated …
…controller (no-changelog) (#7284) 1. Reduce a lot of code duplication 2. Move more endpoints out of `Server.ts` 3. Move all query-param parsing and validation into a middleware to make the route handlers simpler.
- Loading branch information
Showing
14 changed files
with
425 additions
and
571 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/cli/src/controllers/dynamicNodeParameters.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Service } from 'typedi'; | ||
import type { RequestHandler } from 'express'; | ||
import { NextFunction, Response } from 'express'; | ||
import type { | ||
INodeListSearchResult, | ||
INodePropertyOptions, | ||
ResourceMapperFields, | ||
} from 'n8n-workflow'; | ||
import { jsonParse } from 'n8n-workflow'; | ||
|
||
import { Authorized, Get, Middleware, RestController } from '@/decorators'; | ||
import { getBase } from '@/WorkflowExecuteAdditionalData'; | ||
import { DynamicNodeParametersService } from '@/services/dynamicNodeParameters.service'; | ||
import { DynamicNodeParametersRequest } from '@/requests'; | ||
import { BadRequestError } from '@/ResponseHelper'; | ||
|
||
const assertMethodName: RequestHandler = (req, res, next) => { | ||
const { methodName } = req.query as DynamicNodeParametersRequest.BaseRequest['query']; | ||
if (!methodName) { | ||
throw new BadRequestError('Parameter methodName is required.'); | ||
} | ||
next(); | ||
}; | ||
|
||
@Service() | ||
@Authorized() | ||
@RestController('/dynamic-node-parameters') | ||
export class DynamicNodeParametersController { | ||
constructor(private readonly service: DynamicNodeParametersService) {} | ||
|
||
@Middleware() | ||
parseQueryParams( | ||
req: DynamicNodeParametersRequest.BaseRequest, | ||
res: Response, | ||
next: NextFunction, | ||
) { | ||
const { credentials, currentNodeParameters, nodeTypeAndVersion } = req.query; | ||
if (!nodeTypeAndVersion) { | ||
throw new BadRequestError('Parameter nodeTypeAndVersion is required.'); | ||
} | ||
if (!currentNodeParameters) { | ||
throw new BadRequestError('Parameter currentNodeParameters is required.'); | ||
} | ||
|
||
req.params = { | ||
nodeTypeAndVersion: jsonParse(nodeTypeAndVersion), | ||
currentNodeParameters: jsonParse(currentNodeParameters), | ||
credentials: credentials ? jsonParse(credentials) : undefined, | ||
}; | ||
|
||
next(); | ||
} | ||
|
||
/** Returns parameter values which normally get loaded from an external API or get generated dynamically */ | ||
@Get('/options') | ||
async getOptions(req: DynamicNodeParametersRequest.Options): Promise<INodePropertyOptions[]> { | ||
const { path, methodName, loadOptions } = req.query; | ||
const { credentials, currentNodeParameters, nodeTypeAndVersion } = req.params; | ||
const additionalData = await getBase(req.user.id, currentNodeParameters); | ||
|
||
if (methodName) { | ||
return this.service.getOptionsViaMethodName( | ||
methodName, | ||
path, | ||
additionalData, | ||
nodeTypeAndVersion, | ||
currentNodeParameters, | ||
credentials, | ||
); | ||
} | ||
|
||
if (loadOptions) { | ||
return this.service.getOptionsViaLoadOptions( | ||
jsonParse(loadOptions), | ||
additionalData, | ||
nodeTypeAndVersion, | ||
currentNodeParameters, | ||
credentials, | ||
); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
@Get('/resource-locator-results', { middlewares: [assertMethodName] }) | ||
async getResourceLocatorResults( | ||
req: DynamicNodeParametersRequest.ResourceLocatorResults, | ||
): Promise<INodeListSearchResult | undefined> { | ||
const { path, methodName, filter, paginationToken } = req.query; | ||
const { credentials, currentNodeParameters, nodeTypeAndVersion } = req.params; | ||
const additionalData = await getBase(req.user.id, currentNodeParameters); | ||
return this.service.getResourceLocatorResults( | ||
methodName, | ||
path, | ||
additionalData, | ||
nodeTypeAndVersion, | ||
currentNodeParameters, | ||
credentials, | ||
filter, | ||
paginationToken, | ||
); | ||
} | ||
|
||
@Get('/resource-mapper-fields', { middlewares: [assertMethodName] }) | ||
async getResourceMappingFields( | ||
req: DynamicNodeParametersRequest.ResourceMapperFields, | ||
): Promise<ResourceMapperFields | undefined> { | ||
const { path, methodName } = req.query; | ||
const { credentials, currentNodeParameters, nodeTypeAndVersion } = req.params; | ||
const additionalData = await getBase(req.user.id, currentNodeParameters); | ||
return this.service.getResourceMappingFields( | ||
methodName, | ||
path, | ||
additionalData, | ||
nodeTypeAndVersion, | ||
currentNodeParameters, | ||
credentials, | ||
); | ||
} | ||
} |
Oops, something went wrong.