11import type { JobFile , JobsResponse } from "@/types/jobs" ;
22
3+ function normalizeBaseUrl ( value : string | undefined ) : string {
4+ return typeof value === "string" ? value . trim ( ) . replace ( / \/ + $ / , "" ) : "" ;
5+ }
6+
7+ function getApiBaseUrl ( ) : string {
8+ const configuredBaseUrl = normalizeBaseUrl ( import . meta. env . VITE_API_BASE_URL ) ;
9+ if ( configuredBaseUrl ) {
10+ return configuredBaseUrl ;
11+ }
12+
13+ if ( typeof window !== "undefined" && window . location . hostname === "painel-vagas-lake.vercel.app" ) {
14+ return "https://jobsglobalscraper.ddns.net" ;
15+ }
16+
17+ return "" ;
18+ }
19+
20+ function buildApiUrl ( path : string ) : string {
21+ const normalizedPath = path . startsWith ( "/" ) ? path : `/${ path } ` ;
22+ const baseUrl = getApiBaseUrl ( ) ;
23+ return baseUrl ? `${ baseUrl } ${ normalizedPath } ` : normalizedPath ;
24+ }
25+
326function buildError ( message : unknown , fallback : string ) : Error {
427 return new Error ( typeof message === "string" && message ? message : fallback ) ;
528}
@@ -15,7 +38,7 @@ function readMessage(payload: unknown): string | undefined {
1538}
1639
1740export async function fetchJobFiles ( ) : Promise < JobFile [ ] > {
18- const response = await fetch ( "/api/jobs/files" ) ;
41+ const response = await fetch ( buildApiUrl ( "/api/jobs/files" ) ) ;
1942 const payload = ( await response . json ( ) ) as { files ?: unknown } & Record < string , unknown > ;
2043
2144 if ( ! response . ok ) {
@@ -34,7 +57,7 @@ export async function fetchJobFiles(): Promise<JobFile[]> {
3457
3558export async function fetchJobsByFile ( fileName : string ) : Promise < JobsResponse > {
3659 const suffix = fileName ? `?file=${ encodeURIComponent ( fileName ) } ` : "" ;
37- const response = await fetch ( `/api/jobs${ suffix } ` ) ;
60+ const response = await fetch ( buildApiUrl ( `/api/jobs${ suffix } ` ) ) ;
3861 const payload = ( await response . json ( ) ) as Record < string , unknown > ;
3962
4063 if ( ! response . ok ) {
@@ -50,7 +73,7 @@ export async function fetchJobsByFile(fileName: string): Promise<JobsResponse> {
5073}
5174
5275export async function fetchKeywords ( ) : Promise < string [ ] > {
53- const response = await fetch ( "/api/keywords" ) ;
76+ const response = await fetch ( buildApiUrl ( "/api/keywords" ) ) ;
5477 const payload = ( await response . json ( ) ) as { keywords ?: unknown } & Record < string , unknown > ;
5578
5679 if ( ! response . ok ) {
@@ -61,7 +84,7 @@ export async function fetchKeywords(): Promise<string[]> {
6184}
6285
6386export async function saveKeywords ( keywords : string [ ] ) : Promise < void > {
64- const response = await fetch ( "/api/keywords" , {
87+ const response = await fetch ( buildApiUrl ( "/api/keywords" ) , {
6588 method : "POST" ,
6689 headers : {
6790 "Content-Type" : "application/json" ,
@@ -76,7 +99,7 @@ export async function saveKeywords(keywords: string[]): Promise<void> {
7699}
77100
78101export async function runScraperRequest ( ) : Promise < void > {
79- const response = await fetch ( "/api/scraper/run" , {
102+ const response = await fetch ( buildApiUrl ( "/api/scraper/run" ) , {
80103 method : "POST" ,
81104 } ) ;
82105 const payload = ( await response . json ( ) ) as Record < string , unknown > ;
0 commit comments