diff --git a/src/api.ts b/src/api.ts index 36d0a91..aba4277 100644 --- a/src/api.ts +++ b/src/api.ts @@ -11,21 +11,21 @@ type ApiResponse = { async function apiCall(method: string, body?: unknown): Promise> { const config = getConfig(); + const bearer = config.authToken; - if (!config.authToken) { + if (!bearer) { - return {ok: false, error: 'Not logged in. Run: logfox login'}; + return {ok: false, error: 'Not logged in. Run: logfox login or: logfox config set apiKey '}; } try { - // express-typed-rpc expects method name in URL path, args as body const response = await fetch(`${config.apiUrl}/v1/${method}`, { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${config.authToken}`, + 'Authorization': `Bearer ${bearer}`, }, body: JSON.stringify(body ?? {}), }); @@ -112,38 +112,43 @@ export type LogEntry = { export type Collector = 'cli' | 'cloudwatch-logs' | 'sdk' | 'vercel' | 'fluentbit'; -export async function ingestLogs(teamId: string, appId: string, env: string, logs: LogEntry[]): Promise> { +function normalizeEnv(env: string): string { - return apiCall('ingestLogs', {teamId, appId, env, logs}); + if (env === 'prod') return 'production'; + if (env === 'dev') return 'development'; + + return env; } -/** - * Ingest logs via the new /v1/ingest endpoint using API key authentication. - * This is the preferred method for all log ingestion going forward. - */ -export async function ingestLogsV1( - apiKey: string, +export async function ingestLogs( appId: string, env: string, - collector: Collector, logs: LogEntry[], -): Promise> { + collector?: Collector, +): Promise> { const config = getConfig(); + const bearer = config.apiKey ?? config.authToken; + + if (!bearer) { + + return {ok: false, error: 'Not logged in. Run: logfox login or: logfox config set apiKey '}; + + } try { - const response = await fetch(`${config.apiUrl}/v1/ingest`, { + const response = await fetch(`${config.apiUrl}/v1/ingestLogs`, { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${apiKey}`, + 'Authorization': `Bearer ${bearer}`, }, - body: JSON.stringify({appId, env, collector, logs}), + body: JSON.stringify({appId, env: normalizeEnv(env), logs, collector}), }); if (!response.ok) { diff --git a/src/commands/run.ts b/src/commands/run.ts index 4f87b3a..2941688 100644 --- a/src/commands/run.ts +++ b/src/commands/run.ts @@ -153,27 +153,13 @@ export async function run(command: string[], options: RunOptions): Promise const logsToSend = logBuffer; logBuffer = []; - // Use API key auth for non-local, or if API key is available - if (config.apiKey) { + const appId = app?.id ?? appName; - const result = await api.ingestLogsV1(config.apiKey, appName, env, 'cli', logsToSend); + const result = await api.ingestLogs(appId, env, logsToSend, 'cli'); - if (!result.ok) { + if (!result.ok) { - console.error(`[logfox] Failed to send logs: ${result.error}`); - - } - - } else if (isLocal && config.teamId && app) { - - // Fall back to old auth token method for local - const result = await api.ingestLogs(config.teamId, app.id, 'local', logsToSend); - - if (!result.ok) { - - console.error(`[logfox] Failed to send logs: ${result.error}`); - - } + console.error(`[logfox] Failed to send logs: ${result.error}`); } diff --git a/src/commands/setup-cloudwatch.ts b/src/commands/setup-cloudwatch.ts index 64d649e..09c55d9 100644 --- a/src/commands/setup-cloudwatch.ts +++ b/src/commands/setup-cloudwatch.ts @@ -263,7 +263,7 @@ export async function setupCloudwatch(): Promise { } const lambdaEnv = { - LOGFOX_ENDPOINT: `${config.apiUrl}/v1/ingest`, + LOGFOX_ENDPOINT: `${config.apiUrl}/v1/ingestLogs`, LOGFOX_API_KEY: apiKey, LOG_GROUP_MAPPINGS: JSON.stringify(logGroupMappings), };