-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuseFeatureViewMutations.ts
More file actions
101 lines (85 loc) · 2.41 KB
/
Copy pathuseFeatureViewMutations.ts
File metadata and controls
101 lines (85 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { useMutation, useQueryClient } from "react-query";
interface FeaturePayload {
name: string;
value_type: number;
description?: string;
}
interface ApplyFeatureViewPayload {
name: string;
project: string;
entities?: string[];
features?: FeaturePayload[];
batch_source?: string;
ttl_seconds?: number;
online?: boolean;
description?: string;
tags?: Record<string, string>;
owner?: string;
}
interface DeleteFeatureViewPayload {
name: string;
project: string;
}
interface MutationResult {
name: string;
project: string;
status: string;
}
const API_BASE = "/api/v1";
const applyFeatureView = async (
payload: ApplyFeatureViewPayload,
): Promise<MutationResult> => {
const response = await fetch(`${API_BASE}/feature_views`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!response.ok) {
const error = await response
.json()
.catch(() => ({ detail: response.statusText }));
throw new Error(
error.detail || `Failed to apply feature view: ${response.status}`,
);
}
return response.json();
};
const deleteFeatureView = async (
payload: DeleteFeatureViewPayload,
): Promise<MutationResult> => {
const response = await fetch(
`${API_BASE}/feature_views/${encodeURIComponent(payload.name)}?project=${encodeURIComponent(payload.project)}`,
{ method: "DELETE" },
);
if (!response.ok) {
const error = await response
.json()
.catch(() => ({ detail: response.statusText }));
throw new Error(
error.detail || `Failed to delete feature view: ${response.status}`,
);
}
return response.json();
};
const useApplyFeatureView = () => {
const queryClient = useQueryClient();
return useMutation(applyFeatureView, {
onSuccess: () => {
queryClient.invalidateQueries(["rest"]);
queryClient.invalidateQueries(["feature-views-rest"]);
queryClient.invalidateQueries(["feature-view-rest"]);
},
});
};
const useDeleteFeatureView = () => {
const queryClient = useQueryClient();
return useMutation(deleteFeatureView, {
onSuccess: () => {
queryClient.invalidateQueries(["rest"]);
queryClient.invalidateQueries(["feature-views-rest"]);
queryClient.invalidateQueries(["feature-view-rest"]);
},
});
};
export { useApplyFeatureView, useDeleteFeatureView };
export type { ApplyFeatureViewPayload, DeleteFeatureViewPayload };