# All files / src/services resellerApiKeys.ts

0% Statements 0/32
100% Branches 0/0
0% Functions 0/7
0% Lines 0/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167

     import { apiService } from './api';
    import { ApiResult } from '@/types';

    export interface ApiKeyPermission {
      id: string;
      name: string;
      description: string;
    }

    export interface ResellerApiKey {
      id: number;
      reseller_id: number;
      key_name: string;
      api_key_prefix: string;
      permissions: string[];
      allowed_origins: string;
      ip_whitelist: string[] | null;
      rate_limit_per_minute: number;
      rate_limit_per_day: number;
      is_active: boolean;
      last_used_at: string | null;
      total_requests: number;
      created_at: string;
      expires_at: string | null;
    }

    export interface CreateApiKeyRequest {
      key_name: string;
      permissions: string[];
      allowed_origins?: string;
      ip_whitelist?: string[];
      rate_limit_per_minute?: number;
      rate_limit_per_day?: number;
      expires_at?: string;
    }

    export interface UpdateApiKeyRequest {
      key_name?: string;
      permissions?: string[];
      allowed_origins?: string;
      ip_whitelist?: string[];
      rate_limit_per_minute?: number;
      rate_limit_per_day?: number;
      is_active?: boolean;
      expires_at?: string;
    }

    export interface CreateApiKeyResponse {
      api_key: ResellerApiKey;
      secret_key: string;
    }

    export interface PermissionsListResponse {
      permissions: ApiKeyPermission[];
    }

    class ResellerApiKeysService {
      private basePath = '/api/reseller/api-keys';

      async listApiKeys(): Promise<ApiResult<ResellerApiKey[]>> {
        try {
          const result = await apiService.get<ResellerApiKey[]>(this.basePath);
          return result;
        } catch {
          return {
            success: false,
            error: {
              error: 'List API Keys Failed',
              details: 'Failed to fetch API keys',
              timestamp: new Date().toISOString()}
          };
        }
      }

      async getApiKey(id: number): Promise<ApiResult<ResellerApiKey>> {
        try {
          const result = await apiService.get<ResellerApiKey>(`${this.basePath}/${id}`);
          return result;
        } catch {
          return {
            success: false,
            error: {
              error: 'Get API Key Failed',
              details: 'Failed to fetch API key details',
              timestamp: new Date().toISOString()}
          };
        }
      }

      async createApiKey(request: CreateApiKeyRequest): Promise<ApiResult<CreateApiKeyResponse>> {
        try {
          const result = await apiService.post<CreateApiKeyResponse>(this.basePath, request);
          return result;
        } catch {
          return {
            success: false,
            error: {
              error: 'Create API Key Failed',
              details: 'Failed to create API key',
              timestamp: new Date().toISOString()}
          };
        }
      }

      async updateApiKey(id: number, request: UpdateApiKeyRequest): Promise<ApiResult<ResellerApiKey>> {
        try {
          const result = await apiService.put<ResellerApiKey>(`${this.basePath}/${id}`, request);
          return result;
        } catch {
          return {
            success: false,
            error: {
              error: 'Update API Key Failed',
              details: 'Failed to update API key',
              timestamp: new Date().toISOString()}
          };
        }
      }

      async deleteApiKey(id: number): Promise<ApiResult<{ success: boolean; message: string }>> {
        try {
          const result = await apiService.delete<{ success: boolean; message: string }>(`${this.basePath}/${id}`);
          return result;
        } catch {
          return {
            success: false,
            error: {
              error: 'Delete API Key Failed',
              details: 'Failed to delete API key',
              timestamp: new Date().toISOString()}
          };
        }
      }

      async regenerateApiKey(id: number): Promise<ApiResult<CreateApiKeyResponse>> {
        try {
          const result = await apiService.post<CreateApiKeyResponse>(`${this.basePath}/${id}/regenerate`, {});
          return result;
        } catch {
          return {
            success: false,
            error: {
              error: 'Regenerate API Key Failed',
              details: 'Failed to regenerate API key',
              timestamp: new Date().toISOString()}
          };
        }
      }

      async listPermissions(): Promise<ApiResult<PermissionsListResponse>> {
        try {
          const result = await apiService.get<PermissionsListResponse>(`${this.basePath}/permissions`);
          return result;
        } catch {
          return {
            success: false,
            error: {
              error: 'List Permissions Failed',
              details: 'Failed to fetch available permissions',
              timestamp: new Date().toISOString()}
          };
        }
      }
    }

    export const resellerApiKeysService = new ResellerApiKeysService();