@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.
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Type-safe admin RPC hooks for product 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
+ export function useListProducts(
12
+ brandId?: string,
13
+ options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['products']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
14
+ ) {
15
+ const { baseURL, authToken } = useApiConfig();
16
+
17
+ return useQueryUnwrapped({
18
+ queryKey: queryKeys.admin.products.list(brandId),
19
+ queryFn: async () => {
20
+ const clients = createAdminRpcClients(baseURL);
21
+ const res = await clients.products.index.$get(
22
+ brandId ? { query: { brandId } } : {},
23
+ authHeaders(authToken)
24
+ );
25
+ if (!res.ok) throw new Error(`Failed to fetch products: ${res.statusText}`);
26
+ return res.json();
27
+ },
28
+ ...options,
29
+ });
30
+ }
31
+
32
+ export function useGetProduct(
33
+ productId: string,
34
+ options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['products'][':id']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
35
+ ) {
36
+ const { baseURL, authToken } = useApiConfig();
37
+
38
+ return useQueryUnwrapped({
39
+ queryKey: queryKeys.admin.products.detail(productId),
40
+ queryFn: async () => {
41
+ const clients = createAdminRpcClients(baseURL);
42
+ const res = await clients.products[':id'].$get(
43
+ { param: { id: productId } },
44
+ authHeaders(authToken)
45
+ );
46
+ if (!res.ok) throw new Error(`Failed to fetch product: ${res.statusText}`);
47
+ return res.json();
48
+ },
49
+ ...options,
50
+ });
51
+ }
52
+
53
+ export function useCreateProduct(
54
+ options?: UseMutationOptions<any, Error, any>
55
+ ) {
56
+ const { baseURL, authToken } = useApiConfig();
57
+ const queryClient = useQueryClient();
58
+ return useMutation({
59
+ mutationFn: async (data) => {
60
+ const clients = createAdminRpcClients(baseURL);
61
+ const res = await clients.products.index.$post(
62
+ { json: data },
63
+ authHeaders(authToken)
64
+ );
65
+ if (!res.ok) throw new Error(`Failed to create product: ${res.statusText}`);
66
+ return res.json();
67
+ },
68
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: queryKeys.admin.products.all }),
69
+ ...options,
70
+ });
71
+ }
72
+
73
+ export function useUpdateProduct(
74
+ productId: string,
75
+ options?: UseMutationOptions<any, Error, any>
76
+ ) {
77
+ const { baseURL, authToken } = useApiConfig();
78
+ const queryClient = useQueryClient();
79
+ return useMutation({
80
+ mutationFn: async (data) => {
81
+ const clients = createAdminRpcClients(baseURL);
82
+ const res = await clients.products[':id'].$patch(
83
+ { param: { id: productId }, json: data },
84
+ authHeaders(authToken)
85
+ );
86
+ if (!res.ok) throw new Error(`Failed to update product: ${res.statusText}`);
87
+ return res.json();
88
+ },
89
+ onSuccess: () => {
90
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.products.detail(productId) });
91
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.products.all });
92
+ },
93
+ ...options,
94
+ });
95
+ }
96
+
97
+ export function useDeleteProduct(
98
+ productId: string,
99
+ options?: UseMutationOptions<any, Error, void>
100
+ ) {
101
+ const { baseURL, authToken } = useApiConfig();
102
+ const queryClient = useQueryClient();
103
+ return useMutation({
104
+ mutationFn: async () => {
105
+ const clients = createAdminRpcClients(baseURL);
106
+ const res = await clients.products[':id'].$delete(
107
+ { param: { id: productId } },
108
+ authHeaders(authToken)
109
+ );
110
+ if (!res.ok) throw new Error(`Failed to delete product: ${res.statusText}`);
111
+ return res.json();
112
+ },
113
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: queryKeys.admin.products.all }),
114
+ ...options,
115
+ });
116
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Type-safe admin RPC hooks for statistics
3
+ */
4
+
5
+ import { UseQueryOptions } 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 dashboard statistics using admin RPC
13
+ */
14
+ export function useGetStats(
15
+ params?: { brandId?: string },
16
+ options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['stats']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
17
+ ) {
18
+ const { baseURL, authToken } = useApiConfig();
19
+
20
+ return useQueryUnwrapped({
21
+ queryKey: queryKeys.admin.stats.overview(params?.brandId),
22
+ queryFn: async () => {
23
+ const clients = createAdminRpcClients(baseURL);
24
+ const res = await clients.stats.index.$get({ query: params as any }, authHeaders(authToken));
25
+ if (!res.ok) throw new Error(`Failed to fetch statistics: ${res.statusText}`);
26
+ return res.json();
27
+ },
28
+ ...options,
29
+ });
30
+ }
@@ -0,0 +1,173 @@
1
+ /**
2
+ * Type-safe admin RPC hooks for variant 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 search variants across all products using admin RPC
13
+ */
14
+ export function useSearchVariants(
15
+ params?: { brandId?: string; search?: string },
16
+ options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['variants']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
17
+ ) {
18
+ const { baseURL, authToken } = useApiConfig();
19
+
20
+ return useQueryUnwrapped({
21
+ queryKey: queryKeys.admin.variants.search(params),
22
+ queryFn: async () => {
23
+ const clients = createAdminRpcClients(baseURL);
24
+ const res = await clients.variants.index.$get({ query: params as any }, authHeaders(authToken));
25
+ if (!res.ok) throw new Error(`Failed to search variants: ${res.statusText}`);
26
+ return res.json();
27
+ },
28
+ ...options,
29
+ });
30
+ }
31
+
32
+ /**
33
+ * Hook to list variants for a specific product using admin RPC
34
+ */
35
+ export function useListProductVariants(
36
+ productId: string,
37
+ options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['variants']['products'][':productId']['variants']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
38
+ ) {
39
+ const { baseURL, authToken } = useApiConfig();
40
+
41
+ return useQueryUnwrapped({
42
+ queryKey: queryKeys.admin.variants.byProduct(productId),
43
+ queryFn: async () => {
44
+ const clients = createAdminRpcClients(baseURL);
45
+ const res = await clients.variants.products[':productId'].variants.$get(
46
+ { param: { productId } },
47
+ authHeaders(authToken)
48
+ );
49
+ if (!res.ok) throw new Error(`Failed to fetch product variants: ${res.statusText}`);
50
+ return res.json();
51
+ },
52
+ ...options,
53
+ });
54
+ }
55
+
56
+ /**
57
+ * Hook to create a variant for a product using admin RPC
58
+ */
59
+ export function useCreateVariant(
60
+ productId: string,
61
+ options?: UseMutationOptions<
62
+ Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['variants']['products'][':productId']['variants']['$post']>>['json']>>,
63
+ Error,
64
+ any
65
+ >
66
+ ) {
67
+ const { baseURL, authToken } = useApiConfig();
68
+ const queryClient = useQueryClient();
69
+
70
+ return useMutation({
71
+ mutationFn: async (data) => {
72
+ const clients = createAdminRpcClients(baseURL);
73
+ const res = await clients.variants.products[':productId'].variants.$post(
74
+ { json: data, param: { productId } },
75
+ authHeaders(authToken)
76
+ );
77
+ if (!res.ok) throw new Error(`Failed to create variant: ${res.statusText}`);
78
+ return res.json();
79
+ },
80
+ onSuccess: () => {
81
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.variants.all });
82
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.products.detail(productId) });
83
+ },
84
+ ...options,
85
+ });
86
+ }
87
+
88
+ /**
89
+ * Hook to update a variant using admin RPC
90
+ */
91
+ export function useUpdateVariant(
92
+ variantId: string,
93
+ options?: UseMutationOptions<
94
+ Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['variants'][':id']['$patch']>>['json']>>,
95
+ Error,
96
+ any
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.variants[':id'].$patch(
106
+ { json: data, param: { id: variantId } },
107
+ authHeaders(authToken)
108
+ );
109
+ if (!res.ok) throw new Error(`Failed to update variant: ${res.statusText}`);
110
+ return res.json();
111
+ },
112
+ onSuccess: () => {
113
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.variants.detail(variantId) });
114
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.variants.all });
115
+ },
116
+ ...options,
117
+ });
118
+ }
119
+
120
+ /**
121
+ * Hook to get variant inventory across all warehouses using admin RPC
122
+ */
123
+ export function useGetVariantInventory(
124
+ variantId: string,
125
+ options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['variants'][':id']['inventory']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
126
+ ) {
127
+ const { baseURL, authToken } = useApiConfig();
128
+
129
+ return useQueryUnwrapped({
130
+ queryKey: queryKeys.admin.variants.inventory(variantId),
131
+ queryFn: async () => {
132
+ const clients = createAdminRpcClients(baseURL);
133
+ const res = await clients.variants[':id'].inventory.$get(
134
+ { param: { id: variantId } },
135
+ authHeaders(authToken)
136
+ );
137
+ if (!res.ok) throw new Error(`Failed to fetch variant inventory: ${res.statusText}`);
138
+ return res.json();
139
+ },
140
+ ...options,
141
+ });
142
+ }
143
+
144
+ /**
145
+ * Hook to delete a variant using admin RPC
146
+ */
147
+ export function useDeleteVariant(
148
+ variantId: string,
149
+ options?: UseMutationOptions<
150
+ Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['variants'][':id']['$delete']>>['json']>>,
151
+ Error,
152
+ void
153
+ >
154
+ ) {
155
+ const { baseURL, authToken } = useApiConfig();
156
+ const queryClient = useQueryClient();
157
+
158
+ return useMutation({
159
+ mutationFn: async () => {
160
+ const clients = createAdminRpcClients(baseURL);
161
+ const res = await clients.variants[':id'].$delete(
162
+ { param: { id: variantId } },
163
+ authHeaders(authToken)
164
+ );
165
+ if (!res.ok) throw new Error(`Failed to delete variant: ${res.statusText}`);
166
+ return res.json();
167
+ },
168
+ onSuccess: () => {
169
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.variants.all });
170
+ },
171
+ ...options,
172
+ });
173
+ }
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Type-safe admin RPC hooks for warehouse 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 all warehouses using admin RPC
13
+ */
14
+ export function useListWarehouses(
15
+ options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['warehouses']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
16
+ ) {
17
+ const { baseURL, authToken } = useApiConfig();
18
+
19
+ return useQueryUnwrapped({
20
+ queryKey: queryKeys.admin.warehouses.list(),
21
+ queryFn: async () => {
22
+ const clients = createAdminRpcClients(baseURL);
23
+ const res = await clients.warehouses.index.$get({}, authHeaders(authToken));
24
+ if (!res.ok) throw new Error(`Failed to fetch warehouses: ${res.statusText}`);
25
+ return res.json();
26
+ },
27
+ ...options,
28
+ });
29
+ }
30
+
31
+ /**
32
+ * Hook to create a warehouse using admin RPC
33
+ */
34
+ export function useCreateWarehouse(
35
+ options?: UseMutationOptions<
36
+ Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['warehouses']['index']['$post']>>['json']>>,
37
+ Error,
38
+ any
39
+ >
40
+ ) {
41
+ const { baseURL, authToken } = useApiConfig();
42
+ const queryClient = useQueryClient();
43
+
44
+ return useMutation({
45
+ mutationFn: async (data) => {
46
+ const clients = createAdminRpcClients(baseURL);
47
+ const res = await clients.warehouses.index.$post({ json: data }, authHeaders(authToken));
48
+ if (!res.ok) throw new Error(`Failed to create warehouse: ${res.statusText}`);
49
+ return res.json();
50
+ },
51
+ onSuccess: () => {
52
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.warehouses.all });
53
+ },
54
+ ...options,
55
+ });
56
+ }
57
+
58
+ /**
59
+ * Hook to update a warehouse using admin RPC
60
+ */
61
+ export function useUpdateWarehouse(
62
+ warehouseId: string,
63
+ options?: UseMutationOptions<
64
+ Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['warehouses'][':id']['$patch']>>['json']>>,
65
+ Error,
66
+ any
67
+ >
68
+ ) {
69
+ const { baseURL, authToken } = useApiConfig();
70
+ const queryClient = useQueryClient();
71
+
72
+ return useMutation({
73
+ mutationFn: async (data) => {
74
+ const clients = createAdminRpcClients(baseURL);
75
+ const res = await clients.warehouses[':id'].$patch(
76
+ { json: data, param: { id: warehouseId } },
77
+ authHeaders(authToken)
78
+ );
79
+ if (!res.ok) throw new Error(`Failed to update warehouse: ${res.statusText}`);
80
+ return res.json();
81
+ },
82
+ onSuccess: () => {
83
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.warehouses.detail(warehouseId) });
84
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.warehouses.all });
85
+ },
86
+ ...options,
87
+ });
88
+ }
89
+
90
+ /**
91
+ * Hook to get warehouse inventory using admin RPC
92
+ */
93
+ export function useGetWarehouseInventory(
94
+ warehouseId: string,
95
+ options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['warehouses'][':id']['inventory']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
96
+ ) {
97
+ const { baseURL, authToken } = useApiConfig();
98
+
99
+ return useQueryUnwrapped({
100
+ queryKey: queryKeys.admin.warehouses.inventory(warehouseId),
101
+ queryFn: async () => {
102
+ const clients = createAdminRpcClients(baseURL);
103
+ const res = await clients.warehouses[':id'].inventory.$get(
104
+ { param: { id: warehouseId } },
105
+ authHeaders(authToken)
106
+ );
107
+ if (!res.ok) throw new Error(`Failed to fetch warehouse inventory: ${res.statusText}`);
108
+ return res.json();
109
+ },
110
+ ...options,
111
+ });
112
+ }
113
+
114
+ /**
115
+ * Hook to delete a warehouse using admin RPC
116
+ */
117
+ export function useDeleteWarehouse(
118
+ warehouseId: string,
119
+ options?: UseMutationOptions<
120
+ Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['warehouses'][':id']['$delete']>>['json']>>,
121
+ Error,
122
+ void
123
+ >
124
+ ) {
125
+ const { baseURL, authToken } = useApiConfig();
126
+ const queryClient = useQueryClient();
127
+
128
+ return useMutation({
129
+ mutationFn: async () => {
130
+ const clients = createAdminRpcClients(baseURL);
131
+ const res = await clients.warehouses[':id'].$delete(
132
+ { param: { id: warehouseId } },
133
+ authHeaders(authToken)
134
+ );
135
+ if (!res.ok) throw new Error(`Failed to delete warehouse: ${res.statusText}`);
136
+ return res.json();
137
+ },
138
+ onSuccess: () => {
139
+ queryClient.invalidateQueries({ queryKey: queryKeys.admin.warehouses.all });
140
+ },
141
+ ...options,
142
+ });
143
+ }