From 966f21570f359a4bc2e3bf6aaff499a70d8785e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondr=CC=8Cej=20Ny=CC=81vlt?= <7478957+vanniewelt@users.noreply.github.com> Date: Fri, 23 Dec 2022 00:59:47 +0100 Subject: [PATCH] Optimize data fetching --- src/auth/auth.handlers.ts | 16 +--------------- src/errors/errors.handlers.ts | 7 +++++++ src/members/members.handlers.ts | 14 +++++++++----- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/auth/auth.handlers.ts b/src/auth/auth.handlers.ts index 0e88238..e24cd5a 100644 --- a/src/auth/auth.handlers.ts +++ b/src/auth/auth.handlers.ts @@ -1,7 +1,5 @@ import path from "path" import { AUTH_URL, fetchToken } from "./auth.api" -import { HttpError } from "../api/helpers" -import { fetchMe } from "../user/user.api" import type { RequestHandler } from "express" // ----------------------------------------------------------------------------- @@ -16,19 +14,7 @@ export const requireAuth: RequestHandler = async (req, res, next) => { return } - try { - const { user } = await fetchMe(token) - req.user = user - next() - } catch (error) { - if (error instanceof HttpError && error.status === 401) { - res.clearCookie("token") - res.redirect("/login") - return - } - - throw error - } + next() } // ----------------------------------------------------------------------------- diff --git a/src/errors/errors.handlers.ts b/src/errors/errors.handlers.ts index c379902..c4e7d08 100644 --- a/src/errors/errors.handlers.ts +++ b/src/errors/errors.handlers.ts @@ -1,5 +1,6 @@ import type { RequestHandler, ErrorRequestHandler } from "express" import path from "path" +import { HttpError } from "../api/helpers" export const handleNotFound: RequestHandler = (req, res) => { res.status(404) @@ -25,6 +26,12 @@ export const handleError: ErrorRequestHandler = ( res, next ) => { + if (error instanceof HttpError && error.status === 401) { + res.redirect("/login") + res.clearCookie("token") + return + } + if (error instanceof Error) { console.error(error.stack) } diff --git a/src/members/members.handlers.ts b/src/members/members.handlers.ts index 54400d0..eed958c 100644 --- a/src/members/members.handlers.ts +++ b/src/members/members.handlers.ts @@ -2,10 +2,11 @@ import { BOT_TOKEN, GUILD_ID } from "../env" import path from "path" import { Client, Events, GatewayIntentBits } from "discord.js" +import { excludedRoleNames, groups } from "./roles-groups" +import { fetchMe } from "../user/user.api" import type { Guild, Collection, GuildMember, Role } from "discord.js" import type { RequestHandler } from "express" -import { excludedRoleNames, groups } from "./roles-groups" // ----------------------------------------------------------------------------- // Helpers @@ -24,7 +25,7 @@ const getGuildIcon = (guild: Guild): string | null => ? `https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.png` : null -export const getGuildData = () => +const fetchGuildData = () => new Promise<{ guild: Guild members: Collection @@ -95,7 +96,10 @@ const createGroupRoles = (roles: Collection) => { } export const getMembers: RequestHandler = async (req, res) => { - const { guild, roles } = await getGuildData() // Optimize loading, load with user at once + const [{ user }, { guild, roles }] = await Promise.all([ + fetchMe(req.cookies.token), + fetchGuildData(), + ]) res.render(path.join(__dirname, "members.view.ejs"), { layout: path.join(process.cwd(), "src/views/_layout"), @@ -104,8 +108,8 @@ export const getMembers: RequestHandler = async (req, res) => { iconUrl: getGuildIcon(guild), }, user: { - ...req.user, - avatarUrl: req.user ? getAvatarUrl(req.user) : null, + ...user, + avatarUrl: getAvatarUrl(user), }, roleGroups: createGroupRoles(roles), })