1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00
unleash.unleash/src/lib/routes/admin-api/constraints.ts
Fredrik Strand Oseberg bc96216daa
Refactor/move features to feature oriented architecture (#4994)
This PR gathers feature related files in the same folder.
2023-10-11 09:38:57 +02:00

61 lines
2.3 KiB
TypeScript

import { Request, Response } from 'express';
import FeatureToggleService from '../../features/feature-toggle/feature-toggle-service';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types';
import { NONE } from '../../types/permissions';
import Controller from '../controller';
import { Logger } from '../../logger';
import { OpenApiService } from '../../services/openapi-service';
import { createRequestSchema } from '../../openapi/util/create-request-schema';
import { ConstraintSchema, getStandardResponses } from '../../openapi';
export default class ConstraintController extends Controller {
private featureService: FeatureToggleService;
private openApiService: OpenApiService;
private readonly logger: Logger;
constructor(
config: IUnleashConfig,
{
featureToggleServiceV2,
openApiService,
}: Pick<IUnleashServices, 'featureToggleServiceV2' | 'openApiService'>,
) {
super(config);
this.featureService = featureToggleServiceV2;
this.openApiService = openApiService;
this.logger = config.getLogger('/admin-api/validation.ts');
this.route({
method: 'post',
path: '/validate',
handler: this.validateConstraint,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Features'],
operationId: 'validateConstraint',
requestBody: createRequestSchema('constraintSchema'),
summary: 'Validate constraint',
description:
'Validates a constraint definition. Checks whether the context field exists and whether the applied configuration is valid. Additional properties are not allowed on data objects that you send to this endpoint.',
responses: {
204: { description: 'The constraint is valid' },
...getStandardResponses(400, 401, 403, 415),
},
}),
],
});
}
async validateConstraint(
req: Request<void, void, ConstraintSchema>,
res: Response,
): Promise<void> {
await this.featureService.validateConstraint(req.body);
res.status(204).send();
}
}