Skip to content

Commit

Permalink
chore: Introduce a new class WebAppURL that extends URL (calcom#14982)
Browse files Browse the repository at this point in the history
* chore: Introduce a new class WebAppURL that extends URL

* update

---------

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
  • Loading branch information
anikdhabal and PeerRich committed Jul 18, 2024
1 parent 22fc47f commit 2ad3592
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions apps/web/app/WithEmbedSSR.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isNotFoundError } from "next/dist/client/components/not-found";
import { getURLFromRedirectError, isRedirectError } from "next/dist/client/components/redirect";
import { notFound, redirect } from "next/navigation";

import { WEBAPP_URL } from "@calcom/lib/constants";
import { WebAppURL } from "@calcom/lib/WebAppURL";

export type EmbedProps = {
isEmbed?: boolean;
Expand All @@ -28,7 +28,7 @@ export default function withEmbedSsrAppDir<T extends Record<string, any>>(
let urlPrefix = "";

// Get the URL parsed from URL so that we can reliably read pathname and searchParams from it.
const destinationUrlObj = new URL(destinationUrl, WEBAPP_URL);
const destinationUrlObj = new WebAppURL(destinationUrl);

// If it's a complete URL, use the origin as the prefix to ensure we redirect to the same domain.
if (destinationUrl.search(/^(http:|https:).*/) !== -1) {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/lib/withEmbedSsr.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsResult } from "next";

import { WEBAPP_URL } from "@calcom/lib/constants";
import { WebAppURL } from "@calcom/lib/WebAppURL";

export type EmbedProps = {
isEmbed?: boolean;
Expand All @@ -17,7 +17,7 @@ export default function withEmbedSsr(getServerSideProps: GetServerSideProps) {
let urlPrefix = "";

// Get the URL parsed from URL so that we can reliably read pathname and searchParams from it.
const destinationUrlObj = new URL(ssrResponse.redirect.destination, WEBAPP_URL);
const destinationUrlObj = new WebAppURL(ssrResponse.redirect.destination);

// If it's a complete URL, use the origin as the prefix to ensure we redirect to the same domain.
if (destinationUrl.search(/^(http:|https:).*/) !== -1) {
Expand Down
8 changes: 8 additions & 0 deletions packages/lib/WebAppURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { WEBAPP_URL } from "@calcom/lib/constants";

/** This class extends the native URL and uses WEBAPP_URL as the base URL for creating object URLs */
export class WebAppURL extends URL {
constructor(path: string | URL) {
super(path, WEBAPP_URL);
}
}

0 comments on commit 2ad3592

Please sign in to comment.