Skip to content

Commit

Permalink
feat: support API endpoint from env
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed May 29, 2021
1 parent 8925e8d commit e2ace22
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_API_ENDPOINT=http://localhost:3000
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
build/
.idea
.env
12 changes: 7 additions & 5 deletions src/api/BenchmarkServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class BenchmarkServices {
difficulty: string,
): Promise<benchmarkModel> {
const res: AxiosResponse<benchmarkModel> = await axios.post(
'http://localhost:3000/benchmarks',
`${process.env.REACT_APP_API_ENDPOINT}/benchmarks`,
{
title,
subject,
Expand All @@ -26,14 +26,16 @@ export class BenchmarkServices {
}

static async getAllBenchmarks(): Promise<benchmarkModel[]> {
return axios.get('http://localhost:3000/benchmarks').then((response) => {
return response.data;
});
return axios
.get(`${process.env.REACT_APP_API_ENDPOINT}/benchmarks`)
.then((response) => {
return response.data;
});
}

static async getBenchmarkById(id: string): Promise<benchmarkModel> {
return axios
.get('http://localhost:3000/benchmarks/' + id)
.get(`${process.env.REACT_APP_API_ENDPOINT}/benchmarks/${id}`)
.then((response) => {
return response.data;
});
Expand Down
2 changes: 1 addition & 1 deletion src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosResponse } from 'axios';

const login = async (username: string, password: string): Promise<string> => {
const res: AxiosResponse<{ access_token: string }> = await axios.post(
'http://localhost:3000/auth/login',
`${process.env.REACT_APP_API_ENDPOINT}/auth/login`,
{
username,
password,
Expand Down
34 changes: 20 additions & 14 deletions src/api/submissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ function useProcessInterval({

// 1: Handle code submission
async function createJob(code: string) {
const response = await fetch('http://localhost:3000/submissions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
const response = await fetch(
`${process.env.REACT_APP_API_ENDPOINT}/submissions`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
language: 'cpython3',
code: code,
}),
},
body: JSON.stringify({
language: 'cpython3',
code: code,
}),
});
);
return await response.json();
}

Expand All @@ -50,11 +53,14 @@ function useProcessInterval({
['processProgress', token, processId],
async () => {
const res: AxiosResponse<{ status: string; output: string }> =
await axios.get(`http://localhost:3000/submissions/${processId}`, {
headers: {
Authorization: `Bearer ${token}`,
await axios.get(
`${process.env.REACT_APP_API_ENDPOINT}/submissions/${processId}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
});
);
return res.data;
},
{
Expand Down
11 changes: 7 additions & 4 deletions src/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ function useProfile() {
const { token } = useToken();

return useQuery<{ email: string }, Error>('profile', async () => {
const { data } = await axios.get('http://localhost:3000/users/stan', {
headers: {
Authorization: `Bearer ${token}`,
const { data } = await axios.get(
`${process.env.REACT_APP_API_ENDPOINT}/users/stan`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
});
);
return data;
});
}
Expand Down

0 comments on commit e2ace22

Please sign in to comment.