From 00f3d0da92d2610ae684e6fd0851eb4f6a5032dc Mon Sep 17 00:00:00 2001 From: Kristiyan Tachev Date: Wed, 3 Apr 2024 17:24:35 +0300 Subject: [PATCH] fix(fastify): removed cors function there is a package called @fastify/cors --- packages/fastify/src/fastify.module.ts | 5 ++- packages/fastify/src/fastify.tokens.ts | 3 -- .../fastify/src/helpers/hooks/cors.hook.ts | 32 ------------------- packages/fastify/src/helpers/hooks/index.ts | 1 - 4 files changed, 2 insertions(+), 39 deletions(-) delete mode 100644 packages/fastify/src/helpers/hooks/cors.hook.ts diff --git a/packages/fastify/src/fastify.module.ts b/packages/fastify/src/fastify.module.ts index d5a70ae..b7706ff 100644 --- a/packages/fastify/src/fastify.module.ts +++ b/packages/fastify/src/fastify.module.ts @@ -2,7 +2,7 @@ import { Module, ModuleWithProviders } from '@rhtml/di'; import fastify, { FastifyInstance } from 'fastify'; import { Fastify, FastifyListen, FastifyModuleOptions } from './fastify.tokens'; -import { addSchema, corsHook, globalErrorHandler, pipe } from './helpers'; +import { addSchema, globalErrorHandler, pipe } from './helpers'; @Module() export class FastifyModule { @@ -26,8 +26,7 @@ export class FastifyModule { return pipe( addSchema(schemas), - globalErrorHandler(options.globalErrorHandler), - corsHook(options.cors) + globalErrorHandler(options.globalErrorHandler) )(instance); }, }, diff --git a/packages/fastify/src/fastify.tokens.ts b/packages/fastify/src/fastify.tokens.ts index ec6b965..9d4e638 100644 --- a/packages/fastify/src/fastify.tokens.ts +++ b/packages/fastify/src/fastify.tokens.ts @@ -11,8 +11,6 @@ import { FastifySchema, } from 'fastify'; -import { CorsOptions } from './helpers/hooks/cors.hook'; - export const Fastify = new InjectionToken(); export const FastifyListen = new InjectionToken(); @@ -33,6 +31,5 @@ export interface FastifyModuleOptions extends FastifyHttpOptions { plugins: FastifyPlugin[]; schemas: FastifySchema[]; globalErrorHandler: GlobalErrorHandler; - cors: CorsOptions; server: FastifyListenOptions; } diff --git a/packages/fastify/src/helpers/hooks/cors.hook.ts b/packages/fastify/src/helpers/hooks/cors.hook.ts deleted file mode 100644 index cb503e1..0000000 --- a/packages/fastify/src/helpers/hooks/cors.hook.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Reader } from '@rhtml/di'; -import { FastifyInstance } from 'fastify'; - -export interface AllowedType { - name: string; - methods: string[]; -} - -export interface CorsOptions { - allowedOrigins: AllowedType[]; -} - -export function corsHook( - { allowedOrigins = [] }: CorsOptions = { allowedOrigins: [] } -): Reader { - return (instance) => { - instance.addHook('preHandler', (req, res, done) => { - const hostname = allowedOrigins.find((v) => v.name === req.hostname); - if (hostname) { - res.header('Access-Control-Allow-Origin', hostname.name); - // res.header('Access-Control-Allow-Headers', hostname.name); - res.header('Access-Control-Allow-Methods', hostname.methods?.join()); - } - const isPreflight = /options/i.test(req.method); - if (isPreflight) { - return res.send(); - } - return done(); - }); - return instance; - }; -} diff --git a/packages/fastify/src/helpers/hooks/index.ts b/packages/fastify/src/helpers/hooks/index.ts index ae9bf1e..78ad736 100644 --- a/packages/fastify/src/helpers/hooks/index.ts +++ b/packages/fastify/src/helpers/hooks/index.ts @@ -1,3 +1,2 @@ -export * from './cors.hook'; export * from './add-schema.hook'; export * from './global-error.hook';