-
Notifications
You must be signed in to change notification settings - Fork 29
/
api.ts
114 lines (110 loc) · 3.38 KB
/
api.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { API_BASE_PATH } from "./const";
import {
Service,
FullFlow,
Signature,
TickInfo,
Flow,
FlowsQuery,
} from "./types";
export const tulipApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: API_BASE_PATH }),
endpoints: (builder) => ({
getServices: builder.query<Service[], void>({
query: () => "/services",
}),
getFlow: builder.query<FullFlow, string>({
query: (id) => `/flow/${id}`,
}),
getFlows: builder.query<Flow[], FlowsQuery>({
query: (query) => ({
url: `/query`,
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
// TODO: fix the below tags mutation (make backend handle empty tags!)
// Diederik gives you a beer once this has been fixed
body: JSON.stringify({
...query,
tags: query.tags.length > 0 ? query.tags : undefined,
}),
}),
}),
getTags: builder.query<string[], void>({
query: () => `/tags`,
}),
getTickInfo: builder.query<TickInfo, void>({
query: () => `/tick_info`,
}),
getSignature: builder.query<Signature[], number>({
query: (id) => `/signature/${id}`,
}),
toPwnTools: builder.query<string, string>({
query: (id) => ({ url: `/to_pwn/${id}`, responseHandler: "text" }),
}),
toSinglePythonRequest: builder.query<
string,
{ body: string; id: string; tokenize: boolean }
>({
query: ({ body, id, tokenize }) => ({
url: `/to_single_python_request?tokenize/${
tokenize ? "1" : "0"
}&id=${id}`,
method: "POST",
responseHandler: "text",
headers: {
"Content-Type": "text/plain;charset=UTF-8",
},
body,
}),
}),
toFullPythonRequest: builder.query<string, string>({
query: (id) => ({
url: `/to_python_request/${id}`,
responseHandler: "text",
}),
}),
starFlow: builder.mutation<unknown, { id: string; star: boolean }>({
query: ({ id, star }) => `/star/${id}/${star ? "1" : "0"}`,
// TODO: optimistic cache update
// async onQueryStarted({ id, star }, { dispatch, queryFulfilled }) {
// // `updateQueryData` requires the endpoint name and cache key arguments,
// // so it knows which piece of cache state to update
// const patchResult = dispatch(
// tulipApi.util.updateQueryData("getFlows", undefined, (flows) => {
// // The `flows` is Immer-wrapped and can be "mutated" like in createSlice
// const flow = flows.find((flow) => flow._id.$oid === id);
// if (flow) {
// if (star) {
// flow.tags.push("starred");
// } else {
// flow.tags = flow.tags.filter((tag) => tag != "starred");
// }
// }
// })
// );
// try {
// await queryFulfilled;
// } catch {
// patchResult.undo();
// }
// },
}),
}),
});
export const {
useGetServicesQuery,
useGetFlowQuery,
useGetFlowsQuery,
useLazyGetFlowsQuery,
useGetTagsQuery,
useGetSignatureQuery,
useGetTickInfoQuery,
useLazyToPwnToolsQuery,
useLazyToFullPythonRequestQuery,
useToSinglePythonRequestQuery,
useStarFlowMutation,
} = tulipApi;