Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions apps/insights/src/server/pyth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { z } from "zod";

import { VERCEL_REQUEST_HEADERS } from "../config/server";
import { Cluster, ClusterToName, priceFeedsSchema } from "../services/pyth";
import { absoluteUrl } from "../utils/absolute-url";
import { DEFAULT_CACHE_TTL } from "../utils/cache";
import { getHost } from "../utils/get-host";

export async function getPublishersForFeedRequest(
cluster: Cluster,
symbol: string,
) {
const url = await absoluteUrl(
const url = new URL(
`/api/pyth/get-publishers/${encodeURIComponent(symbol)}`,
await getHost(),
);
url.searchParams.set("cluster", ClusterToName[cluster]);

Expand All @@ -29,8 +30,9 @@ export async function getFeedsForPublisherRequest(
cluster: Cluster,
publisher: string,
) {
const url = await absoluteUrl(
const url = new URL(
`/api/pyth/get-feeds-for-publisher/${encodeURIComponent(publisher)}`,
await getHost(),
);
url.searchParams.set("cluster", ClusterToName[cluster]);

Expand All @@ -46,7 +48,7 @@ export async function getFeedsForPublisherRequest(
}

export const getFeedsRequest = async (cluster: Cluster) => {
const url = await absoluteUrl(`/api/pyth/get-feeds`);
const url = new URL(`/api/pyth/get-feeds`, await getHost());
url.searchParams.set("cluster", ClusterToName[cluster]);
url.searchParams.set("excludePriceComponents", "true");

Expand All @@ -72,8 +74,9 @@ export const getFeedForSymbolRequest = async ({
symbol: string;
cluster?: Cluster;
}): Promise<z.infer<typeof priceFeedsSchema.element> | undefined> => {
const url = await absoluteUrl(
const url = new URL(
`/api/pyth/get-feeds/${encodeURIComponent(symbol)}`,
await getHost(),
);
url.searchParams.set("cluster", ClusterToName[cluster]);

Expand Down
51 changes: 0 additions & 51 deletions apps/insights/src/utils/absolute-url.ts

This file was deleted.

30 changes: 30 additions & 0 deletions apps/insights/src/utils/get-host.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { headers } from "next/headers";

/**
* Returns the host of the current request.
*
* @returns The host of the current request.
*/
export const getHost = async () => {
const nextHeaders = await headers();
const xfHost = nextHeaders.get("x-forwarded-host")?.split(",")[0]?.trim();
const host = xfHost ?? nextHeaders.get("host") ?? undefined;
if (host === undefined) {
throw new NoHostError();
} else {
const proto =
nextHeaders.get("x-forwarded-proto")?.split(",")[0]?.trim() ??
(host.startsWith("localhost") ? "http" : "https");

return `${proto}://${host}`;
}
};

class NoHostError extends Error {
constructor() {
super(
"Request had neither an `x-forwarded-host` header nor a `host` header",
);
this.name = "NoHostError";
}
}
Loading