mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 00:54:06 -08:00
⚡ Add tag update endpoint
This commit is contained in:
parent
f7a13b891b
commit
e53c3211d5
|
@ -18,6 +18,9 @@ import {
|
||||||
IDeferredPromise,
|
IDeferredPromise,
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
|
import {
|
||||||
|
UpdateResult
|
||||||
|
} from 'typeorm';
|
||||||
|
|
||||||
import * as PCancelable from 'p-cancelable';
|
import * as PCancelable from 'p-cancelable';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
|
@ -75,13 +78,17 @@ export interface IWorkflowBase extends IWorkflowBaseWorkflow {
|
||||||
id?: number | string;
|
id?: number | string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ITagDb {
|
export interface ITagBase {
|
||||||
id?: number | string; // auto-generated so unneeded in POST payload
|
|
||||||
name: string;
|
name: string;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ITagDb extends ITagBase {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ITagResponse extends UpdateResult {}
|
||||||
|
|
||||||
// Almost identical to editor-ui.Interfaces.ts
|
// Almost identical to editor-ui.Interfaces.ts
|
||||||
export interface IWorkflowDb extends IWorkflowBase {
|
export interface IWorkflowDb extends IWorkflowBase {
|
||||||
|
|
|
@ -108,7 +108,9 @@ import * as querystring from 'querystring';
|
||||||
import * as Queue from '../src/Queue';
|
import * as Queue from '../src/Queue';
|
||||||
import { OptionsWithUrl } from 'request-promise-native';
|
import { OptionsWithUrl } from 'request-promise-native';
|
||||||
import { Registry } from 'prom-client';
|
import { Registry } from 'prom-client';
|
||||||
import { ITagDb } from './Interfaces';
|
import { ITagBase, ITagDb, ITagResponse } from './Interfaces';
|
||||||
|
|
||||||
|
import * as TagHelpers from './TagHelpers';
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
|
|
||||||
|
@ -727,26 +729,17 @@ class App {
|
||||||
|
|
||||||
// Creates a tag
|
// Creates a tag
|
||||||
this.app.post(`/${this.restEndpoint}/tags`, ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<ITagDb> => {
|
this.app.post(`/${this.restEndpoint}/tags`, ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<ITagDb> => {
|
||||||
const { tagName } = req.body as { tagName: string };
|
const { name } = req.body;
|
||||||
|
const existingName = await TagHelpers.nameExists(name);
|
||||||
|
|
||||||
const alreadyExists = async (tagName: string) => {
|
if (existingName) {
|
||||||
const findQuery = { where: { name: tagName } } as FindOneOptions;
|
|
||||||
const result = await Db.collections.Tag!.findOne(findQuery);
|
|
||||||
return result !== undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
const hasInvalidLength = (tagName: string) => !tagName.length || tagName.length > 24;
|
|
||||||
|
|
||||||
if (await alreadyExists(tagName)) {
|
|
||||||
throw new ResponseHelper.ResponseError('Tag name already exists.', undefined, 400);
|
throw new ResponseHelper.ResponseError('Tag name already exists.', undefined, 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasInvalidLength(tagName)) {
|
TagHelpers.validateLength(name);
|
||||||
throw new ResponseHelper.ResponseError('Tag name must be 1 to 24 characters long.', undefined, 400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const newTag: ITagDb = {
|
const newTag: ITagBase = {
|
||||||
name: tagName,
|
name,
|
||||||
createdAt: this.getCurrentDate(),
|
createdAt: this.getCurrentDate(),
|
||||||
updatedAt: this.getCurrentDate(),
|
updatedAt: this.getCurrentDate(),
|
||||||
};
|
};
|
||||||
|
@ -760,6 +753,26 @@ class App {
|
||||||
return true;
|
return true;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Updates an existing tag
|
||||||
|
this.app.patch(`/${this.restEndpoint}/tags/:id`, ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<ITagResponse> => {
|
||||||
|
const { name } = req.body;
|
||||||
|
const { id } = req.params;
|
||||||
|
const existingId = await TagHelpers.idExists(id);
|
||||||
|
|
||||||
|
if (!existingId) {
|
||||||
|
throw new ResponseHelper.ResponseError(`Tag with the ID "${id}" does not exist.`, undefined, 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
TagHelpers.validateLength(name);
|
||||||
|
|
||||||
|
const updatedTag: Partial<ITagDb> = {
|
||||||
|
name,
|
||||||
|
updatedAt: this.getCurrentDate(),
|
||||||
|
};
|
||||||
|
|
||||||
|
return await Db.collections.Tag!.update(id, updatedTag);
|
||||||
|
}));
|
||||||
|
|
||||||
// Returns parameter values which normally get loaded from an external API or
|
// Returns parameter values which normally get loaded from an external API or
|
||||||
// get generated dynamically
|
// get generated dynamically
|
||||||
this.app.get(`/${this.restEndpoint}/node-parameter-options`, ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<INodePropertyOptions[]> => {
|
this.app.get(`/${this.restEndpoint}/node-parameter-options`, ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<INodePropertyOptions[]> => {
|
||||||
|
|
22
packages/cli/src/TagHelpers.ts
Normal file
22
packages/cli/src/TagHelpers.ts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import { FindOneOptions } from "typeorm";
|
||||||
|
import { Db, ResponseHelper } from ".";
|
||||||
|
|
||||||
|
export async function nameExists(name: string) {
|
||||||
|
const findQuery = { where: { name } } as FindOneOptions;
|
||||||
|
const result = await Db.collections.Tag!.findOne(findQuery);
|
||||||
|
|
||||||
|
return result !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function idExists(id: string) {
|
||||||
|
const findQuery = { where: { id } } as FindOneOptions;
|
||||||
|
const result = await Db.collections.Tag!.findOne(findQuery);
|
||||||
|
|
||||||
|
return result !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateLength(name: string) {
|
||||||
|
if (name.length < 0 || name.length > 24) {
|
||||||
|
throw new ResponseHelper.ResponseError('Tag name must be 1 to 24 characters long.', undefined, 400);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue