unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
155 lines • 6.91 kB
JavaScript
import Controller from '../../routes/controller.js';
import { ADMIN, NONE } from '../../types/permissions.js';
import { createRequestSchema } from '../../openapi/util/create-request-schema.js';
import { createResponseSchema } from '../../openapi/util/create-response-schema.js';
import { environmentsSchema, } from '../../openapi/spec/environments-schema.js';
import { environmentSchema, } from '../../openapi/spec/environment-schema.js';
import { emptyResponse, getStandardResponses, } from '../../openapi/util/standard-responses.js';
import { environmentsProjectSchema, } from '../../openapi/spec/environments-project-schema.js';
export class EnvironmentsController extends Controller {
constructor(config, { 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: ['Environments'],
summary: 'Get all environments',
description: 'Retrieves all environments that exist in this Unleash instance.',
operationId: 'getAllEnvironments',
responses: {
200: createResponseSchema('environmentsSchema'),
...getStandardResponses(401, 403),
},
}),
],
});
this.route({
method: 'get',
path: '/:name',
handler: this.getEnvironment,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Environments'],
operationId: 'getEnvironment',
summary: 'Get the environment with `name`',
description: 'Retrieves the environment with `name` if it exists in this Unleash instance',
responses: {
200: createResponseSchema('environmentSchema'),
...getStandardResponses(401, 403, 404),
},
}),
],
});
this.route({
method: 'get',
path: '/project/:projectId',
handler: this.getProjectEnvironments,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Environments'],
operationId: 'getProjectEnvironments',
summary: 'Get the environments available to a project',
description: 'Gets the environments that are available for this project. An environment is available for a project if enabled in the [project configuration](https://docs.getunleash.io/reference/environments#enable-an-environment)',
responses: {
200: createResponseSchema('environmentsProjectSchema'),
...getStandardResponses(401, 403, 404),
},
}),
],
});
this.route({
method: 'put',
path: '/sort-order',
handler: this.updateSortOrder,
permission: ADMIN,
middleware: [
openApiService.validPath({
tags: ['Environments'],
summary: 'Update environment sort orders',
description: 'Updates sort orders for the named environments. Environments not specified are unaffected.',
operationId: 'updateSortOrder',
requestBody: createRequestSchema('sortOrderSchema'),
responses: {
200: emptyResponse,
...getStandardResponses(401, 403, 404),
},
}),
],
});
this.route({
method: 'post',
path: '/:name/on',
acceptAnyContentType: true,
handler: this.toggleEnvironmentOn,
permission: ADMIN,
middleware: [
openApiService.validPath({
tags: ['Environments'],
summary: 'Toggle the environment with `name` on',
description: 'Makes it possible to enable this environment for a project. An environment must first be globally enabled using this endpoint before it can be enabled for a project',
operationId: 'toggleEnvironmentOn',
responses: {
204: emptyResponse,
...getStandardResponses(401, 403, 404),
},
}),
],
});
this.route({
method: 'post',
path: '/:name/off',
acceptAnyContentType: true,
handler: this.toggleEnvironmentOff,
permission: ADMIN,
middleware: [
openApiService.validPath({
tags: ['Environments'],
summary: 'Toggle the environment with `name` off',
description: 'Removes this environment from the list of available environments for projects to use',
operationId: 'toggleEnvironmentOff',
responses: {
204: emptyResponse,
...getStandardResponses(401, 403, 404),
},
}),
],
});
}
async getAllEnvironments(req, res) {
this.openApiService.respondWithValidation(200, res, environmentsSchema.$id, { version: 1, environments: await this.service.getAll() });
}
async updateSortOrder(req, res) {
await this.service.updateSortOrder(req.body);
res.status(200).end();
}
async toggleEnvironmentOn(req, res) {
const { name } = req.params;
await this.service.toggleEnvironment(name, true);
res.status(204).end();
}
async toggleEnvironmentOff(req, res) {
const { name } = req.params;
await this.service.toggleEnvironment(name, false);
res.status(204).end();
}
async getEnvironment(req, res) {
this.openApiService.respondWithValidation(200, res, environmentSchema.$id, await this.service.get(req.params.name));
}
async getProjectEnvironments(req, res) {
const environments = await this.service.getProjectEnvironments(req.params.projectId);
this.openApiService.respondWithValidation(200, res, environmentsProjectSchema.$id, {
version: 1,
environments,
});
}
}
//# sourceMappingURL=environments-controller.js.map