(env: Env, url: URL)
| 793 | * turns what comes back into a Response. |
| 794 | */ |
| 795 | export async function handleApi(env: Env, url: URL): Promise<ApiResult> { |
| 796 | if (url.pathname === '/api/session') return { body: { authenticated: true } }; |
| 797 | if (url.pathname === '/api/health') return health(env); |
| 798 | if (url.pathname === '/api/meta') return meta(env); |
| 799 | |
| 800 | const ranged = new Set(['/api/summary', '/api/timeseries', '/api/breakdown', '/api/activation', '/api/retention']); |
| 801 | if (!ranged.has(url.pathname)) return fail('not found', 404); |
| 802 | |
| 803 | const range = parseRange(url); |
| 804 | if (isApiResult(range)) return range; |
| 805 | |
| 806 | try { |
| 807 | switch (url.pathname) { |
| 808 | case '/api/summary': |
| 809 | return await summary(env, range); |
| 810 | case '/api/timeseries': |
| 811 | return await timeseries(env, url, range); |
| 812 | case '/api/breakdown': |
| 813 | return await breakdown(env, url, range); |
| 814 | case '/api/activation': |
| 815 | return await activation(env, url, range); |
| 816 | case '/api/retention': |
| 817 | return await retention(env, range); |
| 818 | default: |
| 819 | return fail('not found', 404); |
| 820 | } |
| 821 | } catch (err) { |
| 822 | // The query failed, not the caller. Log the cause, tell the page something |
| 823 | // it can put in the panel, and let the other panels carry on. |
| 824 | console.error(JSON.stringify({ msg: 'api query failed', path: url.pathname, err: String(err) })); |
| 825 | return { body: { error: 'query failed' }, status: 503 }; |
| 826 | } |
| 827 | } |
no test coverage detected