Skip to content

Commit

Permalink
fix(plugin): changes plugin register config object structure
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikj31 committed Nov 9, 2023
1 parent 8a184df commit f9c7089
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { FastifyPluginAsync, FastifyPluginCallback } from "fastify";
import { FastifyPluginAsync } from "fastify";
import fastifyPlugin from "fastify-plugin";
import { createOnRequestHook } from "./hooks/onRequest";
import { createOnSendHook } from "./hooks/onSend";
Expand All @@ -21,7 +21,11 @@ export const dynamodbCache: FastifyPluginAsync<PluginOptions> = async (
});

fastify.addHook("onRoute", (routeOptions) => {
if (routeOptions.config && routeOptions.config.cacheEnabled === true) {
if (
routeOptions.config &&
routeOptions.config.cache &&
routeOptions.config.cache.cacheEnabled === true
) {
const onRequestHook = createOnRequestHook({
dynamoClient,
tableName: opts.tableName,
Expand All @@ -30,7 +34,7 @@ export const dynamodbCache: FastifyPluginAsync<PluginOptions> = async (
const onSendHook = createOnSendHook({
dynamoClient,
tableName: opts.tableName,
ttlSeconds: routeOptions.config.ttl || opts.defaultTTL, // Defaults to "defaultTTL" which is specified when registering the plugin
ttlSeconds: routeOptions.config.cache.ttl || opts.defaultTTL, // Defaults to "defaultTTL" which is specified when registering the plugin
});

if (!routeOptions.onRequest) {
Expand All @@ -57,8 +61,10 @@ export const dynamodbCache: FastifyPluginAsync<PluginOptions> = async (

declare module "fastify" {
interface FastifyContextConfig {
cacheEnabled?: boolean;
ttl?: number;
cache?: {
cacheEnabled?: boolean;
ttl?: number;
};
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/src/routes/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export const usersRoutes: FastifyPluginAsync = async function (fastify) {
"/",
{
config: {
cacheEnabled: true,
ttl: 10,
cache: {
cacheEnabled: true,
ttl: 10,
},
},
schema: {
response: {
Expand Down

0 comments on commit f9c7089

Please sign in to comment.