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.

refactor: move SDK client out of dashboard-shared and use config.backendUrl directly

vholik7b5070044c8a
diff --git a/packages/dashboard-sdk/src/plugin.ts b/packages/dashboard-sdk/src/plugin.ts
index baa818e6a..7aba4b72a 100644
--- a/packages/dashboard-sdk/src/plugin.ts
+++ b/packages/dashboard-sdk/src/plugin.ts
@@ -10,6 +10,7 @@ function buildConfig(config: MercurConfig, root: string): BuiltMercurConfig {
return {
...config,
+ backendUrl: config.backendUrl ?? "http://localhost:9000",
root,
srcDir,
configPath: path.resolve(root, CONFIG_NAME),
diff --git a/packages/dashboard-sdk/src/types.ts b/packages/dashboard-sdk/src/types.ts
index 56b96fad6..b60106256 100644
--- a/packages/dashboard-sdk/src/types.ts
+++ b/packages/dashboard-sdk/src/types.ts
@@ -14,6 +14,7 @@ export interface MercurConfig {
}
export interface BuiltMercurConfig extends MercurConfig {
+ backendUrl: string;
root: string
srcDir: string
configPath: string
diff --git a/packages/dashboard-shared/src/components/common/index.ts b/packages/dashboard-shared/src/components/common/index.ts
index ba4522e9e..a1d4d409e 100644
--- a/packages/dashboard-shared/src/components/common/index.ts
+++ b/packages/dashboard-shared/src/components/common/index.ts
@@ -28,4 +28,3 @@ export * from "./sortable-tree"
export * from "./switch-box"
export * from "./tax-badge"
export * from "./thumbnail"
-export * from "./user-link"
diff --git a/packages/dashboard-shared/src/components/common/user-link/index.ts b/packages/dashboard-shared/src/components/common/user-link/index.ts
deleted file mode 100644
index 951235f82..000000000
--- a/packages/dashboard-shared/src/components/common/user-link/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./user-link"
diff --git a/packages/dashboard-shared/src/components/common/user-link/user-link.tsx b/packages/dashboard-shared/src/components/common/user-link/user-link.tsx
deleted file mode 100644
index 239abb8b0..000000000
--- a/packages/dashboard-shared/src/components/common/user-link/user-link.tsx
+++ /dev/null
@@ -1,45 +0,0 @@
-import { Avatar, Text } from "@medusajs/ui"
-import { Link } from "react-router-dom"
-import { useUser } from "../../../hooks/api/users"
-
-type UserLinkProps = {
- id: string
- first_name?: string | null
- last_name?: string | null
- email: string
- type?: "customer" | "user"
-}
-
-export const UserLink = ({
- id,
- first_name,
- last_name,
- email,
- type = "user",
-}: UserLinkProps) => {
- const name = [first_name, last_name].filter(Boolean).join(" ")
- const fallback = name ? name.slice(0, 1) : email.slice(0, 1)
- const link = type === "user" ? `/settings/users/${id}` : `/customers/${id}`
-
- return (
- <Link
- to={link}
- className="flex items-center gap-x-2 w-fit transition-fg hover:text-ui-fg-subtle outline-none focus-visible:shadow-borders-focus rounded-md"
- >
- <Avatar size="2xsmall" fallback={fallback.toUpperCase()} />
- <Text size="small" leading="compact" weight="regular">
- {name || email}
- </Text>
- </Link>
- )
-}
-
-export const By = ({ id }: { id: string }) => {
- const { user } = useUser(id) // todo: extend to support customers
-
- if (!user) {
- return null
- }
-
- return <UserLink {...user} />
-}
diff --git a/packages/dashboard-shared/src/hooks/api/users.tsx b/packages/dashboard-shared/src/hooks/api/users.tsx
deleted file mode 100644
index 01ade1e27..000000000
--- a/packages/dashboard-shared/src/hooks/api/users.tsx
+++ /dev/null
@@ -1,151 +0,0 @@
-import {
- ClientError,
- InferClientInput,
- InferClientOutput,
-} from "@mercurjs/client";
-import {
- UseMutationOptions,
- UseQueryOptions,
- useMutation,
- useQuery,
-} from "@tanstack/react-query";
-import { sdk, fetchQuery } from "../../lib/client";
-import { queryClient } from "../../lib/query-client";
-import { queryKeysFactory } from "../../lib/query-key-factory";
-
-const USERS_QUERY_KEY = "users" as const;
-const usersQueryKeys = {
- ...queryKeysFactory(USERS_QUERY_KEY),
- me: () => [USERS_QUERY_KEY, "me"],
-};
-
-export const useMe = (
- query?: Omit<InferClientInput<typeof sdk.vendor.sellers.me.query>, "$id">,
- options?: UseQueryOptions<
- unknown,
- ClientError,
- InferClientOutput<typeof sdk.vendor.sellers.me.query>
- >
-) => {
- const { data, ...rest } = useQuery({
- queryFn: () => sdk.vendor.sellers.me.query({ ...query }),
- queryKey: usersQueryKeys.me(),
- ...options,
- });
-
- return {
- ...data,
- ...rest,
- };
-};
-
-export const useUser = (
- id: string,
- query?: Omit<InferClientInput<typeof sdk.vendor.users.$id.query>, "$id">,
- options?: UseQueryOptions<
- unknown,
- ClientError,
- InferClientOutput<typeof sdk.vendor.users.$id.query>
- >
-) => {
- const { data, ...rest } = useQuery({
- queryFn: () => sdk.vendor.users.$id.query({ $id: id, ...query }),
- queryKey: usersQueryKeys.detail(id),
- ...options,
- });
-
- return { ...data, ...rest };
-};
-
-export const useUsers = (
- query?: InferClientInput<typeof sdk.vendor.users.query>,
- options?: UseQueryOptions<
- unknown,
- ClientError,
- InferClientOutput<typeof sdk.vendor.users.query>
- >
-) => {
- const { data, ...rest } = useQuery({
- queryFn: () => sdk.vendor.users.query({ ...query }),
- queryKey: usersQueryKeys.list(query),
- ...options,
- });
-
- return { ...data, ...rest };
-};
-
-export const useUpdateUser = (
- id: string,
- options?: UseMutationOptions<
- InferClientOutput<typeof sdk.vendor.users.$id.mutate>,
- ClientError,
- Omit<InferClientInput<typeof sdk.vendor.users.$id.mutate>, "$id">
- >
-) => {
- return useMutation({
- mutationFn: (payload) => sdk.vendor.users.$id.mutate({ $id: id, ...payload }),
- onSuccess: (data, variables, context) => {
- queryClient.invalidateQueries({ queryKey: usersQueryKeys.detail(id) });
- queryClient.invalidateQueries({ queryKey: usersQueryKeys.lists() });
-
- // We invalidate the me query in case the user updates their own profile
- queryClient.invalidateQueries({ queryKey: usersQueryKeys.me() });
-
- options?.onSuccess?.(data, variables, context);
- },
- ...options,
- });
-};
-
-export const useDeleteUser = (
- id: string,
- options?: UseMutationOptions<
- InferClientOutput<typeof sdk.vendor.users.$id.delete>,
- ClientError,
- void
- >
-) => {
- return useMutation({
- mutationFn: () => sdk.vendor.users.$id.delete({ $id: id }),
- onSuccess: (data, variables, context) => {
- queryClient.invalidateQueries({ queryKey: usersQueryKeys.detail(id) });
- queryClient.invalidateQueries({ queryKey: usersQueryKeys.lists() });
-
- // We invalidate the me query in case the user updates their own profile
- queryClient.invalidateQueries({ queryKey: usersQueryKeys.me() });
-
- options?.onSuccess?.(data, variables, context);
- },
- ...options,
- });
-};
-
-export const useUpdateMe = (
- options?: UseMutationOptions<
- InferClientOutput<typeof sdk.vendor.sellers.me.mutate>,
- ClientError,
- Omit<InferClientInput<typeof sdk.vendor.sellers.me.mutate>, "$id">
- >
-) => {
- return useMutation({
- mutationFn: (payload) => sdk.vendor.sellers.me.mutate(payload),
- onSuccess: (data, variables, context) => {
- queryClient.invalidateQueries({ queryKey: usersQueryKeys.me() });
- options?.onSuccess?.(data, variables, context);
- },
- ...options,
- });
-};
-
-export const useUserMe = (
- query?: Record<string, any>,
- options?: UseQueryOptions<any, Error, any>
-) => {
- const { data, ...rest } = useQuery({
- queryFn: () => sdk.vendor.sellers.me.query({ ...query }),
- queryKey: usersQueryKeys.me(),
- ...options,
- });
-
- return { ...data, ...rest };
-};
diff --git a/packages/dashboard-shared/src/index.ts b/packages/dashboard-shared/src/index.ts
index 2b6c65060..b295c5710 100644
--- a/packages/dashboard-shared/src/index.ts
+++ b/packages/dashboard-shared/src/index.ts
@@ -1,4 +1,3 @@
-export { sdk, backendUrl, } from "./lib/client"
export {
queryKeysFactory,
type TQueryKey,
diff --git a/packages/dashboard-shared/src/lib/client.ts b/packages/dashboard-shared/src/lib/client.ts
deleted file mode 100644
index b1f8a513a..000000000
--- a/packages/dashboard-shared/src/lib/client.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import { createClient, InferClient } from '@mercurjs/client'
-import { Routes } from '@mercurjs/core-plugin/_generated'
-import config from 'virtual:mercur/config'
-
-export const backendUrl = config.backendUrl ?? 'http://localhost:9000'
-
-export const sdk: InferClient<Routes> = createClient<Routes>({
- baseUrl: backendUrl,
- fetchOptions: {
- credentials: 'include',
- },
-})
diff --git a/packages/dashboard-shared/src/module.d.ts b/packages/dashboard-shared/src/module.d.ts
index 6bba4d689..57aada97d 100644
--- a/packages/dashboard-shared/src/module.d.ts
+++ b/packages/dashboard-shared/src/module.d.ts
@@ -1,6 +1,6 @@
declare module "virtual:mercur/config" {
- import { MercurConfig } from '@mercurjs/dashboard-sdk'
- const config: MercurConfig
+ import { BuiltMercurConfig } from '@mercurjs/dashboard-sdk'
+ const config: BuiltMercurConfig
export default config
}
diff --git a/packages/dashboard-shared/src/utils/images-conventer.ts b/packages/dashboard-shared/src/utils/images-conventer.ts
index 1323c16ca..48dc51cfc 100644
--- a/packages/dashboard-shared/src/utils/images-conventer.ts
+++ b/packages/dashboard-shared/src/utils/images-conventer.ts
@@ -1,4 +1,4 @@
-import { backendUrl } from "../lib/client"
+import config from "virtual:mercur/config"
export default function imagesConverter(images: string) {
const isLocalhost =
@@ -7,8 +7,8 @@ export default function imagesConverter(images: string) {
if (isLocalhost) {
return images
- .replace("http://localhost:9000", backendUrl)
- .replace("https://localhost:9000", backendUrl)
+ .replace("http://localhost:9000", config.backendUrl)
+ .replace("https://localhost:9000", config.backendUrl)
}
return images
diff --git a/packages/registry/src/reviews/vendor/hooks/api/reviews.tsx b/packages/registry/src/reviews/vendor/hooks/api/reviews.tsx
index 2c20f7a23..136c108ed 100644
--- a/packages/registry/src/reviews/vendor/hooks/api/reviews.tsx
+++ b/packages/registry/src/reviews/vendor/hooks/api/reviews.tsx
@@ -1,30 +1,30 @@
-import { useQuery, UseQueryOptions } from "@tanstack/react-query"
-import { sdk } from "@mercurjs/dashboard-shared"
-import { queryKeysFactory } from "@mercurjs/dashboard-shared"
+import { useQuery, UseQueryOptions } from "@tanstack/react-query";
+import { sdk } from "@mercurjs/dashboard-shared";
+import { queryKeysFactory } from "@mercurjs/dashboard-shared";
export interface ReviewDTO {
- id: string
- reference: "product" | "seller"
- rating: number
- customer_note: string | null
- seller_note: string | null
- created_at: string
- updated_at: string
+ id: string;
+ reference: "product" | "seller";
+ rating: number;
+ customer_note: string | null;
+ seller_note: string | null;
+ created_at: string;
+ updated_at: string;
}
interface ReviewListResponse {
- reviews: ReviewDTO[]
- count: number
- offset: number
- limit: number
+ reviews: ReviewDTO[];
+ count: number;
+ offset: number;
+ limit: number;
}
interface ReviewDetailResponse {
- review: ReviewDTO
+ review: ReviewDTO;
}
-const REVIEWS_QUERY_KEY = "vendor_reviews" as const
-export const reviewsQueryKeys = queryKeysFactory(REVIEWS_QUERY_KEY)
+const REVIEWS_QUERY_KEY = "vendor_reviews" as const;
+export const reviewsQueryKeys = queryKeysFactory(REVIEWS_QUERY_KEY);
export const useReviews = (
query?: Record<string, any>,
@@ -37,10 +37,10 @@ export const useReviews = (
query,
}),
...options,
- })
+ });
- return { ...data, ...rest }
-}
+ return { ...data, ...rest };
+};
export const useReview = (
id: string,
@@ -54,7 +54,7 @@ export const useReview = (
query,
}),
...options,
- })
+ });
- return { ...data, ...rest }
-}
+ return { ...data, ...rest };
+};