-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from vordgi/nmc-24
nmc-24 logger configuration
- Loading branch information
Showing
5 changed files
with
53 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
import type { NextRequest, NextFetchEvent } from "next/server"; | ||
import { type NextRequest, type NextFetchEvent } from "next/server"; | ||
import { type ChainItem, type ChainConfig } from "./lib/types"; | ||
import { collectData } from "./lib/collect-data"; | ||
import { ChainItem } from "./lib/types"; | ||
import { formatResponse } from "./lib/format-response"; | ||
import { Logger } from "./lib/logger"; | ||
|
||
export { FinalNextResponse } from "./lib/final-next-response"; | ||
|
||
export const chain = (middlewares: ChainItem[]) => async (req: NextRequest, event: NextFetchEvent) => { | ||
const summary = await collectData(req, event, middlewares); | ||
const next = formatResponse(summary); | ||
export const chain = | ||
(middlewares: ChainItem[], config?: ChainConfig) => async (req: NextRequest, event: NextFetchEvent) => { | ||
const logger = new Logger(config?.logger); | ||
const summary = await collectData(req, event, middlewares, logger); | ||
const next = formatResponse(summary); | ||
|
||
return next; | ||
}; | ||
return next; | ||
}; | ||
|
||
export default chain; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export class Logger { | ||
private logger?: Logger | null; | ||
|
||
constructor(logger?: Logger | boolean | null) { | ||
if (logger && logger !== true) { | ||
this.logger = logger; | ||
} else if (logger === false || logger === null) { | ||
this.logger = null; | ||
} else { | ||
this.logger = console; | ||
} | ||
} | ||
|
||
log(message: string) { | ||
this.logger?.log(message); | ||
} | ||
|
||
warn(message: string) { | ||
this.logger?.warn(message); | ||
} | ||
|
||
error(message: string) { | ||
this.logger?.error(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters