import { } from "@cloudflare/workers-types" ; import { Router , Method } from "tiny-request-router" ; import { pageRoute } from "./routes/page" ; import { tableRoute } from "./routes/table" ; import { userRoute } from "./routes/user" ; import { searchRoute } from "./routes/search" ; import { createResponse } from "./response" ; import { getCacheKey } from "./get-cache-key" ; import * as types from "./api/types" ; export type Handler = ( req : types . HandlerRequest ) => Promise < Response > | Response ; const corsHeaders = { "Access-Control-Allow-Origin" : "*" , "Access-Control-Allow-Headers" : "*" , "Access-Control-Allow-Methods" : "GET, HEAD, POST, OPTIONS" , } ; const router = new Router < Handler > ( ) ; router . options ( "*" , ( ) => new Response ( null , { headers : corsHeaders } ) ) ; router . get ( "/v1/page/:pageId" , pageRoute ) ; router . get ( "/v1/table/:pageId" , tableRoute ) ; router . get ( "/v1/user/:userId" , userRoute ) ; router . get ( "/v1/search" , searchRoute ) ; router . get ( "*" , async ( ) => createResponse ( { error : `Route not found!` , routes : [ "/v1/page/:pageId" , "/v1/table/:pageId" , "/v1/user/:pageId" ] , } , { } , 404 ) ) ; const cache = ( caches as any ) . default ; const NOTION_API_TOKEN = typeof NOTION_TOKEN !== "undefined" ? NOTION_TOKEN : undefined ; const handleRequest = async ( fetchEvent ) : Promise < Response > => { const request = fetchEvent . request ; const { pathname , searchParams } = new URL ( request . url ) ; const notionToken = NOTION_API_TOKEN || ( request . headers . get ( "Authorization" ) || "" ) . split ( "Bearer " ) [ 1 ] || undefined ; const match = router . match ( request . method as Method , pathname ) ; if ( ! match ) { return new Response ( "Endpoint not found." , { status : 404 } ) ; } const cacheKey = getCacheKey ( request ) ; let response ; if ( cacheKey ) { try { response = await cache . match ( cacheKey ) ; } catch ( err ) { } } const getResponseAndPersist = async ( ) => { const res = await match . handler ( { request , searchParams , params : match . params , notionToken , } ) ; if ( cacheKey ) { await cache . put ( cacheKey , res . clone ( ) ) ; } return res ; } ; if ( response ) { fetchEvent . waitUntil ( getResponseAndPersist ( ) ) ; return response ; } return getResponseAndPersist ( ) ; } ; self . addEventListener ( "fetch" , async ( event ) => { const fetchEvent = event as FetchEvent ; fetchEvent . respondWith ( handleRequest ( fetchEvent ) ) ; } ) ;