Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 59x 59x 59x 59x 59x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 2x 2x 2x 8x 8x 8x 7x 1x 4x 4x 4x 4x 2x 2x 10x 10x 10x 5x 5x 8x 8x 8x 4x 4x 6x 6x 6x 2x 4x 4x 3x 1x 3x 3x 3x 3x 2x 1x 59x | import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import StrategyService from '../../services/strategy-service';
import { Logger } from '../../logger';
import Controller from '../controller';
import { extractUsername } from '../../util/extract-user';
import { handleErrors } from '../util';
import {
DELETE_STRATEGY,
CREATE_STRATEGY,
UPDATE_STRATEGY,
} from '../../types/permissions';
import { Request, Response } from 'express';
import { IAuthRequest } from '../unleash-types';
const version = 1;
class StrategyController extends Controller {
private logger: Logger;
private strategyService: StrategyService;
constructor(
config: IUnleashConfig,
{ strategyService }: Pick<IUnleashServices, 'strategyService'>,
) {
super(config);
this.logger = config.getLogger('/admin-api/strategy.js');
this.strategyService = strategyService;
this.get('/', this.getAllStrategies);
this.get('/:name', this.getStrategy);
this.delete('/:name', this.removeStrategy, DELETE_STRATEGY);
this.post('/', this.createStrategy, CREATE_STRATEGY);
this.put('/:strategyName', this.updateStrategy, UPDATE_STRATEGY);
this.post(
'/:strategyName/deprecate',
this.deprecateStrategy,
UPDATE_STRATEGY,
);
this.post(
'/:strategyName/reactivate',
this.reactivateStrategy,
UPDATE_STRATEGY,
);
}
async getAllStrategies(req: Request, res: Response): Promise<void> {
try {
const strategies = await this.strategyService.getStrategies();
res.json({ version, strategies });
} catch (err) {
handleErrors(res, this.logger, err);
}
}
async getStrategy(req: Request, res: Response): Promise<void> {
try {
const { name } = req.params;
const strategy = await this.strategyService.getStrategy(name);
res.json(strategy).end();
} catch (err) {
res.status(404).json({ error: 'Could not find strategy' });
}
}
async removeStrategy(req: IAuthRequest, res: Response): Promise<void> {
const strategyName = req.params.name;
const userName = extractUsername(req);
try {
await this.strategyService.removeStrategy(strategyName, userName);
res.status(200).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async createStrategy(req: IAuthRequest, res: Response): Promise<void> {
const userName = extractUsername(req);
try {
await this.strategyService.createStrategy(req.body, userName);
res.status(201).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async updateStrategy(req: IAuthRequest, res: Response): Promise<void> {
const userName = extractUsername(req);
try {
await this.strategyService.updateStrategy(req.body, userName);
res.status(200).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async deprecateStrategy(req: IAuthRequest, res: Response): Promise<void> {
const userName = extractUsername(req);
const { strategyName } = req.params;
if (strategyName === 'default') {
res.status(403).end();
} else {
try {
await this.strategyService.deprecateStrategy(
strategyName,
userName,
);
res.status(200).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
}
async reactivateStrategy(req: IAuthRequest, res: Response): Promise<void> {
const userName = extractUsername(req);
const { strategyName } = req.params;
try {
await this.strategyService.reactivateStrategy(
strategyName,
userName,
);
res.status(200).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
}
export default StrategyController;
|