Optimize data fetching
This commit is contained in:
parent
c982c9a2ab
commit
966f21570f
3 changed files with 17 additions and 20 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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<string, GuildMember>
|
||||
|
@ -95,7 +96,10 @@ const createGroupRoles = (roles: Collection<string, Role>) => {
|
|||
}
|
||||
|
||||
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),
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue