From 57bbae2919daa318faeef7cddb0a081993e375b4 Mon Sep 17 00:00:00 2001 From: Darius Date: Fri, 6 Feb 2026 00:00:31 +0100 Subject: [PATCH] add cors handling --- package-lock.json | 21 +++++++++++++++++++++ package.json | 1 + src/index.ts | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/package-lock.json b/package-lock.json index f2c467e..6089aee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "dependencies": { "@dpu/shared": "git+https://git.dariusbag.dev/DarDarBinks/dpu-shared.git", + "@fastify/cors": "^11.2.0", "@fastify/sse": "^0.4.0", "@fastify/swagger": "^9.6.1", "@fastify/swagger-ui": "^5.2.3", @@ -516,6 +517,26 @@ "fast-uri": "^3.0.0" } }, + "node_modules/@fastify/cors": { + "version": "11.2.0", + "resolved": "https://registry.npmjs.org/@fastify/cors/-/cors-11.2.0.tgz", + "integrity": "sha512-LbLHBuSAdGdSFZYTLVA3+Ch2t+sA6nq3Ejc6XLAKiQ6ViS2qFnvicpj0htsx03FyYeLs04HfRNBsz/a8SvbcUw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "fastify-plugin": "^5.0.0", + "toad-cache": "^3.7.0" + } + }, "node_modules/@fastify/error": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/@fastify/error/-/error-4.2.0.tgz", diff --git a/package.json b/package.json index d701452..4a02725 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "type": "module", "dependencies": { "@dpu/shared": "git+https://git.dariusbag.dev/DarDarBinks/dpu-shared.git", + "@fastify/cors": "^11.2.0", "@fastify/sse": "^0.4.0", "@fastify/swagger": "^9.6.1", "@fastify/swagger-ui": "^5.2.3", diff --git a/src/index.ts b/src/index.ts index 275ce72..3d8f8f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,12 +24,19 @@ import { SseService } from "@dpu/shared"; import { HomepageService } from "./homepage/service.js"; import { homepageRoutes } from "./homepage/routes.js"; import fastifySSE from "@fastify/sse"; +import fastifyCors from "@fastify/cors"; const fastify = Fastify().withTypeProvider(); fastify.setValidatorCompiler(validatorCompiler); fastify.setSerializerCompiler(serializerCompiler); +await fastify.register(fastifyCors, { + origin: ["http://localhost:4321", "https://dariusbag.dev"], + methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], + allowedHeaders: ["Content-Type", "Authorization"], +}); + await fastify.register(fastifySwagger, { openapi: { info: {