1- import path from 'path' ;
2- import fs from 'fs/promises' ;
31import type { NextApiRequest , NextApiResponse } from 'next' ;
4- import { checkSecurity , methodNotAllowed , type Request , type Response } from '../http' ;
2+ import { checkSecurity , type Request , type Response } from '../http' ;
53import bodyParser from 'body-parser' ;
64import type { TidewaveConfig } from '../core' ;
7- import { default as tidewavePackage } from '../../package.json' with { type : 'json' } ;
5+ import { getProjectName } from '../core' ;
86
97const DEFAULT_CONFIG : TidewaveConfig = { } ;
108
@@ -55,6 +53,10 @@ export async function tidewaveHandler(
5553 await import ( '../logger/instrumentation' ) ;
5654 }
5755
56+ // Set framework and projectName upfront
57+ config . framework = 'nextjs' ;
58+ config . projectName = config . projectName || ( await getProjectName ( 'next_app' ) ) ;
59+
5860 return async function handler ( req : NextApiRequest , res : NextApiResponse ) : Promise < void > {
5961 const origin = req . headers . host ;
6062 const url = new URL ( req . url ?? '' , `http://${ origin } ` ) ;
@@ -76,81 +78,12 @@ export async function tidewaveHandler(
7678 await connectWrapper ( securityMiddleware ) ( req , res , next ) ;
7779 await connectWrapper ( bodyParser . json ( ) ) ( req , res , next ) ;
7880
79- if ( req . method === 'GET' && endpoint === undefined ) {
80- return await respondTidewaveHTML ( res , config ) ;
81- }
82-
83- if ( req . method === 'GET' && endpoint === 'config' ) {
84- return await respondTidewaveConfigJSON ( res , config ) ;
85- }
86-
87- if ( req . method !== 'POST' ) {
88- return methodNotAllowed ( res ) ;
89- }
81+ const { getHandlers } = await import ( '../http' ) ;
82+ const handlers = getHandlers ( config ) ;
9083
91- const { HANDLERS } = await import ( '../http' ) ;
92- const handler = HANDLERS [ endpoint || '' ] ;
84+ const handler = handlers [ endpoint || '' ] ;
9385 if ( handler ) return await connectWrapper ( handler ) ( req , res , next ) ;
9486
9587 return res . status ( 404 ) . json ( { message : `Route not found: ${ req . method } ${ req . url } ` } ) ;
9688 } ;
9789}
98-
99- async function respondTidewaveHTML ( res : NextApiResponse , config : TidewaveConfig ) : Promise < void > {
100- const clientUrl = config . clientUrl || 'https://tidewave.ai' ;
101-
102- const tidewaveConfig = await tidewaveConfigJSON ( config ) ;
103-
104- res . status ( 200 ) . setHeader ( 'Content-Type' , 'text/html' ) . end ( `
105- <html>
106- <head>
107- <meta charset="UTF-8" />
108- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
109- <meta name="tidewave:config" content="${ escapeHtml ( JSON . stringify ( tidewaveConfig ) ) } " />
110- <script type="module" src="${ clientUrl } /tc/tc.js"></script>
111- </head>
112- <body></body>
113- </html>
114- ` ) ;
115- }
116-
117- async function respondTidewaveConfigJSON (
118- res : NextApiResponse ,
119- config : TidewaveConfig ,
120- ) : Promise < void > {
121- const tidewaveConfig = await tidewaveConfigJSON ( config ) ;
122- res . status ( 200 ) . json ( tidewaveConfig ) ;
123- }
124-
125- async function tidewaveConfigJSON ( config : TidewaveConfig ) : Promise < object > {
126- return {
127- project_name : await getProjectName ( ) ,
128- framework_type : 'nextjs' ,
129- tidewave_version : tidewavePackage . version ,
130- team : config . team || { } ,
131- } ;
132- }
133-
134- async function getProjectName ( ) : Promise < string > {
135- const rootDir = process . cwd ( ) ;
136- const packageJsonPath = path . join ( rootDir , 'package.json' ) ;
137- try {
138- const packageJson = await fs . readFile ( packageJsonPath , 'utf8' ) ;
139- const { name } = JSON . parse ( packageJson ) ;
140- return name || 'next_app' ;
141- } catch {
142- return 'next_app' ;
143- }
144- }
145-
146- function escapeHtml ( text : string ) : string {
147- const map : Record < string , string > = {
148- '&' : '&' ,
149- '<' : '<' ,
150- '>' : '>' ,
151- '"' : '"' ,
152- "'" : ''' ,
153- } ;
154-
155- return text . replace ( / [ & < > " ' ] / g, match => map [ match ] ! ) ;
156- }
0 commit comments