/** * Integration Keys API * * API methods for integration key operations. * Accepts and returns domain IntegrationKey objects. * Uses mappers internally for all schema transformations. * * @layer Infrastructure - API Client */ import { TENANT_KEYS_LIST, } from "@archer/api-interface/endpoints/customer-api"; import type { ListKeysRequest } from '@archer/api-interface'; import { apiClient } from '@/infrastructure/http/ApiClient'; import type { IntegrationKey } from '@/domain/entities/IntegrationKey'; import type { PaginationMeta, ListParams } from '@/infrastructure/http/api/shared/types'; import { IntegrationKeyResponseMapper } from './mappers/IntegrationKeyResponseMapper'; export const integrationKeysApi = { /** * Lists integration keys for a tenant with pagination * * @param tenantId - Tenant ID to list keys for * @param params - Optional pagination and filter parameters * @returns Object with array of IntegrationKey domain entities and pagination metadata * @throws ApiError if request fails * * @example * ```typescript * const { keys, pagination } = await integrationKeysApi.listKeys('tenant-uuid', { * page: 1, * limit: 20, * status: 'ACTIVE', * sortBy: 'createdAt', * sortOrder: 'desc' * }); * ``` */ async listKeys( tenantId: string, params?: ListParams ): Promise<{ keys: IntegrationKey[]; pagination: PaginationMeta }> { // Build query params (ListKeysRequest schema) const queryParams: ListKeysRequest = { page: params?.page, limit: params?.limit, sortBy: params?.sortBy as 'createdAt' | 'status' | 'lastUsedAt' | undefined, sortOrder: params?.sortOrder, status: params?.status as 'ACTIVE' | 'REVOKED' | 'EXPIRED' | undefined, }; // Build endpoint URL with tenantId path parameter const url = TENANT_KEYS_LIST.path.replace(':tenantId', tenantId); // Make HTTP request with query params const response = await apiClient.get>(url, { params: queryParams }); // Map response schema to domain objects return IntegrationKeyResponseMapper.toKeyList(response); }, /** * Gets integration key statistics for a tenant * * @param tenantId - Tenant ID to get statistics for * @returns Statistics object with key counts by status * @throws ApiError if request fails * * @example * ```typescript * const stats = await integrationKeysApi.getStats('tenant-uuid'); * console.log(`Active keys: ${stats.active}`); * console.log(`Revoked keys: ${stats.revoked}`); * console.log(`Expired keys: ${stats.expired}`); * ``` */ async getStats(tenantId: string): Promise<{ total: number; active: number; revoked: number; expired: number; }> { // Get all keys for tenant const { keys } = await this.listKeys(tenantId, { limit: 1000 }); // Calculate statistics from keys const stats = { total: keys.length, active: keys.filter((k) => k.isActive()).length, revoked: keys.filter((k) => k.isRevoked()).length, expired: keys.filter((k) => k.isExpired()).length, }; return stats; }, };