1
1
import { parse } from "superjson" ;
2
2
import { z } from "zod" ;
3
3
4
- import { PUBLIC_URL , VERCEL_AUTOMATION_BYPASS_SECRET } from "../config/server" ;
4
+ import { VERCEL_REQUEST_HEADERS } from "../config/server" ;
5
5
import { Cluster , ClusterToName , priceFeedsSchema } from "../services/pyth" ;
6
+ import { absoluteUrl } from "../utils/absolute-url" ;
6
7
import { DEFAULT_CACHE_TTL } from "../utils/cache" ;
7
8
8
9
export async function getPublishersForFeedRequest (
9
10
cluster : Cluster ,
10
11
symbol : string ,
11
12
) {
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 ) } ` ,
23
15
) ;
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
+ } ) ;
24
24
const parsedData : unknown = await data . json ( ) ;
25
25
return z . array ( z . string ( ) ) . parse ( parsedData ) ;
26
26
}
@@ -29,36 +29,33 @@ export async function getFeedsForPublisherRequest(
29
29
cluster : Cluster ,
30
30
publisher : string ,
31
31
) {
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 ) } ` ,
43
34
) ;
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
+ } ) ;
44
43
const rawData = await data . text ( ) ;
45
44
const parsedData = parse ( rawData ) ;
46
45
return priceFeedsSchema . parse ( parsedData ) ;
47
46
}
48
47
49
48
export 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 ,
60
56
} ,
61
- ) ;
57
+ headers : VERCEL_REQUEST_HEADERS ,
58
+ } ) ;
62
59
const rawData = await data . text ( ) ;
63
60
const parsedData = parse ( rawData ) ;
64
61
@@ -74,19 +71,18 @@ export const getFeedForSymbolRequest = async ({
74
71
} : {
75
72
symbol : string ;
76
73
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 ) } ` ,
89
77
) ;
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
+ } ) ;
90
86
91
87
if ( ! data . ok ) {
92
88
return undefined ;
0 commit comments