Skip to content

Commit

Permalink
fix: 🐛 path-to-regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
gracefullight committed Sep 8, 2024
1 parent cb1f1fc commit 7489768
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
16 changes: 9 additions & 7 deletions apps/web/src/middlewares/withApiLog.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
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({
transport: {
target: "pino-pretty",
options: { colorize: true },
},
level: process.env.LOG_LEVEL || "info",
level: process.env.LOG_LEVEL ?? "info",
});

export const withApiLog: MiddlewareFactory = (next) => {
const loggingPaths = ["/api"];
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) => {
Expand All @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions apps/web/src/middlewares/withAuthorization.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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];
Expand All @@ -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);
Expand Down

0 comments on commit 7489768

Please sign in to comment.