11import { parse } from "superjson" ;
22import { z } from "zod" ;
33
4- import { PUBLIC_URL , VERCEL_AUTOMATION_BYPASS_SECRET } from "../config/server" ;
4+ import { VERCEL_REQUEST_HEADERS } from "../config/server" ;
55import { Cluster , ClusterToName , priceFeedsSchema } from "../services/pyth" ;
6+ import { absoluteUrl } from "../utils/absolute-url" ;
67import { DEFAULT_CACHE_TTL } from "../utils/cache" ;
78
89export async function getPublishersForFeedRequest (
910 cluster : Cluster ,
1011 symbol : string ,
1112) {
12- const data = await fetch (
13- `${ PUBLIC_URL } /api/pyth/get-publishers/${ encodeURIComponent ( symbol ) } ?cluster=${ ClusterToName [ cluster ] } ` ,
14- {
15- next : {
16- revalidate : DEFAULT_CACHE_TTL ,
17- } ,
18- headers : {
19- // this is a way to bypass vercel protection for the internal api route
20- "x-vercel-protection-bypass" : VERCEL_AUTOMATION_BYPASS_SECRET ,
21- } ,
22- } ,
13+ const url = await absoluteUrl (
14+ `/api/pyth/get-publishers/${ encodeURIComponent ( symbol ) } ` ,
2315 ) ;
16+ url . searchParams . set ( "cluster" , ClusterToName [ cluster ] ) ;
17+
18+ const data = await fetch ( url , {
19+ next : {
20+ revalidate : DEFAULT_CACHE_TTL ,
21+ } ,
22+ headers : VERCEL_REQUEST_HEADERS ,
23+ } ) ;
2424 const parsedData : unknown = await data . json ( ) ;
2525 return z . array ( z . string ( ) ) . parse ( parsedData ) ;
2626}
@@ -29,36 +29,33 @@ export async function getFeedsForPublisherRequest(
2929 cluster : Cluster ,
3030 publisher : string ,
3131) {
32- const data = await fetch (
33- `${ PUBLIC_URL } /api/pyth/get-feeds-for-publisher/${ encodeURIComponent ( publisher ) } ?cluster=${ ClusterToName [ cluster ] } ` ,
34- {
35- next : {
36- revalidate : DEFAULT_CACHE_TTL ,
37- } ,
38- headers : {
39- // this is a way to bypass vercel protection for the internal api route
40- "x-vercel-protection-bypass" : VERCEL_AUTOMATION_BYPASS_SECRET ,
41- } ,
42- } ,
32+ const url = await absoluteUrl (
33+ `/api/pyth/get-feeds-for-publisher/${ encodeURIComponent ( publisher ) } ` ,
4334 ) ;
35+ url . searchParams . set ( "cluster" , ClusterToName [ cluster ] ) ;
36+
37+ const data = await fetch ( url , {
38+ next : {
39+ revalidate : DEFAULT_CACHE_TTL ,
40+ } ,
41+ headers : VERCEL_REQUEST_HEADERS ,
42+ } ) ;
4443 const rawData = await data . text ( ) ;
4544 const parsedData = parse ( rawData ) ;
4645 return priceFeedsSchema . parse ( parsedData ) ;
4746}
4847
4948export const getFeedsRequest = async ( cluster : Cluster ) => {
50- const data = await fetch (
51- `${ PUBLIC_URL } /api/pyth/get-feeds?cluster=${ ClusterToName [ cluster ] } &excludePriceComponents=true` ,
52- {
53- next : {
54- revalidate : DEFAULT_CACHE_TTL ,
55- } ,
56- headers : {
57- // this is a way to bypass vercel protection for the internal api route
58- "x-vercel-protection-bypass" : VERCEL_AUTOMATION_BYPASS_SECRET ,
59- } ,
49+ const url = await absoluteUrl ( `/api/pyth/get-feeds` ) ;
50+ url . searchParams . set ( "cluster" , ClusterToName [ cluster ] ) ;
51+ url . searchParams . set ( "excludePriceComponents" , "true" ) ;
52+
53+ const data = await fetch ( url , {
54+ next : {
55+ revalidate : DEFAULT_CACHE_TTL ,
6056 } ,
61- ) ;
57+ headers : VERCEL_REQUEST_HEADERS ,
58+ } ) ;
6259 const rawData = await data . text ( ) ;
6360 const parsedData = parse ( rawData ) ;
6461
@@ -74,19 +71,18 @@ export const getFeedForSymbolRequest = async ({
7471} : {
7572 symbol : string ;
7673 cluster ?: Cluster ;
77- } ) : Promise < z . infer < typeof priceFeedsSchema > [ 0 ] | undefined > => {
78- const data = await fetch (
79- `${ PUBLIC_URL } /api/pyth/get-feeds/${ encodeURIComponent ( symbol ) } ?cluster=${ ClusterToName [ cluster ] } ` ,
80- {
81- next : {
82- revalidate : DEFAULT_CACHE_TTL ,
83- } ,
84- headers : {
85- // this is a way to bypass vercel protection for the internal api route
86- "x-vercel-protection-bypass" : VERCEL_AUTOMATION_BYPASS_SECRET ,
87- } ,
88- } ,
74+ } ) : Promise < z . infer < typeof priceFeedsSchema . element > | undefined > => {
75+ const url = await absoluteUrl (
76+ `/api/pyth/get-feeds/${ encodeURIComponent ( symbol ) } ` ,
8977 ) ;
78+ url . searchParams . set ( "cluster" , ClusterToName [ cluster ] ) ;
79+
80+ const data = await fetch ( url , {
81+ next : {
82+ revalidate : DEFAULT_CACHE_TTL ,
83+ } ,
84+ headers : VERCEL_REQUEST_HEADERS ,
85+ } ) ;
9086
9187 if ( ! data . ok ) {
9288 return undefined ;
0 commit comments