{"size": 6070, "ext": "js", "max_stars_repo_path": "src/dashboard.js", "max_stars_repo_name": "seancheung/mock", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/dashboard.js", "max_issues_repo_name": "seancheung/mock", "max_issues_repo_issues_event_min_datetime": "2020-11-07T17:00:28.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T10:41:24.000Z", "max_forks_repo_path": "src/dashboard.js", "max_forks_repo_name": "seancheung/mockit", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 9, "max_forks_count": null, "avg_line_length": 28.9047619048, "max_line_length": 77, "alphanum_fraction": 0.4039538715}
const express = require('express');

module.exports = (app, config, db, ...plugins) => {
    const dashboard = express.Router({ mergeParams: true });

    if (plugins) {
        plugins.forEach(plugin => plugin(dashboard));
    }

    dashboard.head('/routes', (req, res, next) => {
        try {
            const success = db.exists(req.query.method, req.query.path);
            res.status(success ? 409 : 200).end();
        } catch (error) {
            next(error);
        }
    });

    dashboard.get('/routes', (req, res, next) => {
        try {
            const offset = req.query.offset ? parseInt(req.query.offset) : 0;
            const limit = req.query.limit && parseInt(req.query.limit);
            const count = db.count();
            const records = Array.from(db.select(offset, limit));
            res.json({ count, offset, limit, records });
        } catch (error) {
            next(error);
        }
    });

    dashboard.post('/routes', (req, res, next) => {
        try {
            const {
                method,
                path,
                delay,
                code,
                headers,
                body,
                bypass,
                proxy
            } = req.body;
            if (!method || !path) {
                const error = new Error('missing required arguments');
                error.status = 400;
                throw error;
            }
            const data = {
                path,
                method: method.toLowerCase()
            };
            if (proxy == null) {
                if (delay != undefined) {
                    data.delay = delay;
                }
                if (code != undefined) {
                    data.code = code;
                }
                if (headers != undefined) {
                    data.headers = headers;
                }
                if (body != undefined) {
                    data.body = body;
                }
            } else {
                data.proxy = {};
                if (!proxy.remote) {
                    const error = new Error('missing required arguments');
                    error.status = 400;
                    throw error;
                }
                data.proxy.remote = proxy.remote;
                if (proxy.rewrite) {
                    data.proxy.rewrite = proxy.rewrite;
                }
                if (proxy.headers != undefined) {
                    data.proxy.headers = proxy.headers;
                }
            }
            if (bypass != undefined) {
                data.bypass = bypass;
            }
            const doc = db.insert(data);
            if (!doc) {
                const error = new Error('failed to insert data');
                error.status = 500;
                throw error;
            }
            res.status(201).end();
        } catch (error) {
            next(error);
        }
    });

    dashboard.head('/routes/:id', (req, res, next) => {
        try {
            const success = db.has(req.params.id);
            res.status(success ? 409 : 200).end();
        } catch (error) {
            next(error);
        }
    });

    dashboard.get('/routes/:id', (req, res, next) => {
        try {
            const doc = db.find(req.params.id);
            if (!doc) {
                const error = new Error('not found');
                error.status = 404;
                throw error;
            }
            res.json(doc);
        } catch (error) {
            next(error);
        }
    });

    dashboard.put('/routes/:id', (req, res, next) => {
        try {
            if (!db.has(req.params.id)) {
                const error = new Error('not found');
                error.status = 404;
                throw error;
            }
            const { delay, code, headers, body, bypass } = req.body;
            const data = {};
            if (delay != undefined) {
                data.delay = delay;
            }
            if (code != undefined) {
                data.code = code;
            }
            if (headers != undefined) {
                data.headers = headers;
            }
            if (body != undefined) {
                data.body = body;
            }
            if (bypass != undefined) {
                data.bypass = bypass;
            }
            const doc = db.update(req.params.id, data);
            if (!doc) {
                const error = new Error('failed to update data');
                error.status = 500;
                throw error;
            }
            res.json(doc);
        } catch (error) {
            next(error);
        }
    });

    dashboard.delete('/routes/:id', (req, res, next) => {
        try {
            if (!db.remove(req.params.id)) {
                const error = new Error('not found');
                error.status = 404;
                throw error;
            }
            res.status(204).end();
        } catch (error) {
            next(error);
        }
    });

    dashboard.get('/template', (req, res, next) => {
        try {
            res.json(config.template);
        } catch (error) {
            next(error);
        }
    });

    dashboard.get('/export', async (req, res, next) => {
        try {
            if (/ya?ml$/i.test(req.headers['accept'])) {
                await db.ydump(res.type('yaml'));
            } else {
                await db.dump(res.type('json'));
            }
        } catch (error) {
            next(error);
        }
    });

    dashboard.post('/import', (req, res, next) => {
        try {
            let data;
            if (/ya?ml$/i.test(req.body.type)) {
                data = require('js-yaml').safeLoad(req.body.data);
            } else {
                data = JSON.parse(req.body.data);
            }
            db.load(data);
            res.status(201).end();
        } catch (error) {
            next(error);
        }
    });

    if (config.baseUrl) {
        app.use(config.baseUrl, dashboard);
    } else {
        app.use(dashboard);
    }

    return dashboard;
};
