Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { NextRequest, NextResponse } from "next/server";
import { JWTService } from "@/server/services/jwt.service";

export async function middleware(req: NextRequest) {
// Generate a standard UUID (v4)
const responseId = crypto.randomUUID();

// Clone headers to inject it into the request for downstream use
const requestHeaders = new Headers(req.headers);
requestHeaders.set("x-response-id", responseId);

const allowedOrigins = process.env.CORS_ALLOWED_ORIGINS?.split(",") ?? [];
const origin = req.headers.get("origin");

Expand All @@ -28,6 +35,7 @@ export async function middleware(req: NextRequest) {
"Content-Type, Authorization, X-Requested-With",
);
response.headers.set("Access-Control-Max-Age", "86400");
response.headers.set("x-response-id", responseId);
return response;
}
}
Expand All @@ -45,14 +53,23 @@ export async function middleware(req: NextRequest) {
}
}

const response = NextResponse.next();
// Pass along the modified request headers
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
});

response.headers.set("Vary", "Accept-Encoding");

// For non-OPTIONS requests, add the Access-Control-Allow-Origin header if the origin is allowed.
if (origin && allowedOrigins.includes(origin)) {
response.headers.set("Access-Control-Allow-Origin", origin);
}

// Attach the same UUID to the response headers
response.headers.set("x-response-id", responseId);

return response;
}

Expand Down
11 changes: 10 additions & 1 deletion src/server/utils/api-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ export class ApiResponse {
data: T,
message: string = "Success",
status: number = 200,
headers?: HeadersInit
): NextResponse<SuccessResponse<T>> {
return NextResponse.json(
{
success: true,
message,
data,
},
{ status },
{
status,
headers
}
);
}

Expand All @@ -46,12 +50,14 @@ export class ApiResponse {
* @param errors Optional field-level error map (e.g. from Zod).
* @param req The originating request – used to populate `instance`.
* Falls back to `"unknown"` when omitted.
* @param headers Optional additional headers.
*/
static error(
detail: string = "Internal Server Error",
status: number = 500,
errors: Record<string, unknown> | null = null,
req?: NextRequest,
headers?: HeadersInit
): NextResponse<ProblemDetails> {
const instance = req?.nextUrl?.pathname ?? "unknown";

Expand All @@ -61,6 +67,7 @@ export class ApiResponse {
status,
headers: {
"Content-Type": "application/problem+json",
...Object.fromEntries(new Headers(headers).entries()),
},
});
}
Expand All @@ -72,11 +79,13 @@ export class ApiResponse {
*/
static problemDetails(
problem: ProblemDetails,
headers?: HeadersInit
): NextResponse<ProblemDetails> {
return NextResponse.json(problem, {
status: problem.status,
headers: {
"Content-Type": "application/problem+json",
...Object.fromEntries(new Headers(headers).entries()),
},
});
}
Expand Down
Loading