Skip to content

Commit a1b992a

Browse files
committed
Add API key management methods and standardize API endpoint usage
1 parent e3eb91c commit a1b992a

File tree

2 files changed

+152
-11
lines changed

2 files changed

+152
-11
lines changed

public/js/api-client.js

Lines changed: 132 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,6 @@ export class ApiClient {
153153
return await response.json();
154154
}
155155

156-
/**
157-
* Create a new subscription
158-
* @param {string} email - User email
159-
* @param {string} plan - Subscription plan (monthly, yearly)
160-
* @param {string} coin - Cryptocurrency code (btc, eth, sol, usdc)
161-
* @returns {Promise<Object>} - Subscription details
162-
*/
163156
/**
164157
* Create a new subscription
165158
* @param {string} email - User email
@@ -287,4 +280,136 @@ export class ApiClient {
287280
document.body.removeChild(a);
288281
URL.revokeObjectURL(url);
289282
}
283+
284+
/**
285+
* API Key Management
286+
*/
287+
288+
/**
289+
* Get API keys
290+
* @returns {Promise<Object>} - API keys
291+
*/
292+
static async getApiKeys() {
293+
const url = `${this.baseUrl}/api-keys`;
294+
295+
const headers = {
296+
'Accept': 'application/json',
297+
};
298+
299+
// Add Authorization header if API key exists
300+
const apiKey = localStorage.getItem('api_key');
301+
if (apiKey) {
302+
headers['Authorization'] = `Bearer ${apiKey}`;
303+
}
304+
305+
const response = await fetch(url, {
306+
method: 'GET',
307+
headers
308+
});
309+
310+
if (!response.ok) {
311+
const errorData = await response.json();
312+
throw new Error(errorData.error || response.statusText);
313+
}
314+
315+
return await response.json();
316+
}
317+
318+
/**
319+
* Create a new API key
320+
* @param {string} name - API key name
321+
* @returns {Promise<Object>} - New API key
322+
*/
323+
static async createApiKey(name) {
324+
const url = `${this.baseUrl}/api-keys`;
325+
326+
const headers = {
327+
'Content-Type': 'application/json',
328+
'Accept': 'application/json',
329+
};
330+
331+
// Add Authorization header if API key exists
332+
const apiKey = localStorage.getItem('api_key');
333+
if (apiKey) {
334+
headers['Authorization'] = `Bearer ${apiKey}`;
335+
}
336+
337+
const response = await fetch(url, {
338+
method: 'POST',
339+
headers,
340+
body: JSON.stringify({ name })
341+
});
342+
343+
if (!response.ok) {
344+
const errorData = await response.json();
345+
throw new Error(errorData.error || response.statusText);
346+
}
347+
348+
return await response.json();
349+
}
350+
351+
/**
352+
* Update an API key
353+
* @param {string} keyId - API key ID
354+
* @param {Object} updates - Updates to apply
355+
* @returns {Promise<Object>} - Updated API key
356+
*/
357+
static async updateApiKey(keyId, updates) {
358+
const url = `${this.baseUrl}/api-keys/${keyId}`;
359+
360+
const headers = {
361+
'Content-Type': 'application/json',
362+
'Accept': 'application/json',
363+
};
364+
365+
// Add Authorization header if API key exists
366+
const apiKey = localStorage.getItem('api_key');
367+
if (apiKey) {
368+
headers['Authorization'] = `Bearer ${apiKey}`;
369+
}
370+
371+
const response = await fetch(url, {
372+
method: 'PUT',
373+
headers,
374+
body: JSON.stringify(updates)
375+
});
376+
377+
if (!response.ok) {
378+
const errorData = await response.json();
379+
throw new Error(errorData.error || response.statusText);
380+
}
381+
382+
return await response.json();
383+
}
384+
385+
/**
386+
* Delete an API key
387+
* @param {string} keyId - API key ID
388+
* @returns {Promise<Object>} - Success status
389+
*/
390+
static async deleteApiKey(keyId) {
391+
const url = `${this.baseUrl}/api-keys/${keyId}`;
392+
393+
const headers = {
394+
'Accept': 'application/json',
395+
};
396+
397+
// Add Authorization header if API key exists
398+
const apiKey = localStorage.getItem('api_key');
399+
if (apiKey) {
400+
headers['Authorization'] = `Bearer ${apiKey}`;
401+
}
402+
403+
const response = await fetch(url, {
404+
method: 'DELETE',
405+
headers
406+
});
407+
408+
if (!response.ok) {
409+
const errorData = await response.json();
410+
throw new Error(errorData.error || response.statusText);
411+
}
412+
413+
return await response.json();
414+
}
290415
}

public/js/components/api-key-manager.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,11 @@ export class ApiKeyManager extends BaseComponent {
483483
this._error = null;
484484
this.render();
485485

486-
const response = await fetch('/api/1/api-keys', {
486+
// Use the ApiClient class for consistency
487+
const { ApiClient } = await import('../api-client.js');
488+
const baseUrl = ApiClient.baseUrl || '/api/1';
489+
490+
const response = await fetch(`${baseUrl}/api-keys`, {
487491
method: 'GET',
488492
headers: {
489493
'Accept': 'application/json',
@@ -524,7 +528,11 @@ export class ApiKeyManager extends BaseComponent {
524528
this._error = null;
525529
this.render();
526530

527-
const response = await fetch('/api/1/api-keys', {
531+
// Use the ApiClient class for consistency
532+
const { ApiClient } = await import('../api-client.js');
533+
const baseUrl = ApiClient.baseUrl || '/api/1';
534+
535+
const response = await fetch(`${baseUrl}/api-keys`, {
528536
method: 'POST',
529537
headers: {
530538
'Content-Type': 'application/json',
@@ -564,7 +572,11 @@ export class ApiKeyManager extends BaseComponent {
564572
this._error = null;
565573
this.render();
566574

567-
const response = await fetch(`/api/1/api-keys/${keyId}`, {
575+
// Use the ApiClient class for consistency
576+
const { ApiClient } = await import('../api-client.js');
577+
const baseUrl = ApiClient.baseUrl || '/api/1';
578+
579+
const response = await fetch(`${baseUrl}/api-keys/${keyId}`, {
568580
method: 'PUT',
569581
headers: {
570582
'Content-Type': 'application/json',
@@ -601,7 +613,11 @@ export class ApiKeyManager extends BaseComponent {
601613
this._error = null;
602614
this.render();
603615

604-
const response = await fetch(`/api/1/api-keys/${keyId}`, {
616+
// Use the ApiClient class for consistency
617+
const { ApiClient } = await import('../api-client.js');
618+
const baseUrl = ApiClient.baseUrl || '/api/1';
619+
620+
const response = await fetch(`${baseUrl}/api-keys/${keyId}`, {
605621
method: 'DELETE',
606622
headers: {
607623
'Accept': 'application/json',

0 commit comments

Comments
 (0)