webapp api address management

This commit is contained in:
Luo Tim 2024-06-08 03:19:05 +08:00
parent b3a18fe429
commit 43e5ceade3
4 changed files with 66 additions and 50 deletions

View File

@ -5,6 +5,7 @@ class APIUtil {
private static instance: APIUtil;
private baseUrl: string | undefined;
private accessKey: string | undefined;
private webappUrl: string | undefined;
private currentMessageId: string | undefined;
@ -18,17 +19,51 @@ class APIUtil {
}
return APIUtil.instance;
}
async fetchWebappUrl() {
try {
const res = await axios.get(
`${this.baseUrl}/addresses/webapp`,
{ headers: { 'Authorization': `Bearer ${this.accessKey}` }}
)
const urlOrPath = res?.data;
if (!urlOrPath) {
throw new Error("No webapp url found");
}
let href = "";
if (urlOrPath.startsWith("http://") || urlOrPath.startsWith("https://")) {
href = urlOrPath;
} else {
href = new URL(urlOrPath, this.baseUrl).href
}
if (href.endsWith('/')) {
href = href.slice(0, -1);
}
if (href.endsWith('/api')) {
href = href.slice(0, -4);
}
console.log('Webapp url: ', href)
return href;
} catch (err) {
console.error("Error fetch webapp url:", err);
return "https://app.devchat.ai";
}
}
config(baseUrl: string, accessKey: string) {
this.baseUrl = baseUrl;
this.accessKey = accessKey;
if (!this.webappUrl) {
this.fetchWebappUrl().then(url => {
this.webappUrl = url;
})
}
}
async createMessage(message: object) {
this.currentMessageId = `msg-${uuidv4()}`;
try {
const res = await axios.post(
`${this.baseUrl}/api/v1/messages`,
`${this.webappUrl}/api/v1/messages`,
{...message, message_id: this.currentMessageId},
{ headers: {
Authorization: `Bearer ${this.accessKey}`,
@ -44,7 +79,7 @@ class APIUtil {
async createEvent(event: object) {
try {
const res = await axios.post(
`${this.baseUrl}/api/v1/messages/${this.currentMessageId}/events`,
`${this.webappUrl}/api/v1/messages/${this.currentMessageId}/events`,
event,
{headers: {
Authorization: `Bearer ${this.accessKey}`,
@ -56,6 +91,21 @@ class APIUtil {
console.error(err);
}
}
async getBalance() {
try {
if (!this.webappUrl) this.webappUrl = await this.fetchWebappUrl();
const res = await axios.get(
`${this.webappUrl}/api/v1/users/profile`,
{headers: { Authorization: `Bearer ${this.accessKey}` }}
)
return res?.data?.organization
} catch(err) {
console.error(err);
return null;
}
}
}
export default APIUtil.getInstance();

View File

@ -29,7 +29,7 @@ export default function App() {
MessageUtil.registerHandler("readConfig", (data: { value: any }) => {
console.log("readConfig registerHandler: ", data);
config.setConfig(data.value);
APIUtil.config(config.getAppURL(), config.getUserKey())
APIUtil.config(config.getAPIBase(), config.getUserKey())
config.refreshModelList();
setReady(true);
});

View File

@ -1,5 +1,4 @@
import React, { useEffect, useState } from "react";
import axios from "axios";
import messageUtil from "@/util/MessageUtil";
import { IconWallet } from "@tabler/icons-react";
import {
@ -11,6 +10,7 @@ import {
} from "@mantine/core";
import { Trans } from "react-i18next";
import { useMst } from "@/views/stores/RootStore";
import APIUtil from "@/util/APIUtil";
const currencyMap = {
USD: "$",
@ -28,15 +28,9 @@ function formatCurrency(balance: number | null, currency: string) {
return `${currencyMap[currency] || currency}${balance}`;
}
const envMap = {
dev: {
requestUrl: "https://apptest.devchat.ai",
link: "https://webtest.devchat.ai",
},
prod: {
requestUrl: "https://app.devchat.ai",
link: "https://web.devchat.ai",
},
const links = {
dev: "https://webtest.devchat.ai",
prod: "https://web.devchat.ai",
};
export default function WechatTip() {
@ -50,23 +44,13 @@ export default function WechatTip() {
const platform = process.env.platform;
const getBalance = () => {
if (!envMap[env].requestUrl || !accessKey) {
return;
}
setLoading(true);
axios
.get(`${envMap[env].requestUrl}/api/v1/users/profile`, {
headers: { Authorization: `Bearer ${accessKey}` },
})
.then((res) => {
if (res?.data?.organization?.balance) {
setBalance(formatBalance(res?.data?.organization?.balance));
setCurrency(res?.data?.organization?.currency);
}
})
.finally(() => {
setLoading(false);
});
APIUtil.getBalance().then(org => {
setLoading(true);
setBalance(formatBalance(org?.balance));
setCurrency(org?.currency);
}).finally(() => {
setLoading(false);
})
};
useEffect(() => {
@ -93,7 +77,7 @@ export default function WechatTip() {
e.stopPropagation();
messageUtil.sendMessage({
command: "openLink",
url: envMap[env].link,
url: links[env],
});
};
@ -139,7 +123,7 @@ export default function WechatTip() {
web.devchat.ai{" "}
</Text>
) : (
<a href={envMap[env].link} target="_blank">
<a href={links[env]} target="_blank">
web.devchat.ai{" "}
</a>
)}

View File

@ -2,7 +2,6 @@ import MessageUtil from "@/util/MessageUtil";
import { types, Instance, flow } from "mobx-state-tree";
import modelsTemplate from "@/models";
import cloneDeep from "lodash.clonedeep";
import { set } from "mobx";
import axios from "axios";
const defaultAPIBase = [
@ -113,23 +112,6 @@ export const ConfigStore = types
}
return "";
},
getAppURL: function() {
return "http://localhost:8001"
const apiBase = this.getAPIBase();
if (apiBase.includes("api-test.devchat.ai")) {
return "https://apptest.devchat.ai";
} else {
return "https://app.devchat.ai";
}
},
getWebURL: function() {
const apiBase = this.getAPIBase();
if (apiBase.includes("api-test.devchat.ai")) {
return "https://webtest.devchat.ai";
} else {
return "https://web.devchat.ai";
}
},
setConfig: function (data) {
this.updateSettle(false);
let needUpdate = false;