Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(dashboard-sdk): make PermissionKey augmentable by plugins (#1593)

Viktor Holik51ab648987fe
diff --git a/apps/docs/resources/customization/extending-panels.mdx b/apps/docs/resources/customization/extending-panels.mdx
index 21ab8ed94..63c03089c 100644
--- a/apps/docs/resources/customization/extending-panels.mdx
+++ b/apps/docs/resources/customization/extending-panels.mdx
@@ -270,6 +270,51 @@ File paths map to URL routes automatically.
| `src/routes/(settings)/page.tsx` | Route grouping | Groups routes without adding a URL segment |
| `src/routes/dashboard/@sidebar/page.tsx` | Parallel route | Renders alongside parent |
+### Adding permissions from a plugin
+
+A plugin that ships its own pages usually needs its own permission keys. Declare them on the server, then register the same keys with the dashboard SDK so pages can be guarded and hidden from navigation.
+
+**1. Define the keys on the server.** Import the definitions file once (for example from your module's `index.ts`) so the keys join the catalog:
+
+```typescript src/permissions.ts
+import { defineMercurPermissions } from "@mercurjs/core/permissions/registry"
+
+export const messagingPermissions = defineMercurPermissions([
+ { key: "messaging", group: "messaging", surface: ["vendor"], rights: ["view", "edit", "manage"] },
+])
+```
+
+Guard the plugin's API routes with the same key (`requirePermission("messaging", "view")`).
+
+**2. Register the keys with the dashboard SDK.** `PermissionKey` is built from the `PermissionKeys` interface, so augment it:
+
+```typescript src/permissions.d.ts
+declare module "@mercurjs/dashboard-sdk" {
+ interface PermissionKeys {
+ messaging: true
+ }
+}
+
+export {}
+```
+
+The key now type-checks everywhere the SDK accepts a permission:
+
+```tsx src/routes/messages/page.tsx
+import type { RouteConfig, RouteHandle } from "@mercurjs/dashboard-sdk"
+
+export const config: RouteConfig = {
+ label: "Messages",
+ permissions: ["messaging:view"],
+}
+
+export const handle: RouteHandle = {
+ permissions: "messaging:view",
+}
+```
+
+`can("messaging", "edit")` and `buildPermission("messaging", "manage")` accept it too. This only affects types: the dashboard reads the member's effective rights from the API at runtime.
+
## Branding
Set `name` and `logo` in the plugin options to customize the sidebar header.
diff --git a/packages/dashboard-sdk/package.json b/packages/dashboard-sdk/package.json
index 3eac85b7c..ee44b0d75 100644
--- a/packages/dashboard-sdk/package.json
+++ b/packages/dashboard-sdk/package.json
@@ -31,6 +31,7 @@
"scripts": {
"dev": "tsup --watch",
"build": "tsup",
+ "test:types": "tsc -p src/__tests__/types",
"lint": "oxlint --max-warnings 0"
},
"dependencies": {
diff --git a/packages/dashboard-sdk/src/__tests__/types/permission-keys.augment.ts b/packages/dashboard-sdk/src/__tests__/types/permission-keys.augment.ts
new file mode 100644
index 000000000..67c9293ab
--- /dev/null
+++ b/packages/dashboard-sdk/src/__tests__/types/permission-keys.augment.ts
@@ -0,0 +1,15 @@
+import { buildPermission, type Permission } from "@mercurjs/dashboard-sdk"
+
+declare module "@mercurjs/dashboard-sdk" {
+ interface PermissionKeys {
+ messaging: true
+ }
+}
+
+const plugin: Permission = buildPermission("messaging", "view")
+const core: Permission = buildPermission("orders", "edit")
+
+// @ts-expect-error unregistered keys stay rejected
+buildPermission("not_a_key", "view")
+
+export { plugin, core }
diff --git a/packages/dashboard-sdk/src/__tests__/types/tsconfig.json b/packages/dashboard-sdk/src/__tests__/types/tsconfig.json
new file mode 100644
index 000000000..f10705138
--- /dev/null
+++ b/packages/dashboard-sdk/src/__tests__/types/tsconfig.json
@@ -0,0 +1,12 @@
+{
+ "compilerOptions": {
+ "strict": true,
+ "noEmit": true,
+ "module": "esnext",
+ "moduleResolution": "bundler",
+ "target": "es2020",
+ "skipLibCheck": true,
+ "jsx": "react-jsx"
+ },
+ "files": ["permission-keys.augment.ts"]
+}
diff --git a/packages/dashboard-sdk/src/permissions.ts b/packages/dashboard-sdk/src/permissions.ts
index c07c15e65..ef68c440e 100644
--- a/packages/dashboard-sdk/src/permissions.ts
+++ b/packages/dashboard-sdk/src/permissions.ts
@@ -7,57 +7,71 @@
* `manage` implies `edit`, `edit` implies `view`.
*/
-export type PermissionKey =
- | "sellers"
- | "sellers.approval"
- | "sellers.premium"
- | "members"
- | "members.invites"
- | "roles"
- | "roles.owner"
- | "orders"
- | "orders.refunds"
- | "orders.returns"
- | "orders.edits"
- | "order_groups"
- | "payments"
- | "customers"
- | "customer_groups"
- | "products"
- | "products.review"
- | "product_changes"
- | "product_categories"
- | "product_collections"
- | "product_types"
- | "product_tags"
- | "product_attributes"
- | "offers"
- | "inventory_items"
- | "reservations"
- | "stock_locations"
- | "price_lists"
- | "price_preferences"
- | "promotions"
- | "campaigns"
- | "commission_rates"
- | "commission_lines"
- | "payouts"
- | "payout_accounts"
- | "shipping_profiles"
- | "shipping_options"
- | "fulfillment_sets"
- | "regions"
- | "tax_regions"
- | "store"
- | "sales_channels"
- | "return_reasons"
- | "refund_reasons"
- | "users"
- | "api_keys"
- | "translations"
- | "notifications"
- | "workflow_executions"
- | "reviews"
+/**
+ * Registry of permission keys. Plugins add their own keys through declaration
+ * merging:
+ *
+ * @example
+ * declare module "@mercurjs/dashboard-sdk" {
+ * interface PermissionKeys {
+ * messaging: true
+ * }
+ * }
+ */
+export interface PermissionKeys {
+ "sellers": true
+ "sellers.approval": true
+ "sellers.premium": true
+ "members": true
+ "members.invites": true
+ "roles": true
+ "roles.owner": true
+ "orders": true
+ "orders.refunds": true
+ "orders.returns": true
+ "orders.edits": true
+ "order_groups": true
+ "payments": true
+ "customers": true
+ "customer_groups": true
+ "products": true
+ "products.review": true
+ "product_changes": true
+ "product_categories": true
+ "product_collections": true
+ "product_types": true
+ "product_tags": true
+ "product_attributes": true
+ "offers": true
+ "inventory_items": true
+ "reservations": true
+ "stock_locations": true
+ "price_lists": true
+ "price_preferences": true
+ "promotions": true
+ "campaigns": true
+ "commission_rates": true
+ "commission_lines": true
+ "payouts": true
+ "payout_accounts": true
+ "shipping_profiles": true
+ "shipping_options": true
+ "fulfillment_sets": true
+ "regions": true
+ "tax_regions": true
+ "store": true
+ "sales_channels": true
+ "return_reasons": true
+ "refund_reasons": true
+ "users": true
+ "api_keys": true
+ "translations": true
+ "notifications": true
+ "workflow_executions": true
+ "reviews": true
+}
+
+export type PermissionKey = Extract<keyof PermissionKeys, string>
export type PermissionRight = "view" | "edit" | "manage"
diff --git a/packages/dashboard-sdk/tsconfig.json b/packages/dashboard-sdk/tsconfig.json
index 86437295d..3ed182ab0 100644
--- a/packages/dashboard-sdk/tsconfig.json
+++ b/packages/dashboard-sdk/tsconfig.json
@@ -25,6 +25,9 @@
"include": [
"src"
],
+ "exclude": [
+ "src/__tests__/types"
+ ],
"composite": true,
"skipLibCheck": true,
"module": "ESNext",