Skip to main content
This release is 46 versions behind 0.0.299 — the latest version of @bureaudouble/bureau. Jump to latest
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
0%
Published
3 months ago (0.0.253)
import type { Database } from "./types.sqlite.d.ts"; import type { Kysely } from "npm:kysely@^0.27.5"; import { createRouter, type Router } from "jsr:@fartlabs/rt@^0.0.3"; import { join } from "jsr:/@std/path@^1.0.8/join"; import createRequiredTables from "./utils/createRequiredTables.ts"; import client from "./api/client.ts"; import event from "./api/log/event.ts"; import exit from "./api/log/exit.ts"; import visit from "./api/log/visit.ts"; export interface AnalyticsConfig { basePath: string; databaseKey: string; getDatabase: () => Promise<Kysely<Database["public"]>>; getIpData?: (ip: string) => Promise< | null | undefined | { latitude: string; longitude: string; country_code: string; region_code: string; city_name: string; } >; } export interface AnalyticsState { config: AnalyticsConfig; database: Kysely<Database["public"]>; } export default async ( config?: AnalyticsConfig, ): Promise<Router<AnalyticsConfig & Deno.ServeHandlerInfo>> => { if (!config) return createRouter(); let database: Awaited<ReturnType<AnalyticsConfig["getDatabase"]>>; const state = (fn: (ctx: any) => Promise<Response> | Response) => async (ctx: any) => { if (!database) { database = await config.getDatabase(); await createRequiredTables({ database }) .catch(console.error) } return fn((Object.assign(ctx.state, { config, database }), ctx)); }; const router = createRouter( (router) => router .get(join(config.basePath, "/client.js"), state(client)) .post(join(config.basePath, "/api/log/event"), state(event)) .post(join(config.basePath, "/api/log/exit"), state(exit)) .post(join(config.basePath, "/api/log/visit"), state(visit)), (): AnalyticsConfig & Deno.ServeHandlerInfo => ({ ...config, remoteAddr: null!, }), ); return router; };