Skip to content

Commit

Permalink
feat(fastify): added Middleware type to be exported outside library a…
Browse files Browse the repository at this point in the history
…nd refactored interfaces to use Middleware instead of Reader
  • Loading branch information
Stradivario committed Jun 18, 2024
1 parent 12a385c commit b2f7a5a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/fastify/src/fastify.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
FastifySchema,
} from 'fastify';

import { Middleware } from './helpers';

export const Fastify = new InjectionToken<FastifyInstance>();
export const FastifyListen = new InjectionToken<string>();

Expand All @@ -37,3 +35,5 @@ export interface FastifyModuleOptions extends FastifyHttpOptions<never> {
server: FastifyListenOptions;
middlewares: Middleware[];
}

export type Middleware = (instance: FastifyInstance) => FastifyInstance;
9 changes: 2 additions & 7 deletions packages/fastify/src/helpers/hooks/add-middlewares.ts
Original file line number Diff line number Diff line change
@@ -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<FastifyInstance, FastifyInstance> {
export function addMiddlewares(middlewares: Middleware[] = []): Middleware {
return (instance) => {
for (const middleware of middlewares) {
instance = middleware(instance);
Expand Down
9 changes: 4 additions & 5 deletions packages/fastify/src/helpers/hooks/add-schema.hook.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Reader } from '@rhtml/di';
import { FastifyInstance, FastifySchema } from 'fastify';
import { FastifySchema } from 'fastify';

export function addSchema(
schemas: FastifySchema[]
): Reader<FastifyInstance, FastifyInstance> {
import { Middleware } from '../../fastify.tokens';

export function addSchema(schemas: FastifySchema[]): Middleware {
return (instance) => {
for (const schema of schemas) {
instance.addSchema(schema);
Expand Down
7 changes: 2 additions & 5 deletions packages/fastify/src/helpers/hooks/global-error.hook.ts
Original file line number Diff line number Diff line change
@@ -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<FastifyInstance, FastifyInstance> {
): Middleware {
return (instance) => {
if (globalErrorHandler) {
instance.setErrorHandler(globalErrorHandler(instance));
Expand Down

0 comments on commit b2f7a5a

Please sign in to comment.