Files
calendar/apps/web/app/api/parseRequestData.ts
Benny JooandGitHub e3e624464d fix: update wrong oauth tests and parseRequestData util (#19958)
* update oauth tests

* fix parseRequestData

* add logging

* add fallback

* use ??

* updates

* use parseUrlFormData in saml/callback

* update tests

* fix

* remove log

* update

* any -> unknown

* use HttpErrors

* addressed

* unknown -> any
2025-03-11 14:01:48 +00:00

52 lines
1.9 KiB
TypeScript

import type { NextRequest } from "next/server";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
const log = logger.getSubLogger({ prefix: ["[parseRequestData]"] });
export async function parseUrlFormData(req: NextRequest): Promise<Record<string, any>> {
try {
// Read raw text body (because Next.js does NOT parse x-www-form-urlencoded automatically)
const rawBody = await req.text();
const params = new URLSearchParams(rawBody);
return Object.fromEntries(params);
} catch (e) {
log.error(`Invalid Url Form Data: ${e} from path ${req.nextUrl}`);
throw new HttpError({ statusCode: 400, message: "Bad Request (Invalid Url Form Data)" });
}
}
export async function parseMultiFormData(req: NextRequest): Promise<Record<string, any>> {
try {
const formData = await req.formData();
return Object.fromEntries(formData.entries());
} catch (e) {
log.error(`Invalid Multi Form Data: ${e} from path ${req.nextUrl}`);
throw new HttpError({ statusCode: 400, message: "Bad Request (Invalid Multi Form Data)" });
}
}
export async function parseRequestData(req: NextRequest): Promise<Record<string, any>> {
const contentType = req.headers.get("content-type") ?? "application/json";
if (contentType.includes("application/json")) {
try {
return await req.json();
} catch (e) {
log.error(`Invalid JSON: ${e} from path ${req.nextUrl}`);
throw new HttpError({ statusCode: 400, message: "Bad Request (Invalid JSON)" });
}
}
if (contentType.includes("application/x-www-form-urlencoded")) {
return await parseUrlFormData(req);
}
if (contentType.includes("multipart/form-data")) {
return await parseMultiFormData(req);
}
log.error(`Unsupported content type: ${contentType} from path ${req.nextUrl}`);
throw new HttpError({ statusCode: 415, message: `Unsupported Content-Type. Expected ${contentType}` });
}