Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/logger/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions src/withBetterStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ export function withBetterStackNextConfig(nextConfig: NextConfig): NextConfig {
}

export type BetterStackRequest = NextRequest & { log: Logger };
type NextHandler<T = any> = (
type BetterStackRouteHandler<T = any> = (
req: BetterStackRequest,
arg?: T
) => Promise<Response> | Promise<NextResponse> | NextResponse | Response;
type NextHandler<T = any> = (
req: NextRequest,
arg?: T
) => Promise<Response> | Promise<NextResponse> | NextResponse | Response;

type BetterStackRouteHandlerConfig = {
logRequestDetails?: boolean | (keyof RequestJSON)[];
Expand All @@ -61,7 +65,10 @@ type BetterStackRouteHandlerConfig = {
redirectLogLevel?: LogLevel; // defaults to LogLevel.info
};

export function withBetterStackRouteHandler(handler: NextHandler, config?: BetterStackRouteHandlerConfig): NextHandler {
export function withBetterStackRouteHandler(
handler: BetterStackRouteHandler,
config?: BetterStackRouteHandlerConfig
): NextHandler {
return async (req: Request | NextRequest, arg: any) => {
let region = '';
if ('geo' in req) {
Expand Down Expand Up @@ -183,15 +190,15 @@ export function withBetterStackRouteHandler(handler: NextHandler, config?: Bette
};
}

type WithBetterStackParam = NextConfig | NextHandler;
type WithBetterStackParam = NextConfig | BetterStackRouteHandler;

function isNextConfig(param: WithBetterStackParam): param is NextConfig {
return typeof param == 'object';
}

// withBetterStack can be called either with NextConfig, which will add proxy rewrites
// to improve deliverability of Web-Vitals and logs.
export function withBetterStack(param: NextHandler, config?: BetterStackRouteHandlerConfig): NextHandler;
export function withBetterStack(param: BetterStackRouteHandler, config?: BetterStackRouteHandlerConfig): NextHandler;
export function withBetterStack(param: NextConfig): NextConfig;
export function withBetterStack(param: WithBetterStackParam, config?: BetterStackRouteHandlerConfig) {
if (typeof param == 'function') {
Expand Down