Skip to content

Commit

Permalink
Added logLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
applicazza committed Jul 21, 2021
1 parent 3b227b1 commit 68a345a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 47 deletions.
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ 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 = {
compress: false,
};
```

Default example
###Default example

```ts
import createFastify from 'fastify';
Expand All @@ -49,7 +49,7 @@ fastify.passNextJsRequests();
await fastify.listen(0);
```

Extended example
###Extended example

```ts
import createFastify from 'fastify';
Expand Down Expand Up @@ -77,7 +77,7 @@ fastify.passNextJsPageRequests();
await fastify.listen(0);
```

Plugin accepts following options:
###Plugin accepts following options:

```ts
interface FastifyNextJsOptions {
Expand All @@ -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;
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@
"test": "jest --coverage"
},
"types": "dist/index.d.ts",
"version": "0.1.0"
"version": "0.1.1"
}
78 changes: 44 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FastifyNextJsOptions> = async (fastify, { dev, basePath = '' }) => {
Expand All @@ -56,20 +61,20 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = 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'],
Expand All @@ -78,11 +83,12 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,
});
done();
}, {
logLevel,
prefix: `${basePath}/_next`
});
};

const passNextJsDevRequestsDecorator = () => {
const passNextJsDevRequestsDecorator = ({ logLevel }: FastifyNextJsDecoratorArguments = {}) => {
fastify.register((fastify, _, done) => {
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
Expand All @@ -96,11 +102,12 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,
});
done();
}, {
logLevel,
prefix: `${basePath}/_next`
});
};

const passNextJsImageRequestsDecorator = () => {
const passNextJsImageRequestsDecorator = ({ logLevel }: FastifyNextJsDecoratorArguments = {}) => {
fastify.register((fastify, _, done) => {
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
Expand All @@ -109,19 +116,21 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = 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'],
Expand All @@ -131,6 +140,7 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,

done();
}, {
logLevel,
prefix: basePath || '/'
});
};
Expand Down

0 comments on commit 68a345a

Please sign in to comment.