From be62e909102b5b9847ee207720aec1310e2ea902 Mon Sep 17 00:00:00 2001 From: aristides Date: Fri, 5 Jan 2024 14:06:58 -0700 Subject: [PATCH] Adds cors open to all origins (#16) * adds cors open to all origins, fixes error helper for broader error cases * tweaks test for async update to init api --- package.json | 1 + src/helper/test-helper.ts | 2 +- src/index.ts | 2 +- src/route/index.ts | 6 +++++- src/service/mercury/index.ts | 11 ++++++++++- yarn.lock | 20 ++++++++++++++++++++ 6 files changed, 38 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 87b1fd1..4d51d88 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "node": ">=18.12.0" }, "dependencies": { + "@fastify/cors": "^8.5.0", "@fastify/helmet": "^11.1.1", "@fastify/rate-limit": "^8.0.3", "@urql/core": "^4.1.3", diff --git a/src/helper/test-helper.ts b/src/helper/test-helper.ts index 63743e0..cd90429 100644 --- a/src/helper/test-helper.ts +++ b/src/helper/test-helper.ts @@ -278,7 +278,7 @@ jest } ); async function getDevServer() { - const server = initApiServer(mockMercuryClient, testLogger, register); + const server = await initApiServer(mockMercuryClient, testLogger, register); await server.listen(); return server; } diff --git a/src/index.ts b/src/index.ts index 7da0873..aabc25d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,7 +92,7 @@ async function main() { register, redis ); - const server = initApiServer(mercuryClient, logger, register, redis); + const server = await initApiServer(mercuryClient, logger, register, redis); try { await server.listen({ port, host: "0.0.0.0" }); diff --git a/src/route/index.ts b/src/route/index.ts index 0850eae..66d3fbf 100644 --- a/src/route/index.ts +++ b/src/route/index.ts @@ -1,6 +1,7 @@ import Fastify, { FastifyRequest } from "fastify"; import helmet from "@fastify/helmet"; import rateLimiter from "@fastify/rate-limit"; +import cors from "@fastify/cors"; import { Logger } from "pino"; import { Redis } from "ioredis"; import Prometheus from "prom-client"; @@ -28,7 +29,7 @@ import { buildTransfer, simulateTx } from "../helper/soroban-rpc"; const API_VERSION = "v1"; -export function initApiServer( +export async function initApiServer( mercuryClient: MercuryClient, logger: Logger, register: Prometheus.Registry, @@ -47,6 +48,9 @@ export function initApiServer( }); server.register(helmet, { global: true }); + await server.register(cors, { + origin: "*", + }); server.register( (instance, _opts, next) => { instance.route({ diff --git a/src/service/mercury/index.ts b/src/service/mercury/index.ts index e1a5f52..f6731c4 100644 --- a/src/service/mercury/index.ts +++ b/src/service/mercury/index.ts @@ -40,7 +40,16 @@ const ERROR_MESSAGES = { function getGraphQlError(error?: CombinedError) { if (!error) return; const [err] = error.graphQLErrors; - return err.message; + + if (err) { + return err.message; + } + + if (error.networkError) { + return error.networkError.message; + } + + return JSON.stringify(error); } const hasIndexerSupport = (network: NetworkNames) => { diff --git a/yarn.lock b/yarn.lock index caf58f4..96aee39 100644 --- a/yarn.lock +++ b/yarn.lock @@ -398,6 +398,14 @@ ajv-formats "^2.1.1" fast-uri "^2.0.0" +"@fastify/cors@^8.5.0": + version "8.5.0" + resolved "https://registry.yarnpkg.com/@fastify/cors/-/cors-8.5.0.tgz#4e9be0d72bfaa63e0918fd43dedd046d9fb1c0d4" + integrity sha512-/oZ1QSb02XjP0IK1U0IXktEsw/dUBTxJOW7IpIeO8c/tNalw/KjoNSJv1Sf6eqoBPO+TDGkifq6ynFK3v68HFQ== + dependencies: + fastify-plugin "^4.0.0" + mnemonist "0.39.6" + "@fastify/deepmerge@^1.0.0": version "1.3.0" resolved "https://registry.yarnpkg.com/@fastify/deepmerge/-/deepmerge-1.3.0.tgz#8116858108f0c7d9fd460d05a7d637a13fe3239a" @@ -2818,6 +2826,13 @@ minimist@^1.2.6: resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c" integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA== +mnemonist@0.39.6: + version "0.39.6" + resolved "https://registry.yarnpkg.com/mnemonist/-/mnemonist-0.39.6.tgz#0b3c9b7381d9edf6ce1957e74b25a8ad25732f57" + integrity sha512-A/0v5Z59y63US00cRSLiloEIw3t5G+MiKz4BhX21FI+YBJXBOGW0ohFxTxO08dsOYlzxo87T7vGfZKYp2bcAWA== + dependencies: + obliterator "^2.0.1" + mri@^1.1.4: version "1.2.0" resolved "https://registry.yarnpkg.com/mri/-/mri-1.2.0.tgz#6721480fec2a11a4889861115a48b6cbe7cc8f0b" @@ -2895,6 +2910,11 @@ npm-run-path@^5.1.0: dependencies: path-key "^4.0.0" +obliterator@^2.0.1: + version "2.0.4" + resolved "https://registry.yarnpkg.com/obliterator/-/obliterator-2.0.4.tgz#fa650e019b2d075d745e44f1effeb13a2adbe816" + integrity sha512-lgHwxlxV1qIg1Eap7LgIeoBWIMFibOjbrYPIPJZcI1mmGAI2m3lNYpK12Y+GBdPQ0U1hRwSord7GIaawz962qQ== + on-exit-leak-free@^2.1.0: version "2.1.2" resolved "https://registry.yarnpkg.com/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz#fed195c9ebddb7d9e4c3842f93f281ac8dadd3b8"