Spaces:
Build error
Build error
Esteves Enzo
commited on
Commit
·
03ce1cf
1
Parent(s):
741554a
add post function
Browse files- components/editor/main/endpoint.tsx +3 -1
- components/editor/main/hooks/useRequest.ts +27 -17
- components/editor/main/index.tsx +16 -3
- components/editor/main/request.tsx +54 -4
- utils/datas/api_collections.ts +40 -16
- utils/type.ts +9 -1
components/editor/main/endpoint.tsx
CHANGED
|
@@ -41,7 +41,9 @@ export const Endpoint = ({
|
|
| 41 |
onChange={(value) => handleChange(value, i)}
|
| 42 |
/>
|
| 43 |
) : (
|
| 44 |
-
<p className="text-slate-300">
|
|
|
|
|
|
|
| 45 |
);
|
| 46 |
})}
|
| 47 |
</div>
|
|
|
|
| 41 |
onChange={(value) => handleChange(value, i)}
|
| 42 |
/>
|
| 43 |
) : (
|
| 44 |
+
<p key={i} className="text-slate-300">
|
| 45 |
+
{p}
|
| 46 |
+
</p>
|
| 47 |
);
|
| 48 |
})}
|
| 49 |
</div>
|
components/editor/main/hooks/useRequest.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
| 1 |
import { useState } from "react"
|
| 2 |
|
| 3 |
import axios from "@/utils/axios";
|
| 4 |
-
import {
|
| 5 |
|
| 6 |
-
export const useRequest = (endpoint: string, params:
|
| 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(
|
| 16 |
-
([
|
| 17 |
value !== "" &&
|
| 18 |
value !== null &&
|
| 19 |
value !== undefined &&
|
|
@@ -24,19 +24,29 @@ export const useRequest = (endpoint: string, params: any) => {
|
|
| 24 |
});
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
};
|
| 41 |
|
| 42 |
return {
|
|
|
|
| 1 |
import { useState } from "react"
|
| 2 |
|
| 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(
|
| 16 |
+
([_, value]) =>
|
| 17 |
value !== "" &&
|
| 18 |
value !== null &&
|
| 19 |
value !== undefined &&
|
|
|
|
| 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) => {
|
| 41 |
+
if (res.ok) {
|
| 42 |
+
setData(res.data);
|
| 43 |
+
}
|
| 44 |
+
})
|
| 45 |
+
.catch((err) => {
|
| 46 |
+
setData(err.data);
|
| 47 |
+
})
|
| 48 |
+
.finally(() => setLoading(false));
|
| 49 |
+
|
| 50 |
};
|
| 51 |
|
| 52 |
return {
|
components/editor/main/index.tsx
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
"use client";
|
| 2 |
import { useState } from "react";
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import { ApiRoute } from "@/utils/type";
|
| 5 |
|
|
@@ -7,7 +9,6 @@ import { Endpoint } from "./endpoint";
|
|
| 7 |
import { Request } from "./request";
|
| 8 |
import { Response } from "./response";
|
| 9 |
import { useRequest } from "./hooks/useRequest";
|
| 10 |
-
import { useMount, useUpdateEffect } from "react-use";
|
| 11 |
|
| 12 |
export const EditorMain = ({ endpoint }: { endpoint: ApiRoute }) => {
|
| 13 |
const [formattedEndpoint, setFormattedEndpoint] = useState<string>(
|
|
@@ -16,9 +17,18 @@ export const EditorMain = ({ endpoint }: { endpoint: ApiRoute }) => {
|
|
| 16 |
const [formattedParameters, setFormattedParameters] = useState(
|
| 17 |
endpoint?.parameters ? { ...endpoint.parameters } : undefined
|
| 18 |
);
|
|
|
|
|
|
|
| 19 |
const { loading, submit, data } = useRequest(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
formattedEndpoint,
|
| 21 |
-
formattedParameters
|
|
|
|
| 22 |
);
|
| 23 |
|
| 24 |
useMount(() => {
|
|
@@ -37,12 +47,15 @@ export const EditorMain = ({ endpoint }: { endpoint: ApiRoute }) => {
|
|
| 37 |
<div className="h-full grid grid-cols-1 xl:grid-cols-3">
|
| 38 |
<Request
|
| 39 |
parameters={formattedParameters}
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
setFormattedParameters({
|
| 42 |
...formattedParameters,
|
| 43 |
[k]: v,
|
| 44 |
});
|
| 45 |
}}
|
|
|
|
| 46 |
>
|
| 47 |
<Endpoint endpoint={endpoint} onChange={setFormattedEndpoint}>
|
| 48 |
<button
|
|
|
|
| 1 |
"use client";
|
| 2 |
import { useState } from "react";
|
| 3 |
+
import { useMount } from "react-use";
|
| 4 |
+
import { Options } from "redaxios";
|
| 5 |
|
| 6 |
import { ApiRoute } from "@/utils/type";
|
| 7 |
|
|
|
|
| 9 |
import { Request } from "./request";
|
| 10 |
import { Response } from "./response";
|
| 11 |
import { useRequest } from "./hooks/useRequest";
|
|
|
|
| 12 |
|
| 13 |
export const EditorMain = ({ endpoint }: { endpoint: ApiRoute }) => {
|
| 14 |
const [formattedEndpoint, setFormattedEndpoint] = useState<string>(
|
|
|
|
| 17 |
const [formattedParameters, setFormattedParameters] = useState(
|
| 18 |
endpoint?.parameters ? { ...endpoint.parameters } : undefined
|
| 19 |
);
|
| 20 |
+
const [formattedBody, setFormattedBody] = useState<Options>();
|
| 21 |
+
|
| 22 |
const { loading, submit, data } = useRequest(
|
| 23 |
+
endpoint.method.toLocaleLowerCase() as
|
| 24 |
+
| "post"
|
| 25 |
+
| "put"
|
| 26 |
+
| "patch"
|
| 27 |
+
| "delete"
|
| 28 |
+
| "get",
|
| 29 |
formattedEndpoint,
|
| 30 |
+
formattedParameters,
|
| 31 |
+
formattedBody
|
| 32 |
);
|
| 33 |
|
| 34 |
useMount(() => {
|
|
|
|
| 47 |
<div className="h-full grid grid-cols-1 xl:grid-cols-3">
|
| 48 |
<Request
|
| 49 |
parameters={formattedParameters}
|
| 50 |
+
body={endpoint?.body}
|
| 51 |
+
formattedBody={formattedBody}
|
| 52 |
+
onParamsChange={(k: string, v: string | boolean) => {
|
| 53 |
setFormattedParameters({
|
| 54 |
...formattedParameters,
|
| 55 |
[k]: v,
|
| 56 |
});
|
| 57 |
}}
|
| 58 |
+
onBodyChange={(b: Options) => setFormattedBody(b)}
|
| 59 |
>
|
| 60 |
<Endpoint endpoint={endpoint} onChange={setFormattedEndpoint}>
|
| 61 |
<button
|
components/editor/main/request.tsx
CHANGED
|
@@ -1,21 +1,36 @@
|
|
| 1 |
"use client";
|
|
|
|
|
|
|
|
|
|
| 2 |
import { Toggle } from "@/components/input/toggle";
|
| 3 |
import { TextInput } from "@/components/input/input";
|
| 4 |
import { usePersistentState } from "@/utils/usePersistentState";
|
|
|
|
|
|
|
| 5 |
|
| 6 |
export const Request = ({
|
| 7 |
parameters,
|
|
|
|
|
|
|
|
|
|
| 8 |
children,
|
| 9 |
-
|
| 10 |
}: {
|
| 11 |
parameters: any;
|
| 12 |
children: React.ReactElement;
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
}) => {
|
| 15 |
const [headers, setHeaders] = usePersistentState("headers", {
|
| 16 |
Authorization: "hf_api_key",
|
| 17 |
});
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
return (
|
| 20 |
<div className="h-full bg-slate-900 px-4 xl:px-6 py-5">
|
| 21 |
{children}
|
|
@@ -35,7 +50,7 @@ export const Request = ({
|
|
| 35 |
<Toggle
|
| 36 |
checked={value}
|
| 37 |
label={key}
|
| 38 |
-
onChange={(e) =>
|
| 39 |
/>
|
| 40 |
</div>
|
| 41 |
) : (
|
|
@@ -43,7 +58,42 @@ export const Request = ({
|
|
| 43 |
value={value as string}
|
| 44 |
label={key}
|
| 45 |
placeholder="value"
|
| 46 |
-
onChange={(e) =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
/>
|
| 48 |
)}
|
| 49 |
</div>
|
|
|
|
| 1 |
"use client";
|
| 2 |
+
import { useState } from "react";
|
| 3 |
+
import { Options } from "redaxios";
|
| 4 |
+
|
| 5 |
import { Toggle } from "@/components/input/toggle";
|
| 6 |
import { TextInput } from "@/components/input/input";
|
| 7 |
import { usePersistentState } from "@/utils/usePersistentState";
|
| 8 |
+
import { Body } from "@/utils/type";
|
| 9 |
+
import { useUpdateEffect } from "react-use";
|
| 10 |
|
| 11 |
export const Request = ({
|
| 12 |
parameters,
|
| 13 |
+
body,
|
| 14 |
+
formattedBody,
|
| 15 |
+
onBodyChange,
|
| 16 |
children,
|
| 17 |
+
onParamsChange,
|
| 18 |
}: {
|
| 19 |
parameters: any;
|
| 20 |
children: React.ReactElement;
|
| 21 |
+
body: Array<Body> | undefined;
|
| 22 |
+
formattedBody: Options | undefined;
|
| 23 |
+
onBodyChange: (o: Options) => void;
|
| 24 |
+
onParamsChange: (key: string, value: string | boolean) => void;
|
| 25 |
}) => {
|
| 26 |
const [headers, setHeaders] = usePersistentState("headers", {
|
| 27 |
Authorization: "hf_api_key",
|
| 28 |
});
|
| 29 |
|
| 30 |
+
const [bodyForm, setBodyForm] = useState<Options>({});
|
| 31 |
+
|
| 32 |
+
useUpdateEffect(() => onBodyChange(bodyForm), [bodyForm]);
|
| 33 |
+
|
| 34 |
return (
|
| 35 |
<div className="h-full bg-slate-900 px-4 xl:px-6 py-5">
|
| 36 |
{children}
|
|
|
|
| 50 |
<Toggle
|
| 51 |
checked={value}
|
| 52 |
label={key}
|
| 53 |
+
onChange={(e) => onParamsChange(key, e)}
|
| 54 |
/>
|
| 55 |
</div>
|
| 56 |
) : (
|
|
|
|
| 58 |
value={value as string}
|
| 59 |
label={key}
|
| 60 |
placeholder="value"
|
| 61 |
+
onChange={(e) => onParamsChange(key, e)}
|
| 62 |
+
/>
|
| 63 |
+
)}
|
| 64 |
+
</div>
|
| 65 |
+
))}
|
| 66 |
+
</div>
|
| 67 |
+
)}
|
| 68 |
+
{body?.length && (
|
| 69 |
+
<div className="mt-6 grid grid-cols-2 gap-6 w-full">
|
| 70 |
+
<p className="text-slate-200 uppercase text-xs font-semibold col-span-2">
|
| 71 |
+
Body
|
| 72 |
+
</p>
|
| 73 |
+
{body?.length &&
|
| 74 |
+
body.map((b, key) => (
|
| 75 |
+
<div
|
| 76 |
+
key={key}
|
| 77 |
+
className="flex items-center justify-between gap-2"
|
| 78 |
+
>
|
| 79 |
+
{typeof b.defaultValue === "boolean" ? (
|
| 80 |
+
<div>
|
| 81 |
+
<Toggle
|
| 82 |
+
checked={b.defaultValue}
|
| 83 |
+
label={b.key}
|
| 84 |
+
onChange={(e) => setBodyForm({ ...bodyForm, [b.key]: e })}
|
| 85 |
+
/>
|
| 86 |
+
</div>
|
| 87 |
+
) : (
|
| 88 |
+
<TextInput
|
| 89 |
+
value={
|
| 90 |
+
(formattedBody?.[
|
| 91 |
+
b.key as keyof typeof formattedBody
|
| 92 |
+
] as string) ?? (b.defaultValue as string)
|
| 93 |
+
}
|
| 94 |
+
label={b.key}
|
| 95 |
+
placeholder="value"
|
| 96 |
+
onChange={(e) => setBodyForm({ ...bodyForm, [b.key]: e })}
|
| 97 |
/>
|
| 98 |
)}
|
| 99 |
</div>
|
utils/datas/api_collections.ts
CHANGED
|
@@ -70,22 +70,46 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
|
|
| 70 |
path: '/api/metrics'
|
| 71 |
}],
|
| 72 |
},
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
{
|
| 90 |
key: 'user',
|
| 91 |
endpoints: [{
|
|
|
|
| 70 |
path: '/api/metrics'
|
| 71 |
}],
|
| 72 |
},
|
| 73 |
+
{
|
| 74 |
+
key: 'repo',
|
| 75 |
+
endpoints: [{
|
| 76 |
+
method: 'POST',
|
| 77 |
+
path: '/api/repos/create',
|
| 78 |
+
body: [
|
| 79 |
+
{
|
| 80 |
+
label: "Type of repo (dataset or space; model by default)",
|
| 81 |
+
defaultValue: "model",
|
| 82 |
+
key: "type",
|
| 83 |
+
required: true,
|
| 84 |
+
},
|
| 85 |
+
{
|
| 86 |
+
label: "Name of repo",
|
| 87 |
+
key: "name",
|
| 88 |
+
required: true,
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
label: "Name of organization (optional)",
|
| 92 |
+
required: true,
|
| 93 |
+
key: "organization",
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
label: "Whether the repo is private",
|
| 97 |
+
required: true,
|
| 98 |
+
key: "private",
|
| 99 |
+
defaultValue: false,
|
| 100 |
+
},
|
| 101 |
+
]
|
| 102 |
+
}, {
|
| 103 |
+
method: 'DELETE',
|
| 104 |
+
path: '/api/repos/delete'
|
| 105 |
+
}, {
|
| 106 |
+
method: 'PUT',
|
| 107 |
+
path: '/api/repos/{repo_type}/{repo_id}/settings'
|
| 108 |
+
}, {
|
| 109 |
+
method: 'POST',
|
| 110 |
+
path: '/api/repos/move'
|
| 111 |
+
}],
|
| 112 |
+
},
|
| 113 |
{
|
| 114 |
key: 'user',
|
| 115 |
endpoints: [{
|
utils/type.ts
CHANGED
|
@@ -6,5 +6,13 @@ export interface ApiCollection {
|
|
| 6 |
export interface ApiRoute {
|
| 7 |
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
|
| 8 |
path: string,
|
| 9 |
-
parameters?: any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
|
|
|
| 6 |
export interface ApiRoute {
|
| 7 |
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
|
| 8 |
path: string,
|
| 9 |
+
parameters?: any,
|
| 10 |
+
body?: Array<Body>
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
export interface Body {
|
| 14 |
+
key: string
|
| 15 |
+
label: string
|
| 16 |
+
required?: boolean
|
| 17 |
+
defaultValue?: any
|
| 18 |
}
|