diff --git a/apps/web/src/middlewares/withApiLog.ts b/apps/web/src/middlewares/withApiLog.ts index 3babf27..21e351e 100644 --- a/apps/web/src/middlewares/withApiLog.ts +++ b/apps/web/src/middlewares/withApiLog.ts @@ -1,6 +1,8 @@ -import { NextFetchEvent, NextRequest, NextResponse } from "next/server"; -import { pathToRegexp } from "path-to-regexp"; +import type { NextFetchEvent, NextRequest } from "next/server"; +import { NextResponse } from "next/server"; +import { match } from "path-to-regexp"; import pino from "pino"; + import type { MiddlewareFactory } from "./stackMiddlewares"; const logger = pino({ @@ -8,7 +10,7 @@ const logger = pino({ target: "pino-pretty", options: { colorize: true }, }, - level: process.env.LOG_LEVEL || "info", + level: process.env.LOG_LEVEL ?? "info", }); export const withApiLog: MiddlewareFactory = (next) => { @@ -16,11 +18,11 @@ export const withApiLog: MiddlewareFactory = (next) => { const excludeLoggingPaths = ["/api/trpc"]; const loggingPathPatterns = loggingPaths.map((pattern) => - pathToRegexp(pattern, undefined, { end: false }), + match(pattern, { end: false }), ); const excludeLoggingPathPatterns = excludeLoggingPaths.map((pattern) => - pathToRegexp(pattern, undefined, { end: false }), + match(pattern, { end: false }), ); return async (req: NextRequest, ev: NextFetchEvent) => { @@ -29,8 +31,8 @@ export const withApiLog: MiddlewareFactory = (next) => { } = req; const shouldLog = - loggingPathPatterns.some((pattern) => pattern.test(pathname)) && - !excludeLoggingPathPatterns.some((pattern) => pattern.test(pathname)) && + loggingPathPatterns.some((matcher) => matcher(pathname)) && + !excludeLoggingPathPatterns.some((matcher) => matcher(pathname)) && logger.isLevelEnabled("info"); if (shouldLog) { diff --git a/apps/web/src/middlewares/withAuthorization.ts b/apps/web/src/middlewares/withAuthorization.ts index 9eb4c21..f022815 100644 --- a/apps/web/src/middlewares/withAuthorization.ts +++ b/apps/web/src/middlewares/withAuthorization.ts @@ -1,7 +1,7 @@ import { getToken } from "next-auth/jwt"; import type { NextFetchEvent, NextRequest } from "next/server"; import { NextResponse } from "next/server"; -import { pathToRegexp } from "path-to-regexp"; +import { match } from "path-to-regexp"; import { PATH_SIGNIN } from "~/constants"; import type { MiddlewareFactory } from "./stackMiddlewares"; @@ -18,12 +18,10 @@ export const withAuthorization: MiddlewareFactory = (next) => { "/stock", ]; - const exactPathPatterns = exactMatchPaths.map((pattern) => - pathToRegexp(pattern), - ); + const exactPathPatterns = exactMatchPaths.map((pattern) => match(pattern)); const prefixMatchPatterns = prefixMatchPaths.map((pattern) => - pathToRegexp(pattern, undefined, { end: false }), + match(pattern, { end: false }), ); const allPathPatterns = [...exactPathPatterns, ...prefixMatchPatterns]; @@ -33,7 +31,7 @@ export const withAuthorization: MiddlewareFactory = (next) => { nextUrl: { pathname }, } = req; - if (allPathPatterns.some((pattern) => pattern.test(pathname))) { + if (allPathPatterns.some((matcher) => matcher(pathname))) { const token = await getToken({ req }); if (!token) { const nextUrl = new URL(PATH_SIGNIN, req.url);