Skip to content

Commit 671c5e2

Browse files
authored
Merge pull request #16 from bcgov/ci/set-api-url-in-vite
Update frontend API routes to use VITE_API_BASE_URL
2 parents 47affac + ebf848e commit 671c5e2

File tree

1 file changed

+35
-19
lines changed
  • validator/frontend/src/services

1 file changed

+35
-19
lines changed

validator/frontend/src/services/api.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import type { Confusable } from "../types";
22

3+
// In local development, proxying is handled by the configuration defined in
4+
// vite.config.ts. In production, we need a full base URL.
5+
const API_BASE_URL =
6+
import.meta.env.MODE === "development"
7+
? ""
8+
: import.meta.env.VITE_API_BASE_URL;
9+
310
/**
411
* Get an array of all Confusable objects.
512
*/
613
export async function getAllConfusables(): Promise<Confusable[]> {
7-
const response = await fetch("/api/v1/confusable", {
14+
const response = await fetch(`${API_BASE_URL}/api/v1/confusable`, {
815
method: "GET",
916
headers: {
1017
"Content-Type": "application/json",
@@ -24,12 +31,15 @@ export async function getAllConfusables(): Promise<Confusable[]> {
2431
export async function getOneConfusableByLabel(
2532
label: string
2633
): Promise<Confusable> {
27-
const response = await fetch(`/api/v1/confusable/label/${label}`, {
28-
method: "GET",
29-
headers: {
30-
"Content-Type": "application/json",
31-
},
32-
});
34+
const response = await fetch(
35+
`${API_BASE_URL}/api/v1/confusable/label/${label}`,
36+
{
37+
method: "GET",
38+
headers: {
39+
"Content-Type": "application/json",
40+
},
41+
}
42+
);
3343

3444
if (!response.ok) {
3545
throw new Error(`Failed to fetch data: ${response.statusText}`);
@@ -44,12 +54,15 @@ export async function getOneConfusableByLabel(
4454
export async function getConfusablesByCharacter(
4555
confusableChar: string
4656
): Promise<Confusable[]> {
47-
const response = await fetch(`/api/v1/confusable/char/${confusableChar}`, {
48-
method: "GET",
49-
headers: {
50-
"Content-Type": "application/json",
51-
},
52-
});
57+
const response = await fetch(
58+
`${API_BASE_URL}/api/v1/confusable/char/${confusableChar}`,
59+
{
60+
method: "GET",
61+
headers: {
62+
"Content-Type": "application/json",
63+
},
64+
}
65+
);
5366

5467
if (!response.ok) {
5568
throw new Error(`Failed to fetch data: ${response.statusText}`);
@@ -73,12 +86,15 @@ interface SearchStringResponse {
7386
export async function getSuggestedStringsFromSearchString(
7487
search: string
7588
): Promise<SearchStringResponse> {
76-
const response = await fetch(`api/v1/confusable/search/${search}`, {
77-
method: "GET",
78-
headers: {
79-
"Content-Type": "application/json",
80-
},
81-
});
89+
const response = await fetch(
90+
`${API_BASE_URL}/api/v1/confusable/search/${search}`,
91+
{
92+
method: "GET",
93+
headers: {
94+
"Content-Type": "application/json",
95+
},
96+
}
97+
);
8298

8399
if (!response.ok) {
84100
throw new Error(`Failed to fetch data: ${response.statusText}`);

0 commit comments

Comments
 (0)