|
1
|
import { FetchError } from "@medusajs/js-sdk"; |
|
2
|
import { HttpTypes } from "@medusajs/types"; |
|
3
|
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"; |
|
4
|
import { queryKeysFactory } from "../../lib/query-key"; |
|
5
|
import { sdk } from "../../lib/sdk"; |
|
6
|
|
|
7
|
const STORE_QUERY_KEY = "store" as const; |
|
8
|
export const storeQueryKeys = queryKeysFactory(STORE_QUERY_KEY); |
|
9
|
|
|
10
|
export async function retrieveActiveStore( |
|
11
|
query?: HttpTypes.AdminStoreParams |
|
12
|
): Promise<HttpTypes.AdminStoreResponse> { |
|
13
|
const response = await sdk.admin.store.list(query); |
|
14
|
|
|
15
|
const activeStore = response.stores?.[0]; |
|
16
|
|
|
17
|
if (!activeStore) { |
|
18
|
throw new FetchError("No active store found", "Not Found", 404); |
|
19
|
} |
|
20
|
|
|
21
|
return { store: activeStore }; |
|
22
|
} |
|
23
|
|
|
24
|
export const useStore = ( |
|
25
|
query?: HttpTypes.SelectParams, |
|
26
|
options?: Omit< |
|
27
|
UseQueryOptions< |
|
28
|
HttpTypes.AdminStoreResponse, |
|
29
|
FetchError, |
|
30
|
HttpTypes.AdminStoreResponse, |
|
31
|
QueryKey |
|
32
|
>, |
|
33
|
"queryFn" | "queryKey" |
|
34
|
> |
|
35
|
) => { |
|
36
|
const { data, ...rest } = useQuery({ |
|
37
|
queryFn: () => retrieveActiveStore(query), |
|
38
|
queryKey: storeQueryKeys.details(), |
|
39
|
...options, |
|
40
|
}); |
|
41
|
|
|
42
|
return { |
|
43
|
...data, |
|
44
|
...rest, |
|
45
|
}; |
|
46
|
}; |