m-oauth/hooks/useCreateApp.ts
2023-06-06 00:31:36 +08:00

54 lines
1.2 KiB
TypeScript

import { FormSchema } from "@/components/InputForm";
import { useCallback, useState } from "react";
import { AppEntry } from "@/lib/types";
type MError = {
error: string;
};
const useCreateApp = () => {
const [appEntry, setAppEntry] = useState<AppEntry>();
const createApp = useCallback(
async ({
instance,
website,
clientName,
redirectUris,
scopes,
}: FormSchema) => {
const app = {
website,
client_name: clientName,
redirect_uris: redirectUris,
scopes: scopes?.join(" "),
};
console.log("app,", app);
try {
let request = await fetch(`${instance}/api/v1/apps`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(app),
});
if (!request.ok || request.status === 424) {
throw new Error((await request.json()).error);
}
setAppEntry(await request.json());
} catch (error) {
throw new Error((error as MError).error);
}
},
[]
);
console.log(appEntry);
return {
appEntry,
createApp,
};
};
export default useCreateApp;