Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: re-write GUI to typescript part 3 #521

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions lib/gui/routes/plugins.js → lib/gui/routes/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
'use strict';
import {Router} from 'express';
import {INTERNAL_SERVER_ERROR, BAD_REQUEST} from 'http-codes';

const {Router} = require('express');
const {INTERNAL_SERVER_ERROR, BAD_REQUEST} = require('http-codes');

const {
import {
isUnexpectedPlugin,
getPluginClientScriptPath,
getPluginMiddleware,
forEachPlugin,
logError
} = require('../../server-utils');
} from '../../server-utils';
import {ReporterConfig} from '../../types';

module.exports = function(router, pluginConfig) {
export const initPluginsRoutes = (router: Router, pluginConfig: ReporterConfig): Router =>{
shadowusr marked this conversation as resolved.
Show resolved Hide resolved
if (!pluginConfig.pluginsEnabled) {
return router;
}

router.get(`/plugins/:pluginName/plugin.js`, (req, res) => {
router.get<{pluginName: string}>(`/plugins/:pluginName/plugin.js`, (req, res) => {
if (isUnexpectedPlugin(pluginConfig.plugins, req.params.pluginName)) {
res.status(BAD_REQUEST).send({error: `Unexpected plugin "${req.params.pluginName}" requested.`});
return;
Expand All @@ -42,12 +41,12 @@ module.exports = function(router, pluginConfig) {
initPluginMiddleware(pluginRouter);

router.use(`/plugin-routes/${pluginName}`, pluginRouter);
} catch (err) {
logError(err);
} catch (err: unknown) {
logError(err as Error);
}
});

router.use('/plugin-routes/:pluginName', (req, res) => {
router.use<{pluginName: string}>('/plugin-routes/:pluginName', (req, res) => {
res.status(BAD_REQUEST).send({error: `No middleware is registered for "${req.params.pluginName}".`});
});

Expand Down
2 changes: 1 addition & 1 deletion lib/gui/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {App} = require('./app');
const {MAX_REQUEST_SIZE, KEEP_ALIVE_TIMEOUT, HEADERS_TIMEOUT} = require('./constants');
const {initializeCustomGui, runCustomGuiAction} = require('../server-utils');
const {logger} = require('../common-utils');
const initPluginsRoutes = require('./routes/plugins');
const {initPluginsRoutes} = require('./routes/plugins');

exports.start = async ({paths, hermione, guiApi, configs}) => {
const {options, pluginConfig} = configs;
Expand Down
7 changes: 6 additions & 1 deletion lib/server-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type Hermione from 'hermione';
import crypto from 'crypto';
import {ImageHandler, ImagesInfoFormatter} from './image-handler';
import {HermioneTestAdapter} from './test-adapter';
import {Router} from 'express';

const DATA_FILE_NAME = 'data.js';

Expand Down Expand Up @@ -270,7 +271,11 @@ export function getPluginClientScriptPath(pluginName: string): string | null {
}
}

export function getPluginMiddleware(pluginName: string): unknown | null {
interface PluginMiddleware {
(pluginRouter: Router): unknown;
}

export function getPluginMiddleware(pluginName: string): PluginMiddleware | null {
try {
return require(`${pluginName}/middleware.js`);
} catch (e: any) { // eslint-disable-line @typescript-eslint/no-explicit-any
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"@types/enzyme": "^3.10.13",
"@types/express": "4.16",
"@types/fs-extra": "^7.0.0",
"@types/http-codes": "^1.0.4",
"@types/json-stringify-safe": "^5.0.2",
"@types/lodash": "^4.14.195",
"@types/nested-error-stacks": "^2.1.0",
Expand Down