1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00
unleash.unleash/src/lib/routes/admin-api/environments.ts

170 lines
5.0 KiB
TypeScript
Raw Normal View History

import { Request, Response } from 'express';
import Controller from '../controller';
import { IUnleashServices } from '../../types/services';
import { IUnleashConfig } from '../../types/option';
import EnvironmentService from '../../services/environment-service';
import { Logger } from '../../logger';
import { ADMIN, NONE } from '../../types/permissions';
import { OpenApiService } from '../../services/openapi-service';
import { createRequestSchema, createResponseSchema } from '../../openapi';
import {
environmentsSchema,
EnvironmentsSchema,
} from '../../openapi/spec/environments-schema';
import {
environmentSchema,
EnvironmentSchema,
} from '../../openapi/spec/environment-schema';
import { SortOrderSchema } from '../../openapi/spec/sort-order-schema';
import { emptyResponse } from '../../openapi/spec/empty-response';
interface EnvironmentParam {
name: string;
}
export class EnvironmentsController extends Controller {
private logger: Logger;
private openApiService: OpenApiService;
private service: EnvironmentService;
constructor(
config: IUnleashConfig,
{
environmentService,
openApiService,
}: Pick<IUnleashServices, 'environmentService' | 'openApiService'>,
) {
super(config);
this.logger = config.getLogger('admin-api/environments-controller.ts');
this.openApiService = openApiService;
this.service = environmentService;
this.route({
method: 'get',
path: '',
handler: this.getAllEnvironments,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['admin'],
operationId: 'getAllEnvironments',
responses: { 200: emptyResponse },
}),
],
});
this.route({
method: 'get',
path: '/:name',
handler: this.getEnvironment,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['admin'],
operationId: 'getEnvironment',
responses: {
200: createResponseSchema('environmentSchema'),
},
}),
],
});
this.route({
method: 'put',
path: '/sort-order',
handler: this.updateSortOrder,
permission: ADMIN,
middleware: [
openApiService.validPath({
tags: ['admin'],
operationId: 'updateSortOrder',
requestBody: createRequestSchema('sortOrderSchema'),
responses: { 200: emptyResponse },
}),
],
});
this.route({
method: 'post',
path: '/:name/on',
acceptAnyContentType: true,
handler: this.toggleEnvironmentOn,
permission: ADMIN,
middleware: [
openApiService.validPath({
tags: ['admin'],
operationId: 'toggleEnvironmentOn',
responses: { 200: emptyResponse },
}),
],
});
this.route({
method: 'post',
path: '/:name/off',
acceptAnyContentType: true,
handler: this.toggleEnvironmentOff,
permission: ADMIN,
middleware: [
openApiService.validPath({
tags: ['admin'],
operationId: 'toggleEnvironmentOff',
responses: { 200: emptyResponse },
}),
],
});
}
async getAllEnvironments(
req: Request,
res: Response<EnvironmentsSchema>,
): Promise<void> {
this.openApiService.respondWithValidation(
200,
res,
environmentsSchema.$id,
{ version: 1, environments: await this.service.getAll() },
);
}
async updateSortOrder(
req: Request<unknown, unknown, SortOrderSchema>,
res: Response,
): Promise<void> {
await this.service.updateSortOrder(req.body);
res.status(200).end();
}
async toggleEnvironmentOn(
req: Request<EnvironmentParam>,
res: Response,
): Promise<void> {
const { name } = req.params;
await this.service.toggleEnvironment(name, true);
res.status(204).end();
}
async toggleEnvironmentOff(
req: Request<EnvironmentParam>,
res: Response,
): Promise<void> {
const { name } = req.params;
await this.service.toggleEnvironment(name, false);
res.status(204).end();
}
async getEnvironment(
req: Request<EnvironmentParam>,
res: Response<EnvironmentSchema>,
): Promise<void> {
this.openApiService.respondWithValidation(
200,
res,
environmentSchema.$id,
await this.service.get(req.params.name),
);
}
}