Skip to content

Commit 18dd72e

Browse files
committed
refactor: getHost
1 parent f468e50 commit 18dd72e

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

apps/insights/src/server/pyth.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { z } from "zod";
33

44
import { VERCEL_REQUEST_HEADERS } from "../config/server";
55
import { Cluster, ClusterToName, priceFeedsSchema } from "../services/pyth";
6-
import { absoluteUrl } from "../utils/absolute-url";
76
import { DEFAULT_CACHE_TTL } from "../utils/cache";
7+
import { getHost } from "../utils/get-host";
88

99
export async function getPublishersForFeedRequest(
1010
cluster: Cluster,
1111
symbol: string,
1212
) {
13-
const url = await absoluteUrl(
13+
const url = new URL(
1414
`/api/pyth/get-publishers/${encodeURIComponent(symbol)}`,
15+
await getHost(),
1516
);
1617
url.searchParams.set("cluster", ClusterToName[cluster]);
1718

@@ -29,8 +30,9 @@ export async function getFeedsForPublisherRequest(
2930
cluster: Cluster,
3031
publisher: string,
3132
) {
32-
const url = await absoluteUrl(
33+
const url = new URL(
3334
`/api/pyth/get-feeds-for-publisher/${encodeURIComponent(publisher)}`,
35+
await getHost(),
3436
);
3537
url.searchParams.set("cluster", ClusterToName[cluster]);
3638

@@ -46,7 +48,10 @@ export async function getFeedsForPublisherRequest(
4648
}
4749

4850
export const getFeedsRequest = async (cluster: Cluster) => {
49-
const url = await absoluteUrl(`/api/pyth/get-feeds`);
51+
const url = new URL(
52+
`/api/pyth/get-feeds`,
53+
await getHost(),
54+
);
5055
url.searchParams.set("cluster", ClusterToName[cluster]);
5156
url.searchParams.set("excludePriceComponents", "true");
5257

@@ -72,8 +77,9 @@ export const getFeedForSymbolRequest = async ({
7277
symbol: string;
7378
cluster?: Cluster;
7479
}): Promise<z.infer<typeof priceFeedsSchema.element> | undefined> => {
75-
const url = await absoluteUrl(
80+
const url = new URL(
7681
`/api/pyth/get-feeds/${encodeURIComponent(symbol)}`,
82+
await getHost(),
7783
);
7884
url.searchParams.set("cluster", ClusterToName[cluster]);
7985

apps/insights/src/utils/absolute-url.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { headers } from "next/headers";
2+
3+
/**
4+
* Returns the host of the current request.
5+
*
6+
* @returns The host of the current request.
7+
*/
8+
export const getHost = async () => {
9+
const nextHeaders = await headers();
10+
const xfHost = nextHeaders.get("x-forwarded-host")?.split(",")[0]?.trim();
11+
const host = xfHost ?? nextHeaders.get("host") ?? undefined;
12+
if (host === undefined) {
13+
throw new NoHostError();
14+
} else {
15+
const proto =
16+
nextHeaders.get("x-forwarded-proto")?.split(",")[0]?.trim() ??
17+
(host.startsWith("localhost") ? "http" : "https");
18+
19+
return `${proto}://${host}`;
20+
}
21+
}
22+
23+
class NoHostError extends Error {
24+
constructor() {
25+
super("Request had neither an `x-forwarded-host` header nor a `host` header");
26+
this.name = "NoHostError";
27+
}
28+
}

0 commit comments

Comments
 (0)