All files / src/lib/routes/admin-api tag.ts

100% Statements 27/27
100% Branches 0/0
100% Functions 6/6
100% Lines 27/27

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            59x   59x 59x     59x                     141x 141x 141x   141x 141x 141x 141x 141x       4x 4x       2x 2x       4x 4x 2x       4x 4x 2x       2x 2x 2x 2x     59x  
import { Request, Response } from 'express';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import TagService from '../../services/tag-service';
import { Logger } from '../../logger';
 
import Controller from '../controller';
 
import { UPDATE_FEATURE } from '../../types/permissions';
import { extractUsername } from '../../util/extract-user';
import { IAuthRequest } from '../unleash-types';
 
const version = 1;
 
class TagController extends Controller {
    private logger: Logger;
 
    private tagService: TagService;
 
    constructor(
        config: IUnleashConfig,
        { tagService }: Pick<IUnleashServices, 'tagService'>,
    ) {
        super(config);
        this.tagService = tagService;
        this.logger = config.getLogger('/admin-api/tag.js');
 
        this.get('/', this.getTags);
        this.post('/', this.createTag, UPDATE_FEATURE);
        this.get('/:type', this.getTagsByType);
        this.get('/:type/:value', this.getTag);
        this.delete('/:type/:value', this.deleteTag, UPDATE_FEATURE);
    }
 
    async getTags(req: Request, res: Response): Promise<void> {
        const tags = await this.tagService.getTags();
        res.json({ version, tags });
    }
 
    async getTagsByType(req: Request, res: Response): Promise<void> {
        const tags = await this.tagService.getTagsByType(req.params.type);
        res.json({ version, tags });
    }
 
    async getTag(req: Request, res: Response): Promise<void> {
        const { type, value } = req.params;
        const tag = await this.tagService.getTag({ type, value });
        res.json({ version, tag });
    }
 
    async createTag(req: IAuthRequest, res: Response): Promise<void> {
        const userName = extractUsername(req);
        await this.tagService.createTag(req.body, userName);
        res.status(201).end();
    }
 
    async deleteTag(req: IAuthRequest, res: Response): Promise<void> {
        const { type, value } = req.params;
        const userName = extractUsername(req);
        await this.tagService.deleteTag({ type, value }, userName);
        res.status(200).end();
    }
}
export default TagController;