-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuseDataSourceMutations.ts
More file actions
99 lines (84 loc) · 2.61 KB
/
Copy pathuseDataSourceMutations.ts
File metadata and controls
99 lines (84 loc) · 2.61 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
import { useMutation, useQueryClient } from "react-query";
interface ApplyDataSourcePayload {
name: string;
project: string;
type?: number;
timestamp_field?: string;
created_timestamp_column?: string;
description?: string;
tags?: Record<string, string>;
owner?: string;
file_options?: { uri: string };
bigquery_options?: { table: string; query: string };
snowflake_options?: { table: string; database: string; schema_: string };
redshift_options?: { table: string; database: string; schema_: string };
kafka_options?: { kafka_bootstrap_servers: string; topic: string };
spark_options?: { table: string; path: string };
}
interface DeleteDataSourcePayload {
name: string;
project: string;
}
interface MutationResult {
name: string;
project: string;
status: string;
}
const API_BASE = "/api/v1";
const applyDataSource = async (
payload: ApplyDataSourcePayload,
): Promise<MutationResult> => {
const response = await fetch(`${API_BASE}/data_sources`, {
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 data source: ${response.status}`,
);
}
return response.json();
};
const deleteDataSource = async (
payload: DeleteDataSourcePayload,
): Promise<MutationResult> => {
const response = await fetch(
`${API_BASE}/data_sources/${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 data source: ${response.status}`,
);
}
return response.json();
};
const useApplyDataSource = () => {
const queryClient = useQueryClient();
return useMutation(applyDataSource, {
onSuccess: () => {
queryClient.invalidateQueries(["rest"]);
queryClient.invalidateQueries(["data-sources-rest"]);
queryClient.invalidateQueries(["data-source-rest"]);
},
});
};
const useDeleteDataSource = () => {
const queryClient = useQueryClient();
return useMutation(deleteDataSource, {
onSuccess: () => {
queryClient.invalidateQueries(["rest"]);
queryClient.invalidateQueries(["data-sources-rest"]);
queryClient.invalidateQueries(["data-source-rest"]);
},
});
};
export { useApplyDataSource, useDeleteDataSource };
export type { ApplyDataSourcePayload, DeleteDataSourcePayload };