({
authToken,
logger = defaultLogger,
clientEnv = env,
}: FetchUsageParams)
| 39 | * Fetches usage data from the API |
| 40 | */ |
| 41 | export async function fetchUsageData({ |
| 42 | authToken, |
| 43 | logger = defaultLogger, |
| 44 | clientEnv = env, |
| 45 | }: FetchUsageParams): Promise<UsageResponse> { |
| 46 | const appUrl = clientEnv.NEXT_PUBLIC_CODEBUFF_APP_URL |
| 47 | if (!appUrl) { |
| 48 | throw new Error('NEXT_PUBLIC_CODEBUFF_APP_URL is not set') |
| 49 | } |
| 50 | |
| 51 | const response = await fetch(`${appUrl}/api/v1/usage`, { |
| 52 | method: 'POST', |
| 53 | headers: { |
| 54 | 'Content-Type': 'application/json', |
| 55 | }, |
| 56 | body: JSON.stringify({ |
| 57 | fingerprintId: 'cli-usage', |
| 58 | authToken, |
| 59 | }), |
| 60 | }) |
| 61 | |
| 62 | if (!response.ok) { |
| 63 | logger.error( |
| 64 | { status: response.status }, |
| 65 | 'Failed to fetch usage data from API', |
| 66 | ) |
| 67 | throw new Error(`Failed to fetch usage: ${response.status}`) |
| 68 | } |
| 69 | |
| 70 | const responseBody = await response.json() |
| 71 | const data = responseBody as UsageResponse |
| 72 | return data |
| 73 | } |
| 74 | |
| 75 | export interface UseUsageQueryDeps { |
| 76 | logger?: Logger |
no test coverage detected