From 67ebf0b869ab0e7b482531fe51a3e02c3cd7f2ae Mon Sep 17 00:00:00 2001 From: Maurici Abad Gutierrez Date: Thu, 26 Oct 2023 18:21:28 +0200 Subject: [PATCH 1/3] Fix wrong inference of `NextResponse` in `withAxiomRouteHandler` --- .vscode/settings.json | 5 +++++ src/withAxiom.ts | 43 ++++++++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..60882dd4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "prettier.semi": true, + "javascript.format.semicolons": "insert", + "typescript.format.semicolons": "insert" +} \ No newline at end of file diff --git a/src/withAxiom.ts b/src/withAxiom.ts index 0889c4a3..c3480b77 100644 --- a/src/withAxiom.ts +++ b/src/withAxiom.ts @@ -5,7 +5,9 @@ import { Logger, RequestReport } from './logger'; import { type NextRequest, type NextResponse } from 'next/server'; import { EndpointType } from './shared'; -export function withAxiomNextConfig(nextConfig: NextConfig): NextConfig { +export function withAxiomNextConfig( + nextConfig: T +): Omit & Pick { return { ...nextConfig, rewrites: async () => { @@ -49,13 +51,13 @@ export function withAxiomNextConfig(nextConfig: NextConfig): NextConfig { } export type AxiomRequest = NextRequest & { log: Logger }; -type NextHandler = ( - req: AxiomRequest, - arg?: T -) => Promise | Promise | NextResponse | Response; +type AxiomHandler< + Args = any, + Res extends Response | NextResponse = Response | NextResponse +> = (req: AxiomRequest, arg?: Args) => Promise | Res; -export function withAxiomRouteHandler(handler: NextHandler): NextHandler { - return async (req: Request | NextRequest, arg: any) => { +export function withAxiomRouteHandler(handler: T): T { + return (async (req, arg) => { let region = ''; if ('geo' in req) { region = req.geo?.region ?? ''; @@ -72,13 +74,14 @@ export function withAxiomRouteHandler(handler: NextHandler): NextHandler { region, }; - const logger = new Logger({ req: report, source: isEdgeRuntime ? 'edge' : 'lambda' }); - const axiomContext = req as AxiomRequest; - const args = arg; - axiomContext.log = logger; + const logger = new Logger({ + req: report, + source: isEdgeRuntime ? 'edge' : 'lambda', + }); + req.log = logger; try { - const result = await handler(axiomContext, args); + const result = await handler(req, arg); await logger.flush(); if (isEdgeRuntime) { logEdgeReport(report); @@ -93,24 +96,26 @@ export function withAxiomRouteHandler(handler: NextHandler): NextHandler { } throw error; } - }; + }) as T; } function logEdgeReport(report: RequestReport) { console.log(`AXIOM_EDGE_REPORT::${JSON.stringify(report)}`); } -type WithAxiomParam = NextConfig | NextHandler; - -function isNextConfig(param: WithAxiomParam): param is NextConfig { +function isNextConfig(param: NextConfig | AxiomHandler): param is NextConfig { return typeof param == 'object'; } // withAxiom can be called either with NextConfig, which will add proxy rewrites // to improve deliverability of Web-Vitals and logs. -export function withAxiom(param: NextHandler): NextHandler; -export function withAxiom(param: NextConfig): NextConfig; -export function withAxiom(param: WithAxiomParam) { +export function withAxiom( + param: T +): ReturnType>; +export function withAxiom( + param: T +): ReturnType>; +export function withAxiom(param: T) { if (typeof param == 'function') { return withAxiomRouteHandler(param); } else if (isNextConfig(param)) { From bdd71ed86f9779e12a9bf4b4b1a4fad9c73e732f Mon Sep 17 00:00:00 2001 From: Maurici Abad Gutierrez Date: Thu, 26 Oct 2023 18:25:05 +0200 Subject: [PATCH 2/3] Delete unnecessary explicit types of AxiomRequest --- README.md | 4 ++-- examples/logger/app/api/dynamic/[id]/route.ts | 4 ++-- examples/logger/app/api/edge/route.ts | 4 ++-- examples/logger/app/api/lambda/route.ts | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 54c4caf5..1e3a13de 100644 --- a/README.md +++ b/README.md @@ -86,9 +86,9 @@ Wrapping your Route Handlers in `withAxiom` will add a logger to your request and automatically log exceptions: ```typescript -import { withAxiom, AxiomRequest } from 'next-axiom'; +import { withAxiom } from 'next-axiom'; -export const GET = withAxiom((req: AxiomRequest) => { +export const GET = withAxiom((req) => { req.log.info('Login function called'); // You can create intermediate loggers diff --git a/examples/logger/app/api/dynamic/[id]/route.ts b/examples/logger/app/api/dynamic/[id]/route.ts index 251fbd31..05e355ce 100644 --- a/examples/logger/app/api/dynamic/[id]/route.ts +++ b/examples/logger/app/api/dynamic/[id]/route.ts @@ -1,8 +1,8 @@ -import { AxiomRequest, withAxiom } from 'next-axiom'; +import { withAxiom } from 'next-axiom'; export const runtime = 'edge'; -export const POST = withAxiom((req: AxiomRequest, { params }: { params: { id: string}}) => { +export const POST = withAxiom((req, { params }: { params: { id: string}}) => { req.log.info('axiom dynamic route'); return new Response(`Hello, Next.js! ${params.id}`); }) \ No newline at end of file diff --git a/examples/logger/app/api/edge/route.ts b/examples/logger/app/api/edge/route.ts index bfe13f37..80c9d1fe 100644 --- a/examples/logger/app/api/edge/route.ts +++ b/examples/logger/app/api/edge/route.ts @@ -1,8 +1,8 @@ -import { AxiomRequest, withAxiom } from 'next-axiom'; +import { withAxiom } from 'next-axiom'; export const runtime = 'edge'; -export const GET = withAxiom(async (req: AxiomRequest) => { +export const GET = withAxiom(async (req) => { req.log.info('fired from edge route'); return new Response('Hello, Next.js!'); }); diff --git a/examples/logger/app/api/lambda/route.ts b/examples/logger/app/api/lambda/route.ts index e04f5803..5b4a6eb2 100644 --- a/examples/logger/app/api/lambda/route.ts +++ b/examples/logger/app/api/lambda/route.ts @@ -1,8 +1,8 @@ -import { AxiomRequest, withAxiom } from 'next-axiom'; +import { withAxiom } from 'next-axiom'; export const runtime = 'edge'; -export const GET = withAxiom(async (req: AxiomRequest) => { +export const GET = withAxiom(async (req) => { req.log.info('axiom lambda route'); return new Response('Hello, Next.js!'); }); From fba8736f7a0780a80ae0cbaf5fc7ca45b75b6950 Mon Sep 17 00:00:00 2001 From: Maurici Abad Gutierrez Date: Thu, 26 Oct 2023 18:36:22 +0200 Subject: [PATCH 3/3] Delete vscode config file commited by mistake --- .vscode/settings.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 60882dd4..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "prettier.semi": true, - "javascript.format.semicolons": "insert", - "typescript.format.semicolons": "insert" -} \ No newline at end of file