Created
January 18, 2021 06:16
-
-
Save vassalloandrea/e920271c71fd80fababdd442fca46a41 to your computer and use it in GitHub Desktop.
Medium Morgan Winston Example - morganMiddleware GraphQL TS
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
import morgan, { StreamOptions } from "morgan"; | |
import { IncomingMessage } from "http"; | |
import Logger from "../lib/logger"; | |
interface Request extends IncomingMessage { | |
body: { | |
query: String; | |
}; | |
} | |
const stream: StreamOptions = { | |
write: (message) => | |
Logger.http(message.substring(0, message.lastIndexOf("\n"))), | |
}; | |
const skip = () => { | |
const env = process.env.NODE_ENV || "development"; | |
return env !== "development"; | |
}; | |
const registerGraphQLToken = () => { | |
morgan.token("graphql-query", (req: Request) => `GraphQL ${req.body.query}`); | |
}; | |
registerGraphQLToken(); | |
const morganMiddleware = morgan( | |
":method :url :status :res[content-length] - :response-time ms\n:graphql-query", | |
{ stream, skip } | |
); | |
export default morganMiddleware; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment