@instockng/api-client 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +263 -0
- package/package.json +45 -0
- package/src/client.ts +57 -0
- package/src/fetchers/carts.ts +202 -0
- package/src/fetchers/delivery-zones.ts +29 -0
- package/src/fetchers/index.ts +22 -0
- package/src/fetchers/orders.ts +48 -0
- package/src/fetchers/products.ts +46 -0
- package/src/hooks/admin/abandoned-carts.ts +102 -0
- package/src/hooks/admin/brands.ts +134 -0
- package/src/hooks/admin/customers.ts +31 -0
- package/src/hooks/admin/delivery-zones.ts +236 -0
- package/src/hooks/admin/discount-codes.ts +222 -0
- package/src/hooks/admin/index.ts +17 -0
- package/src/hooks/admin/inventory.ts +137 -0
- package/src/hooks/admin/orders.ts +229 -0
- package/src/hooks/admin/products.ts +116 -0
- package/src/hooks/admin/stats.ts +30 -0
- package/src/hooks/admin/variants.ts +173 -0
- package/src/hooks/admin/warehouses.ts +143 -0
- package/src/hooks/public/carts.ts +298 -0
- package/src/hooks/public/delivery-zones.ts +34 -0
- package/src/hooks/public/index.ts +10 -0
- package/src/hooks/public/orders.ts +66 -0
- package/src/hooks/public/products.ts +57 -0
- package/src/hooks/use-query-unwrapped.ts +30 -0
- package/src/hooks/useApiConfig.ts +22 -0
- package/src/index.ts +42 -0
- package/src/provider.tsx +89 -0
- package/src/rpc-client.ts +106 -0
- package/src/rpc-types.ts +121 -0
- package/src/types.ts +39 -0
- package/src/utils/query-keys.ts +121 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe admin RPC hooks for discount code management
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { useMutation, useQueryClient, UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
|
|
6
|
+
import { useQueryUnwrapped } from '../use-query-unwrapped';
|
|
7
|
+
import { createAdminRpcClients, authHeaders } from '../../rpc-client';
|
|
8
|
+
import { queryKeys } from '../../utils/query-keys';
|
|
9
|
+
import { useApiConfig } from '../useApiConfig';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Hook to list discount codes using admin RPC
|
|
13
|
+
*/
|
|
14
|
+
export function useListDiscountCodes(
|
|
15
|
+
params?: {
|
|
16
|
+
page?: number;
|
|
17
|
+
limit?: number;
|
|
18
|
+
category?: string;
|
|
19
|
+
brandId?: string;
|
|
20
|
+
isActive?: string;
|
|
21
|
+
search?: string;
|
|
22
|
+
},
|
|
23
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['discountCodes']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
24
|
+
) {
|
|
25
|
+
const { baseURL, authToken } = useApiConfig();
|
|
26
|
+
|
|
27
|
+
return useQueryUnwrapped({
|
|
28
|
+
queryKey: queryKeys.admin.discountCodes.list(params),
|
|
29
|
+
queryFn: async () => {
|
|
30
|
+
const clients = createAdminRpcClients(baseURL);
|
|
31
|
+
const res = await clients.discountCodes.index.$get({ query: params as any }, authHeaders(authToken));
|
|
32
|
+
if (!res.ok) throw new Error(`Failed to fetch discount codes: ${res.statusText}`);
|
|
33
|
+
return res.json();
|
|
34
|
+
},
|
|
35
|
+
...options,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Hook to get discount code by ID using admin RPC
|
|
41
|
+
*/
|
|
42
|
+
export function useGetDiscountCode(
|
|
43
|
+
codeId: string,
|
|
44
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['discountCodes'][':id']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
45
|
+
) {
|
|
46
|
+
const { baseURL, authToken } = useApiConfig();
|
|
47
|
+
|
|
48
|
+
return useQueryUnwrapped({
|
|
49
|
+
queryKey: queryKeys.admin.discountCodes.detail(codeId),
|
|
50
|
+
queryFn: async () => {
|
|
51
|
+
const clients = createAdminRpcClients(baseURL);
|
|
52
|
+
const res = await clients.discountCodes[':id'].$get(
|
|
53
|
+
{ param: { id: codeId } },
|
|
54
|
+
authHeaders(authToken)
|
|
55
|
+
);
|
|
56
|
+
if (!res.ok) throw new Error(`Failed to fetch discount code: ${res.statusText}`);
|
|
57
|
+
return res.json();
|
|
58
|
+
},
|
|
59
|
+
...options,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Hook to create a discount code using admin RPC
|
|
65
|
+
*/
|
|
66
|
+
export function useCreateDiscountCode(
|
|
67
|
+
options?: UseMutationOptions<
|
|
68
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['discountCodes']['index']['$post']>>['json']>>,
|
|
69
|
+
Error,
|
|
70
|
+
any
|
|
71
|
+
>
|
|
72
|
+
) {
|
|
73
|
+
const { baseURL, authToken } = useApiConfig();
|
|
74
|
+
const queryClient = useQueryClient();
|
|
75
|
+
|
|
76
|
+
return useMutation({
|
|
77
|
+
mutationFn: async (data) => {
|
|
78
|
+
const clients = createAdminRpcClients(baseURL);
|
|
79
|
+
const res = await clients.discountCodes.index.$post({ json: data }, authHeaders(authToken));
|
|
80
|
+
if (!res.ok) throw new Error(`Failed to create discount code: ${res.statusText}`);
|
|
81
|
+
return res.json();
|
|
82
|
+
},
|
|
83
|
+
onSuccess: () => {
|
|
84
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.discountCodes.all });
|
|
85
|
+
},
|
|
86
|
+
...options,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Hook to update a discount code using admin RPC
|
|
92
|
+
*/
|
|
93
|
+
export function useUpdateDiscountCode(
|
|
94
|
+
codeId: string,
|
|
95
|
+
options?: UseMutationOptions<
|
|
96
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['discountCodes'][':id']['$patch']>>['json']>>,
|
|
97
|
+
Error,
|
|
98
|
+
any
|
|
99
|
+
>
|
|
100
|
+
) {
|
|
101
|
+
const { baseURL, authToken } = useApiConfig();
|
|
102
|
+
const queryClient = useQueryClient();
|
|
103
|
+
|
|
104
|
+
return useMutation({
|
|
105
|
+
mutationFn: async (data) => {
|
|
106
|
+
const clients = createAdminRpcClients(baseURL);
|
|
107
|
+
const res = await clients.discountCodes[':id'].$patch(
|
|
108
|
+
{ json: data, param: { id: codeId } },
|
|
109
|
+
authHeaders(authToken)
|
|
110
|
+
);
|
|
111
|
+
if (!res.ok) throw new Error(`Failed to update discount code: ${res.statusText}`);
|
|
112
|
+
return res.json();
|
|
113
|
+
},
|
|
114
|
+
onSuccess: () => {
|
|
115
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.discountCodes.detail(codeId) });
|
|
116
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.discountCodes.all });
|
|
117
|
+
},
|
|
118
|
+
...options,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Hook to delete a discount code using admin RPC
|
|
124
|
+
*/
|
|
125
|
+
export function useDeleteDiscountCode(
|
|
126
|
+
codeId: string,
|
|
127
|
+
options?: UseMutationOptions<
|
|
128
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['discountCodes'][':id']['$delete']>>['json']>>,
|
|
129
|
+
Error,
|
|
130
|
+
void
|
|
131
|
+
>
|
|
132
|
+
) {
|
|
133
|
+
const { baseURL, authToken } = useApiConfig();
|
|
134
|
+
const queryClient = useQueryClient();
|
|
135
|
+
|
|
136
|
+
return useMutation({
|
|
137
|
+
mutationFn: async () => {
|
|
138
|
+
const clients = createAdminRpcClients(baseURL);
|
|
139
|
+
const res = await clients.discountCodes[':id'].$delete(
|
|
140
|
+
{ param: { id: codeId } },
|
|
141
|
+
authHeaders(authToken)
|
|
142
|
+
);
|
|
143
|
+
if (!res.ok) throw new Error(`Failed to delete discount code: ${res.statusText}`);
|
|
144
|
+
return res.json();
|
|
145
|
+
},
|
|
146
|
+
onSuccess: () => {
|
|
147
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.discountCodes.all });
|
|
148
|
+
},
|
|
149
|
+
...options,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Hook to get discount code analytics using admin RPC
|
|
155
|
+
*/
|
|
156
|
+
export function useGetDiscountCodeAnalytics(
|
|
157
|
+
codeId: string,
|
|
158
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['discountCodes'][':id']['analytics']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
159
|
+
) {
|
|
160
|
+
const { baseURL, authToken } = useApiConfig();
|
|
161
|
+
|
|
162
|
+
return useQueryUnwrapped({
|
|
163
|
+
queryKey: queryKeys.admin.discountCodes.analytics(codeId),
|
|
164
|
+
queryFn: async () => {
|
|
165
|
+
const clients = createAdminRpcClients(baseURL);
|
|
166
|
+
const res = await clients.discountCodes[':id'].analytics.$get(
|
|
167
|
+
{ param: { id: codeId } },
|
|
168
|
+
authHeaders(authToken)
|
|
169
|
+
);
|
|
170
|
+
if (!res.ok) throw new Error(`Failed to fetch discount code analytics: ${res.statusText}`);
|
|
171
|
+
return res.json();
|
|
172
|
+
},
|
|
173
|
+
...options,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Hook to bulk generate discount codes using admin RPC
|
|
179
|
+
*/
|
|
180
|
+
export function useBulkGenerateDiscountCodes(
|
|
181
|
+
options?: UseMutationOptions<
|
|
182
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['discountCodes']['bulk-generate']['$post']>>['json']>>,
|
|
183
|
+
Error,
|
|
184
|
+
any
|
|
185
|
+
>
|
|
186
|
+
) {
|
|
187
|
+
const { baseURL, authToken } = useApiConfig();
|
|
188
|
+
const queryClient = useQueryClient();
|
|
189
|
+
|
|
190
|
+
return useMutation({
|
|
191
|
+
mutationFn: async (data) => {
|
|
192
|
+
const clients = createAdminRpcClients(baseURL);
|
|
193
|
+
const res = await clients.discountCodes['bulk-generate'].$post({ json: data }, authHeaders(authToken));
|
|
194
|
+
if (!res.ok) throw new Error(`Failed to bulk generate discount codes: ${res.statusText}`);
|
|
195
|
+
return res.json();
|
|
196
|
+
},
|
|
197
|
+
onSuccess: () => {
|
|
198
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.discountCodes.all });
|
|
199
|
+
},
|
|
200
|
+
...options,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Hook to get discount code overview stats using admin RPC
|
|
206
|
+
*/
|
|
207
|
+
export function useGetDiscountCodeOverviewStats(
|
|
208
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['discountCodes']['stats']['overview']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
209
|
+
) {
|
|
210
|
+
const { baseURL, authToken } = useApiConfig();
|
|
211
|
+
|
|
212
|
+
return useQueryUnwrapped({
|
|
213
|
+
queryKey: queryKeys.admin.discountCodes.overviewStats(),
|
|
214
|
+
queryFn: async () => {
|
|
215
|
+
const clients = createAdminRpcClients(baseURL);
|
|
216
|
+
const res = await clients.discountCodes.stats.overview.$get({}, authHeaders(authToken));
|
|
217
|
+
if (!res.ok) throw new Error(`Failed to fetch discount code overview stats: ${res.statusText}`);
|
|
218
|
+
return res.json();
|
|
219
|
+
},
|
|
220
|
+
...options,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe admin RPC hooks
|
|
3
|
+
*
|
|
4
|
+
* Export all admin RPC hooks for use in admin dashboards
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export * from './orders';
|
|
8
|
+
export * from './brands';
|
|
9
|
+
export * from './products';
|
|
10
|
+
export * from './variants';
|
|
11
|
+
export * from './warehouses';
|
|
12
|
+
export * from './inventory';
|
|
13
|
+
export * from './customers';
|
|
14
|
+
export * from './stats';
|
|
15
|
+
export * from './abandoned-carts';
|
|
16
|
+
export * from './discount-codes';
|
|
17
|
+
export * from './delivery-zones';
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe admin RPC hooks for inventory management
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { useMutation, useQueryClient, UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
|
|
6
|
+
import { useQueryUnwrapped } from '../use-query-unwrapped';
|
|
7
|
+
import { createAdminRpcClients, authHeaders } from '../../rpc-client';
|
|
8
|
+
import { queryKeys } from '../../utils/query-keys';
|
|
9
|
+
import { useApiConfig } from '../useApiConfig';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Hook to get inventory overview using admin RPC
|
|
13
|
+
*/
|
|
14
|
+
export function useListInventory(
|
|
15
|
+
params?: { brandId?: string; warehouseId?: string; productId?: string },
|
|
16
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['inventory']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
17
|
+
) {
|
|
18
|
+
const { baseURL, authToken } = useApiConfig();
|
|
19
|
+
|
|
20
|
+
return useQueryUnwrapped({
|
|
21
|
+
queryKey: queryKeys.admin.inventory.list(params),
|
|
22
|
+
queryFn: async () => {
|
|
23
|
+
const clients = createAdminRpcClients(baseURL);
|
|
24
|
+
const res = await clients.inventory.index.$get({ query: params as any }, authHeaders(authToken));
|
|
25
|
+
if (!res.ok) throw new Error(`Failed to fetch inventory: ${res.statusText}`);
|
|
26
|
+
return res.json();
|
|
27
|
+
},
|
|
28
|
+
...options,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Hook to adjust inventory using admin RPC
|
|
34
|
+
*/
|
|
35
|
+
export function useAdjustInventory(
|
|
36
|
+
options?: UseMutationOptions<
|
|
37
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['inventory']['adjust']['$post']>>['json']>>,
|
|
38
|
+
Error,
|
|
39
|
+
any
|
|
40
|
+
>
|
|
41
|
+
) {
|
|
42
|
+
const { baseURL, authToken } = useApiConfig();
|
|
43
|
+
const queryClient = useQueryClient();
|
|
44
|
+
|
|
45
|
+
return useMutation({
|
|
46
|
+
mutationFn: async (data) => {
|
|
47
|
+
const clients = createAdminRpcClients(baseURL);
|
|
48
|
+
const res = await clients.inventory.adjust.$post({ json: data }, authHeaders(authToken));
|
|
49
|
+
if (!res.ok) throw new Error(`Failed to adjust inventory: ${res.statusText}`);
|
|
50
|
+
return res.json();
|
|
51
|
+
},
|
|
52
|
+
onSuccess: () => {
|
|
53
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.inventory.all });
|
|
54
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.variants.all });
|
|
55
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.warehouses.all });
|
|
56
|
+
},
|
|
57
|
+
...options,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Hook to transfer inventory between warehouses using admin RPC
|
|
63
|
+
*/
|
|
64
|
+
export function useTransferInventory(
|
|
65
|
+
options?: UseMutationOptions<
|
|
66
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['inventory']['transfer']['$post']>>['json']>>,
|
|
67
|
+
Error,
|
|
68
|
+
any
|
|
69
|
+
>
|
|
70
|
+
) {
|
|
71
|
+
const { baseURL, authToken } = useApiConfig();
|
|
72
|
+
const queryClient = useQueryClient();
|
|
73
|
+
|
|
74
|
+
return useMutation({
|
|
75
|
+
mutationFn: async (data) => {
|
|
76
|
+
const clients = createAdminRpcClients(baseURL);
|
|
77
|
+
const res = await clients.inventory.transfer.$post({ json: data }, authHeaders(authToken));
|
|
78
|
+
if (!res.ok) throw new Error(`Failed to transfer inventory: ${res.statusText}`);
|
|
79
|
+
return res.json();
|
|
80
|
+
},
|
|
81
|
+
onSuccess: () => {
|
|
82
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.inventory.all });
|
|
83
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.warehouses.all });
|
|
84
|
+
},
|
|
85
|
+
...options,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Hook to get inventory transaction history using admin RPC
|
|
91
|
+
*/
|
|
92
|
+
export function useGetInventoryTransactions(
|
|
93
|
+
params?: {
|
|
94
|
+
variantId?: string;
|
|
95
|
+
warehouseId?: string;
|
|
96
|
+
type?: string;
|
|
97
|
+
startDate?: string;
|
|
98
|
+
endDate?: string;
|
|
99
|
+
page?: number;
|
|
100
|
+
limit?: number;
|
|
101
|
+
},
|
|
102
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['inventory']['transactions']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
103
|
+
) {
|
|
104
|
+
const { baseURL, authToken } = useApiConfig();
|
|
105
|
+
|
|
106
|
+
return useQueryUnwrapped({
|
|
107
|
+
queryKey: queryKeys.admin.inventory.transactions(params),
|
|
108
|
+
queryFn: async () => {
|
|
109
|
+
const clients = createAdminRpcClients(baseURL);
|
|
110
|
+
const res = await clients.inventory.transactions.$get({ query: params as any }, authHeaders(authToken));
|
|
111
|
+
if (!res.ok) throw new Error(`Failed to fetch inventory transactions: ${res.statusText}`);
|
|
112
|
+
return res.json();
|
|
113
|
+
},
|
|
114
|
+
...options,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Hook to get low stock alerts using admin RPC
|
|
120
|
+
*/
|
|
121
|
+
export function useGetLowStockVariants(
|
|
122
|
+
params?: { brandId?: string },
|
|
123
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['inventory']['low-stock']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
124
|
+
) {
|
|
125
|
+
const { baseURL, authToken } = useApiConfig();
|
|
126
|
+
|
|
127
|
+
return useQueryUnwrapped({
|
|
128
|
+
queryKey: queryKeys.admin.inventory.lowStock(params?.brandId),
|
|
129
|
+
queryFn: async () => {
|
|
130
|
+
const clients = createAdminRpcClients(baseURL);
|
|
131
|
+
const res = await clients.inventory['low-stock'].$get({ query: params as any }, authHeaders(authToken));
|
|
132
|
+
if (!res.ok) throw new Error(`Failed to fetch low stock alerts: ${res.statusText}`);
|
|
133
|
+
return res.json();
|
|
134
|
+
},
|
|
135
|
+
...options,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe admin RPC hooks for order management
|
|
3
|
+
*
|
|
4
|
+
* These hooks use Hono RPC client with types directly from the backend,
|
|
5
|
+
* providing end-to-end type safety without code generation.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { useMutation, useQueryClient, UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
|
|
9
|
+
import { useQueryUnwrapped } from '../use-query-unwrapped';
|
|
10
|
+
import { createAdminRpcClients, authHeaders } from '../../rpc-client';
|
|
11
|
+
import { queryKeys } from '../../utils/query-keys';
|
|
12
|
+
import { useApiConfig } from '../useApiConfig';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hook to list all orders with pagination using admin RPC
|
|
16
|
+
*
|
|
17
|
+
* @param options - React Query options
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const { data: orders, isLoading } = useListOrders();
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function useListOrders(
|
|
25
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['orders']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
26
|
+
) {
|
|
27
|
+
const { baseURL, authToken } = useApiConfig();
|
|
28
|
+
|
|
29
|
+
return useQueryUnwrapped({
|
|
30
|
+
queryKey: queryKeys.admin.orders.list(),
|
|
31
|
+
queryFn: async () => {
|
|
32
|
+
const clients = createAdminRpcClients(baseURL);
|
|
33
|
+
const res = await clients.orders.index.$get({}, authHeaders(authToken));
|
|
34
|
+
if (!res.ok) {
|
|
35
|
+
throw new Error(`Failed to fetch orders: ${res.statusText}`);
|
|
36
|
+
}
|
|
37
|
+
return res.json();
|
|
38
|
+
},
|
|
39
|
+
...options,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Hook to get order by ID using admin RPC
|
|
45
|
+
*
|
|
46
|
+
* @param orderId - Order UUID
|
|
47
|
+
* @param options - React Query options
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```tsx
|
|
51
|
+
* const { data: order } = useGetOrder('order-123');
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function useGetOrder(
|
|
55
|
+
orderId: string,
|
|
56
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['orders'][':id']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
57
|
+
) {
|
|
58
|
+
const { baseURL, authToken } = useApiConfig();
|
|
59
|
+
|
|
60
|
+
return useQueryUnwrapped({
|
|
61
|
+
queryKey: queryKeys.admin.orders.detail(orderId),
|
|
62
|
+
queryFn: async () => {
|
|
63
|
+
const clients = createAdminRpcClients(baseURL);
|
|
64
|
+
const res = await clients.orders[':id'].$get(
|
|
65
|
+
{ param: { id: orderId } },
|
|
66
|
+
authHeaders(authToken)
|
|
67
|
+
);
|
|
68
|
+
if (!res.ok) {
|
|
69
|
+
throw new Error(`Failed to fetch order: ${res.statusText}`);
|
|
70
|
+
}
|
|
71
|
+
return res.json();
|
|
72
|
+
},
|
|
73
|
+
...options,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Hook to create a new order using admin RPC
|
|
79
|
+
*
|
|
80
|
+
* @param options - React Query mutation options
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```tsx
|
|
84
|
+
* const createOrder = useCreateOrder();
|
|
85
|
+
* createOrder.mutate({
|
|
86
|
+
* brandSlug: 'my-brand',
|
|
87
|
+
* items: [{ sku: 'SKU-001', quantity: 2 }],
|
|
88
|
+
* // ... other order fields
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export function useCreateOrder(
|
|
93
|
+
options?: UseMutationOptions<
|
|
94
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['orders']['index']['$post']>>['json']>>,
|
|
95
|
+
Error,
|
|
96
|
+
any // Type will be inferred from backend
|
|
97
|
+
>
|
|
98
|
+
) {
|
|
99
|
+
const { baseURL, authToken } = useApiConfig();
|
|
100
|
+
const queryClient = useQueryClient();
|
|
101
|
+
|
|
102
|
+
return useMutation({
|
|
103
|
+
mutationFn: async (data) => {
|
|
104
|
+
const clients = createAdminRpcClients(baseURL);
|
|
105
|
+
const res = await clients.orders.index.$post(
|
|
106
|
+
{ json: data },
|
|
107
|
+
authHeaders(authToken)
|
|
108
|
+
);
|
|
109
|
+
if (!res.ok) {
|
|
110
|
+
throw new Error(`Failed to create order: ${res.statusText}`);
|
|
111
|
+
}
|
|
112
|
+
return res.json();
|
|
113
|
+
},
|
|
114
|
+
onSuccess: () => {
|
|
115
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.orders.all });
|
|
116
|
+
},
|
|
117
|
+
...options,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Hook to update an order using admin RPC
|
|
123
|
+
*
|
|
124
|
+
* @param orderId - Order UUID
|
|
125
|
+
* @param options - React Query mutation options
|
|
126
|
+
*/
|
|
127
|
+
export function useUpdateOrder(
|
|
128
|
+
orderId: string,
|
|
129
|
+
options?: UseMutationOptions<
|
|
130
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['orders'][':id']['$patch']>>['json']>>,
|
|
131
|
+
Error,
|
|
132
|
+
any
|
|
133
|
+
>
|
|
134
|
+
) {
|
|
135
|
+
const { baseURL, authToken } = useApiConfig();
|
|
136
|
+
const queryClient = useQueryClient();
|
|
137
|
+
|
|
138
|
+
return useMutation({
|
|
139
|
+
mutationFn: async (data) => {
|
|
140
|
+
const clients = createAdminRpcClients(baseURL);
|
|
141
|
+
const res = await clients.orders[':id'].$patch(
|
|
142
|
+
{ param: { id: orderId }, json: data },
|
|
143
|
+
authHeaders(authToken)
|
|
144
|
+
);
|
|
145
|
+
if (!res.ok) {
|
|
146
|
+
throw new Error(`Failed to update order: ${res.statusText}`);
|
|
147
|
+
}
|
|
148
|
+
return res.json();
|
|
149
|
+
},
|
|
150
|
+
onSuccess: () => {
|
|
151
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.orders.detail(orderId) });
|
|
152
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.orders.all });
|
|
153
|
+
},
|
|
154
|
+
...options,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Hook to update order status using admin RPC
|
|
160
|
+
*
|
|
161
|
+
* @param orderId - Order UUID
|
|
162
|
+
* @param options - React Query mutation options
|
|
163
|
+
*/
|
|
164
|
+
export function useUpdateOrderStatus(
|
|
165
|
+
orderId: string,
|
|
166
|
+
options?: UseMutationOptions<
|
|
167
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['orders'][':id']['status']['$patch']>>['json']>>,
|
|
168
|
+
Error,
|
|
169
|
+
any
|
|
170
|
+
>
|
|
171
|
+
) {
|
|
172
|
+
const { baseURL, authToken } = useApiConfig();
|
|
173
|
+
const queryClient = useQueryClient();
|
|
174
|
+
|
|
175
|
+
return useMutation({
|
|
176
|
+
mutationFn: async (data) => {
|
|
177
|
+
const clients = createAdminRpcClients(baseURL);
|
|
178
|
+
const res = await clients.orders[':id'].status.$patch(
|
|
179
|
+
{ param: { id: orderId }, json: data },
|
|
180
|
+
authHeaders(authToken)
|
|
181
|
+
);
|
|
182
|
+
if (!res.ok) {
|
|
183
|
+
throw new Error(`Failed to update order status: ${res.statusText}`);
|
|
184
|
+
}
|
|
185
|
+
return res.json();
|
|
186
|
+
},
|
|
187
|
+
onSuccess: () => {
|
|
188
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.orders.detail(orderId) });
|
|
189
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.orders.all });
|
|
190
|
+
},
|
|
191
|
+
...options,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Hook to delete an order using admin RPC
|
|
197
|
+
*
|
|
198
|
+
* @param orderId - Order UUID
|
|
199
|
+
* @param options - React Query mutation options
|
|
200
|
+
*/
|
|
201
|
+
export function useDeleteOrder(
|
|
202
|
+
orderId: string,
|
|
203
|
+
options?: UseMutationOptions<
|
|
204
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['orders'][':id']['$delete']>>['json']>>,
|
|
205
|
+
Error,
|
|
206
|
+
void
|
|
207
|
+
>
|
|
208
|
+
) {
|
|
209
|
+
const { baseURL, authToken } = useApiConfig();
|
|
210
|
+
const queryClient = useQueryClient();
|
|
211
|
+
|
|
212
|
+
return useMutation({
|
|
213
|
+
mutationFn: async () => {
|
|
214
|
+
const clients = createAdminRpcClients(baseURL);
|
|
215
|
+
const res = await clients.orders[':id'].$delete(
|
|
216
|
+
{ param: { id: orderId } },
|
|
217
|
+
authHeaders(authToken)
|
|
218
|
+
);
|
|
219
|
+
if (!res.ok) {
|
|
220
|
+
throw new Error(`Failed to delete order: ${res.statusText}`);
|
|
221
|
+
}
|
|
222
|
+
return res.json();
|
|
223
|
+
},
|
|
224
|
+
onSuccess: () => {
|
|
225
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.orders.all });
|
|
226
|
+
},
|
|
227
|
+
...options,
|
|
228
|
+
});
|
|
229
|
+
}
|