(name: string, query: string)
| 24 | * @returns Query response with results or error |
| 25 | */ |
| 26 | export async function posthogQuery(name: string, query: string): Promise<PostHogQueryResponse> { |
| 27 | const apiKey = getEnvVariable('POSTHOG_QUERY_API_KEY'); |
| 28 | if (!apiKey) { |
| 29 | throw new Error('No PostHog Query API Key'); |
| 30 | } |
| 31 | |
| 32 | const response = await fetch('https://us.posthog.com/api/projects/141915/query/', { |
| 33 | method: 'POST', |
| 34 | headers: { |
| 35 | Authorization: `Bearer ${apiKey}`, |
| 36 | 'Content-Type': 'application/json', |
| 37 | }, |
| 38 | body: JSON.stringify({ |
| 39 | query: { |
| 40 | kind: 'HogQLQuery', |
| 41 | query, |
| 42 | }, |
| 43 | name, |
| 44 | }), |
| 45 | cache: 'no-store', |
| 46 | }); |
| 47 | |
| 48 | if (!response.ok) { |
| 49 | return { |
| 50 | status: 'error', |
| 51 | statusCode: response.status, |
| 52 | error: await response.json().catch(() => ({ error: 'Unknown error' })), |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | return { |
| 57 | status: 'ok', |
| 58 | body: await response.json(), |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | const CACHE_TTL_SECONDS = 60 * 60 * 24; // 24 hours |
| 63 | const MEMORY_CACHE_TTL_MS = 60 * 60 * 1000; // 1 hour |
no test coverage detected