Skip to content

Commit 35ef5ec

Browse files
committed
2 parents 8824d7b + 93f5a55 commit 35ef5ec

File tree

6 files changed

+65
-10
lines changed

6 files changed

+65
-10
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262

6363
workflows:
6464
version: 2
65-
test-build-and-deploy:
65+
build-test-and-deploy:
6666
jobs:
6767
- build
6868
- test:

package-lock.json

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
},
2424
"scripts": {
2525
"api:serve": "netlify-lambda serve .netlify/functions",
26-
"start": "react-scripts start",
26+
"start:dev": "react-scripts start",
2727
"build": "react-scripts build",
28+
"build:prod": "env-cmd -f ./.env.production react-scripts build",
2829
"test": "react-scripts test",
2930
"eject": "react-scripts eject",
3031
"netlify:deploy": "netlify deploy --dir=./build -p -m \"$git log -1 --pretty=%B)\""
@@ -52,6 +53,7 @@
5253
"@babel/preset-react": "^7.16.7",
5354
"@types/styled-components": "^5.1.22",
5455
"babel-jest": "^27.5.1",
56+
"env-cmd": "^10.1.0",
5557
"jest": "^27.5.1",
5658
"react-test-renderer": "^17.0.2",
5759
"sass": "^1.49.7"

src/Constants.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const NYT_APP_API_KEY = process.env.REACT_APP_NYT_APP_API_KEY
2+
const BASE_URL = process.env.REACT_APP_BASE_URL_API_NYT
3+
4+
export const API_URL_DEV = `${BASE_URL}/lists/overview.json?api-key=${NYT_APP_API_KEY}`
5+
export const API_URL_PROD = `/.netlify/functions/books`

src/services/OverviewBooksServices.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import axios from "axios";
2-
const NYT_APP_API_KEY = process.env.REACT_APP_NYT_APP_API_KEY
3-
const BASE_URL = process.env.REACT_APP_BASE_URL_API_NYT
2+
import { API_URL_DEV, API_URL_PROD } from "../Constants";
43

5-
export const API_URL = `${BASE_URL}/lists/overview.json?api-key=${NYT_APP_API_KEY}`
6-
//export const API_URL2 = `/.netlify/functions/books`
4+
export let API_ENDPOINT: string;
5+
6+
if (process.env.NODE_ENV === "production") {
7+
API_ENDPOINT = API_URL_PROD
8+
} else {
9+
API_ENDPOINT = API_URL_DEV
10+
}
711

812
export const getOverviewBooks = async () => {
913
try {
10-
return await axios.get(API_URL);
14+
return await axios.get(API_ENDPOINT);
1115
} catch (e) {
1216
return [];
1317
}

src/services/__test__/OverviewBooksService.test.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { API_URL, getOverviewBooks } from '../OverviewBooksServices';
1+
import { API_ENDPOINT, getOverviewBooks } from '../OverviewBooksServices';
22
import axios from '../__mocks__/axios';
33

44
describe("fetchBooks", () => {
@@ -12,7 +12,7 @@ describe("fetchBooks", () => {
1212
const result = await getOverviewBooks();
1313

1414
// then
15-
expect(axios.get).toHaveBeenCalledWith(API_URL);
15+
expect(axios.get).toHaveBeenCalledWith(API_ENDPOINT);
1616
expect(result).toEqual(books);
1717
});
1818
})
@@ -28,7 +28,7 @@ describe("fetchBooks", () => {
2828
const result = await getOverviewBooks();
2929

3030
// then
31-
expect(axios.get).toHaveBeenCalledWith(API_URL);
31+
expect(axios.get).toHaveBeenCalledWith(API_ENDPOINT);
3232
expect(result).toEqual([]);
3333
});
3434
});

0 commit comments

Comments
 (0)