Spaces:
Sleeping
Sleeping
Esteves Enzo
commited on
Commit
•
1ce932e
1
Parent(s):
570e2d9
rework endpoint behaviour
Browse files
components/editor/main/endpoint.tsx
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import { useMemo } from "react";
|
2 |
-
import classNames from "classnames";
|
3 |
|
4 |
import { Method } from "@/components/method";
|
5 |
import { splitStringBracket } from "@/utils";
|
@@ -7,42 +6,43 @@ import { ApiRoute } from "@/utils/type";
|
|
7 |
import { Parameter } from "./parameter";
|
8 |
|
9 |
export const Endpoint = ({
|
10 |
-
|
|
|
|
|
11 |
children,
|
12 |
onChange,
|
13 |
}: {
|
14 |
-
|
|
|
|
|
15 |
children: React.ReactElement;
|
16 |
onChange: (value: string) => void;
|
17 |
}) => {
|
18 |
const path_formatted = useMemo(
|
19 |
-
() => splitStringBracket(
|
20 |
-
[
|
21 |
);
|
22 |
|
23 |
-
const handleChange = (value: string,
|
24 |
-
|
25 |
-
currentString[index] = value;
|
26 |
-
onChange(currentString.join(""));
|
27 |
};
|
28 |
|
29 |
return (
|
30 |
<div className="bg-slate-900 w-full">
|
31 |
<div className="bg-slate-950/50 p-3 rounded-lg flex items-center justify-between">
|
32 |
<div className="text-white text-sm flex items-center justify-start gap-2 w-full">
|
33 |
-
<Method method={
|
34 |
<div className="flex items-center justify-start gap-1">
|
35 |
{path_formatted.map((p, i) => {
|
36 |
-
|
37 |
-
return isCustomizable ? (
|
38 |
<Parameter
|
39 |
key={i}
|
40 |
-
value={p}
|
41 |
-
onChange={(value) => handleChange(value,
|
42 |
/>
|
43 |
) : (
|
44 |
<p key={i} className="text-slate-300">
|
45 |
-
{p}
|
46 |
</p>
|
47 |
);
|
48 |
})}
|
|
|
1 |
import { useMemo } from "react";
|
|
|
2 |
|
3 |
import { Method } from "@/components/method";
|
4 |
import { splitStringBracket } from "@/utils";
|
|
|
6 |
import { Parameter } from "./parameter";
|
7 |
|
8 |
export const Endpoint = ({
|
9 |
+
method,
|
10 |
+
path,
|
11 |
+
initialPath,
|
12 |
children,
|
13 |
onChange,
|
14 |
}: {
|
15 |
+
method: ApiRoute["method"];
|
16 |
+
path: string;
|
17 |
+
initialPath: ApiRoute["path"];
|
18 |
children: React.ReactElement;
|
19 |
onChange: (value: string) => void;
|
20 |
}) => {
|
21 |
const path_formatted = useMemo(
|
22 |
+
() => splitStringBracket(initialPath),
|
23 |
+
[initialPath]
|
24 |
);
|
25 |
|
26 |
+
const handleChange = (value: string, key: string) => {
|
27 |
+
onChange(path.replace(key, value));
|
|
|
|
|
28 |
};
|
29 |
|
30 |
return (
|
31 |
<div className="bg-slate-900 w-full">
|
32 |
<div className="bg-slate-950/50 p-3 rounded-lg flex items-center justify-between">
|
33 |
<div className="text-white text-sm flex items-center justify-start gap-2 w-full">
|
34 |
+
<Method method={method} />
|
35 |
<div className="flex items-center justify-start gap-1">
|
36 |
{path_formatted.map((p, i) => {
|
37 |
+
return p.editable ? (
|
|
|
38 |
<Parameter
|
39 |
key={i}
|
40 |
+
value={p.content}
|
41 |
+
onChange={(value) => handleChange(value, p.key)}
|
42 |
/>
|
43 |
) : (
|
44 |
<p key={i} className="text-slate-300">
|
45 |
+
{p.content}
|
46 |
</p>
|
47 |
);
|
48 |
})}
|
components/editor/main/hooks/useRequest.ts
CHANGED
@@ -3,13 +3,15 @@ import { useState } from "react"
|
|
3 |
import axios from "@/utils/axios";
|
4 |
import { Options } from "redaxios";
|
5 |
|
6 |
-
export const useRequest = (method: "post" | "put" | "patch" | "delete" | "get", endpoint: string, params: Options, body: Options | undefined) => {
|
7 |
const [loading, setLoading] = useState<boolean>(false)
|
8 |
const [data, setData] = useState<any>(null)
|
9 |
|
10 |
const submit = async () => {
|
|
|
11 |
setLoading(true);
|
12 |
|
|
|
13 |
const url = new URL(endpoint, process.env.NEXT_PUBLIC_APP_APIURL);
|
14 |
if (params) {
|
15 |
const parameters = Object.entries(params).filter(
|
@@ -24,17 +26,10 @@ export const useRequest = (method: "post" | "put" | "patch" | "delete" | "get",
|
|
24 |
});
|
25 |
}
|
26 |
|
27 |
-
console.log(
|
28 |
-
"Requesting",
|
29 |
-
method,
|
30 |
-
url.pathname,
|
31 |
-
url.searchParams.toString(),
|
32 |
-
body
|
33 |
-
)
|
34 |
-
|
35 |
const needBody = ["post", "put", "patch"].includes(method);
|
36 |
|
37 |
axios[method](url.pathname, needBody ? body : {
|
|
|
38 |
params: url.searchParams
|
39 |
})
|
40 |
.then((res: any) => {
|
|
|
3 |
import axios from "@/utils/axios";
|
4 |
import { Options } from "redaxios";
|
5 |
|
6 |
+
export const useRequest = (method: "post" | "put" | "patch" | "delete" | "get", endpoint: string | undefined, params: Options, body: Options | undefined) => {
|
7 |
const [loading, setLoading] = useState<boolean>(false)
|
8 |
const [data, setData] = useState<any>(null)
|
9 |
|
10 |
const submit = async () => {
|
11 |
+
if (!endpoint) return;
|
12 |
setLoading(true);
|
13 |
|
14 |
+
|
15 |
const url = new URL(endpoint, process.env.NEXT_PUBLIC_APP_APIURL);
|
16 |
if (params) {
|
17 |
const parameters = Object.entries(params).filter(
|
|
|
26 |
});
|
27 |
}
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
const needBody = ["post", "put", "patch"].includes(method);
|
30 |
|
31 |
axios[method](url.pathname, needBody ? body : {
|
32 |
+
data: method === "delete" ? body : undefined,
|
33 |
params: url.searchParams
|
34 |
})
|
35 |
.then((res: any) => {
|
components/editor/main/index.tsx
CHANGED
@@ -57,7 +57,12 @@ export const EditorMain = ({ endpoint }: { endpoint: ApiRoute }) => {
|
|
57 |
}}
|
58 |
onBodyChange={(b: Options) => setFormattedBody(b)}
|
59 |
>
|
60 |
-
<Endpoint
|
|
|
|
|
|
|
|
|
|
|
61 |
<button
|
62 |
className="bg-indigo-500 hover:bg-indigo-500/80 text-white px-3 py-1 rounded-lg text-sm"
|
63 |
onClick={submit}
|
|
|
57 |
}}
|
58 |
onBodyChange={(b: Options) => setFormattedBody(b)}
|
59 |
>
|
60 |
+
<Endpoint
|
61 |
+
path={formattedEndpoint}
|
62 |
+
initialPath={endpoint.path}
|
63 |
+
method={endpoint.method}
|
64 |
+
onChange={setFormattedEndpoint}
|
65 |
+
>
|
66 |
<button
|
67 |
className="bg-indigo-500 hover:bg-indigo-500/80 text-white px-3 py-1 rounded-lg text-sm"
|
68 |
onClick={submit}
|
utils/datas/api_collections.ts
CHANGED
@@ -105,13 +105,36 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
|
|
105 |
]
|
106 |
}, {
|
107 |
method: 'DELETE',
|
108 |
-
path: '/api/repos/delete'
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
}, {
|
113 |
method: 'POST',
|
114 |
-
path: '/api/repos/move'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
}],
|
116 |
},
|
117 |
{
|
|
|
105 |
]
|
106 |
}, {
|
107 |
method: 'DELETE',
|
108 |
+
path: '/api/repos/delete',
|
109 |
+
body: [{
|
110 |
+
label: "Type of repo (dataset or space; model by default)",
|
111 |
+
defaultValue: "model",
|
112 |
+
key: "type",
|
113 |
+
required: true,
|
114 |
+
},
|
115 |
+
{
|
116 |
+
label: "Name of repo",
|
117 |
+
key: "name",
|
118 |
+
required: true,
|
119 |
+
},
|
120 |
+
{
|
121 |
+
label: "Name of organization (optional)",
|
122 |
+
required: true,
|
123 |
+
key: "organization",
|
124 |
+
}]
|
125 |
}, {
|
126 |
method: 'POST',
|
127 |
+
path: '/api/repos/move',
|
128 |
+
body: [{
|
129 |
+
label: "From which repo",
|
130 |
+
key: "fromRepo",
|
131 |
+
required: true,
|
132 |
+
},
|
133 |
+
{
|
134 |
+
label: "To which repo",
|
135 |
+
key: "toRepo",
|
136 |
+
required: true,
|
137 |
+
}]
|
138 |
}],
|
139 |
},
|
140 |
{
|
utils/index.ts
CHANGED
@@ -1,5 +1,16 @@
|
|
1 |
-
export const splitStringBracket = (str: string):
|
2 |
// Split string by bracket but keep the bracket
|
3 |
const result = str.split(/(\{.*?\})/g)
|
4 |
-
return result.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
}
|
|
|
1 |
+
export const splitStringBracket = (str: string): any[] => {
|
2 |
// Split string by bracket but keep the bracket
|
3 |
const result = str.split(/(\{.*?\})/g)
|
4 |
+
return result.map((item) => {
|
5 |
+
if (item.startsWith('{') && item.endsWith('}')) {
|
6 |
+
return {
|
7 |
+
editable: true,
|
8 |
+
content: item.slice(1, -1),
|
9 |
+
key: item,
|
10 |
+
}
|
11 |
+
} return {
|
12 |
+
editable: false,
|
13 |
+
content: item
|
14 |
+
}
|
15 |
+
})
|
16 |
}
|