moxie-site/js/api.js
Z User 17bfefdbfa Add media section with YouTube Short embed before footer
- New 'See Moxie in Action' section with heading and subtitle
- Embedded YouTube Short (b4ly_hECE_I) in a responsive 16:9 container
- Decorative emerald glow effects and rounded card styling
- 'Watch on YouTube' link with YouTube icon below the embed
- Scroll-animate integration for reveal on scroll
2026-05-12 20:43:06 +00:00

178 lines
3.9 KiB
JavaScript
Executable File

/**
* Moxiegen API Client
* Handles all API calls to the backend with authentication
*/
class MoxieAPI {
constructor() {
this.baseUrl = CONFIG.api.baseUrl;
}
/**
* Make authenticated API request
*/
async request(endpoint, options = {}) {
const token = await moxieAuth.getToken();
const headers = {
'Content-Type': 'application/json',
...options.headers
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const url = endpoint.startsWith('http') ? endpoint : this.baseUrl + endpoint;
try {
const response = await fetch(url, {
...options,
headers
});
const data = await response.json();
if (!response.ok) {
throw new APIError(data.message || 'Request failed', response.status, data);
}
return data;
} catch (error) {
if (error instanceof APIError) {
throw error;
}
throw new APIError(error.message, 0, { error: error.message });
}
}
/**
* GET request
*/
async get(endpoint) {
return this.request(endpoint, { method: 'GET' });
}
/**
* POST request
*/
async post(endpoint, data) {
return this.request(endpoint, {
method: 'POST',
body: JSON.stringify(data)
});
}
/**
* PUT request
*/
async put(endpoint, data) {
return this.request(endpoint, {
method: 'PUT',
body: JSON.stringify(data)
});
}
/**
* DELETE request
*/
async delete(endpoint, data) {
return this.request(endpoint, {
method: 'DELETE',
body: data ? JSON.stringify(data) : undefined
});
}
// ============================================
// User Endpoints
// ============================================
/**
* Get current user profile
*/
async getProfile() {
return this.get(CONFIG.api.endpoints.me);
}
/**
* Update user profile
*/
async updateProfile(data) {
return this.put(CONFIG.api.endpoints.me, data);
}
/**
* Deactivate account
*/
async deactivateAccount() {
return this.delete(CONFIG.api.endpoints.me);
}
// ============================================
// Credits Endpoints
// ============================================
/**
* Get credits balance and history
*/
async getCredits(page = 1, limit = 10) {
return this.get(`${CONFIG.api.endpoints.credits}?page=${page}&limit=${limit}`);
}
// ============================================
// API Keys Endpoints
// ============================================
/**
* Get all API keys
*/
async getApiKeys() {
return this.get(CONFIG.api.endpoints.apiKeys);
}
/**
* Create new API key
*/
async createApiKey(name = 'API Key') {
return this.post(CONFIG.api.endpoints.apiKeys, { name });
}
/**
* Revoke API key
*/
async revokeApiKey(keyId) {
return this.delete(`${CONFIG.api.endpoints.apiKeys}/${keyId}`);
}
// ============================================
// Health Check
// ============================================
/**
* Check API health
*/
async checkHealth() {
return this.get(CONFIG.api.endpoints.health);
}
}
/**
* Custom API Error class
*/
class APIError extends Error {
constructor(message, status, data) {
super(message);
this.name = 'APIError';
this.status = status;
this.data = data;
}
}
// Create singleton instance
const moxieAPI = new MoxieAPI();
// Export for use in other modules
window.moxieAPI = moxieAPI;
window.APIError = APIError;