Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Deeplinks #2044

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions apps/web/app/api/links/resolve/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { DubApiError } from "@/lib/api/errors";
import { basicLink, transformLink } from "@/lib/api/links";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { withWorkspace } from "@/lib/auth";
import { getLinkInfoQuerySchema } from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";

// GET /api/links/route - SDK link resolution
export const GET = withWorkspace(
async ({ headers, searchParams, workspace }) => {
const { domain, key, linkId, externalId } =
getLinkInfoQuerySchema.parse(searchParams);

if (!domain && !key && !linkId && !externalId) {
throw new DubApiError({
code: "bad_request",
message:
"You must provide a domain and a key or a linkId or an externalId to retrieve a link.",
docUrl: "https://dub.co/docs/api-reference/endpoint/retrieve-a-link",
});
}

const link = await getLinkOrThrow({
workspaceId: workspace.id,
linkId,
externalId,
domain,
key,
});

const tags = await prisma.tag.findMany({
where: {
links: {
some: {
linkId: link.id,
},
},
},
select: {
id: true,
name: true,
color: true,
},
});

const response = basicLink(link);

return NextResponse.json(response, {
headers,
});
},
{
requiredPermissions: ["links.read"],
},
);
30 changes: 30 additions & 0 deletions apps/web/app/exit/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client';

export const runtime = "edge";

// Configurable landing page to break out of WebViews on iOS
// It would be better if the universal link page, attempted to click the equivalent scheme. No interstitial page necessary.
export default function ExitPage({ params }: { params: { placeholder: string } }) {

// TODO: map a universal link with an equivalent scheme, this requires knowledge about how the target app works
// for now, lets hard code the amazon app since it's well documented
let scheme = "com.amazon.mobile.shopping.web://www.amazon.com/gp/product/B0DF49NM7Q";
let universalLink = "https://a.co/d/cfDKBco";

// TODO: filter by apps we want to try to break out of.
// Running this outside a webview results in a poor user experience.
// Also dependent on the host apps webview config, by default universal links work and schemes do not!
// Many apps disable universal links and enable schemes.

return (
<div>
<a id="myscheme" href={ `${scheme}` }>scheme</a>
<br/>
<a id="mylink" href={ `${universalLink}` }>link</a>
<script type="text/javascript">
document.getElementById(`myscheme`)?.click();
document.getElementById(`mylink`)?.click();
</script>
</div>
);
}
53 changes: 53 additions & 0 deletions apps/web/lib/api/links/utils/transform-link.ts
Original file line number Diff line number Diff line change
@@ -35,3 +35,56 @@ export const transformLink = (
...(dashboard && { dashboardId: dashboard.id || null }),
};
};

export const basicLink = (link: Link) => {
// remove everything but the required data for link resolution
// TODO: see about a more restricted Link query
const {
id,
domain,
key,
//url,
//shortLink,
archived,
expiresAt,
expiredUrl,
password,
trackConversion,
proxy,
title,
description,
image,
video,
utm_source,
utm_medium,
utm_campaign,
utm_term,
utm_content,
rewrite,
doIndex,
ios,
android,
geo,
userId,
projectId,
programId,
externalId,
tenantId,
publicStats,
clicks,
lastClicked,
leads,
sales,
saleAmount,
createdAt,
updatedAt,
comments,
partnerId,
//qrCode,
...rest
} = link;

return {
...rest
};
}
2 changes: 1 addition & 1 deletion apps/web/middleware.ts
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ export const config = {
* 3. /_proxy/ (proxies for third-party services)
* 4. Metadata files: favicon.ico, sitemap.xml, robots.txt, manifest.webmanifest
*/
"/((?!api/|_next/|_proxy/|favicon.ico|sitemap.xml|robots.txt|manifest.webmanifest).*)",
"/((?!api/|_next/|_proxy/|favicon.ico|sitemap.xml|robots.txt|manifest.webmanifest|exit).*)",
],
};