Skip to content

WIP: Deeplinks #2044

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

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

// POST /api/links/route - SDK link resolution
export const POST = withWorkspace(
async ({ req, headers, workspace }) => {

const schema = resolveLinkSchema.parse(await parseRequestBody(req))

let url = URL.parse(schema.link)
let domain = url?.host
let key = url?.pathname.split('/')[1]

// Apps never use linkId or externalId
let linkId = undefined
let externalId = undefined

if (!domain && !key) {
throw new DubApiError({
code: "bad_request",
message:
"You must provide a domain and a key 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>
);
}
54 changes: 54 additions & 0 deletions apps/web/lib/api/links/utils/transform-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,57 @@ 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,
folderId,
...rest
} = link;

return {
...rest
};
}
9 changes: 9 additions & 0 deletions apps/web/lib/zod/schemas/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export const getUrlQuerySchema = z.object({
url: parseUrlSchema,
});

export const resolveLinkSchema = z.object({
version: z
.string()
.describe("The link resolution schema version."),
link: z
.string()
.describe("The link the app wants to resolve.")
})

export const getDomainQuerySchema = z.object({
domain: z
.string()
Expand Down
2 changes: 1 addition & 1 deletion apps/web/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).*)",
],
};

Expand Down