Web: Login: deleted devMiddleware, fix types

This commit is contained in:
Artem Tarasov 2022-08-04 13:53:02 +03:00
parent 276bfff287
commit c6d00f1a76
5 changed files with 18 additions and 33 deletions

View File

@ -13,9 +13,11 @@ declare global {
error?: string | object;
}
interface DevRequest extends Request {
assets?: object;
interface DevRequest {
assets: assetsType;
}
var IS_DEVELOPMENT: boolean;
var PORT: number;
type assetsType = { [key: string]: string } | undefined;
}

View File

@ -1,6 +1,5 @@
import express from "express";
import express, { Request, Response } from "express";
import template from "./lib/template";
import devMiddleware from "./lib/middleware/devMiddleware";
import path from "path";
import compression from "compression";
import ws from "./lib/websocket";
@ -29,10 +28,18 @@ app.use("/login", express.static(path.resolve(path.join(__dirname, "client"))));
//app.use(logger("dev", { stream: winston.stream }));
if (IS_DEVELOPMENT) {
app.use(devMiddleware);
app.get("/login", async (req: Request, res: Response) => {
let assets: assetsType;
app.get("/login", async (req: DevRequest, res) => {
const { assets } = req;
try {
assets = await getAssets();
} catch (e) {
let message: string | unknown = e;
if (e instanceof Error) {
message = e.message;
}
console.log(message);
}
const appComponent = renderApp();
const htmlString = template(

View File

@ -1,7 +1,7 @@
import path from "path";
import fs from "fs";
export const getAssets = (): object => {
export const getAssets = (): assetsType => {
const manifest = fs.readFileSync(
path.join(__dirname, "client/manifest.json"),
"utf-8"
@ -12,7 +12,7 @@ export const getAssets = (): object => {
return assets;
};
export const getScripts = (assets: object | undefined): string[] | void => {
export const getScripts = (assets: assetsType): string[] | void => {
if (!assets || typeof assets !== "object") return;
const regTest = /static\/js\/.*/;
const keys = [];

View File

@ -1,22 +0,0 @@
// import winston from "../logger";
import { Response, NextFunction } from "express";
import { getAssets } from "../helpers";
// winston.stream = {
// write: (message) => winston.info(message), //ts check
// };
export default async (req: DevRequest, res: Response, next: NextFunction) => {
try {
const assets = await getAssets();
req.assets = assets;
} catch (e) {
let message: string | unknown = e;
if (e instanceof Error) {
message = e.message;
}
console.log(message);
}
next();
};

View File

@ -1,7 +1,5 @@
import { getScripts } from "./helpers";
type assetsType = { [key: string]: string } | undefined;
type Template = (
initLoginState: IInitLoginState,
appComponent: string,