Skip to content

chore: write defaultResponder for App Router APIs#19727

Merged
sean-brydon merged 5 commits intomainfrom
chore/defaultResponder-app-router
Mar 5, 2025
Merged

chore: write defaultResponder for App Router APIs#19727
sean-brydon merged 5 commits intomainfrom
chore/defaultResponder-app-router

Conversation

@hbjORbj
Copy link
Copy Markdown
Contributor

@hbjORbj hbjORbj commented Mar 4, 2025

What does this PR do?

  • We wrap Pages Router APIs with defaultResponder (a wrapper for measuring performance and sending reports to Sentry)
  • This is a clone of the util but for App Router APIs

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • N/A - I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Please use the latest Vercel preview and test please 🙏.

@graphite-app graphite-app bot requested a review from a team March 4, 2025 21:08
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 4, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Visit Preview Mar 4, 2025 9:31pm
calcom-web-canary ⬜️ Ignored (Inspect) Visit Preview Mar 4, 2025 9:31pm

@keithwillcode keithwillcode added consumer core area: core, team members only labels Mar 4, 2025
@hbjORbj hbjORbj changed the title chore: defaultresponder app router chore: write defaultResponder for App Router APIs Mar 4, 2025
@graphite-app
Copy link
Copy Markdown

graphite-app bot commented Mar 4, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (03/04/25)

1 reviewer was added to this PR based on Keith Williams's automation.

@dosubot dosubot bot added the api area: API, enterprise API, access token, OAuth label Mar 4, 2025
Comment on lines -8 to -36
export const apiRouteMiddleware =
(handler: (req: NextRequest, { params }: { params: Params }) => Promise<NextResponse | Response>) =>
async (req: NextRequest, { params }: { params: Params }) => {
try {
return await handler(req, { params });
} catch (error) {
if (error instanceof ApiError) {
return NextResponse.json({ message: error.message }, { status: error.statusCode });
} else {
const serverError = getServerErrorFromUnknown(error);
// we don't want to report Bad Request errors to Sentry / console
if (!(serverError.statusCode >= 400 && serverError.statusCode < 500)) {
console.error(error);
const captureException = (await import("@sentry/nextjs")).captureException;
captureException(error);
}
return NextResponse.json(
{
message: serverError.message,
url: serverError.url,
method: serverError.method,
},
{
status: serverError.statusCode,
}
);
}
}
};
Copy link
Copy Markdown
Contributor Author

@hbjORbj hbjORbj Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This util had some parts of defaultResponder code but it's still missing a lot of it. I removed this entirely and rewrote a new wrapper called defaultResponderForAppDir

Comment on lines -118 to -120
const getHandler = apiRouteMiddleware(handler);

export { getHandler as GET };
Copy link
Copy Markdown
Contributor Author

@hbjORbj hbjORbj Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • We run this API (/social/og/image) on Edge, and import { performance } from "@calcom/lib/server/perfObserver"; in defaultResponderForAppDir can only run in Node.js.
  • Ideally, we should modify this performance function, so that we can use the wrapper in APIs running on Edge too
  • But it is not high priority, because in Pages Router, we weren't using defaultResponder for this API anyways

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 4, 2025

E2E results are ready!

Copy link
Copy Markdown
Member

@sean-brydon sean-brydon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - will merge and implement in our other api PRs

@sean-brydon sean-brydon merged commit 251121a into main Mar 5, 2025
@sean-brydon sean-brydon deleted the chore/defaultResponder-app-router branch March 5, 2025 09:12
itsalam pushed a commit to itsalam/cal.com that referenced this pull request Mar 6, 2025
* create defaultResponderForAppDir

* rename all usages

* finish

* fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api area: API, enterprise API, access token, OAuth consumer core area: core, team members only ready-for-e2e

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants