Files
2025-02-11 09:03:15 +09:00

46 lines
1.3 KiB
JavaScript

export class API {
constructor(settings) {
this.settings = settings;
}
updateSettings(newSettings) {
this.settings = newSettings;
}
createAuthHeader(username, password) {
return 'Basic ' + btoa(username + ':' + password);
}
createRequestHeaders() {
const headers = {
'Content-Type': 'application/json'
};
if (this.settings.username && this.settings.password) {
headers['Authorization'] = this.createAuthHeader(this.settings.username, this.settings.password);
}
return headers;
}
async getQueueStatus() {
const response = await fetch(`${this.settings.origin}queue`, {
headers: this.createRequestHeaders()
});
return await response.json();
}
async getHistory(maxItemCount = 64) {
const response = await fetch(`${this.settings.origin}api/history?max_items=${maxItemCount}`, {
headers: this.createRequestHeaders()
});
return await response.json();
}
async postPrompt(prompt) {
const response = await fetch(`${this.settings.origin}api/prompt`, {
method: 'POST',
headers: this.createRequestHeaders(),
body: JSON.stringify({ prompt })
});
return await response.json();
}
}