From 2762d02e69bb358a97709b31664de4e991c95b8d Mon Sep 17 00:00:00 2001 From: Caleb Hill Date: Sat, 21 Sep 2024 11:34:25 -0600 Subject: [PATCH] Fix cors errors --- server/package-lock.json | 28 ++++++++++++++++++++++++++++ server/package.json | 1 + server/src/server.ts | 3 +++ web/src/components/main.tsx | 8 ++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/server/package-lock.json b/server/package-lock.json index 83b972c..f0b750f 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@fastify/autoload": "^6.0.0", + "@fastify/cors": "^10.0.0", "@fastify/type-provider-typebox": "^5.0.0", "fastify": "^5.0.0", "pino": "^9.4.0", @@ -70,6 +71,15 @@ "resolved": "https://registry.npmjs.org/@fastify/autoload/-/autoload-6.0.0.tgz", "integrity": "sha512-6L8LNxpgjBoY1MU0iUwDpDh3BEFY7yqZB3b8az4CpxvfvwizAxNp8/IdEWKgybSPIc8dxIP4EWIoaD5NKo3JVQ==" }, + "node_modules/@fastify/cors": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@fastify/cors/-/cors-10.0.0.tgz", + "integrity": "sha512-kb9fkc/LVbLTQ3lhA+ZZjC/Styzysodo/MTCdVCvTtgHa/gBwxrEEkcp3fuoKIfAQt85wksrpXjUGbw5NQffEQ==", + "dependencies": { + "fastify-plugin": "^5.0.0", + "mnemonist": "0.39.8" + } + }, "node_modules/@fastify/error": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@fastify/error/-/error-4.0.0.tgz", @@ -573,6 +583,11 @@ "toad-cache": "^3.7.0" } }, + "node_modules/fastify-plugin": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-5.0.1.tgz", + "integrity": "sha512-HCxs+YnRaWzCl+cWRYFnHmeRFyR5GVnJTAaCJQiYzQSDwK9MgJdyAsuL3nh0EWRCYMgQ5MeziymvmAhUHYHDUQ==" + }, "node_modules/fastq": { "version": "1.17.1", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.1.tgz", @@ -784,6 +799,19 @@ "node": ">=16 || 14 >=14.17" } }, + "node_modules/mnemonist": { + "version": "0.39.8", + "resolved": "https://registry.npmjs.org/mnemonist/-/mnemonist-0.39.8.tgz", + "integrity": "sha512-vyWo2K3fjrUw8YeeZ1zF0fy6Mu59RHokURlld8ymdUPjMlD9EC9ov1/YPqTgqRvUN9nTr3Gqfz29LYAmu0PHPQ==", + "dependencies": { + "obliterator": "^2.0.1" + } + }, + "node_modules/obliterator": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/obliterator/-/obliterator-2.0.4.tgz", + "integrity": "sha512-lgHwxlxV1qIg1Eap7LgIeoBWIMFibOjbrYPIPJZcI1mmGAI2m3lNYpK12Y+GBdPQ0U1hRwSord7GIaawz962qQ==" + }, "node_modules/on-exit-leak-free": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz", diff --git a/server/package.json b/server/package.json index a2d18e2..d7885aa 100644 --- a/server/package.json +++ b/server/package.json @@ -20,6 +20,7 @@ }, "dependencies": { "@fastify/autoload": "^6.0.0", + "@fastify/cors": "^10.0.0", "@fastify/type-provider-typebox": "^5.0.0", "fastify": "^5.0.0", "pino": "^9.4.0", diff --git a/server/src/server.ts b/server/src/server.ts index 743e2a6..2e550e8 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -14,6 +14,7 @@ import { sendBasicMessageResponse, } from './util/errors' import { logger, fastifyLogOpts } from './util/logger' +import cors from '@fastify/cors' export default async function server (): Promise { const fastify = Fastify({ @@ -22,6 +23,8 @@ export default async function server (): Promise { fastify.setValidatorCompiler(TypeBoxValidatorCompiler) + fastify.register(cors) + // This loads all plugins defined in routes // define your routes in one of these fastify.register(AutoLoad, { // eslint-disable-line @typescript-eslint/no-floating-promises diff --git a/web/src/components/main.tsx b/web/src/components/main.tsx index b74e0c3..36d527a 100644 --- a/web/src/components/main.tsx +++ b/web/src/components/main.tsx @@ -3,16 +3,17 @@ import {useLoad, useTriggerLoad} from '../util/load'; import {useContext} from 'react'; import {ClientContext} from '../context/client-context'; import {add} from "../client/add"; +import { AddResponse } from 'shared' export const Main = memo(() => { - const client = useContext(ClientContext); + const client = useContext(ClientContext) const quoteLoad = useLoad(async (abort) => { const response = await fetch('https://bible-api.com/john 3:16', { signal: abort }).then((res) => res.json()); return response.text }, []); - const [addLoadState, doAdd] = useTriggerLoad(async (abort) => { + const [addLoadState, doAdd] = useTriggerLoad(async (abort) => { if (!client) { return } @@ -36,6 +37,9 @@ export const Main = memo(() => { ) : ( )} + {addLoadState.value && ( +
Hey, this is the value! {addLoadState.value?.result ?? 'undefined'}
+ )} ); }); \ No newline at end of file