-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add sorting to GET
/workflows
endpoint (#13029)
- Loading branch information
1 parent
18eaa54
commit b60011a
Showing
7 changed files
with
246 additions
and
1 deletion.
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
22 changes: 22 additions & 0 deletions
22
packages/cli/src/middlewares/list-query/dtos/workflow.sort-by.dto.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,22 @@ | ||
import type { ValidatorConstraintInterface, ValidationArguments } from 'class-validator'; | ||
import { IsString, Validate, ValidatorConstraint } from 'class-validator'; | ||
|
||
@ValidatorConstraint({ name: 'WorkflowSortByParameter', async: false }) | ||
export class WorkflowSortByParameter implements ValidatorConstraintInterface { | ||
validate(text: string, _: ValidationArguments) { | ||
const [column, order] = text.split(':'); | ||
if (!column || !order) return false; | ||
|
||
return ['name', 'createdAt', 'updatedAt'].includes(column) && ['asc', 'desc'].includes(order); | ||
} | ||
|
||
defaultMessage(_: ValidationArguments) { | ||
return 'Invalid value for sortBy parameter'; | ||
} | ||
} | ||
|
||
export class WorkflowSorting { | ||
@IsString() | ||
@Validate(WorkflowSortByParameter) | ||
sortBy?: string; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { plainToInstance } from 'class-transformer'; | ||
import { validateSync } from 'class-validator'; | ||
import type { RequestHandler } from 'express'; | ||
import { ApplicationError } from 'n8n-workflow'; | ||
|
||
import type { ListQuery } from '@/requests'; | ||
import * as ResponseHelper from '@/response-helper'; | ||
import { toError } from '@/utils'; | ||
|
||
import { WorkflowSorting } from './dtos/workflow.sort-by.dto'; | ||
|
||
export const sortByQueryMiddleware: RequestHandler = (req: ListQuery.Request, res, next) => { | ||
const { sortBy } = req.query; | ||
|
||
if (!sortBy) return next(); | ||
|
||
let SortBy; | ||
|
||
try { | ||
if (req.baseUrl.endsWith('workflows')) { | ||
SortBy = WorkflowSorting; | ||
} else { | ||
return next(); | ||
} | ||
|
||
const validationResponse = validateSync(plainToInstance(SortBy, { sortBy })); | ||
|
||
if (validationResponse.length) { | ||
const validationError = validationResponse[0]; | ||
throw new ApplicationError(validationError.constraints?.workflowSortBy ?? ''); | ||
} | ||
|
||
req.listQueryOptions = { ...req.listQueryOptions, sortBy }; | ||
|
||
next(); | ||
} catch (maybeError) { | ||
ResponseHelper.sendErrorResponse(res, toError(maybeError)); | ||
} | ||
}; |
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