aura-glass
Version:
A comprehensive glassmorphism design system for React applications with 142+ production-ready components
222 lines (220 loc) • 5.65 kB
JavaScript
/**
* AuraGlass AI API Client
*
* Easy-to-use client for accessing production AI features from React components.
* Handles authentication, error handling, and request/response formatting.
*/
class AIClient {
constructor(config = {}) {
this.authToken = null;
this.config = {
apiUrl: config.apiUrl || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001',
wsUrl: config.wsUrl || process.env.NEXT_PUBLIC_WS_URL || 'ws://localhost:3002',
getAuthToken: config.getAuthToken || (() => Promise.resolve(this.authToken)),
onError: config.onError || (error => console.error('AI Client Error:', error))
};
}
/**
* Set authentication token for API requests
*/
setAuthToken(token) {
this.authToken = token;
}
/**
* Make authenticated API request
*/
async request(endpoint, options = {}) {
try {
const token = await this.config.getAuthToken();
const url = `${this.config.apiUrl}${endpoint}`;
const headers = {
'Content-Type': 'application/json',
...options.headers
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const response = await fetch(url, {
...options,
headers
});
if (!response.ok) {
const error = await response.json().catch(() => ({
error: response.statusText
}));
throw new Error(error.error || error.message || 'Request failed');
}
return await response.json();
} catch (error) {
this.config.onError(error);
throw error;
}
}
// ============================================
// Authentication Methods
// ============================================
/**
* Login with email and password
*/
async login(email, password) {
const result = await this.request('/api/auth/login', {
method: 'POST',
body: JSON.stringify({
email,
password
})
});
this.authToken = result.token;
return result;
}
/**
* Register new user
*/
async register(email, password, name) {
const result = await this.request('/api/auth/register', {
method: 'POST',
body: JSON.stringify({
email,
password,
name
})
});
this.authToken = result.token;
return result;
}
/**
* Refresh authentication token
*/
async refreshToken(refreshToken) {
const result = await this.request('/api/auth/refresh', {
method: 'POST',
body: JSON.stringify({
refreshToken
})
});
this.authToken = result.token;
return result;
}
/**
* Logout current user
*/
async logout() {
await this.request('/api/auth/logout', {
method: 'POST'
});
this.authToken = null;
}
// ============================================
// AI Methods
// ============================================
/**
* Generate smart form fields based on context
*
* @example
* const fields = await client.generateFormFields('user registration form');
*/
async generateFormFields(context, existingFields = []) {
const result = await this.request('/api/ai/generate-form', {
method: 'POST',
body: JSON.stringify({
context,
existingFields
})
});
return result.fields;
}
/**
* Perform semantic search with AI-enhanced query
*
* @example
* const results = await client.search('how to add glassmorphism', { limit: 10 });
*/
async search(query, options = {}) {
return await this.request('/api/ai/search', {
method: 'POST',
body: JSON.stringify({
query,
options
})
});
}
/**
* Index documents for semantic search
*
* @example
* await client.indexDocuments([
* { id: '1', content: 'Document content...', title: 'Doc 1' }
* ]);
*/
async indexDocuments(documents) {
return await this.request('/api/ai/index-documents', {
method: 'POST',
body: JSON.stringify({
documents
})
});
}
/**
* Analyze image with Google Vision API
*
* @example
* const analysis = await client.analyzeImage(base64Image, ['faces', 'objects']);
*/
async analyzeImage(imageData, analysisTypes = ['all']) {
const result = await this.request('/api/ai/analyze-image', {
method: 'POST',
body: JSON.stringify({
image: imageData,
analysisTypes
})
});
return result.analysis;
}
/**
* Remove background from image
*
* @example
* const processedImage = await client.removeBackground(base64Image);
*/
async removeBackground(imageData) {
const result = await this.request('/api/ai/remove-background', {
method: 'POST',
body: JSON.stringify({
image: imageData
})
});
return result.image;
}
/**
* Generate content summary
*
* @example
* const summary = await client.summarize(longText, 200);
*/
async summarize(content, maxLength = 200) {
const result = await this.request('/api/ai/summarize', {
method: 'POST',
body: JSON.stringify({
content,
maxLength
})
});
return result.summary;
}
/**
* Check server health
*/
async healthCheck() {
// Health check doesn't require authentication
const url = `${this.config.apiUrl}/health`;
const response = await fetch(url);
if (!response.ok) {
throw new Error('Health check failed');
}
return await response.json();
}
}
// Export singleton instance
const aiClient = new AIClient();
export { aiClient, AIClient as default };
//# sourceMappingURL=ai-client.js.map