diff --git a/adminSiteServer/FunctionalRouter.ts b/adminSiteServer/FunctionalRouter.ts deleted file mode 100644 index 28275eb8d0..0000000000 --- a/adminSiteServer/FunctionalRouter.ts +++ /dev/null @@ -1,60 +0,0 @@ -import express, { NextFunction, Router } from "express" -import { Request, Response } from "./authentication.js" - -// Little wrapper to automatically send returned objects as JSON, makes -// the API code a bit cleaner -export class FunctionalRouter { - router: Router - constructor() { - this.router = Router() - this.router.use(express.urlencoded({ extended: true })) - // Parse incoming requests with JSON payloads http://expressjs.com/en/api.html - this.router.use(express.json({ limit: "50mb" })) - } - - wrap(callback: (req: Request, res: Response) => Promise) { - return async (req: Request, res: Response, next: NextFunction) => { - try { - res.send(await callback(req, res)) - } catch (e) { - console.error(e) - next(e) - } - } - } - - get( - targetPath: string, - callback: (req: Request, res: Response) => Promise - ) { - this.router.get(targetPath, this.wrap(callback)) - } - - post( - targetPath: string, - callback: (req: Request, res: Response) => Promise - ) { - this.router.post(targetPath, this.wrap(callback)) - } - - patch( - targetPath: string, - callback: (req: Request, res: Response) => Promise - ) { - this.router.patch(targetPath, this.wrap(callback)) - } - - put( - targetPath: string, - callback: (req: Request, res: Response) => Promise - ) { - this.router.put(targetPath, this.wrap(callback)) - } - - delete( - targetPath: string, - callback: (req: Request, res: Response) => Promise - ) { - this.router.delete(targetPath, this.wrap(callback)) - } -} diff --git a/adminSiteServer/apiRouter.ts b/adminSiteServer/apiRouter.ts index 90afc08798..5d97a52322 100644 --- a/adminSiteServer/apiRouter.ts +++ b/adminSiteServer/apiRouter.ts @@ -99,7 +99,6 @@ import { deleteVariablesVariableIdGrapherConfigAdmin, getVariablesVariableIdChartsJson, } from "./apiRoutes/variables.js" -import { FunctionalRouter } from "./FunctionalRouter.js" import { patchRouteWithRWTransaction, getRouteWithROTransaction, @@ -123,8 +122,9 @@ import { updateChart, deleteChart, } from "./apiRoutes/charts.js" +import e, { Router } from "express" -const apiRouter = new FunctionalRouter() +const apiRouter = Router() // Bulk chart update routes patchRouteWithRWTransaction( @@ -428,8 +428,11 @@ apiRouter.get("/deploys.json", async () => ({ deploys: await new DeployQueueServer().getDeploys(), })) -apiRouter.put("/deploy", async (req, res) => { - return triggerStaticBuild(res.locals.user, "Manually triggered deploy") -}) +apiRouter.put( + "/deploy", + async (_req: e.Request, res: e.Response>) => { + return triggerStaticBuild(res.locals.user, "Manually triggered deploy") + } +) export { apiRouter } diff --git a/adminSiteServer/apiRoutes/bulkUpdates.ts b/adminSiteServer/apiRoutes/bulkUpdates.ts index 82aa5b598c..eb97dba8ab 100644 --- a/adminSiteServer/apiRoutes/bulkUpdates.ts +++ b/adminSiteServer/apiRoutes/bulkUpdates.ts @@ -23,10 +23,9 @@ import { updateGrapherConfigAdminOfVariable, } from "../../db/model/Variable.js" import { saveGrapher } from "./charts.js" +import e, { Request } from "express" import * as db from "../../db/db.js" import * as lodash from "lodash" -import { Request } from "../authentication.js" -import e from "express" export async function getChartBulkUpdate( req: Request, diff --git a/adminSiteServer/apiRoutes/chartViews.ts b/adminSiteServer/apiRoutes/chartViews.ts index 1bb86557bd..2fbe15dd96 100644 --- a/adminSiteServer/apiRoutes/chartViews.ts +++ b/adminSiteServer/apiRoutes/chartViews.ts @@ -27,8 +27,7 @@ import { deleteGrapherConfigFromR2ByUUID } from "../chartConfigR2Helpers.js" import * as db from "../../db/db.js" import { expectChartById } from "./charts.js" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" const createPatchConfigAndQueryParamsForChartView = async ( knex: db.KnexReadonlyTransaction, parentChartId: number, diff --git a/adminSiteServer/apiRoutes/charts.ts b/adminSiteServer/apiRoutes/charts.ts index 2b19a204a8..366efba23c 100644 --- a/adminSiteServer/apiRoutes/charts.ts +++ b/adminSiteServer/apiRoutes/charts.ts @@ -59,8 +59,7 @@ import * as db from "../../db/db.js" import { getLogsByChartId } from "../getLogsByChartId.js" import { getPublishedLinksTo } from "../../db/model/Link.js" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" export const getReferencesByChartId = async ( chartId: number, knex: db.KnexReadonlyTransaction diff --git a/adminSiteServer/apiRoutes/datasets.ts b/adminSiteServer/apiRoutes/datasets.ts index fb490bc42e..f14d721683 100644 --- a/adminSiteServer/apiRoutes/datasets.ts +++ b/adminSiteServer/apiRoutes/datasets.ts @@ -21,8 +21,7 @@ import { import { triggerStaticBuild } from "./routeUtils.js" import * as db from "../../db/db.js" import * as lodash from "lodash" -import { Request } from "express" -import * as e from "express" +import e, { Request } from "express" export async function getDatasets( req: Request, diff --git a/adminSiteServer/apiRoutes/explorer.ts b/adminSiteServer/apiRoutes/explorer.ts index 44b9caf630..ecdb46f54c 100644 --- a/adminSiteServer/apiRoutes/explorer.ts +++ b/adminSiteServer/apiRoutes/explorer.ts @@ -1,6 +1,5 @@ import { JsonError } from "@ourworldindata/types" -import { Request } from "express" -import * as e from "express" +import e, { Request } from "express" import * as db from "../../db/db.js" export async function addExplorerTags( diff --git a/adminSiteServer/apiRoutes/gdocs.ts b/adminSiteServer/apiRoutes/gdocs.ts index fbeb412e0d..3d5537b615 100644 --- a/adminSiteServer/apiRoutes/gdocs.ts +++ b/adminSiteServer/apiRoutes/gdocs.ts @@ -45,8 +45,7 @@ import { GdocPost } from "../../db/model/Gdoc/GdocPost.js" import { triggerStaticBuild, enqueueLightningChange } from "./routeUtils.js" import * as db from "../../db/db.js" import * as lodash from "lodash" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" export async function getAllGdocIndexItems( req: Request, diff --git a/adminSiteServer/apiRoutes/images.ts b/adminSiteServer/apiRoutes/images.ts index 7fc71c08e7..e99bf86d88 100644 --- a/adminSiteServer/apiRoutes/images.ts +++ b/adminSiteServer/apiRoutes/images.ts @@ -10,8 +10,7 @@ import { triggerStaticBuild } from "./routeUtils.js" import * as db from "../../db/db.js" import * as lodash from "lodash" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" export async function getImagesHandler( _: Request, res: e.Response>, diff --git a/adminSiteServer/apiRoutes/mdims.ts b/adminSiteServer/apiRoutes/mdims.ts index 7880662928..9556e3d486 100644 --- a/adminSiteServer/apiRoutes/mdims.ts +++ b/adminSiteServer/apiRoutes/mdims.ts @@ -7,9 +7,8 @@ import { } from "../../settings/clientSettings.js" import { createMultiDimConfig } from "../multiDim.js" import { triggerStaticBuild } from "./routeUtils.js" -import { Request } from "../authentication.js" import * as db from "../../db/db.js" -import e from "express" +import e, { Request } from "express" export async function handleMultiDimDataPageRequest( req: Request, diff --git a/adminSiteServer/apiRoutes/misc.ts b/adminSiteServer/apiRoutes/misc.ts index f31d6c2d6f..80ec73ba29 100644 --- a/adminSiteServer/apiRoutes/misc.ts +++ b/adminSiteServer/apiRoutes/misc.ts @@ -9,8 +9,7 @@ import * as db from "../../db/db.js" import * as lodash from "lodash" import path from "path" import { expectInt } from "../../serverUtils/serverUtil.js" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" // using the alternate template, which highlights topics rather than articles. export async function fetchAllWork( req: Request, diff --git a/adminSiteServer/apiRoutes/posts.ts b/adminSiteServer/apiRoutes/posts.ts index f34714fc30..78dad0983f 100644 --- a/adminSiteServer/apiRoutes/posts.ts +++ b/adminSiteServer/apiRoutes/posts.ts @@ -14,8 +14,7 @@ import { GdocPost } from "../../db/model/Gdoc/GdocPost.js" import { setTagsForPost, getTagsByPostId } from "../../db/model/Post.js" import { expectInt } from "../../serverUtils/serverUtil.js" import * as db from "../../db/db.js" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" export async function handleGetPostsJson( req: Request, _res: e.Response>, diff --git a/adminSiteServer/apiRoutes/redirects.ts b/adminSiteServer/apiRoutes/redirects.ts index 44452fe026..342bdb8b81 100644 --- a/adminSiteServer/apiRoutes/redirects.ts +++ b/adminSiteServer/apiRoutes/redirects.ts @@ -8,8 +8,7 @@ import { import { expectInt } from "../../serverUtils/serverUtil.js" import { triggerStaticBuild } from "./routeUtils.js" import * as db from "../../db/db.js" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" export async function handleGetSiteRedirects( req: Request, res: e.Response>, diff --git a/adminSiteServer/apiRoutes/suggest.ts b/adminSiteServer/apiRoutes/suggest.ts index 4a294d4328..bc165128e2 100644 --- a/adminSiteServer/apiRoutes/suggest.ts +++ b/adminSiteServer/apiRoutes/suggest.ts @@ -8,8 +8,7 @@ import { getGptTopicSuggestions } from "../../db/model/Chart.js" import { CLOUDFLARE_IMAGES_URL } from "../../settings/clientSettings.js" import { fetchGptGeneratedAltText } from "../imagesHelpers.js" import * as db from "../../db/db.js" -import e from "express" -import { Request } from "../authentication.js" +import e, { Request } from "express" export async function suggestGptTopics( req: Request, diff --git a/adminSiteServer/apiRoutes/tagGraph.ts b/adminSiteServer/apiRoutes/tagGraph.ts index bafeec6d51..5e0df422a2 100644 --- a/adminSiteServer/apiRoutes/tagGraph.ts +++ b/adminSiteServer/apiRoutes/tagGraph.ts @@ -2,8 +2,7 @@ import { JsonError, FlatTagGraph } from "@ourworldindata/types" import { checkIsPlainObjectWithGuard } from "@ourworldindata/utils" import * as db from "../../db/db.js" import * as lodash from "lodash" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" export async function handleGetFlatTagGraph( req: Request, diff --git a/adminSiteServer/apiRoutes/tags.ts b/adminSiteServer/apiRoutes/tags.ts index 40bec68cec..d40d3bfd47 100644 --- a/adminSiteServer/apiRoutes/tags.ts +++ b/adminSiteServer/apiRoutes/tags.ts @@ -14,8 +14,7 @@ import { expectInt } from "../../serverUtils/serverUtil.js" import { UNCATEGORIZED_TAG_ID } from "../../settings/serverSettings.js" import * as db from "../../db/db.js" import * as lodash from "lodash" -import e from "express" -import { Request } from "../authentication.js" +import e, { Request } from "express" export async function getTagById( req: Request, diff --git a/adminSiteServer/apiRoutes/users.ts b/adminSiteServer/apiRoutes/users.ts index e232fd15a3..82e8da958c 100644 --- a/adminSiteServer/apiRoutes/users.ts +++ b/adminSiteServer/apiRoutes/users.ts @@ -4,8 +4,7 @@ import { pick } from "lodash" import { getUserById, updateUser, insertUser } from "../../db/model/User.js" import { expectInt } from "../../serverUtils/serverUtil.js" import * as db from "../../db/db.js" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" export async function getUsers( req: Request, _res: e.Response>, diff --git a/adminSiteServer/apiRoutes/variables.ts b/adminSiteServer/apiRoutes/variables.ts index d8f21c20ed..414c265db7 100644 --- a/adminSiteServer/apiRoutes/variables.ts +++ b/adminSiteServer/apiRoutes/variables.ts @@ -42,8 +42,7 @@ import { expectInt } from "../../serverUtils/serverUtil.js" import { triggerStaticBuild } from "./routeUtils.js" import * as lodash from "lodash" import { updateGrapherConfigsInR2 } from "./charts.js" -import { Request } from "../authentication.js" -import e from "express" +import e, { Request } from "express" export async function getEditorVariablesJson( req: Request, diff --git a/adminSiteServer/appClass.tsx b/adminSiteServer/appClass.tsx index 1ef3cc0546..247a23a17b 100644 --- a/adminSiteServer/appClass.tsx +++ b/adminSiteServer/appClass.tsx @@ -108,8 +108,8 @@ export class OwidAdminApp { app.use("/fonts", express.static("public/fonts")) app.use("/assets-admin", express.static("dist/assets-admin")) - app.use("/api", publicApiRouter.router) - app.use("/admin/api", apiRouter.router) + app.use("/api", publicApiRouter) + app.use("/admin/api", apiRouter) app.use("/admin/test", testPageRouter) app.use("/admin/storybook", express.static(".storybook/build")) app.use("/admin", adminRouter) diff --git a/adminSiteServer/authentication.ts b/adminSiteServer/authentication.ts index 605fb4c674..6bc420976e 100644 --- a/adminSiteServer/authentication.ts +++ b/adminSiteServer/authentication.ts @@ -14,12 +14,6 @@ import { Secret, verify } from "jsonwebtoken" import { DbPlainSession, DbPlainUser, JsonError } from "@ourworldindata/utils" import { exec } from "child_process" -export type Request = express.Request - -export interface Response extends express.Response { - locals: { user: DbPlainUser; session: Session } -} - interface Session { id: string expiryDate: Date diff --git a/adminSiteServer/functionalRouterHelpers.ts b/adminSiteServer/functionalRouterHelpers.ts index e9ae007d2b..d3b1d83ee3 100644 --- a/adminSiteServer/functionalRouterHelpers.ts +++ b/adminSiteServer/functionalRouterHelpers.ts @@ -1,8 +1,7 @@ -import { FunctionalRouter } from "./FunctionalRouter.js" -import { Request, Response } from "express" +import { Request, Response, Router } from "express" import * as db from "../db/db.js" export function getRouteWithROTransaction( - router: FunctionalRouter, + router: Router, targetPath: string, handler: ( req: Request, @@ -22,7 +21,7 @@ export function getRouteWithROTransaction( fetching it from the google API. */ export function getRouteNonIdempotentWithRWTransaction( - router: FunctionalRouter, + router: Router, targetPath: string, handler: ( req: Request, @@ -38,7 +37,7 @@ export function getRouteNonIdempotentWithRWTransaction( } export function postRouteWithRWTransaction( - router: FunctionalRouter, + router: Router, targetPath: string, handler: ( req: Request, @@ -54,7 +53,7 @@ export function postRouteWithRWTransaction( } export function putRouteWithRWTransaction( - router: FunctionalRouter, + router: Router, targetPath: string, handler: ( req: Request, @@ -70,7 +69,7 @@ export function putRouteWithRWTransaction( } export function patchRouteWithRWTransaction( - router: FunctionalRouter, + router: Router, targetPath: string, handler: ( req: Request, @@ -86,7 +85,7 @@ export function patchRouteWithRWTransaction( } export function deleteRouteWithRWTransaction( - router: FunctionalRouter, + router: Router, targetPath: string, handler: ( req: Request, diff --git a/adminSiteServer/publicApiRouter.ts b/adminSiteServer/publicApiRouter.ts index 946c8859ce..21078dfe9c 100644 --- a/adminSiteServer/publicApiRouter.ts +++ b/adminSiteServer/publicApiRouter.ts @@ -1,14 +1,13 @@ -import { FunctionalRouter } from "./FunctionalRouter.js" -import { Request, Response } from "./authentication.js" +import { Router, Request, Response } from "express" import * as db from "../db/db.js" -export const publicApiRouter = new FunctionalRouter() +export const publicApiRouter = Router() function rejectAfterDelay(ms: number) { return new Promise((resolve, reject) => setTimeout(reject, ms)) } -publicApiRouter.router.get("/health", async (req: Request, res: Response) => { +publicApiRouter.get("/health", async (req: Request, res: Response) => { try { const sqlPromise = db.knexRaw( db.knexInstance() as db.KnexReadonlyTransaction,