Skip to content

Commit a1e91d6

Browse files
committed
v2 Step 4: Fetch API data while keeping secrets secure
1 parent f582ea6 commit a1e91d6

File tree

8 files changed

+70
-12
lines changed

8 files changed

+70
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
npm-debug.log*
2020
yarn-debug.log*
2121
yarn-error.log*
22+
23+
now-secrets.json

now-staging.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"alias": ["create-react-app-devops-staging"],
33
"files": ["public", "src", "config-overrides.js", "now.json", "yarn.lock"],
44
"env": {
5-
"NODE_ENV": "production"
5+
"NODE_ENV": "production",
6+
"githubAccessToken": "@github"
67
},
78
"scale": {
89
"sfo1": {

now.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"alias": ["create-react-app-devops"],
33
"files": ["public", "src", "config-overrides.js", "now.json", "yarn.lock"],
44
"env": {
5-
"NODE_ENV": "production"
5+
"NODE_ENV": "production",
6+
"githubAccessToken": "@github"
67
},
78
"scale": {
89
"sfo1": {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"babel-preset-env": "^1.7.0",
99
"compression": "^1.7.2",
1010
"express": "^4.16.3",
11+
"now-env": "^3.1.0",
1112
"react": "^16.4.1",
1213
"react-app-rewired": "^1.5.2",
1314
"react-dom": "^16.4.1",
14-
"react-scripts": "1.1.4"
15+
"react-scripts": "1.1.4",
16+
"request": "^2.87.0"
1517
},
1618
"scripts": {
1719
"start": "yarn dev",

src/App.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import React, { Component } from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
1+
import React, { Component } from "react";
2+
import logo from "./logo.svg";
3+
import "./App.css";
44

55
class App extends Component {
6+
state = {
7+
repos: []
8+
};
9+
10+
componentDidMount() {
11+
fetch(`/api/v1/github`, {
12+
credentials: "same-origin"
13+
})
14+
.then(response => response.json())
15+
.then(data => {
16+
if (data.length > 0) {
17+
const repos = data.map(repo => <p key={repo.id}>{repo.name}</p>);
18+
this.setState({ repos });
19+
}
20+
});
21+
}
22+
623
render() {
724
return (
825
<div className="App">
@@ -13,6 +30,8 @@ class App extends Component {
1330
<p className="App-intro">
1431
To get started, edit <code>src/App.js</code> and save to reload.
1532
</p>
33+
<h3>App Creator's Repos:</h3>
34+
{this.state.repos}
1635
</div>
1736
);
1837
}

src/App.test.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import React from 'react';
2-
import ReactDOM from 'react-dom';
3-
import App from './App';
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import App from "./App";
44

5-
it('renders without crashing', () => {
6-
const div = document.createElement('div');
5+
global.fetch = jest.fn(() =>
6+
Promise.resolve({
7+
json: () => Promise.resolve([])
8+
})
9+
);
10+
11+
it("renders without crashing", () => {
12+
const div = document.createElement("div");
713
ReactDOM.render(<App />, div);
814
ReactDOM.unmountComponentAtNode(div);
915
});

src/server/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import compression from "compression";
22
import express from "express";
3+
import request from "request";
4+
5+
if (process.env.DEV === "true") {
6+
const nowEnv = require("now-env");
7+
}
38

49
const app = express();
510
const port = process.env.PORT || 8080;
@@ -14,6 +19,24 @@ app.use((req, res, next) => {
1419
}
1520
});
1621

22+
app.get("/api/v1/github", (req, res) => {
23+
request(
24+
{
25+
url: `https://api.github.com/user/repos?affiliation=owner,collaborator&access_token=${
26+
process.env.githubAccessToken
27+
}`,
28+
headers: {
29+
"user-agent": "node.js"
30+
}
31+
},
32+
(err, response, body) => {
33+
if (!err && response.statusCode === 200) {
34+
res.send(body);
35+
}
36+
}
37+
);
38+
});
39+
1740
app.use(express.static("./build"));
1841

1942
app.listen(port, err => {

yarn.lock

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4784,6 +4784,10 @@ normalize-url@^1.4.0:
47844784
query-string "^4.1.0"
47854785
sort-keys "^1.0.0"
47864786

4787+
now-env@^3.1.0:
4788+
version "3.1.0"
4789+
resolved "https://registry.yarnpkg.com/now-env/-/now-env-3.1.0.tgz#e0198b67279d387229cfd4b25de4c2fc7156943f"
4790+
47874791
npm-bundled@^1.0.1:
47884792
version "1.0.3"
47894793
resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.0.3.tgz#7e71703d973af3370a9591bafe3a63aca0be2308"
@@ -5954,7 +5958,7 @@ repeating@^2.0.0:
59545958
dependencies:
59555959
is-finite "^1.0.0"
59565960

5957-
request@^2.79.0:
5961+
request@^2.79.0, request@^2.87.0:
59585962
version "2.87.0"
59595963
resolved "https://registry.yarnpkg.com/request/-/request-2.87.0.tgz#32f00235cd08d482b4d0d68db93a829c0ed5756e"
59605964
dependencies:

0 commit comments

Comments
 (0)