|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query" |
|
4
|
|
|
5
|
import { sdk } from "../../lib/queries/sdk" |
|
6
|
|
|
7
|
const CUSTOMER_QUERY_KEY = "customers" |
|
8
|
|
|
9
|
export const customersQueryKeys = { |
|
10
|
list: (query?: Record<string, any>) => [ |
|
11
|
CUSTOMER_QUERY_KEY, |
|
12
|
query ? query : undefined, |
|
13
|
], |
|
14
|
detail: (id: string, query?: Record<string, any>) => [ |
|
15
|
CUSTOMER_QUERY_KEY, |
|
16
|
id, |
|
17
|
query ? query : undefined, |
|
18
|
], |
|
19
|
addresses: (id: string, query?: Record<string, any>) => [ |
|
20
|
CUSTOMER_QUERY_KEY, |
|
21
|
id, |
|
22
|
"addresses", |
|
23
|
query ? query : undefined, |
|
24
|
], |
|
25
|
address: (id: string, addressId: string, query?: Record<string, any>) => [ |
|
26
|
CUSTOMER_QUERY_KEY, |
|
27
|
id, |
|
28
|
"addresses", |
|
29
|
addressId, |
|
30
|
query ? query : undefined, |
|
31
|
], |
|
32
|
} |
|
33
|
|
|
34
|
export const useCustomer = ( |
|
35
|
id: string, |
|
36
|
query?: HttpTypes.SelectParams, |
|
37
|
options?: Omit< |
|
38
|
UseQueryOptions< |
|
39
|
HttpTypes.AdminCustomerResponse, |
|
40
|
FetchError, |
|
41
|
HttpTypes.AdminCustomerResponse, |
|
42
|
QueryKey |
|
43
|
>, |
|
44
|
"queryFn" | "queryKey" |
|
45
|
> |
|
46
|
) => { |
|
47
|
const { data, ...rest } = useQuery({ |
|
48
|
queryKey: customersQueryKeys.detail(id, query), |
|
49
|
queryFn: async () => sdk.admin.customer.retrieve(id, query), |
|
50
|
...options, |
|
51
|
}) |
|
52
|
|
|
53
|
return { ...data, ...rest } |
|
54
|
} |
|
55
|
|
|
56
|
export const useCustomers = ( |
|
57
|
query?: HttpTypes.AdminCustomerFilters, |
|
58
|
options?: Omit< |
|
59
|
UseQueryOptions< |
|
60
|
HttpTypes.AdminCustomerListResponse, |
|
61
|
FetchError, |
|
62
|
HttpTypes.AdminCustomerListResponse, |
|
63
|
QueryKey |
|
64
|
>, |
|
65
|
"queryFn" | "queryKey" |
|
66
|
> |
|
67
|
) => { |
|
68
|
const { data, ...rest } = useQuery({ |
|
69
|
queryKey: customersQueryKeys.list(query), |
|
70
|
queryFn: async () => sdk.admin.customer.list(query), |
|
71
|
...options, |
|
72
|
}) |
|
73
|
|
|
74
|
return { ...data, ...rest } |
|
75
|
} |
|
76
|
|
|
77
|
export const useCustomerAddresses = ( |
|
78
|
id: string, |
|
79
|
query?: HttpTypes.FindParams & HttpTypes.AdminCustomerAddressFilters, |
|
80
|
options?: Omit< |
|
81
|
UseQueryOptions< |
|
82
|
HttpTypes.AdminCustomerAddressListResponse, |
|
83
|
FetchError, |
|
84
|
HttpTypes.AdminCustomerAddressListResponse, |
|
85
|
QueryKey |
|
86
|
>, |
|
87
|
"queryFn" | "queryKey" |
|
88
|
> |
|
89
|
) => { |
|
90
|
const { data, ...rest } = useQuery({ |
|
91
|
queryKey: customersQueryKeys.addresses(id, query), |
|
92
|
queryFn: async () => { |
|
93
|
const response = await sdk.client.fetch( |
|
94
|
"/admin/customers/" + id + "/addresses", |
|
95
|
{ |
|
96
|
method: "GET", |
|
97
|
headers: { |
|
98
|
"Content-Type": "application/json", |
|
99
|
}, |
|
100
|
credentials: "include", |
|
101
|
} |
|
102
|
) |
|
103
|
|
|
104
|
return response as HttpTypes.AdminCustomerAddressListResponse |
|
105
|
}, |
|
106
|
...options, |
|
107
|
}) |
|
108
|
|
|
109
|
return { ...data, ...rest } |
|
110
|
} |
|
111
|
|
|
112
|
export const useCustomerAddress = ( |
|
113
|
id: string, |
|
114
|
addressId: string, |
|
115
|
query?: HttpTypes.FindParams & HttpTypes.AdminCustomerAddressFilters, |
|
116
|
options?: Omit< |
|
117
|
UseQueryOptions< |
|
118
|
HttpTypes.AdminCustomerAddressResponse, |
|
119
|
FetchError, |
|
120
|
HttpTypes.AdminCustomerAddressResponse, |
|
121
|
QueryKey |
|
122
|
>, |
|
123
|
"queryFn" | "queryKey" |
|
124
|
> |
|
125
|
) => { |
|
126
|
const { data, ...rest } = useQuery({ |
|
127
|
queryKey: customersQueryKeys.address(id, addressId, query), |
|
128
|
queryFn: async () => { |
|
129
|
const response = await sdk.client.fetch( |
|
130
|
"/admin/customers/" + id + "/addresses/" + addressId |
|
131
|
) |
|
132
|
|
|
133
|
return response as HttpTypes.AdminCustomerAddressResponse |
|
134
|
}, |
|
135
|
...options, |
|
136
|
}) |
|
137
|
|
|
138
|
return { ...data, ...rest } |
|
139
|
} |