Merge pull request #58 from devchat-ai/usages

Handle address not fetched on initialization
This commit is contained in:
Tim 2024-06-11 18:40:07 +08:00 committed by GitHub
commit 2bfcc6cc30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -44,8 +44,7 @@ class APIUtil {
console.log('Webapp url: ', href) console.log('Webapp url: ', href)
return href; return href;
} catch (err) { } catch (err) {
console.error("Error fetch webapp url:", err); throw(`Error fetch webapp url: ${err}`)
return "https://app.devchat.ai";
} }
} }
@ -55,6 +54,8 @@ class APIUtil {
if (!this.webappUrl) { if (!this.webappUrl) {
this.fetchWebappUrl().then(url => { this.fetchWebappUrl().then(url => {
this.webappUrl = url; this.webappUrl = url;
}).catch(err => {
console.error(err);
}) })
} }
} }
@ -62,6 +63,7 @@ class APIUtil {
async createMessage(message: object) { async createMessage(message: object) {
this.currentMessageId = `msg-${uuidv4()}`; this.currentMessageId = `msg-${uuidv4()}`;
try { try {
if (!this.webappUrl) this.webappUrl = await this.fetchWebappUrl();
const res = await axios.post( const res = await axios.post(
`${this.webappUrl}/api/v1/messages`, `${this.webappUrl}/api/v1/messages`,
{...message, message_id: this.currentMessageId}, {...message, message_id: this.currentMessageId},
@ -78,6 +80,7 @@ class APIUtil {
async createEvent(event: object) { async createEvent(event: object) {
try { try {
if (!this.webappUrl) this.webappUrl = await this.fetchWebappUrl();
const res = await axios.post( const res = await axios.post(
`${this.webappUrl}/api/v1/messages/${this.currentMessageId}/events`, `${this.webappUrl}/api/v1/messages/${this.currentMessageId}/events`,
event, event,