Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix the build

Kiran Kb87fd0608a89
diff --git a/apps/web/app/api/tokens/scopes/route.ts b/apps/web/app/api/tokens/scopes/route.ts
index 98f3ff08b8..ad8c06a99c 100644
--- a/apps/web/app/api/tokens/scopes/route.ts
+++ b/apps/web/app/api/tokens/scopes/route.ts
@@ -1,8 +1,8 @@
-import { scopeDescriptions } from "@/lib/api/tokens/scopes";
+import { resourcePermissions } from "@/lib/api/tokens/scopes";
import { withWorkspace } from "@/lib/auth";
import { NextResponse } from "next/server";
// GET /api/tokens/scopes – returns a list of all resource scopes
export const GET = withWorkspace(async () => {
- return NextResponse.json(scopeDescriptions);
+ return NextResponse.json(resourcePermissions);
});
diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.ts
index 79ad2e6511..a1f2d0acb9 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -1,49 +1,26 @@
import { Role } from "@prisma/client";
-export const scopes = [
- // Workspaces
+export const availableScopes = [
"workspaces.read",
"workspaces.write",
-
- // Links
"links.read",
"links.write",
-
- // Tags
"tags.read",
"tags.write",
-
- // Analytics
"analytics.read",
-
- // Domains
"domains.read",
"domains.write",
-
- // API tokens
"tokens.read",
"tokens.write",
] as const;
-export type Scope = (typeof scopes)[number];
-
-interface ScopeDescription {
- name: string;
- description: string;
- endpoints: string[];
- permissions: {
- scope: Scope;
- description: string;
- roles: Role[];
- }[];
-}
+export type Scope = (typeof availableScopes)[number];
-export const scopeDescriptions = [
+export const resourcePermissions = [
{
- resource: "Workspaces",
+ name: "Workspaces",
key: "workspaces",
description: "Read, update, and delete workspaces",
- endpoints: ["/workspaces"],
permissions: [
{
permission: "Read",
@@ -60,10 +37,9 @@ export const scopeDescriptions = [
],
},
{
- resource: "Links",
+ name: "Links",
key: "links",
description: "Create, read, update, and delete links",
- endpoints: ["/links"],
permissions: [
{
permission: "Read",
@@ -80,10 +56,9 @@ export const scopeDescriptions = [
],
},
{
- resource: "Tags",
+ name: "Tags",
key: "tags",
description: "Create, read, update, and delete tags",
- endpoints: ["/tags"],
permissions: [
{
permission: "Read",
@@ -100,10 +75,9 @@ export const scopeDescriptions = [
],
},
{
- resource: "Domains",
+ name: "Domains",
key: "domains",
description: "Create, read, update, and delete domains",
- endpoints: ["/domains"],
permissions: [
{
permission: "Read",
@@ -120,10 +94,9 @@ export const scopeDescriptions = [
],
},
{
- resource: "API Keys",
+ name: "API Keys",
key: "tokens",
description: "Create, read, update, and delete API keys",
- endpoints: ["/tokens"],
permissions: [
{
permission: "Read",
@@ -140,10 +113,9 @@ export const scopeDescriptions = [
],
},
{
- resource: "Analytics",
+ name: "Analytics",
key: "analytics",
description: "Read analytics",
- endpoints: ["/analytics"],
permissions: [
{
permission: "Read",
@@ -157,7 +129,9 @@ export const scopeDescriptions = [
// Map roles to scopes
// { owner: ["workspace.write"], member: ["workspace.read"] }
-export const roleToScopes = scopeDescriptions.reduce<Record<Role, Scope[]>>(
+export const roleScopeMapping = resourcePermissions.reduce<
+ Record<Role, Scope[]>
+>(
(acc, { permissions }) => {
permissions.forEach(({ scope, roles }) => {
roles.forEach((role) => {
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index 12b3e5f883..abd0a712e5 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -20,7 +20,7 @@ import {
} from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { throwIfNoAccess } from "../api/tokens/permissions";
-import { Scope, scopes as allScopes, roleToScopes } from "../api/tokens/scopes";
+import { Scope, roleScopeMapping } from "../api/tokens/scopes";
import { isBetaTester } from "../edge-config";
import { hashToken } from "./hash-token";
import { Session, getSession } from "./utils";
@@ -63,7 +63,6 @@ export const withWorkspace = (
"business extra",
"enterprise",
], // if the action needs a specific plan
- requiredRole = ["owner", "member"],
needNotExceededClicks, // if the action needs the user to not have exceeded their clicks usage
needNotExceededLinks, // if the action needs the user to not have exceeded their links usage
allowAnonymous, // special case for /api/links (POST /api/links) – allow no session
@@ -295,7 +294,7 @@ export const withWorkspace = (
// For session requests, find the scopes based on the user's role
if (!apiKey) {
- scopes = roleToScopes[workspace.users[0].role];
+ scopes = roleScopeMapping[workspace.users[0].role];
}
// Check user has permission to make the action
diff --git a/apps/web/lib/zod/schemas/token.ts b/apps/web/lib/zod/schemas/token.ts
index 4593c9fd3c..592e98de10 100644
--- a/apps/web/lib/zod/schemas/token.ts
+++ b/apps/web/lib/zod/schemas/token.ts
@@ -1,4 +1,4 @@
-import { scopes } from "@/lib/api/tokens/scopes";
+import { availableScopes } from "@/lib/api/tokens/scopes";
import z from "@/lib/zod";
// Schema to validate the request body when creating a new token
@@ -10,7 +10,7 @@ export const createTokenSchema = z.object({
.min(1)
.max(50),
isMachine: z.boolean().optional().default(false),
- scopes: z.array(z.enum(scopes)).optional(),
+ scopes: z.array(z.enum(availableScopes)).optional(),
});
// Schema to validate the request body when updating a token
diff --git a/apps/web/ui/modals/add-edit-token-modal/index.tsx b/apps/web/ui/modals/add-edit-token-modal/index.tsx
index d1007ef6f1..66b32bb98a 100644
--- a/apps/web/ui/modals/add-edit-token-modal/index.tsx
+++ b/apps/web/ui/modals/add-edit-token-modal/index.tsx
@@ -1,4 +1,4 @@
-import { scopeDescriptions as allScopes } from "@/lib/api/tokens/scopes";
+import { resourcePermissions } from "@/lib/api/tokens/scopes";
import useWorkspace from "@/lib/swr/use-workspace";
import {
BlurImage,
@@ -187,25 +187,25 @@ function AddEditTokenModal({
</span>
<div className="flex flex-col divide-y text-sm">
- {allScopes.map((scope) => (
+ {resourcePermissions.map((resource) => (
<div
className="flex items-center justify-between py-4"
- key={scope.key}
+ key={resource.key}
>
<div className="flex gap-1 text-gray-500">
- <p>{scope.resource}</p>
- <InfoTooltip content={scope.description} />
+ <p>{resource.name}</p>
+ <InfoTooltip content={resource.description} />
</div>
<div>
<RadioGroup
- defaultValue={scopes[scope.key] || ""}
+ defaultValue={scopes[resource.key] || ""}
className="flex gap-4"
onValueChange={(v) => {
setData({
...data,
scopes: {
...scopes,
- [scope.key]: v,
+ [resource.key]: v,
},
});
}}
@@ -214,7 +214,7 @@ function AddEditTokenModal({
<RadioGroupItem value="" />
<div>None</div>
</div>
- {scope.permissions.map((permission) => (
+ {resource.permissions.map((permission) => (
<div
className="flex items-center space-x-2"
key={permission.scope}