From 0459085de0655a3f7249d61d03a1d72b62e97e34 Mon Sep 17 00:00:00 2001 From: gmbronco <83549293+gmbronco@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:59:39 +0200 Subject: [PATCH] fix: network context chainId header --- apps/api/gql/resolver-context.ts | 10 ---------- apps/api/middleware/index.ts | 1 + apps/api/middleware/sessionMiddleware.ts | 24 ++++++++++++++++++++++++ apps/api/server.ts | 3 ++- 4 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 apps/api/middleware/sessionMiddleware.ts diff --git a/apps/api/gql/resolver-context.ts b/apps/api/gql/resolver-context.ts index 55dccf8a..eee1c52c 100644 --- a/apps/api/gql/resolver-context.ts +++ b/apps/api/gql/resolver-context.ts @@ -1,9 +1,4 @@ import { Request } from 'express'; -import { networkContext } from '../../../modules/network/network-context.service'; -import { - initRequestScopedContext, - setRequestScopedContextValue, -} from '../../../modules/context/request-scoped-context'; function getHeader(req: Request, key: string): string | undefined { const value = req.headers[key.toLowerCase()]; @@ -21,11 +16,6 @@ export async function resolverContext(req: Request) { const adminApiKey = getHeader(req, 'AdminApiKey'); const chainId = getHeader(req, 'ChainId'); - if (chainId && networkContext.isValidChainId(chainId)) { - initRequestScopedContext(); - setRequestScopedContextValue('chainId', chainId); - } - // Initialize context if it doesn't exist return { accountAddress: accountAddress ? accountAddress.toLowerCase() : null, diff --git a/apps/api/middleware/index.ts b/apps/api/middleware/index.ts index 9489a52f..b56294bf 100644 --- a/apps/api/middleware/index.ts +++ b/apps/api/middleware/index.ts @@ -1,2 +1,3 @@ export * from './corsMiddleware'; export * from './lowerCaseMiddleware'; +export * from './sessionMiddleware'; diff --git a/apps/api/middleware/sessionMiddleware.ts b/apps/api/middleware/sessionMiddleware.ts new file mode 100644 index 00000000..46ac2153 --- /dev/null +++ b/apps/api/middleware/sessionMiddleware.ts @@ -0,0 +1,24 @@ +import { NextFunction, Request, Response } from 'express'; +import { networkContext } from '../../../modules/network/network-context.service'; +import { + initRequestScopedContext, + setRequestScopedContextValue, +} from '../../../modules/context/request-scoped-context'; + +function getHeader(req: Request, key: string): string | undefined { + const value = req.headers[key.toLowerCase()]; + return Array.isArray(value) ? value[0] : value; +} + +export async function sessionMiddleware(req: Request, res: Response, next: NextFunction) { + const chainId = getHeader(req, 'ChainId'); + + if (chainId && networkContext.isValidChainId(chainId)) { + initRequestScopedContext(); + setRequestScopedContextValue('chainId', chainId); + + next(); + } else { + next(); + } +} diff --git a/apps/api/server.ts b/apps/api/server.ts index f0d3c081..5af26e24 100644 --- a/apps/api/server.ts +++ b/apps/api/server.ts @@ -6,7 +6,7 @@ import helmet from 'helmet'; import * as http from 'http'; import { env } from '../env'; import { loadRestRoutes } from './rest-routes'; -import { corsMiddleware, lowerCaseMiddleware } from './middleware'; +import { corsMiddleware, lowerCaseMiddleware, sessionMiddleware } from './middleware'; import { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageGraphQLPlayground, @@ -34,6 +34,7 @@ const configureHelmet = (app: express.Express) => { const configureMiddlewares = (app: express.Express) => { app.use(corsMiddleware); + app.use(sessionMiddleware); app.use(lowerCaseMiddleware); };