From b2f7a5aaabe78aad4eb6329755ee830f3c4bf4a5 Mon Sep 17 00:00:00 2001 From: Kristiyan Tachev Date: Tue, 18 Jun 2024 16:55:03 +0300 Subject: [PATCH] feat(fastify): added Middleware type to be exported outside library and refactored interfaces to use Middleware instead of Reader --- packages/fastify/src/fastify.tokens.ts | 4 ++-- packages/fastify/src/helpers/hooks/add-middlewares.ts | 9 ++------- packages/fastify/src/helpers/hooks/add-schema.hook.ts | 9 ++++----- packages/fastify/src/helpers/hooks/global-error.hook.ts | 7 ++----- 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/fastify/src/fastify.tokens.ts b/packages/fastify/src/fastify.tokens.ts index df31ea8..bdad4ed 100644 --- a/packages/fastify/src/fastify.tokens.ts +++ b/packages/fastify/src/fastify.tokens.ts @@ -11,8 +11,6 @@ import { FastifySchema, } from 'fastify'; -import { Middleware } from './helpers'; - export const Fastify = new InjectionToken(); export const FastifyListen = new InjectionToken(); @@ -37,3 +35,5 @@ export interface FastifyModuleOptions extends FastifyHttpOptions { server: FastifyListenOptions; middlewares: Middleware[]; } + +export type Middleware = (instance: FastifyInstance) => FastifyInstance; diff --git a/packages/fastify/src/helpers/hooks/add-middlewares.ts b/packages/fastify/src/helpers/hooks/add-middlewares.ts index c5bbfa0..3ba8187 100644 --- a/packages/fastify/src/helpers/hooks/add-middlewares.ts +++ b/packages/fastify/src/helpers/hooks/add-middlewares.ts @@ -1,11 +1,6 @@ -import { Reader } from '@rhtml/di'; -import { FastifyInstance } from 'fastify'; +import { Middleware } from '../../fastify.tokens'; -export type Middleware = (instance: FastifyInstance) => FastifyInstance; - -export function addMiddlewares( - middlewares: Middleware[] = [] -): Reader { +export function addMiddlewares(middlewares: Middleware[] = []): Middleware { return (instance) => { for (const middleware of middlewares) { instance = middleware(instance); diff --git a/packages/fastify/src/helpers/hooks/add-schema.hook.ts b/packages/fastify/src/helpers/hooks/add-schema.hook.ts index 843f464..a33cd03 100644 --- a/packages/fastify/src/helpers/hooks/add-schema.hook.ts +++ b/packages/fastify/src/helpers/hooks/add-schema.hook.ts @@ -1,9 +1,8 @@ -import { Reader } from '@rhtml/di'; -import { FastifyInstance, FastifySchema } from 'fastify'; +import { FastifySchema } from 'fastify'; -export function addSchema( - schemas: FastifySchema[] -): Reader { +import { Middleware } from '../../fastify.tokens'; + +export function addSchema(schemas: FastifySchema[]): Middleware { return (instance) => { for (const schema of schemas) { instance.addSchema(schema); diff --git a/packages/fastify/src/helpers/hooks/global-error.hook.ts b/packages/fastify/src/helpers/hooks/global-error.hook.ts index 03bf58e..8643e01 100644 --- a/packages/fastify/src/helpers/hooks/global-error.hook.ts +++ b/packages/fastify/src/helpers/hooks/global-error.hook.ts @@ -1,11 +1,8 @@ -import { Reader } from '@rhtml/di'; -import { FastifyInstance } from 'fastify'; - -import { GlobalErrorHandler } from '../../fastify.tokens'; +import { GlobalErrorHandler, Middleware } from '../../fastify.tokens'; export function globalErrorHandler( globalErrorHandler: GlobalErrorHandler -): Reader { +): Middleware { return (instance) => { if (globalErrorHandler) { instance.setErrorHandler(globalErrorHandler(instance));