From 68a345af23c1c15f7ee75c12224814958dd18ef3 Mon Sep 17 00:00:00 2001 From: Artyom Sokolov Date: Wed, 21 Jul 2021 21:26:48 +0300 Subject: [PATCH] Added logLevel --- README.md | 28 +++++++++++-------- package.json | 2 +- src/index.ts | 78 +++++++++++++++++++++++++++++----------------------- 3 files changed, 61 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 40e3e73..50ffb2c 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,14 @@ Original [fastify-nextjs](https://github.com/fastify/fastify-nextjs) doesn't pas ## Usage -####Add dependencies +###Add dependencies ```shell yarn add @applicazza/fastify-nextjs yarn add fastify-static ``` -####Disable compression in Next.js (next.config.js) +###Disable compression in Next.js (next.config.js) ```js module.exports = { @@ -28,7 +28,7 @@ module.exports = { }; ``` -Default example +###Default example ```ts import createFastify from 'fastify'; @@ -49,7 +49,7 @@ fastify.passNextJsRequests(); await fastify.listen(0); ``` -Extended example +###Extended example ```ts import createFastify from 'fastify'; @@ -77,7 +77,7 @@ fastify.passNextJsPageRequests(); await fastify.listen(0); ``` -Plugin accepts following options: +###Plugin accepts following options: ```ts interface FastifyNextJsOptions { @@ -86,19 +86,23 @@ interface FastifyNextJsOptions { } ``` -Plugin augments fastify instance with following properties and methods: +###Plugin augments fastify instance with following properties and methods: ```ts +interface FastifyNextJsDecoratorArguments { + logLevel?: LogLevel; +} + interface FastifyInstance { nextJsProxyRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void; nextJsRawRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void; nextServer: NextServer; - passNextJsRequests: () => void; - passNextJsImageRequests: () => void; - passNextJsDataRequests: () => void; - passNextJsDevRequests: () => void; - passNextJsPageRequests: () => void; - passNextJsStaticRequests: () => void; + passNextJsRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsDataRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsDevRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsImageRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsPageRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsStaticRequests: (args?: FastifyNextJsDecoratorArguments) => void; } ``` diff --git a/package.json b/package.json index 9b8abd3..a4ebe9c 100644 --- a/package.json +++ b/package.json @@ -52,5 +52,5 @@ "test": "jest --coverage" }, "types": "dist/index.d.ts", - "version": "0.1.0" + "version": "0.1.1" } diff --git a/src/index.ts b/src/index.ts index 0df2847..68e8a70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,39 +4,44 @@ import { IncomingMessage, ServerResponse } from 'http'; import Next from 'next'; import { NextServer } from 'next/dist/server/next'; import fastifyStatic from 'fastify-static'; +import { LogLevel } from 'fastify/types/logger'; + +export interface FastifyNextJsDecoratorArguments { + logLevel?: LogLevel; +} declare module 'fastify' { - // eslint-disable-next-line no-unused-vars - // noinspection JSUnusedGlobalSymbols - interface FastifyInstance { - nextJsProxyRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void; - nextJsRawRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void; - nextServer: NextServer; - passNextJsRequests: () => void; - passNextJsDataRequests: () => void; - passNextJsDevRequests: () => void; - passNextJsImageRequests: () => void; - passNextJsPageRequests: () => void; - passNextJsStaticRequests: () => void; - } + // eslint-disable-next-line no-unused-vars + // noinspection JSUnusedGlobalSymbols + interface FastifyInstance { + nextJsProxyRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void; + nextJsRawRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void; + nextServer: NextServer; + passNextJsRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsDataRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsDevRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsImageRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsPageRequests: (args?: FastifyNextJsDecoratorArguments) => void; + passNextJsStaticRequests: (args?: FastifyNextJsDecoratorArguments) => void; + } } declare module 'http' { - // eslint-disable-next-line no-unused-vars - interface IncomingMessage { - fastify: FastifyRequest; - } + // eslint-disable-next-line no-unused-vars + interface IncomingMessage { + fastify: FastifyRequest; + } - // eslint-disable-next-line no-unused-vars - interface OutgoingMessage { - fastify: FastifyReply; - } + // eslint-disable-next-line no-unused-vars + interface OutgoingMessage { + fastify: FastifyReply; + } } export interface FastifyNextJsOptions { - dev?: boolean; - basePath?: string; + dev?: boolean; + basePath?: string; } const fastifyNextJs: FastifyPluginAsync = async (fastify, { dev, basePath = '' }) => { @@ -56,20 +61,20 @@ const fastifyNextJs: FastifyPluginAsync = async (fastify, nextServer.getRequestHandler()(request.raw, reply.raw); }; - const passNextJsRequestsDecorator = () => { - fastify.passNextJsDataRequests(); - fastify.passNextJsImageRequests(); + const passNextJsRequestsDecorator = (args?: FastifyNextJsDecoratorArguments) => { + fastify.passNextJsDataRequests(args); + fastify.passNextJsImageRequests(args); if (dev) { - fastify.passNextJsDevRequests(); + fastify.passNextJsDevRequests(args); } else { - fastify.passNextJsStaticRequests(); + fastify.passNextJsStaticRequests(args); } - fastify.passNextJsPageRequests(); + fastify.passNextJsPageRequests(args); }; - const passNextJsDataRequestsDecorator = () => { + const passNextJsDataRequestsDecorator = ({ logLevel }: FastifyNextJsDecoratorArguments = {}) => { fastify.register((fastify, _, done) => { fastify.route({ method: ['GET', 'HEAD', 'OPTIONS'], @@ -78,11 +83,12 @@ const fastifyNextJs: FastifyPluginAsync = async (fastify, }); done(); }, { + logLevel, prefix: `${basePath}/_next` }); }; - const passNextJsDevRequestsDecorator = () => { + const passNextJsDevRequestsDecorator = ({ logLevel }: FastifyNextJsDecoratorArguments = {}) => { fastify.register((fastify, _, done) => { fastify.route({ method: ['GET', 'HEAD', 'OPTIONS'], @@ -96,11 +102,12 @@ const fastifyNextJs: FastifyPluginAsync = async (fastify, }); done(); }, { + logLevel, prefix: `${basePath}/_next` }); }; - const passNextJsImageRequestsDecorator = () => { + const passNextJsImageRequestsDecorator = ({ logLevel }: FastifyNextJsDecoratorArguments = {}) => { fastify.register((fastify, _, done) => { fastify.route({ method: ['GET', 'HEAD', 'OPTIONS'], @@ -109,19 +116,21 @@ const fastifyNextJs: FastifyPluginAsync = async (fastify, }); done(); }, { + logLevel, prefix: `${basePath}/_next` }); }; - const passNextJsStaticRequestsDecorator = () => { + const passNextJsStaticRequestsDecorator = ({ logLevel }: FastifyNextJsDecoratorArguments = {}) => { fastify.register(fastifyStatic, { + logLevel, prefix: `${basePath}/_next/static/`, root: `${process.cwd()}/.next/static`, decorateReply: false, }); }; - const passNextJsPageRequestsDecorator = function () { + const passNextJsPageRequestsDecorator = ({ logLevel }: FastifyNextJsDecoratorArguments = {}) => { fastify.register((fastify, _, done) => { fastify.route({ method: ['GET', 'HEAD', 'OPTIONS'], @@ -131,6 +140,7 @@ const fastifyNextJs: FastifyPluginAsync = async (fastify, done(); }, { + logLevel, prefix: basePath || '/' }); };