11import { type NextRequest } from 'next/server'
22import { createRouter } from 'radix3'
33
4- import { type ApiRoute , type ApiRouter , Context , type ServerConfig } from '@kivotos/core'
4+ import {
5+ type ApiRoute ,
6+ type ApiRouter ,
7+ Context ,
8+ createAuth ,
9+ type ServerConfig ,
10+ } from '@kivotos/core'
511
612function extractHeaders ( headers : Headers ) {
713 const headersRecord : Record < string , string > = { }
@@ -21,7 +27,7 @@ function extractSearchParams(searchParams: URLSearchParams) {
2127
2228async function makeApiRoute (
2329 req : NextRequest ,
24- context : Record < string , unknown > ,
30+ serverConfig : ServerConfig < any , any , any , ApiRouter < any > > ,
2531 route : ApiRoute ,
2632 pathParams : Record < string , string > | undefined
2733) {
@@ -41,11 +47,11 @@ async function makeApiRoute(
4147 // This is useful for file uploads or plain text requests
4248 }
4349
44- const wrappedContext = new Context ( context )
50+ const { context } = createAuth ( serverConfig . auth , serverConfig . context )
51+ const requestContext = Context . toRequestContext ( context , reqHeaders )
4552
4653 const rawResponse = await route . handler ( {
47- context : wrappedContext ,
48- requestContext : Context . toRequestContext ( wrappedContext , reqHeaders ) ,
54+ requestContext,
4955 headers : reqHeaders ,
5056 pathParams : pathParams ,
5157 query : reqSearchParams ,
@@ -64,13 +70,13 @@ async function makeApiRoute(
6470async function lookupRoute (
6571 radixRouter : ReturnType < typeof createRouter > ,
6672 req : NextRequest ,
67- context : Record < string , unknown >
73+ serverConfig : ServerConfig < any , any , any , ApiRouter < any > >
6874) {
6975 const match = radixRouter . lookup ( req . nextUrl . pathname )
7076 if ( ! match ) return Response . json ( { message : 'Not Found' } , { status : 404 } )
7177 const pathParams = match . params
7278 const route = match . route as ApiRoute < any >
73- return makeApiRoute ( req , context , route , pathParams )
79+ return makeApiRoute ( req , serverConfig , route , pathParams )
7480}
7581
7682export function createApiResourceRouter ( serverConfig : ServerConfig < any , any , any , ApiRouter < any > > ) {
@@ -136,19 +142,19 @@ export function createApiResourceRouter(serverConfig: ServerConfig<any, any, any
136142
137143 return {
138144 GET : async ( req : NextRequest ) => {
139- return lookupRoute ( radixGetRouter , req , serverConfig . context )
145+ return lookupRoute ( radixGetRouter , req , serverConfig )
140146 } ,
141147 POST : async ( req : NextRequest ) => {
142- return lookupRoute ( radixPostRouter , req , serverConfig . context )
148+ return lookupRoute ( radixPostRouter , req , serverConfig )
143149 } ,
144150 PUT : async ( req : NextRequest ) => {
145- return lookupRoute ( radixPutRouter , req , serverConfig . context )
151+ return lookupRoute ( radixPutRouter , req , serverConfig )
146152 } ,
147153 DELETE : async ( req : NextRequest ) => {
148- return lookupRoute ( radixDeleteRouter , req , serverConfig . context )
154+ return lookupRoute ( radixDeleteRouter , req , serverConfig )
149155 } ,
150156 PATCH : async ( req : NextRequest ) => {
151- return lookupRoute ( radixPatchRouter , req , serverConfig . context )
157+ return lookupRoute ( radixPatchRouter , req , serverConfig )
152158 } ,
153159 }
154160}
0 commit comments