Skip to content

Commit

Permalink
Adds cors open to all origins (#16)
Browse files Browse the repository at this point in the history
* adds cors open to all origins, fixes error helper for broader error cases

* tweaks test for async update to init api
  • Loading branch information
aristidesstaffieri authored Jan 5, 2024
1 parent 733ad67 commit be62e90
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/helper/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
6 changes: 5 additions & 1 deletion src/route/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -47,6 +48,9 @@ export function initApiServer(
});

server.register(helmet, { global: true });
await server.register(cors, {
origin: "*",
});
server.register(
(instance, _opts, next) => {
instance.route({
Expand Down
11 changes: 10 additions & 1 deletion src/service/mercury/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2818,6 +2826,13 @@ minimist@^1.2.6:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

[email protected]:
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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit be62e90

Please sign in to comment.