2023-04-09 16:20:43 +00:00
|
|
|
import { Client } from "@hibas123/openauth-internalapi";
|
|
|
|
import request, { RequestError } from "./request";
|
|
|
|
|
|
|
|
const provider = new Client.ServiceProvider((data) => {
|
2023-04-14 13:13:53 +00:00
|
|
|
fetch("/api/jrpc", {
|
|
|
|
method: "POST",
|
|
|
|
credentials: "same-origin",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
}).then(res => {
|
|
|
|
if (res.ok) return res.json();
|
|
|
|
else throw new Error(res.statusText);
|
|
|
|
}).then(res => {
|
|
|
|
provider.onPacket(res);
|
2023-04-09 16:20:43 +00:00
|
|
|
}).catch(err => {
|
2023-04-14 13:13:53 +00:00
|
|
|
provider.onPacket({
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
method: data.method,
|
|
|
|
id: data.id,
|
|
|
|
error: {
|
|
|
|
code: -32603,
|
|
|
|
message: err.message,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2023-04-09 16:20:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const InternalAPI = {
|
|
|
|
Account: new Client.AccountService(provider),
|
|
|
|
Security: new Client.SecurityService(provider),
|
2023-04-14 13:13:53 +00:00
|
|
|
TwoFactor: new Client.TFAService(provider),
|
|
|
|
Login: new Client.LoginService(provider),
|
2023-04-09 16:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default InternalAPI;
|
2023-04-14 13:13:53 +00:00
|
|
|
|
|
|
|
(window as any).InternalAPI = InternalAPI;
|