feat: Support create, read, delete variables in Public API (#10241)
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
delete:
|
||||
x-eov-operation-id: deleteVariable
|
||||
x-eov-operation-handler: v1/handlers/variables/variables.handler
|
||||
tags:
|
||||
- Variables
|
||||
summary: Delete a variable
|
||||
description: Delete a variable from your instance.
|
||||
parameters:
|
||||
- $ref: '../schemas/parameters/variableId.yml'
|
||||
responses:
|
||||
'204':
|
||||
description: Operation successful.
|
||||
'401':
|
||||
$ref: '../../../../shared/spec/responses/unauthorized.yml'
|
||||
'404':
|
||||
$ref: '../../../../shared/spec/responses/notFound.yml'
|
||||
@@ -0,0 +1,40 @@
|
||||
post:
|
||||
x-eov-operation-id: createVariable
|
||||
x-eov-operation-handler: v1/handlers/variables/variables.handler
|
||||
tags:
|
||||
- Variables
|
||||
summary: Create a variable
|
||||
description: Create a variable in your instance.
|
||||
requestBody:
|
||||
description: Payload for variable to create.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '../schemas/variable.yml'
|
||||
required: true
|
||||
responses:
|
||||
'201':
|
||||
description: Operation successful.
|
||||
'400':
|
||||
$ref: '../../../../shared/spec/responses/badRequest.yml'
|
||||
'401':
|
||||
$ref: '../../../../shared/spec/responses/unauthorized.yml'
|
||||
get:
|
||||
x-eov-operation-id: getVariables
|
||||
x-eov-operation-handler: v1/handlers/variables/variables.handler
|
||||
tags:
|
||||
- Variables
|
||||
summary: Retrieve variables
|
||||
description: Retrieve variables from your instance.
|
||||
parameters:
|
||||
- $ref: '../../../../shared/spec/parameters/limit.yml'
|
||||
- $ref: '../../../../shared/spec/parameters/cursor.yml'
|
||||
responses:
|
||||
'200':
|
||||
description: Operation successful.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '../schemas/variableList.yml'
|
||||
'401':
|
||||
$ref: '../../../../shared/spec/responses/unauthorized.yml'
|
||||
@@ -0,0 +1,6 @@
|
||||
name: id
|
||||
in: path
|
||||
description: The ID of the variable.
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
@@ -0,0 +1,17 @@
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required:
|
||||
- key
|
||||
- value
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
readOnly: true
|
||||
key:
|
||||
type: string
|
||||
value:
|
||||
type: string
|
||||
example: test
|
||||
type:
|
||||
type: string
|
||||
readOnly: true
|
||||
@@ -0,0 +1,11 @@
|
||||
type: object
|
||||
properties:
|
||||
data:
|
||||
type: array
|
||||
items:
|
||||
$ref: './variable.yml'
|
||||
nextCursor:
|
||||
type: string
|
||||
description: Paginate through variables by setting the cursor parameter to a nextCursor attribute returned by a previous request. Default value fetches the first "page" of the collection.
|
||||
nullable: true
|
||||
example: MTIzZTQ1NjctZTg5Yi0xMmQzLWE0NTYtNDI2NjE0MTc0MDA
|
||||
@@ -0,0 +1,55 @@
|
||||
import Container from 'typedi';
|
||||
import { VariablesRepository } from '@/databases/repositories/variables.repository';
|
||||
import { VariablesController } from '@/environments/variables/variables.controller.ee';
|
||||
import { globalScope, isLicensed, validCursor } from '../../shared/middlewares/global.middleware';
|
||||
import { encodeNextCursor } from '../../shared/services/pagination.service';
|
||||
import type { Response } from 'express';
|
||||
import type { VariablesRequest } from '@/requests';
|
||||
import type { PaginatedRequest } from '@/PublicApi/types';
|
||||
|
||||
type Create = VariablesRequest.Create;
|
||||
type Delete = VariablesRequest.Delete;
|
||||
type GetAll = PaginatedRequest;
|
||||
|
||||
export = {
|
||||
createVariable: [
|
||||
isLicensed('feat:variables'),
|
||||
globalScope('variable:create'),
|
||||
async (req: Create, res: Response) => {
|
||||
await Container.get(VariablesController).createVariable(req);
|
||||
|
||||
res.status(201).send();
|
||||
},
|
||||
],
|
||||
deleteVariable: [
|
||||
isLicensed('feat:variables'),
|
||||
globalScope('variable:delete'),
|
||||
async (req: Delete, res: Response) => {
|
||||
await Container.get(VariablesController).deleteVariable(req);
|
||||
|
||||
res.status(204).send();
|
||||
},
|
||||
],
|
||||
getVariables: [
|
||||
isLicensed('feat:variables'),
|
||||
globalScope('variable:list'),
|
||||
validCursor,
|
||||
async (req: GetAll, res: Response) => {
|
||||
const { offset = 0, limit = 100 } = req.query;
|
||||
|
||||
const [variables, count] = await Container.get(VariablesRepository).findAndCount({
|
||||
skip: offset,
|
||||
take: limit,
|
||||
});
|
||||
|
||||
return res.json({
|
||||
data: variables,
|
||||
nextCursor: encodeNextCursor({
|
||||
offset,
|
||||
limit,
|
||||
numberOfTotalRecords: count,
|
||||
}),
|
||||
});
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -30,6 +30,8 @@ tags:
|
||||
description: Operations about tags
|
||||
- name: SourceControl
|
||||
description: Operations about source control
|
||||
- name: Variables
|
||||
description: Operations about variables
|
||||
|
||||
paths:
|
||||
/audit:
|
||||
@@ -64,6 +66,10 @@ paths:
|
||||
$ref: './handlers/users/spec/paths/users.id.yml'
|
||||
/source-control/pull:
|
||||
$ref: './handlers/sourceControl/spec/paths/sourceControl.yml'
|
||||
/variables:
|
||||
$ref: './handlers/variables/spec/paths/variables.yml'
|
||||
/variables/{id}:
|
||||
$ref: './handlers/variables/spec/paths/variables.id.yml'
|
||||
components:
|
||||
schemas:
|
||||
$ref: './shared/spec/schemas/_index.yml'
|
||||
|
||||
@@ -9,6 +9,8 @@ import type { PaginatedRequest } from '../../../types';
|
||||
import { decodeCursor } from '../services/pagination.service';
|
||||
import type { Scope } from '@n8n/permissions';
|
||||
import { userHasScope } from '@/permissions/checkAccess';
|
||||
import type { BooleanLicenseFeature } from '@/Interfaces';
|
||||
import { FeatureNotLicensedError } from '@/errors/feature-not-licensed.error';
|
||||
|
||||
const UNLIMITED_USERS_QUOTA = -1;
|
||||
|
||||
@@ -86,3 +88,11 @@ export const validLicenseWithUserQuota = (
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
export const isLicensed = (feature: BooleanLicenseFeature) => {
|
||||
return async (_: AuthenticatedRequest, res: express.Response, next: express.NextFunction) => {
|
||||
if (Container.get(License).isFeatureEnabled(feature)) return next();
|
||||
|
||||
return res.status(403).json({ message: new FeatureNotLicensedError(feature).message });
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user