Skip to content

Commit

Permalink
fix: network context chainId header (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Sep 4, 2024
1 parent be64f9b commit 332be51
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
10 changes: 0 additions & 10 deletions apps/api/gql/resolver-context.ts
Original file line number Diff line number Diff line change
@@ -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()];
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions apps/api/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './corsMiddleware';
export * from './lowerCaseMiddleware';
export * from './sessionMiddleware';
24 changes: 24 additions & 0 deletions apps/api/middleware/sessionMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 2 additions & 1 deletion apps/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,6 +34,7 @@ const configureHelmet = (app: express.Express) => {

const configureMiddlewares = (app: express.Express) => {
app.use(corsMiddleware);
app.use(sessionMiddleware);
app.use(lowerCaseMiddleware);
};

Expand Down

0 comments on commit 332be51

Please sign in to comment.