unleash-server
Version:
Unleash is an enterprise ready feature toggles service. It provides different strategies for handling feature toggles.
167 lines • 6.9 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const controller_1 = __importDefault(require("../controller"));
const extract_user_1 = require("../../util/extract-user");
const permissions_1 = require("../../types/permissions");
const standard_responses_1 = require("../../openapi/util/standard-responses");
const create_request_schema_1 = require("../../openapi/util/create-request-schema");
const create_response_schema_1 = require("../../openapi/util/create-response-schema");
const strategy_schema_1 = require("../../openapi/spec/strategy-schema");
const strategies_schema_1 = require("../../openapi/spec/strategies-schema");
const version = 1;
class StrategyController extends controller_1.default {
constructor(config, { strategyService, openApiService, }) {
super(config);
this.logger = config.getLogger('/admin-api/strategy.js');
this.strategyService = strategyService;
this.openApiService = openApiService;
this.route({
method: 'get',
path: '',
handler: this.getAllStrategies,
permission: permissions_1.NONE,
middleware: [
openApiService.validPath({
tags: ['Strategies'],
operationId: 'getAllStrategies',
responses: {
200: (0, create_response_schema_1.createResponseSchema)('strategiesSchema'),
},
}),
],
});
this.route({
method: 'get',
path: '/:name',
handler: this.getStrategy,
permission: permissions_1.NONE,
middleware: [
openApiService.validPath({
tags: ['Strategies'],
operationId: 'getStrategy',
responses: { 200: (0, create_response_schema_1.createResponseSchema)('strategySchema') },
}),
],
});
this.route({
method: 'delete',
path: '/:name',
handler: this.removeStrategy,
permission: permissions_1.DELETE_STRATEGY,
acceptAnyContentType: true,
middleware: [
openApiService.validPath({
tags: ['Strategies'],
operationId: 'removeStrategy',
responses: { 200: standard_responses_1.emptyResponse },
}),
],
});
this.route({
method: 'post',
path: '',
handler: this.createStrategy,
permission: permissions_1.CREATE_STRATEGY,
middleware: [
openApiService.validPath({
tags: ['Strategies'],
operationId: 'createStrategy',
requestBody: (0, create_request_schema_1.createRequestSchema)('upsertStrategySchema'),
responses: {
201: (0, create_response_schema_1.resourceCreatedResponseSchema)('strategySchema'),
},
}),
],
});
this.route({
method: 'put',
path: '/:strategyName',
handler: this.updateStrategy,
permission: permissions_1.UPDATE_STRATEGY,
middleware: [
openApiService.validPath({
tags: ['Strategies'],
operationId: 'updateStrategy',
requestBody: (0, create_request_schema_1.createRequestSchema)('upsertStrategySchema'),
responses: { 200: standard_responses_1.emptyResponse },
}),
],
});
this.route({
method: 'post',
path: '/:strategyName/deprecate',
handler: this.deprecateStrategy,
permission: permissions_1.UPDATE_STRATEGY,
acceptAnyContentType: true,
middleware: [
openApiService.validPath({
tags: ['Strategies'],
operationId: 'deprecateStrategy',
responses: { 200: standard_responses_1.emptyResponse },
}),
],
});
this.route({
method: 'post',
path: '/:strategyName/reactivate',
handler: this.reactivateStrategy,
permission: permissions_1.UPDATE_STRATEGY,
acceptAnyContentType: true,
middleware: [
openApiService.validPath({
tags: ['Strategies'],
operationId: 'reactivateStrategy',
responses: { 200: standard_responses_1.emptyResponse },
}),
],
});
}
async getAllStrategies(req, res) {
const strategies = await this.strategyService.getStrategies();
this.openApiService.respondWithValidation(200, res, strategies_schema_1.strategiesSchema.$id, { version, strategies });
}
async getStrategy(req, res) {
const strategy = await this.strategyService.getStrategy(req.params.name);
this.openApiService.respondWithValidation(200, res, strategy_schema_1.strategySchema.$id, strategy);
}
async removeStrategy(req, res) {
const strategyName = req.params.name;
const userName = (0, extract_user_1.extractUsername)(req);
await this.strategyService.removeStrategy(strategyName, userName);
res.status(200).end();
}
async createStrategy(req, res) {
const userName = (0, extract_user_1.extractUsername)(req);
const strategy = await this.strategyService.createStrategy(req.body, userName);
res.header('location', `strategies/${strategy.name}`)
.status(201)
.json(strategy)
.end();
}
async updateStrategy(req, res) {
const userName = (0, extract_user_1.extractUsername)(req);
await this.strategyService.updateStrategy(req.body, userName);
res.status(200).end();
}
async deprecateStrategy(req, res) {
const userName = (0, extract_user_1.extractUsername)(req);
const { strategyName } = req.params;
if (strategyName === 'default') {
res.status(403).end();
return;
}
await this.strategyService.deprecateStrategy(strategyName, userName);
res.status(200).end();
}
async reactivateStrategy(req, res) {
const userName = (0, extract_user_1.extractUsername)(req);
const { strategyName } = req.params;
await this.strategyService.reactivateStrategy(strategyName, userName);
res.status(200).end();
}
}
exports.default = StrategyController;
//# sourceMappingURL=strategy.js.map