@@ -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}
0 commit comments